DynmapCitizens/src/main/java/net/knarcraft/dynmapcitizens/DynmapCitizens.java

121 lines
4.0 KiB
Java

package net.knarcraft.dynmapcitizens;
import net.knarcraft.dynmapcitizens.trait.BlacksmithHandler;
import net.knarcraft.dynmapcitizens.trait.CitizensTraitHandler;
import net.knarcraft.dynmapcitizens.trait.QuestsHandler;
import net.knarcraft.dynmapcitizens.trait.SentinelHandler;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.dynmap.DynmapAPI;
import org.dynmap.markers.MarkerAPI;
import org.dynmap.markers.MarkerIcon;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
@SuppressWarnings("unused")
public final class DynmapCitizens extends JavaPlugin {
private static DynmapCitizens instance;
private DynmapAPI dynmapAPI;
private Map<Icon, MarkerIcon> markerIcons;
@Override
public void onEnable() {
instance = this;
//Initialize quest and dynmap APIs
PluginManager pluginManager = Bukkit.getPluginManager();
Plugin dynmapPlugin = pluginManager.getPlugin("dynmap");
if (!(dynmapPlugin instanceof DynmapAPI dynmapAPI) || dynmapAPI.getMarkerAPI() == null) {
this.getLogger().log(Level.SEVERE, "Could not initialize Dynmap");
this.onDisable();
return;
}
this.dynmapAPI = dynmapAPI;
//Sets all icons used for displaying NPC locations
loadMarkerIcons();
//Setup all trait handlers
List<CitizensTraitHandler> handlers = new ArrayList<>();
handlers.add(new BlacksmithHandler());
handlers.add(new QuestsHandler());
handlers.add(new SentinelHandler());
for (CitizensTraitHandler handler : handlers) {
handler.initialize();
}
//Schedule handlers to run periodically
AtomicInteger elapsedSeconds = new AtomicInteger();
elapsedSeconds.set(240);
Bukkit.getScheduler().runTaskTimer(this, () -> {
for (CitizensTraitHandler handler : handlers) {
if (handler.isEnabled() && elapsedSeconds.get() % handler.getUpdateRate().getEveryNSeconds() == 0) {
handler.updateMarkers();
}
}
elapsedSeconds.addAndGet(5);
if (elapsedSeconds.get() > 3600) {
elapsedSeconds.set(240);
}
//TODO: It would probably make more sense to have individual timers for each marker based on whether the
// NPC is likely to move about.
}, 5 * 20, 5 * 20);
}
@Override
public void onDisable() {
// Plugin shutdown logic
}
/**
* Gets a reference to the Dynmap API
*
* @return <p>A reference to the Dynmap API</p>
*/
public DynmapAPI getDynmapAPI() {
return this.dynmapAPI;
}
/**
* Gets a map of loaded marker icons
*
* @return <p>All marker icons</p>
*/
public Map<Icon, MarkerIcon> getMarkerIcons() {
return new HashMap<>(this.markerIcons);
}
/**
* Gets an instance of this plugin
*
* @return <p>An instance of this plugin</p>
*/
public static DynmapCitizens getInstance() {
return instance;
}
/**
* Loads necessary marker icons, and updates icons for the dynmap drawer
*/
private void loadMarkerIcons() {
//TODO: Make every icon configurable
MarkerAPI markerAPI = dynmapAPI.getMarkerAPI();
Map<Icon, MarkerIcon> markerIcons = new HashMap<>();
markerIcons.put(Icon.QUEST_GIVER, markerAPI.getMarkerIcon("exclamation"));
markerIcons.put(Icon.QUEST_DELIVER, markerAPI.getMarkerIcon("basket"));
markerIcons.put(Icon.QUEST_KILL, markerAPI.getMarkerIcon("skull"));
markerIcons.put(Icon.QUEST_INTERACT, markerAPI.getMarkerIcon("comment"));
markerIcons.put(Icon.BLACKSMITH, markerAPI.getMarkerIcon("hammer"));
markerIcons.put(Icon.SENTINEL, markerAPI.getMarkerIcon("shield"));
this.markerIcons = markerIcons;
}
}