2022-11-01 19:18:00 +01:00
|
|
|
package net.knarcraft.dynmapcitizens.handler.trait;
|
2022-10-19 12:41:26 +02:00
|
|
|
|
|
|
|
import net.citizensnpcs.api.CitizensAPI;
|
|
|
|
import net.citizensnpcs.api.npc.NPC;
|
|
|
|
import net.knarcraft.dynmapcitizens.DynmapCitizens;
|
2022-11-03 14:14:24 +01:00
|
|
|
import net.knarcraft.dynmapcitizens.settings.TraitSettings;
|
2022-11-02 01:51:51 +01:00
|
|
|
import org.bukkit.Bukkit;
|
2022-10-19 12:41:26 +02:00
|
|
|
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;
|
|
|
|
|
2022-10-19 12:58:12 +02:00
|
|
|
/**
|
|
|
|
* An abstract class with useful code for all citizens-trait-handlers
|
|
|
|
*/
|
2022-10-19 12:41:26 +02:00
|
|
|
public abstract class AbstractTraitHandler implements CitizensTraitHandler {
|
|
|
|
|
|
|
|
protected boolean isEnabled = false;
|
2022-11-03 14:14:24 +01:00
|
|
|
protected MarkerSet markerSet;
|
2022-10-19 12:41:26 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isEnabled() {
|
|
|
|
return isEnabled;
|
|
|
|
}
|
|
|
|
|
2022-11-03 14:14:24 +01:00
|
|
|
/**
|
|
|
|
* Initializes this trait's marker set
|
|
|
|
*/
|
|
|
|
protected void initializeMarkerSet() {
|
|
|
|
TraitSettings settings = getSettings();
|
|
|
|
DynmapAPI dynmapAPI = DynmapCitizens.getInstance().getDynmapAPI();
|
|
|
|
markerSet = getMarkerSet(dynmapAPI, settings.getMarkerSetId(), settings.getMarkerSetName());
|
|
|
|
if (markerSet != null) {
|
|
|
|
markerSet.setHideByDefault(settings.markersHiddenByDefault());
|
|
|
|
markerSet.setLayerPriority(settings.getMarkerSetPriority());
|
|
|
|
isEnabled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-19 12:41:26 +02:00
|
|
|
/**
|
|
|
|
* Gets the given marker set (and creates it if necessary)
|
|
|
|
*
|
|
|
|
* @param dynmapAPI <p>A reference to dynmap's API</p>
|
|
|
|
* @param setId <p>The id of the marker set to get</p>
|
|
|
|
* @param label <p>The label of the marker set if creation is necessary</p>
|
|
|
|
* @return <p>The marker set, or null if something went wrong</p>
|
|
|
|
*/
|
|
|
|
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 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;
|
|
|
|
}
|
|
|
|
|
2022-11-03 01:05:30 +01:00
|
|
|
//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.
|
|
|
|
|
2022-11-02 01:51:51 +01:00
|
|
|
Marker marker = markerSet.createMarker(npcId.toString(), markerName + npc.getName(),
|
|
|
|
npcLocation.getWorld().getName(), npcLocation.getX(), npcLocation.getY(), npcLocation.getZ(), icon,
|
|
|
|
false);
|
2022-10-19 12:41:26 +02:00
|
|
|
if (marker != null) {
|
|
|
|
marker.setDescription(markerDescription);
|
|
|
|
}
|
2022-11-02 01:51:51 +01:00
|
|
|
|
|
|
|
//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(),
|
2022-11-03 14:14:24 +01:00
|
|
|
npc.getStoredLocation().getY(), npc.getStoredLocation().getZ()), 20,
|
|
|
|
DynmapCitizens.getInstance().getGlobalSettings().getUpdateMovingNPCDelay() * 20);
|
2022-11-02 01:51:51 +01:00
|
|
|
}
|
2022-10-19 12:41:26 +02:00
|
|
|
}
|
|
|
|
|
2022-10-20 20:40:17 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2022-10-19 12:41:26 +02:00
|
|
|
}
|