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

112 lines
3.5 KiB
Java
Raw Normal View History

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.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
Bukkit.getScheduler().runTaskTimer(this, () -> {
for (CitizensTraitHandler handler : handlers) {
if (handler.isEnabled()) {
handler.updateMarkers();
}
}
2022-10-19 13:20:57 +02:00
}, 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;
}
}