Puts code for different citizens traits in separate modules

Also fixes blacksmith integration to be able to display detailed information about each blacksmith
This commit is contained in:
Kristian Knarvik 2022-10-19 12:41:26 +02:00
parent 2f535f9aa7
commit 5fa6408f90
10 changed files with 352 additions and 189 deletions

View File

@ -114,5 +114,11 @@
<version>3.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.knarcraft</groupId>
<artifactId>blacksmith</artifactId>
<version>1.0.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,111 @@
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();
}
}
}, 5 * 20, 120 * 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;
}
}

View File

@ -1,11 +1,15 @@
package net.knarcraft.questsdynmap;
package net.knarcraft.dynmapcitizens;
/**
* Every icon type used
*/
public enum Icon {
QUEST_GIVER,
QUEST_INTERACT,
QUEST_KILL,
QUEST_DELIVER,
SENTINEL,
BLACKSMITH
}

View File

@ -0,0 +1,77 @@
package net.knarcraft.dynmapcitizens.trait;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.knarcraft.dynmapcitizens.DynmapCitizens;
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;
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 <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;
}
Marker marker = markerSet.createMarker(null, markerName + npc.getName(), npcLocation.getWorld().getName(),
npcLocation.getX(), npcLocation.getY(), npcLocation.getZ(), icon, false);
if (marker != null) {
marker.setDescription(markerDescription);
}
}
}

View File

@ -0,0 +1,83 @@
package net.knarcraft.dynmapcitizens.trait;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.trait.Trait;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.config.NPCSettings;
import net.knarcraft.blacksmith.trait.BlacksmithTrait;
import net.knarcraft.dynmapcitizens.DynmapCitizens;
import net.knarcraft.dynmapcitizens.Icon;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.dynmap.DynmapAPI;
import org.dynmap.markers.GenericMarker;
import org.dynmap.markers.MarkerSet;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
/**
* A handler class for the blacksmith trait
*/
public class BlacksmithHandler extends AbstractTraitHandler {
private MarkerSet blacksmithSet;
@Override
public void initialize() {
BlacksmithPlugin blacksmithPlugin = (BlacksmithPlugin) Bukkit.getServer().getPluginManager().getPlugin("Blacksmith");
DynmapAPI dynmapAPI = DynmapCitizens.getInstance().getDynmapAPI();
if (blacksmithPlugin != null) {
blacksmithSet = getMarkerSet(dynmapAPI, "blacksmiths", "Blacksmiths");
if (blacksmithSet != null) {
blacksmithSet.setHideByDefault(false);
isEnabled = true;
return;
}
}
isEnabled = false;
}
@Override
public void updateMarkers() {
//Remove existing markers
blacksmithSet.getMarkers().forEach(GenericMarker::deleteMarker);
Class<? extends Trait> blacksmithTrait = CitizensAPI.getTraitFactory().getTraitClass("blacksmith");
for (NPC npc : CitizensAPI.getNPCRegistry()) {
if (npc.hasTrait(blacksmithTrait)) {
BlacksmithTrait trait = CitizensAPI.getTraitFactory().getTrait("blacksmith");
String description = null;
if (trait == null) {
DynmapCitizens.getInstance().getLogger().log(Level.WARNING, "Unable to get blacksmith trait");
} else {
NPCSettings npcSettings = trait.getSettings();
//TODO: Make a setting which allows disabling extra information (except title and reforge-able items?)
description = "<b>Blacksmith name:</b> " + npc.getName() + "<br><b>Blacksmith title:</b> " +
npcSettings.getBlacksmithTitle() + "<br><b>Fail chance:</b> " + npcSettings.getFailChance() +
"<br><b>Enchantment chance:</b> " + npcSettings.getExtraEnchantmentChance() +
"<br><b>Reforge-able items:</b> " + getReforgeAbleItemsString(npcSettings.getReforgeAbleItems());
}
addNPCMarker(npc.getUniqueId(), "Blacksmith NPC: ", description,
DynmapCitizens.getInstance().getMarkerIcons().get(Icon.BLACKSMITH), blacksmithSet);
}
}
}
/**
* Gets reforge-able items as a string
*
* @param materials <p>The materials specified as reforge-able items</p>
* @return <p>The reforge-able items</p>
*/
private static String getReforgeAbleItemsString(List<Material> materials) {
List<String> materialNames = new ArrayList<>();
for (Material material : materials) {
materialNames.add(material.name());
}
return String.join(", ", materialNames);
}
}

View File

@ -0,0 +1,22 @@
package net.knarcraft.dynmapcitizens.trait;
public interface CitizensTraitHandler {
/**
* Initializes this trait handler
*/
void initialize();
/**
* Gets whether this handler was properly initialized and enabled
*
* @return <p>True if this handler was enabled</p>
*/
boolean isEnabled();
/**
* Updates all markers used for this handler
*/
void updateMarkers();
}

View File

@ -1,74 +1,56 @@
package net.knarcraft.questsdynmap;
package net.knarcraft.dynmapcitizens.trait;
import me.blackvein.quests.QuestsAPI;
import me.blackvein.quests.quests.IQuest;
import me.blackvein.quests.quests.IStage;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.trait.Trait;
import net.knarcraft.dynmapcitizens.DynmapCitizens;
import net.knarcraft.dynmapcitizens.Icon;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.dynmap.DynmapAPI;
import org.dynmap.markers.CircleMarker;
import org.dynmap.markers.GenericMarker;
import org.dynmap.markers.Marker;
import org.dynmap.markers.MarkerIcon;
import org.dynmap.markers.MarkerSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.logging.Level;
/**
* A class for rendering icons and areas on dynmap
*/
public class DynmapDrawer {
public class QuestsHandler extends AbstractTraitHandler {
private static MarkerSet questMarkerSet;
private static MarkerSet questAreaMarkerSet;
private static MarkerSet blacksmithSet;
private static QuestsAPI questsAPI;
private static Map<Icon, MarkerIcon> markerIcons;
private QuestsAPI questsAPI;
private MarkerSet questMarkerSet;
private MarkerSet questAreaMarkerSet;
private Map<Icon, MarkerIcon> markerIcons;
private DynmapDrawer() {
}
/**
* Initializes the dynmap drawer
*
* @param questsAPI <p>The quests API to get quest information from</p>
* @param questMarkerSet <p>The marker set for quests</p>
* @param questAreaMarkerSet <p>The marker set for quest areas</p>
* @param blacksmithSet <p>The marker set for blacksmiths</p>
* @param markerIcons <p>The marker icons to use</p>
*/
public static void initialize(QuestsAPI questsAPI, MarkerSet questMarkerSet, MarkerSet questAreaMarkerSet,
MarkerSet blacksmithSet, Map<Icon, MarkerIcon> markerIcons) {
if (DynmapDrawer.questsAPI != null || DynmapDrawer.questMarkerSet != null ||
DynmapDrawer.questAreaMarkerSet != null || DynmapDrawer.markerIcons != null) {
throw new IllegalStateException("The dynmap drawer has already been initialized");
@Override
public void initialize() {
questsAPI = (QuestsAPI) Bukkit.getServer().getPluginManager().getPlugin("Quests");
DynmapAPI dynmapAPI = DynmapCitizens.getInstance().getDynmapAPI();
markerIcons = DynmapCitizens.getInstance().getMarkerIcons();
if (questsAPI != null) {
questMarkerSet = getMarkerSet(dynmapAPI, "quests", "Quests");
questAreaMarkerSet = getMarkerSet(dynmapAPI, "quest_areas", "Quest areas");
if (questMarkerSet != null && questAreaMarkerSet != null) {
questMarkerSet.setHideByDefault(false);
questAreaMarkerSet.setHideByDefault(true);
isEnabled = true;
//TODO: Initialize the dynmap drawer with the marker sets
}
}
DynmapDrawer.questsAPI = questsAPI;
DynmapDrawer.questMarkerSet = questMarkerSet;
DynmapDrawer.questAreaMarkerSet = questAreaMarkerSet;
DynmapDrawer.blacksmithSet = blacksmithSet;
DynmapDrawer.markerIcons = new HashMap<>(markerIcons);
}
/**
* Updates markers for all quests
*/
public static void updateCitizensMarkers() {
@Override
public void updateMarkers() {
if (questsAPI.isLoading()) {
return;
}
//Remove old quest markers
questMarkerSet.getMarkers().forEach(GenericMarker::deleteMarker);
blacksmithSet.getMarkers().forEach(GenericMarker::deleteMarker);
questAreaMarkerSet.getCircleMarkers().forEach(GenericMarker::deleteMarker);
for (IQuest quest : questsAPI.getLoadedQuests()) {
@ -85,15 +67,6 @@ public class DynmapDrawer {
//TODO: See if there is anything to do against overlapping markers
}
}
//Mark all blacksmiths on the map
Class<? extends Trait> blacksmithTrait = CitizensAPI.getTraitFactory().getTraitClass("blacksmith");
for (NPC npc : CitizensAPI.getNPCRegistry()) {
if (npc.hasTrait(blacksmithTrait)) {
addNPCMarker(npc.getUniqueId(), "Blacksmith NPC: ", "",
markerIcons.get(Icon.BLACKSMITH), blacksmithSet);
}
}
}
/**
@ -102,7 +75,7 @@ public class DynmapDrawer {
* @param quest <p>The quest the stage belongs to</p>
* @param stage <p>The stage to search for reach locations</p>
*/
private static void markReachLocations(IQuest quest, IStage stage) {
private void markReachLocations(IQuest quest, IStage stage) {
markLocations(stage.getLocationsToReach(), stage.getRadiiToReachWithin(),
"<b>Target location for:</b> " + quest.getName() + "<br><b>Description:</b> " +
quest.getDescription());
@ -114,7 +87,7 @@ public class DynmapDrawer {
* @param quest <p>The quest the stage belongs to</p>
* @param stage <p>The stage to search for kill locations</p>
*/
private static void markKillLocations(IQuest quest, IStage stage) {
private void markKillLocations(IQuest quest, IStage stage) {
markLocations(stage.getLocationsToKillWithin(), stage.getRadiiToKillWithin(),
"<b>Kill location for:</b> " + quest.getName() + "<br><b>Description:</b> " +
quest.getDescription());
@ -127,7 +100,7 @@ public class DynmapDrawer {
* @param radii <p>The radius of each location's circle</p>
* @param description <p>The description for what the location means</p>
*/
private static void markLocations(List<Location> locations, List<Integer> radii, String description) {
private void markLocations(List<Location> locations, List<Integer> radii, String description) {
for (int i = 0; i < locations.size(); i++) {
Location location = locations.get(i);
int radius = radii.get(i);
@ -157,7 +130,7 @@ public class DynmapDrawer {
* @param stage <p>The quest stage to mark NPCs for</p>
* @param npcStartId <p>The id of the quest's start NPC</p>
*/
private static void markNPCsInQuest(IQuest quest, IStage stage, UUID npcStartId) {
private void markNPCsInQuest(IQuest quest, IStage stage, UUID npcStartId) {
String markerDescription = "Part of quest: " + quest.getName();
//Mark all interaction targets
for (UUID interactNpcId : stage.getNpcsToInteract()) {
@ -178,34 +151,4 @@ public class DynmapDrawer {
}
}
/**
* 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>
*/
private static 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;
}
Marker marker = markerSet.createMarker(null, markerName + npc.getName(), npcLocation.getWorld().getName(),
npcLocation.getX(), npcLocation.getY(), npcLocation.getZ(), icon, false);
if (marker != null) {
marker.setDescription(markerDescription);
}
}
}

View File

@ -0,0 +1,15 @@
package net.knarcraft.dynmapcitizens.trait;
public class SentinelHandler extends AbstractTraitHandler {
@Override
public void initialize() {
}
@Override
public void updateMarkers() {
}
}

View File

@ -1,99 +0,0 @@
package net.knarcraft.questsdynmap;
import me.blackvein.quests.QuestsAPI;
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 org.dynmap.markers.MarkerSet;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
@SuppressWarnings("unused")
public final class DynmapCitizens extends JavaPlugin {
private static DynmapCitizens instance;
@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;
}
QuestsAPI questsAPI = (QuestsAPI) Bukkit.getServer().getPluginManager().getPlugin("Quests");
MarkerSet questMarkerSet = getMarkerSet(dynmapAPI, "quests", "Quests");
MarkerSet questAreaMarkerSet = getMarkerSet(dynmapAPI, "quest_areas", "Quest areas");
MarkerSet blacksmithSet = getMarkerSet(dynmapAPI, "blacksmiths", "Blacksmiths");
if (questMarkerSet == null || questAreaMarkerSet == null || blacksmithSet == null) {
return;
}
//TODO: Perhaps make this configurable
questMarkerSet.setHideByDefault(false);
questAreaMarkerSet.setHideByDefault(true);
blacksmithSet.setHideByDefault(false);
//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"));
DynmapDrawer.initialize(questsAPI, questMarkerSet, questAreaMarkerSet, blacksmithSet, markerIcons);
Bukkit.getScheduler().runTaskTimer(this, this::updateIcons, 10 * 20, 120 * 20);
}
@Override
public void onDisable() {
// Plugin shutdown logic
}
/**
* Gets an instance of this plugin
*
* @return <p>An instance of this plugin</p>
*/
public static DynmapCitizens getInstance() {
return instance;
}
/**
* 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>
*/
private 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) {
this.getLogger().log(Level.SEVERE, "Unable to get or create " + setId + " marker set");
this.onDisable();
return null;
}
}
return markerSet;
}
private void updateIcons() {
DynmapDrawer.updateCitizensMarkers();
}
}

View File

@ -1,8 +1,9 @@
name: DynmapCitizens
version: '${project.version}'
main: net.knarcraft.questsdynmap.DynmapCitizens
main: net.knarcraft.dynmapcitizens.DynmapCitizens
api-version: 1.19
prefix: DynmapCitizens
depend: [ dynmap, Quests, Citizens ]
depend: [ dynmap, Citizens ]
softdepend: [ Blacksmith, Sentinel, Quests ]
authors: [ EpicKnarvik97 ]
description: A plugin for displaying citizens info on the dynmap map