Skip to content

Instantly share code, notes, and snippets.

@riftrsps
Last active August 24, 2019 19:01
Show Gist options
  • Save riftrsps/7bed287d3ef48676e47c1404a9d09856 to your computer and use it in GitHub Desktop.
Save riftrsps/7bed287d3ef48676e47c1404a9d09856 to your computer and use it in GitHub Desktop.
Main Package
package main;
import api.YourAPI;
class Launcher extends JavaPlugin {
private YourAPI api;
@Override
public void onEnable() {
api = new YourAPIV1(this);
api.enable();
startUpdater();
}
private void startUpdater() {
int secondsPerDelay = 30; // 30 seconds for each delay
int updateDelay = 20 * secondsPerDelay; // server runs at 20 ticks per second
Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(getPlugin(), api::updatePeriodically, 0, updateDelay);
}
}
package main;
import api.YourAPI;
import api.buff.BuffModule; // https://gist.github.com/riftrsps/57634ff1f4c8ac58ff7db2d03d3ffcd5#file-buffmodule-java
import api.buff.CommandModule;
import api.quest.QuestModule; // https://gist.github.com/riftrsps/84e2151f0c366b3f9de058442af02aef#file-questmodule-java
public class YourAPIV1 implements YourAPI {
private final JavaPlugin plugin;
private BuffModule buffModule;
private CommandModule commandModule;
private QuestModule questModule;
public YourAPIV1(JavaPlugin plugin) {
this.plugin = plugin;
}
@Override
public void enable() {
buffModule = BuffModule.load(this);
commandModule = CommandModule.load(this);
questModule = QuestModule.load(this);
registerListener(buffModule);
registerListener(commandModule);
registerListener(questModule);
}
@Override
public void registerListener(Listener listener) {
plugin.getServer().getPluginManager().registerListener(listener, plugin);
}
@Override
public void deregisterListener(Listener listener) {
plugin.getServer().getPluginManager().deregisterListener(listener, plugin);
}
@Override
private void updatePeriodically() {
buffModule.updatePeriodically();
}
@Override
public Set<Villager> findVillagersNearby(Entity entity, int radius) {
return entity.getWorld().getNearbyEntities(entity.getLocation(), radius, radius, radius)
.stream()
.filter(e -> e instanceof Villager)
.map(e -> (Villager) e)
.peek(v -> buffModule.activateBuffs(v, entity))
.collect(Collectors.toSet());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment