Implements translatable messages for quest requirements

Splits DynmapCitizensTranslatableMessage into QuestsTranslatableMessage and SentinelTranslatableMessage.
Adds a new method in DynmapCitizens to get a string formatter.
Replaces hard-coded formatting with formatting defined in strings.yml.
This commit is contained in:
Kristian Knarvik 2022-11-16 16:35:50 +01:00
parent fbeb03db5e
commit 39c0e81a9e
4 changed files with 158 additions and 64 deletions

View File

@ -1,6 +1,7 @@
package net.knarcraft.dynmapcitizens; package net.knarcraft.dynmapcitizens;
import net.knarcraft.dynmapcitizens.formatting.DynmapCitizensTranslatableMessage; import net.knarcraft.dynmapcitizens.formatting.QuestsTranslatableMessage;
import net.knarcraft.dynmapcitizens.formatting.SentinelTranslatableMessage;
import net.knarcraft.dynmapcitizens.handler.VaultHandler; import net.knarcraft.dynmapcitizens.handler.VaultHandler;
import net.knarcraft.dynmapcitizens.handler.trait.BlacksmithHandler; import net.knarcraft.dynmapcitizens.handler.trait.BlacksmithHandler;
import net.knarcraft.dynmapcitizens.handler.trait.CitizensTraitHandler; import net.knarcraft.dynmapcitizens.handler.trait.CitizensTraitHandler;
@ -8,6 +9,7 @@ import net.knarcraft.dynmapcitizens.handler.trait.MinstrelHandler;
import net.knarcraft.dynmapcitizens.handler.trait.SentinelHandler; import net.knarcraft.dynmapcitizens.handler.trait.SentinelHandler;
import net.knarcraft.dynmapcitizens.handler.trait.quests.QuestsHandler; import net.knarcraft.dynmapcitizens.handler.trait.quests.QuestsHandler;
import net.knarcraft.dynmapcitizens.settings.GlobalSettings; import net.knarcraft.dynmapcitizens.settings.GlobalSettings;
import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.knarlib.formatting.TranslatableTimeUnit; import net.knarcraft.knarlib.formatting.TranslatableTimeUnit;
import net.knarcraft.knarlib.formatting.Translator; import net.knarcraft.knarlib.formatting.Translator;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -26,6 +28,7 @@ public final class DynmapCitizens extends JavaPlugin {
private static DynmapCitizens instance; private static DynmapCitizens instance;
private static Translator translator; private static Translator translator;
private static StringFormatter stringFormatter;
private DynmapAPI dynmapAPIInstance; private DynmapAPI dynmapAPIInstance;
private VaultHandler vaultHandler; private VaultHandler vaultHandler;
private GlobalSettings globalSettings; private GlobalSettings globalSettings;
@ -57,8 +60,10 @@ public final class DynmapCitizens extends JavaPlugin {
//Load all messages //Load all messages
translator = new Translator(); translator = new Translator();
translator.registerMessageCategory(TranslatableTimeUnit.UNIT_SECOND); translator.registerMessageCategory(TranslatableTimeUnit.UNIT_SECOND);
translator.registerMessageCategory(DynmapCitizensTranslatableMessage.SENTINEL_DESCRIPTION); translator.registerMessageCategory(QuestsTranslatableMessage.QUESTS_REQUIREMENTS_FORMAT);
translator.registerMessageCategory(SentinelTranslatableMessage.SENTINEL_DESCRIPTION);
translator.loadLanguages(this.getDataFolder(), "en"); translator.loadLanguages(this.getDataFolder(), "en");
stringFormatter = new StringFormatter(this.getDescription().getName(), translator);
//Initialize all enabled traits //Initialize all enabled traits
initializeTraitHandlers(configuration); initializeTraitHandlers(configuration);
@ -89,6 +94,15 @@ public final class DynmapCitizens extends JavaPlugin {
return translator; return translator;
} }
/**
* Gets the string formatter to use for formatting
*
* @return <p>The string formatter to use</p>
*/
public static StringFormatter getFormatter() {
return stringFormatter;
}
/** /**
* Gets the global settings for this plugin * Gets the global settings for this plugin
* *

View File

@ -5,22 +5,7 @@ import net.knarcraft.knarlib.formatting.TranslatableMessage;
/** /**
* An enum describing all of DynmapCitizens' translatable messages * An enum describing all of DynmapCitizens' translatable messages
*/ */
public enum DynmapCitizensTranslatableMessage implements TranslatableMessage { public enum QuestsTranslatableMessage implements TranslatableMessage {
/**
* The format for the basic description of any sentinel
*
* <p>Placeholders: {name}, {squad}, {sentinelDetails}</p>
*/
SENTINEL_DESCRIPTION,
/**
* The format for the detailed description of any sentinel
*
* <p>Placeholders: {invincible}, {armor}, {health}, {accuracy}, {damage}, {speed}, {allowKnockback}, {range},
* {reach}, {targets}, {avoids}, {ignores}</p>
*/
SENTINEL_DETAILS,
/** /**
* The format for a quest's planner description * The format for a quest's planner description
@ -150,6 +135,6 @@ public enum DynmapCitizensTranslatableMessage implements TranslatableMessage {
@Override @Override
public TranslatableMessage[] getAllMessages() { public TranslatableMessage[] getAllMessages() {
return DynmapCitizensTranslatableMessage.values(); return QuestsTranslatableMessage.values();
} }
} }

View File

@ -0,0 +1,31 @@
package net.knarcraft.dynmapcitizens.formatting;
import net.knarcraft.knarlib.formatting.TranslatableMessage;
/**
* An enum describing all translatable messages for sentinels
*/
public enum SentinelTranslatableMessage implements TranslatableMessage {
/**
* The format for the basic description of any sentinel
*
* <p>Placeholders: {name}, {squad}, {sentinelDetails}</p>
*/
SENTINEL_DESCRIPTION,
/**
* The format for the detailed description of any sentinel
*
* <p>Placeholders: {invincible}, {armor}, {health}, {accuracy}, {damage}, {speed}, {allowKnockback}, {range},
* {reach}, {targets}, {avoids}, {ignores}</p>
*/
SENTINEL_DETAILS,
;
@Override
public TranslatableMessage[] getAllMessages() {
return SentinelTranslatableMessage.values();
}
}

View File

@ -2,18 +2,37 @@ package net.knarcraft.dynmapcitizens.handler.trait.quests;
import me.blackvein.quests.quests.IQuest; import me.blackvein.quests.quests.IQuest;
import me.blackvein.quests.quests.Requirements; import me.blackvein.quests.quests.Requirements;
import net.knarcraft.dynmapcitizens.DynmapCitizens;
import net.knarcraft.dynmapcitizens.util.QuestsHelper; import net.knarcraft.dynmapcitizens.util.QuestsHelper;
import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.knarlib.formatting.StringReplacer;
import net.knarcraft.knarlib.formatting.TranslatableMessage;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static net.knarcraft.dynmapcitizens.formatting.QuestsTranslatableMessage.QUESTS_REQUIREMENTS_BLOCKED_BY_QUEST_FORMAT;
import static net.knarcraft.dynmapcitizens.formatting.QuestsTranslatableMessage.QUESTS_REQUIREMENTS_BLOCKED_BY_QUEST_ITEM;
import static net.knarcraft.dynmapcitizens.formatting.QuestsTranslatableMessage.QUESTS_REQUIREMENTS_EXP;
import static net.knarcraft.dynmapcitizens.formatting.QuestsTranslatableMessage.QUESTS_REQUIREMENTS_FORMAT;
import static net.knarcraft.dynmapcitizens.formatting.QuestsTranslatableMessage.QUESTS_REQUIREMENTS_MC_MMO_SKILL;
import static net.knarcraft.dynmapcitizens.formatting.QuestsTranslatableMessage.QUESTS_REQUIREMENTS_QUEST_POINTS;
import static net.knarcraft.dynmapcitizens.formatting.QuestsTranslatableMessage.QUESTS_REQUIREMENTS_REQUIRED_ITEM_FORMAT;
import static net.knarcraft.dynmapcitizens.formatting.QuestsTranslatableMessage.QUESTS_REQUIREMENTS_REQUIRED_ITEM_ITEM;
import static net.knarcraft.dynmapcitizens.formatting.QuestsTranslatableMessage.QUESTS_REQUIREMENTS_REQUIRED_PERMISSION_FORMAT;
import static net.knarcraft.dynmapcitizens.formatting.QuestsTranslatableMessage.QUESTS_REQUIREMENTS_REQUIRED_PERMISSION_ITEM;
import static net.knarcraft.dynmapcitizens.formatting.QuestsTranslatableMessage.QUESTS_REQUIREMENTS_REQUIRED_QUEST_FORMAT;
import static net.knarcraft.dynmapcitizens.formatting.QuestsTranslatableMessage.QUESTS_REQUIREMENTS_REQUIRED_QUEST_ITEM;
/** /**
* A class to generate a string containing all information about a quest's requirements * A class to generate a string containing all information about a quest's requirements
*/ */
public class QuestRequirementsInfoGenerator { public class QuestRequirementsInfoGenerator {
private final IQuest quest; private final IQuest quest;
private final StringFormatter formatter;
/** /**
* Instantiates a new quest requirement info generator * Instantiates a new quest requirement info generator
@ -22,6 +41,7 @@ public class QuestRequirementsInfoGenerator {
*/ */
public QuestRequirementsInfoGenerator(IQuest quest) { public QuestRequirementsInfoGenerator(IQuest quest) {
this.quest = quest; this.quest = quest;
formatter = DynmapCitizens.getFormatter();
} }
/** /**
@ -31,78 +51,122 @@ public class QuestRequirementsInfoGenerator {
*/ */
public String getQuestRequirementsInfo() { public String getQuestRequirementsInfo() {
Requirements requirements = quest.getRequirements(); Requirements requirements = quest.getRequirements();
StringBuilder requirementInfo = new StringBuilder();
if (!requirements.hasRequirement()) { if (!requirements.hasRequirement()) {
return requirementInfo.toString(); return "";
} }
requirementInfo.append("<b>Requirements: </b><ul>"); StringReplacer replacer = new StringReplacer(DynmapCitizens.getTranslator().getTranslatedMessage(
QUESTS_REQUIREMENTS_FORMAT));
if (requirements.getQuestPoints() > 0) { //Add info about quest point requirement
requirementInfo.append("<li>").append(requirements.getQuestPoints()).append(" quest points</li>"); replacer.add("{requirementQuestPoints}", requirements.getQuestPoints() > 0 ?
} formatter.replacePlaceholder(QUESTS_REQUIREMENTS_QUEST_POINTS,
"{questPoints}", String.valueOf(requirements.getQuestPoints())) : "");
if (requirements.getExp() > 0) { //Add info about exp requirement
requirementInfo.append("<li>").append(requirements.getExp()).append(" exp</li>"); replacer.add("{requirementExp}", requirements.getExp() > 0 ? formatter.replacePlaceholder(
} QUESTS_REQUIREMENTS_EXP, "{exp}", String.valueOf(requirements.getExp())) : "");
if (!requirements.getBlockQuests().isEmpty()) { //Add info about blocking quests
requirementInfo.append("<li>Blocked by quests:<ul>"); replacer.add("{requirementBlockedByQuests}", !requirements.getBlockQuests().isEmpty() ?
for (IQuest blockQuest : requirements.getBlockQuests()) { getRequirementList(getQuestNames(requirements.getBlockQuests()),
requirementInfo.append("<li>").append(blockQuest.getName()).append("</li>"); QUESTS_REQUIREMENTS_BLOCKED_BY_QUEST_FORMAT, "{blockingQuests}",
} QUESTS_REQUIREMENTS_BLOCKED_BY_QUEST_ITEM, "{questName}") : "");
requirementInfo.append("</ul></li>");
}
if (!requirements.getNeededQuests().isEmpty()) { //Add info about required quests
requirementInfo.append("<li>Required quests:<ul>"); replacer.add("{requirementRequiredQuests}", !requirements.getNeededQuests().isEmpty() ?
for (IQuest neededQuest : requirements.getNeededQuests()) { getRequirementList(getQuestNames(requirements.getNeededQuests()),
requirementInfo.append("<li>").append(neededQuest.getName()).append("</li>"); QUESTS_REQUIREMENTS_REQUIRED_QUEST_FORMAT, "{requiredQuests}",
} QUESTS_REQUIREMENTS_REQUIRED_QUEST_ITEM, "{questName}") : "");
requirementInfo.append("</ul></li>");
}
if (!requirements.getItems().isEmpty()) { //Add info about required items
requirementInfo.append("<li>Required items:<ul>"); replacer.add("{requirementRequiredItems}", !requirements.getItems().isEmpty() ?
for (ItemStack item : requirements.getItems()) { getRequirementList(getItemNames(requirements.getItems()), QUESTS_REQUIREMENTS_REQUIRED_ITEM_FORMAT,
requirementInfo.append("<li>").append(QuestsHelper.getUpperCasedItemStackString(item)).append("</li>"); "{requiredItems}", QUESTS_REQUIREMENTS_REQUIRED_ITEM_ITEM, "{itemName}") : "");
}
requirementInfo.append("</ul></li>");
}
//Add info about required mcMMO skills
if (!requirements.getMcmmoSkills().isEmpty()) { if (!requirements.getMcmmoSkills().isEmpty()) {
List<String> skills = requirements.getMcmmoSkills(); List<String> skills = requirements.getMcmmoSkills();
List<Integer> amounts = requirements.getMcmmoAmounts(); List<Integer> amounts = requirements.getMcmmoAmounts();
StringBuilder mcMMOSkillsBuilder = new StringBuilder();
for (int i = 0; i < skills.size(); i++) { for (int i = 0; i < skills.size(); i++) {
requirementInfo.append("<li>Requires mcMMO skill ").append(skills.get(i)).append(" at level "); mcMMOSkillsBuilder.append(formatter.replacePlaceholders(QUESTS_REQUIREMENTS_MC_MMO_SKILL, new String[]{
requirementInfo.append(amounts.get(i)).append("</li>"); "{skill}", "{level}"}, new String[]{skills.get(i), String.valueOf(amounts.get(i))}));
} }
replacer.add("{requirementMCMMOSkills}", mcMMOSkillsBuilder.toString());
} else {
replacer.add("{requirementMCMMOSkills}", "");
} }
if (!requirements.getPermissions().isEmpty()) { //Add info about required permissions
requirementInfo.append("<li>Required permissions:<ul>"); replacer.add("{requirementPermissions}", !requirements.getPermissions().isEmpty() ?
for (String permission : requirements.getPermissions()) { getRequirementList(requirements.getPermissions(), QUESTS_REQUIREMENTS_REQUIRED_PERMISSION_FORMAT,
requirementInfo.append("<li>").append(permission).append("</li>"); "{permissions}", QUESTS_REQUIREMENTS_REQUIRED_PERMISSION_ITEM, "{permission}") : "");
}
requirementInfo.append("</ul></li>");
}
Map<String, Map<String, Object>> customRequirementPlugins = requirements.getCustomRequirements(); Map<String, Map<String, Object>> customRequirementPlugins = requirements.getCustomRequirements();
StringBuilder customRequirementsBuilder = new StringBuilder();
for (String plugin : customRequirementPlugins.keySet()) { for (String plugin : customRequirementPlugins.keySet()) {
requirementInfo.append("<li>").append(plugin).append(":<ul>"); customRequirementsBuilder.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 //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 // 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. // 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); Map<String, Object> customRequirementEntry = customRequirementPlugins.get(plugin);
for (String requirementDescription : customRequirementEntry.keySet()) { for (String requirementDescription : customRequirementEntry.keySet()) {
requirementInfo.append("<li>").append(requirementDescription).append(" "); customRequirementsBuilder.append("<li>").append(requirementDescription).append(" ");
requirementInfo.append(customRequirementEntry.get(requirementDescription)).append("</li>"); customRequirementsBuilder.append(customRequirementEntry.get(requirementDescription)).append("</li>");
} }
requirementInfo.append("</ul></li>"); customRequirementsBuilder.append("</ul></li>");
}
replacer.add("{requirementCustom}", customRequirementsBuilder.toString());
return replacer.replace();
} }
requirementInfo.append("</ul>"); /**
return requirementInfo.toString(); * Gets a list of item names from the given list of items
*
* @param items <p>The items to get the names of</p>
* @return <p>The names of the given items</p>
*/
private List<String> getItemNames(List<ItemStack> items) {
List<String> itemNames = new ArrayList<>();
for (ItemStack itemStack : items) {
itemNames.add(QuestsHelper.getUpperCasedItemStackString(itemStack));
}
return itemNames;
}
/**
* Gets a list of the quest names for the given quests
*
* @param quests <p>The quests to get names for</p>
* @return <p>A list of quest names</p>
*/
private List<String> getQuestNames(List<IQuest> quests) {
List<String> questNames = new ArrayList<>(quests.size());
for (IQuest quest : quests) {
questNames.add(quest.getName());
}
return questNames;
}
/**
* Gets a string for the given list of requirements
*
* @param itemList <p>The items to display in the list of requirements</p>
* @param formatMessage <p>The translatable message describing the list format</p>
* @param formatPlaceholder <p>The placeholder to replace with the list items</p>
* @param itemMessage <p>The translatable message describing each item's format</p>
* @param itemPlaceholder <p>The placeholder to replace with each item in the list</p>
* @return <p>The string corresponding to the given requirement list</p>
*/
private String getRequirementList(List<String> itemList, TranslatableMessage formatMessage, String formatPlaceholder,
TranslatableMessage itemMessage, String itemPlaceholder) {
StringBuilder blockedBuilder = new StringBuilder();
for (Object requirements : itemList) {
blockedBuilder.append(formatter.replacePlaceholder(itemMessage, itemPlaceholder,
String.valueOf(requirements)));
}
return formatter.replacePlaceholder(formatMessage, formatPlaceholder, blockedBuilder.toString());
} }
} }