package net.knarcraft.dynmapcitizens.handler.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.property.Icon; import net.knarcraft.dynmapcitizens.property.UpdateRate; 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); blacksmithSet.setLayerPriority(3); isEnabled = true; return; } } isEnabled = false; } @Override public UpdateRate getUpdateRate() { return UpdateRate.VERY_SLOW; } @Override public void updateMarkers() { //Remove existing markers blacksmithSet.getMarkers().forEach(GenericMarker::deleteMarker); Class blacksmithTrait = CitizensAPI.getTraitFactory().getTraitClass("blacksmith"); for (NPC npc : CitizensAPI.getNPCRegistry()) { if (npc.hasTrait(blacksmithTrait)) { BlacksmithTrait trait = npc.getTraitNullable(BlacksmithTrait.class); String description = null; if (trait == null) { DynmapCitizens.getInstance().getLogger().log(Level.WARNING, "Unable to get blacksmith trait"); } else { //TODO: Make a setting which allows disabling extra information (except title and reforge-able items?) description = getDetailedBlacksmithInfo(npc, trait.getSettings()); } addNPCMarker(npc.getUniqueId(), "Blacksmith NPC: ", description, DynmapCitizens.getInstance().getMarkerIcons().get(Icon.BLACKSMITH), blacksmithSet); } } } /** * Gets detailed information about a blacksmith NPC * * @param npc

The NPC the settings belong to

* @param npcSettings

The settings to search for information

* @return

A string describing the blacksmith

*/ private String getDetailedBlacksmithInfo(NPC npc, NPCSettings npcSettings) { String info = "

" + npc.getName() + " the " + npcSettings.getBlacksmithTitle() + "

Fail chance: " + npcSettings.getFailChance() + "
Enchantment chance: " + npcSettings.getExtraEnchantmentChance() + "
Delay: " + npcSettings.getMinReforgeDelay() + " to " + npcSettings.getMaxReforgeDelay() + " seconds
Cool-down: " + npcSettings.getReforgeCoolDown() + " seconds
Drop item: " + npcSettings.getDropItem() + "
Max enchantments: " + npcSettings.getMaxEnchantments(); if (!npcSettings.getReforgeAbleItems().isEmpty()) { info += "
Reforge-able items: " + getReforgeAbleItemsString(npcSettings.getReforgeAbleItems()); } return info; } /** * Gets reforge-able items as a string * * @param materials

The materials specified as reforge-able items

* @return

The reforge-able items

*/ private static String getReforgeAbleItemsString(List materials) { List materialNames = new ArrayList<>(); for (Material material : materials) { materialNames.add(material.name()); } return String.join(", ", materialNames); } }