473 lines
20 KiB
Java
473 lines
20 KiB
Java
package net.knarcraft.dynmapcitizens.trait.quests;
|
|
|
|
import me.blackvein.quests.QuestsAPI;
|
|
import me.blackvein.quests.quests.IQuest;
|
|
import me.blackvein.quests.quests.IStage;
|
|
import me.blackvein.quests.quests.Requirements;
|
|
import me.blackvein.quests.quests.Rewards;
|
|
import net.citizensnpcs.api.CitizensAPI;
|
|
import net.citizensnpcs.api.npc.NPC;
|
|
import net.citizensnpcs.api.npc.NPCRegistry;
|
|
import net.knarcraft.dynmapcitizens.DynmapCitizens;
|
|
import net.knarcraft.dynmapcitizens.Icon;
|
|
import net.knarcraft.dynmapcitizens.UpdateRate;
|
|
import net.knarcraft.dynmapcitizens.trait.AbstractTraitHandler;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.Location;
|
|
import org.bukkit.World;
|
|
import org.bukkit.inventory.ItemStack;
|
|
import org.dynmap.DynmapAPI;
|
|
import org.dynmap.markers.CircleMarker;
|
|
import org.dynmap.markers.GenericMarker;
|
|
import org.dynmap.markers.MarkerIcon;
|
|
import org.dynmap.markers.MarkerSet;
|
|
|
|
import java.util.Collection;
|
|
import java.util.HashMap;
|
|
import java.util.HashSet;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.UUID;
|
|
import java.util.logging.Level;
|
|
|
|
/**
|
|
* A handler class for the quests trait
|
|
*/
|
|
public class QuestsHandler extends AbstractTraitHandler {
|
|
|
|
private QuestsAPI questsAPI;
|
|
private MarkerSet questMarkerSet;
|
|
private MarkerSet questAreaMarkerSet;
|
|
private Map<Icon, MarkerIcon> markerIcons;
|
|
private Collection<IQuest> loadedQuests;
|
|
private Map<UUID, NPCQuestInfo> questGiverInfo;
|
|
|
|
@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);
|
|
questMarkerSet.setLayerPriority(3);
|
|
questAreaMarkerSet.setLayerPriority(2);
|
|
isEnabled = true;
|
|
return;
|
|
}
|
|
}
|
|
isEnabled = false;
|
|
}
|
|
|
|
@Override
|
|
public UpdateRate getUpdateRate() {
|
|
return UpdateRate.VERY_SLOW;
|
|
}
|
|
|
|
@Override
|
|
public void updateMarkers() {
|
|
if (questsAPI.isLoading()) {
|
|
return;
|
|
}
|
|
|
|
questGiverInfo = new HashMap<>();
|
|
|
|
//There is no point in updating if there has been no changes in quests
|
|
boolean questsChanged = loadedQuests == null || !loadedQuests.equals(questsAPI.getLoadedQuests());
|
|
loadedQuests = questsAPI.getLoadedQuests();
|
|
|
|
//Remove old quest markers
|
|
questMarkerSet.getMarkers().forEach(GenericMarker::deleteMarker);
|
|
|
|
//Updates all quest area markers
|
|
if (questsChanged) {
|
|
updateQuestAreas();
|
|
}
|
|
|
|
NPCRegistry registry = CitizensAPI.getNPCRegistry();
|
|
|
|
//Generation information about NPC's parts in each quest
|
|
for (IQuest quest : questsAPI.getLoadedQuests()) {
|
|
if (quest.getNpcStart() != null) {
|
|
getInfo(quest.getNpcStart()).addQuestStart(quest);
|
|
}
|
|
for (IStage stage : quest.getStages()) {
|
|
for (UUID npcId : stage.getNpcsToKill()) {
|
|
getInfo(npcId).addQuestKill(quest);
|
|
}
|
|
for (UUID npcId : stage.getItemDeliveryTargets()) {
|
|
getInfo(npcId).addQuestDeliver(quest);
|
|
}
|
|
for (UUID npcId : stage.getNpcsToInteract()) {
|
|
getInfo(npcId).addQuestInteract(quest);
|
|
}
|
|
}
|
|
}
|
|
|
|
//Add markers for each NPC detected as part of a quest
|
|
for (UUID npcId : questGiverInfo.keySet()) {
|
|
NPCQuestInfo info = questGiverInfo.get(npcId);
|
|
MarkerIcon icon = markerIcons.get(info.getNPCIcon());
|
|
List<IQuest> questStarts = info.getQuestStarts();
|
|
List<IQuest> questKills = info.getQuestKills();
|
|
List<IQuest> questInteractions = info.getQuestInteractions();
|
|
List<IQuest> questDeliveries = info.getQuestDeliveries();
|
|
StringBuilder markerDescription = new StringBuilder();
|
|
|
|
markerDescription.append("<h2>").append(registry.getByUniqueId(npcId).getName()).append("</h2>");
|
|
|
|
if (!questStarts.isEmpty()) {
|
|
markerDescription.append("<h3>Quests offered:</h3><ul>");
|
|
for (IQuest quest : questStarts) {
|
|
markerDescription.append("<li><h4><b>").append(quest.getName()).append("</b></h4><h5><b>- ");
|
|
markerDescription.append(quest.getDescription()).append("</b></h5>").append(getQuestStagesInfo(quest));
|
|
markerDescription.append(getQuestRewardsInfo(quest)).append(getQuestRequirementsInfo(quest)).append("</li>");
|
|
}
|
|
markerDescription.append("</ul>");
|
|
}
|
|
//TODO: Get information about the planner (repeatable and/or limited)
|
|
|
|
if (!questKills.isEmpty() || !questInteractions.isEmpty() || !questDeliveries.isEmpty()) {
|
|
markerDescription.append("<h3>Involved in quests:</h3><ul>");
|
|
|
|
for (IQuest quest : new HashSet<>(questKills)) {
|
|
markerDescription.append("<li>Killed in: ").append(quest.getName()).append("</li>");
|
|
}
|
|
for (IQuest quest : new HashSet<>(questDeliveries)) {
|
|
markerDescription.append("<li>Delivery target in: ").append(quest.getName()).append("</li>");
|
|
}
|
|
for (IQuest quest : new HashSet<>(questInteractions)) {
|
|
markerDescription.append("<li>Interacted with in quest: ").append(quest.getName()).append("</li>");
|
|
}
|
|
|
|
markerDescription.append("</ul>");
|
|
}
|
|
|
|
addNPCMarker(npcId, getMarkerTitle(info.getQuestNPCType()), markerDescription.toString(), icon, questMarkerSet);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Gets information about all requirements for the given quest
|
|
*
|
|
* @param quest <p>The quest to get requirements for</p>
|
|
* @return <p>Information about the quest's requirements</p>
|
|
*/
|
|
private String getQuestRequirementsInfo(IQuest quest) {
|
|
Requirements requirements = quest.getRequirements();
|
|
StringBuilder requirementInfo = new StringBuilder();
|
|
if (!requirements.hasRequirement()) {
|
|
return requirementInfo.toString();
|
|
}
|
|
|
|
requirementInfo.append("<br><b>Requirements: </b><ul>");
|
|
|
|
if (requirements.getQuestPoints() > 0) {
|
|
requirementInfo.append("<li>").append(requirements.getQuestPoints()).append(" quest points</li>");
|
|
}
|
|
|
|
if (requirements.getExp() > 0) {
|
|
requirementInfo.append("<li>").append(requirements.getExp()).append(" exp</li>");
|
|
}
|
|
|
|
if (!requirements.getBlockQuests().isEmpty()) {
|
|
requirementInfo.append("<li>Blocked by quests:<ul>");
|
|
for (IQuest blockQuest : requirements.getBlockQuests()) {
|
|
requirementInfo.append("<li>").append(blockQuest.getName()).append("</li>");
|
|
}
|
|
requirementInfo.append("</ul></li>");
|
|
}
|
|
|
|
if (!requirements.getNeededQuests().isEmpty()) {
|
|
requirementInfo.append("<li>Required quests:<ul>");
|
|
for (IQuest neededQuest : requirements.getNeededQuests()) {
|
|
requirementInfo.append("<li>").append(neededQuest.getName()).append("</li>");
|
|
}
|
|
requirementInfo.append("</ul></li>");
|
|
}
|
|
|
|
if (!requirements.getItems().isEmpty()) {
|
|
requirementInfo.append("<li>Required items:<ul>");
|
|
for (ItemStack item : requirements.getItems()) {
|
|
requirementInfo.append("<li>").append(uppercaseFirst(getItemStackString(item))).append("</li>");
|
|
}
|
|
requirementInfo.append("</ul></li>");
|
|
}
|
|
|
|
if (!requirements.getMcmmoSkills().isEmpty()) {
|
|
List<String> skills = requirements.getMcmmoSkills();
|
|
List<Integer> amounts = requirements.getMcmmoAmounts();
|
|
for (int i = 0; i < skills.size(); i++) {
|
|
requirementInfo.append("<li>Requires mcMMO skill ").append(skills.get(i)).append(" at level ");
|
|
requirementInfo.append(amounts.get(i)).append("</li>");
|
|
}
|
|
}
|
|
|
|
if (!requirements.getPermissions().isEmpty()) {
|
|
requirementInfo.append("<li>Required permissions:<ul>");
|
|
for (String permission : requirements.getPermissions()) {
|
|
requirementInfo.append("<li>").append(permission).append("</li>");
|
|
}
|
|
requirementInfo.append("</ul></li>");
|
|
}
|
|
|
|
Map<String, Map<String, Object>> customRequirementPlugins = requirements.getCustomRequirements();
|
|
for (String plugin : customRequirementPlugins.keySet()) {
|
|
requirementInfo.append("<li>").append(plugin).append(":<ul>");
|
|
//Note: The format of custom requirements is kind of weird. First, you have the key for which plugin the
|
|
// requirement belongs to. Getting the value of the key gives another map. The map contains as key, the type
|
|
// of value, like "Skill Amount" or "Skill Type". The value is the actual value of whatever it is.
|
|
Map<String, Object> customRequirementEntry = customRequirementPlugins.get(plugin);
|
|
for (String requirementDescription : customRequirementEntry.keySet()) {
|
|
requirementInfo.append("<li>").append(requirementDescription).append(" ").append(customRequirementEntry.get(requirementDescription)).append("</li>");
|
|
}
|
|
requirementInfo.append("</ul></li>");
|
|
}
|
|
|
|
requirementInfo.append("</ul>");
|
|
return requirementInfo.toString();
|
|
}
|
|
|
|
/**
|
|
* Gets information about all rewards for the given quest
|
|
*
|
|
* @param quest <p>The quest to get reward information for</p>
|
|
* @return <p>Information about the quest's rewards</p>
|
|
*/
|
|
private String getQuestRewardsInfo(IQuest quest) {
|
|
Rewards reward = quest.getRewards();
|
|
StringBuilder rewardInfo = new StringBuilder();
|
|
rewardInfo.append("<br><b>Rewards:</b><ul>");
|
|
|
|
if (reward.getMoney() > 0) {
|
|
//TODO: Get the currency from Vault
|
|
rewardInfo.append("<li>").append(reward.getMoney()).append(" money").append("</li>");
|
|
}
|
|
|
|
if (reward.getExp() > 0) {
|
|
rewardInfo.append("<li>").append(reward.getMoney()).append(" exp").append("</li>");
|
|
}
|
|
|
|
for (String permission : reward.getPermissions()) {
|
|
rewardInfo.append("<li>").append("Permission: ").append(permission).append("</li>");
|
|
}
|
|
|
|
for (ItemStack item : reward.getItems()) {
|
|
rewardInfo.append("<li>").append(uppercaseFirst(getItemStackString(item))).append("</li>");
|
|
}
|
|
|
|
for (String command : reward.getCommands()) {
|
|
rewardInfo.append("<li>Command: ").append(command).append("</li>");
|
|
}
|
|
|
|
rewardInfo.append("</ul>");
|
|
return rewardInfo.toString();
|
|
}
|
|
|
|
/**
|
|
* Gets the marker title to use for the given quest NPC type
|
|
*
|
|
* @param type <p>The type of marker to get the title for</p>
|
|
* @return <p>The title to use for the marker</p>
|
|
*/
|
|
private String getMarkerTitle(QuestNPCType type) {
|
|
return switch (type) {
|
|
case GIVER -> "Quest Start NPC: ";
|
|
case INTERACT -> "Quest Interact NPC: ";
|
|
case DELIVER -> "Quest Deliver NPC: ";
|
|
case KILL -> "Quest Kill NPC: ";
|
|
case CHAIN -> "Quest Chain NPC: ";
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Gets the info object for the given NPC
|
|
*
|
|
* @param npcId <p>The id of the NPC to get information about</p>
|
|
* @return <p>The NPC's info object</p>
|
|
*/
|
|
private NPCQuestInfo getInfo(UUID npcId) {
|
|
if (questGiverInfo.get(npcId) == null) {
|
|
questGiverInfo.put(npcId, new NPCQuestInfo());
|
|
}
|
|
return questGiverInfo.get(npcId);
|
|
}
|
|
|
|
/**
|
|
* Updates all quest area markers
|
|
*/
|
|
private void updateQuestAreas() {
|
|
questAreaMarkerSet.getCircleMarkers().forEach(GenericMarker::deleteMarker);
|
|
for (IQuest quest : questsAPI.getLoadedQuests()) {
|
|
for (IStage stage : quest.getStages()) {
|
|
markKillLocations(quest, stage);
|
|
markReachLocations(quest, stage);
|
|
}
|
|
}
|
|
//TODO: Mark WorldGuard areas part of quests. Requires WorldGuard integration
|
|
//TODO: See if there is anything to do against overlapping markers
|
|
}
|
|
|
|
/**
|
|
* Gets information about a quest's stages
|
|
*
|
|
* @param quest <p>The quest to get information about</p>
|
|
* @return <p>A string with information about the quest's stages</p>
|
|
*/
|
|
private String getQuestStagesInfo(IQuest quest) {
|
|
StringBuilder questInfo = new StringBuilder();
|
|
NPCRegistry registry = CitizensAPI.getNPCRegistry();
|
|
for (IStage stage : quest.getStages()) {
|
|
questInfo.append("<br><b>Tasks:</b><ul>");
|
|
int mobTypes = stage.getMobsToKill().size();
|
|
for (int i = 0; i < mobTypes; i++) {
|
|
questInfo.append("<li>Kill ").append(normalizeName(stage.getMobsToKill().get(i).name())).append(
|
|
" X ").append(stage.getMobNumToKill().get(i)).append("</li>");
|
|
}
|
|
int deliveries = stage.getItemDeliveryTargets().size();
|
|
for (int i = 0; i < deliveries; i++) {
|
|
NPC npc = registry.getByUniqueId(stage.getItemDeliveryTargets().get(i));
|
|
questInfo.append("<li>Deliver ").append(getItemStackString(stage.getItemsToDeliver().get(i))).append(
|
|
" to ").append(npc.getName()).append("</li>");
|
|
}
|
|
if (stage.getFishToCatch() != null) {
|
|
questInfo.append("<li>Catch ").append(stage.getFishToCatch()).append(" fish").append("</li>");
|
|
}
|
|
for (UUID npcId : stage.getNpcsToKill()) {
|
|
questInfo.append("<li>Kill NPC ").append(registry.getByUniqueId(npcId).getName()).append("</li>");
|
|
}
|
|
|
|
questInfo.append(getQuestItemsTaskString(stage.getBlocksToBreak(), "<li>Break ")).append("</li>");
|
|
questInfo.append(getQuestItemsTaskString(stage.getBlocksToCut(), "<li>Cut ")).append("</li>");
|
|
questInfo.append(getQuestItemsTaskString(stage.getBlocksToDamage(), "<li>Damage ")).append("</li>");
|
|
questInfo.append(getQuestItemsTaskString(stage.getBlocksToUse(), "<li>Use ")).append("</li>");
|
|
questInfo.append(getQuestItemsTaskString(stage.getBlocksToPlace(), "<li>Place ")).append("</li>");
|
|
|
|
questInfo.append(getQuestItemsTaskString(stage.getItemsToBrew(), "<li>Brew ")).append("</li>");
|
|
questInfo.append(getQuestItemsTaskString(stage.getItemsToConsume(), "<li>Consume ")).append("</li>");
|
|
questInfo.append(getQuestItemsTaskString(stage.getItemsToCraft(), "<li>Craft ")).append("</li>");
|
|
questInfo.append(getQuestItemsTaskString(stage.getItemsToEnchant(), "<li>Enchant ")).append("</li>");
|
|
questInfo.append(getQuestItemsTaskString(stage.getItemsToSmelt(), "<li>Smelt ")).append("</li>");
|
|
|
|
int sheepTypes = stage.getSheepToShear().size();
|
|
for (int i = 0; i < sheepTypes; i++) {
|
|
questInfo.append("<li>Shear ").append(stage.getSheepNumToShear().get(i)).append(" ").append(
|
|
normalizeName(stage.getSheepToShear().get(i).name())).append(" sheep").append("</li>");
|
|
}
|
|
if (stage.getCowsToMilk() != null) {
|
|
questInfo.append("<li>Milk ").append(stage.getCowsToMilk()).append(" cows").append("</li>");
|
|
}
|
|
|
|
int mobTamingEntries = stage.getMobsToTame().size();
|
|
for (int i = 0; i < mobTamingEntries; i++) {
|
|
questInfo.append("<li>Tame ").append(stage.getMobNumToTame().get(i)).append(" ").append(
|
|
normalizeName(stage.getMobsToTame().get(i).name())).append("</li>");
|
|
}
|
|
|
|
questInfo.append("</ul>");
|
|
}
|
|
return questInfo.toString();
|
|
}
|
|
|
|
/**
|
|
* Gets a string to display a quest task involving some action on an item
|
|
*
|
|
* @param items <p>The items that are part of the task</p>
|
|
* @param explanation <p>The explanation of what the player needs to do with the items</p>
|
|
* @return <p>A string describing the necessary tasks</p>
|
|
*/
|
|
private String getQuestItemsTaskString(List<ItemStack> items, String explanation) {
|
|
StringBuilder questInfo = new StringBuilder();
|
|
for (ItemStack itemStack : items) {
|
|
questInfo.append(explanation).append(getItemStackString(itemStack));
|
|
}
|
|
return questInfo.toString();
|
|
}
|
|
|
|
/**
|
|
* Gets the proper string representation of an item stack
|
|
*
|
|
* @param itemStack <p>The item stack to print</p>
|
|
* @return <p>The string representation of the item stack</p>
|
|
*/
|
|
private String getItemStackString(ItemStack itemStack) {
|
|
return normalizeName(itemStack.getType().name()) + " X " + itemStack.getAmount();
|
|
}
|
|
|
|
/**
|
|
* Makes the first character in a string uppercase
|
|
*
|
|
* @param string <p>The string to run on</p>
|
|
* @return <p>The same string, with the first character converted to uppercase</p>
|
|
*/
|
|
private String uppercaseFirst(String string) {
|
|
return string.substring(0, 1).toUpperCase() + string.substring(1);
|
|
}
|
|
|
|
/**
|
|
* Normalizes an internal name to make it human-readable
|
|
*
|
|
* @param name <p>The name to normalize</p>
|
|
* @return <p>The normalized name</p>
|
|
*/
|
|
private String normalizeName(String name) {
|
|
return name.toLowerCase().replace("_", " ");
|
|
}
|
|
|
|
/**
|
|
* Marks any reach locations found in the given stage
|
|
*
|
|
* @param quest <p>The quest the stage belongs to</p>
|
|
* @param stage <p>The stage to search for reach locations</p>
|
|
*/
|
|
private void markReachLocations(IQuest quest, IStage stage) {
|
|
markLocations(stage.getLocationsToReach(), stage.getRadiiToReachWithin(),
|
|
"<b>Target location for:</b> " + quest.getName());
|
|
}
|
|
|
|
/**
|
|
* Marks any kill locations found in the given stage
|
|
*
|
|
* @param quest <p>The quest the stage belongs to</p>
|
|
* @param stage <p>The stage to search for kill locations</p>
|
|
*/
|
|
private void markKillLocations(IQuest quest, IStage stage) {
|
|
markLocations(stage.getLocationsToKillWithin(), stage.getRadiiToKillWithin(),
|
|
"<b>Kill location for:</b> " + quest.getName());
|
|
}
|
|
|
|
/**
|
|
* Marks the given locations on the dynamic map
|
|
*
|
|
* @param locations <p>The locations to mark</p>
|
|
* @param radii <p>The radius of each location's circle</p>
|
|
* @param description <p>The description for what the location means</p>
|
|
*/
|
|
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);
|
|
|
|
//Skip if location is invalid
|
|
World world = location.getWorld();
|
|
if (world == null) {
|
|
continue;
|
|
}
|
|
|
|
CircleMarker circleMarker = questAreaMarkerSet.createCircleMarker(null, description, true,
|
|
world.getName(), location.getX(), location.getY(), location.getZ(), radius, radius, false);
|
|
if (circleMarker == null) {
|
|
DynmapCitizens.getInstance().getLogger().log(Level.WARNING, "Unable to create circle marker at " +
|
|
location + " with radius " + radius);
|
|
} else {
|
|
circleMarker.setFillStyle(0.3, 0x75AFD2);
|
|
circleMarker.setLineStyle(1, 1.0, 0x36c90e);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|