DynmapCitizens/src/main/java/net/knarcraft/dynmapcitizens/handler/trait/AbstractTraitHandler.java

97 lines
3.6 KiB
Java
Raw Normal View History

package net.knarcraft.dynmapcitizens.handler.trait;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.knarcraft.dynmapcitizens.DynmapCitizens;
import net.knarcraft.dynmapcitizens.settings.TraitSettings;
2022-11-04 01:13:38 +01:00
import net.knarcraft.dynmapcitizens.util.DynmapHelper;
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;
/**
* An abstract class with useful code for all citizens-trait-handlers
*/
public abstract class AbstractTraitHandler implements CitizensTraitHandler {
protected boolean isEnabled = false;
protected MarkerSet markerSet;
@Override
public boolean isEnabled() {
return isEnabled;
}
/**
* Initializes this trait's marker set
*/
protected void initializeMarkerSet() {
TraitSettings settings = getSettings();
DynmapAPI dynmapAPI = DynmapCitizens.getInstance().getDynmapAPI();
2022-11-04 01:13:38 +01:00
markerSet = DynmapHelper.getMarkerSet(dynmapAPI, settings.getMarkerSetId(), settings.getMarkerSetName());
if (markerSet != null) {
markerSet.setHideByDefault(settings.markersHiddenByDefault());
markerSet.setLayerPriority(settings.getMarkerSetPriority());
isEnabled = true;
}
}
/**
* Adds a marker for an NPC
*
* @param npcId <p>The if of the NPC</p>
* @param markerName <p>The name of the NPC marker</p>
* @param markerDescription <p>The description of the NPC marker</p>
* @param icon <p>The icon used for the marker</p>
* @param markerSet <p>The marker set to add the marker to</p>
*/
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,
DynmapCitizens.getInstance().getGlobalSettings().getUpdateMovingNPCDelay() * 20);
}
}
/**
* Gets whether the given NPC is currently moving
*
* @param npc <p>The NPC to check</p>
* @return <p>True if the NPC is currently moving about</p>
*/
protected boolean isMoving(NPC npc) {
return npc.getNavigator().getTargetAsLocation() != null;
}
}