package net.knarcraft.dynmapcitizens.handler.trait; import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.npc.NPC; import net.knarcraft.dynmapcitizens.DynmapCitizens; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; import org.dynmap.DynmapAPI; import org.dynmap.markers.Marker; import org.dynmap.markers.MarkerIcon; import org.dynmap.markers.MarkerSet; import java.util.UUID; import java.util.logging.Level; /** * An abstract class with useful code for all citizens-trait-handlers */ public abstract class AbstractTraitHandler implements CitizensTraitHandler { protected boolean isEnabled = false; @Override public boolean isEnabled() { return isEnabled; } /** * Gets the given marker set (and creates it if necessary) * * @param dynmapAPI
A reference to dynmap's API
* @param setIdThe id of the marker set to get
* @param labelThe label of the marker set if creation is necessary
* @returnThe marker set, or null if something went wrong
*/ protected MarkerSet getMarkerSet(DynmapAPI dynmapAPI, String setId, String label) { MarkerSet markerSet = dynmapAPI.getMarkerAPI().getMarkerSet(setId); if (markerSet == null) { markerSet = dynmapAPI.getMarkerAPI().createMarkerSet(setId, label, null, false); if (markerSet == null) { DynmapCitizens.getInstance().getLogger().log(Level.SEVERE, "Unable to get or create " + setId + " marker set"); DynmapCitizens.getInstance().onDisable(); return null; } } return markerSet; } /** * Adds a marker for an NPC * * @param npcIdThe if of the NPC
* @param markerNameThe name of the NPC marker
* @param markerDescriptionThe description of the NPC marker
* @param iconThe icon used for the marker
* @param markerSetThe marker set to add the marker to
*/ protected void addNPCMarker(UUID npcId, String markerName, String markerDescription, MarkerIcon icon, MarkerSet markerSet) { NPC npc = CitizensAPI.getNPCRegistry().getByUniqueId(npcId); //If the NPC has been removed, abort if (npc == null) { return; } //Skip if not a proper location Location npcLocation = npc.getStoredLocation(); World world = npcLocation.getWorld(); if (world == null) { return; } //TODO: If marker already exists, and the NPC is in the same location, check if description has changed. If changed, update it. If not, do nothing. Marker marker = markerSet.createMarker(npcId.toString(), markerName + npc.getName(), npcLocation.getWorld().getName(), npcLocation.getX(), npcLocation.getY(), npcLocation.getZ(), icon, false); if (marker != null) { marker.setDescription(markerDescription); } //For NPCs that move around, update their marker's location every n seconds if (marker != null && isMoving(npc)) { Bukkit.getScheduler().scheduleSyncRepeatingTask(DynmapCitizens.getInstance(), () -> marker.setLocation(npcLocation.getWorld().getName(), npc.getStoredLocation().getX(), npc.getStoredLocation().getY(), npc.getStoredLocation().getZ()), 20, 10 * 20); //TODO: Make the update rate configurable } } /** * Gets whether the given NPC is currently moving * * @param npcThe NPC to check
* @returnTrue if the NPC is currently moving about
*/ protected boolean isMoving(NPC npc) { return npc.getNavigator().getTargetAsLocation() != null; } }