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:
parent
fbeb03db5e
commit
39c0e81a9e
@ -1,6 +1,7 @@
|
||||
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.trait.BlacksmithHandler;
|
||||
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.quests.QuestsHandler;
|
||||
import net.knarcraft.dynmapcitizens.settings.GlobalSettings;
|
||||
import net.knarcraft.knarlib.formatting.StringFormatter;
|
||||
import net.knarcraft.knarlib.formatting.TranslatableTimeUnit;
|
||||
import net.knarcraft.knarlib.formatting.Translator;
|
||||
import org.bukkit.Bukkit;
|
||||
@ -26,6 +28,7 @@ public final class DynmapCitizens extends JavaPlugin {
|
||||
|
||||
private static DynmapCitizens instance;
|
||||
private static Translator translator;
|
||||
private static StringFormatter stringFormatter;
|
||||
private DynmapAPI dynmapAPIInstance;
|
||||
private VaultHandler vaultHandler;
|
||||
private GlobalSettings globalSettings;
|
||||
@ -57,8 +60,10 @@ public final class DynmapCitizens extends JavaPlugin {
|
||||
//Load all messages
|
||||
translator = new Translator();
|
||||
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");
|
||||
stringFormatter = new StringFormatter(this.getDescription().getName(), translator);
|
||||
|
||||
//Initialize all enabled traits
|
||||
initializeTraitHandlers(configuration);
|
||||
@ -89,6 +94,15 @@ public final class DynmapCitizens extends JavaPlugin {
|
||||
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
|
||||
*
|
||||
|
@ -5,22 +5,7 @@ import net.knarcraft.knarlib.formatting.TranslatableMessage;
|
||||
/**
|
||||
* An enum describing all of DynmapCitizens' translatable messages
|
||||
*/
|
||||
public enum DynmapCitizensTranslatableMessage 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,
|
||||
public enum QuestsTranslatableMessage implements TranslatableMessage {
|
||||
|
||||
/**
|
||||
* The format for a quest's planner description
|
||||
@ -150,6 +135,6 @@ public enum DynmapCitizensTranslatableMessage implements TranslatableMessage {
|
||||
|
||||
@Override
|
||||
public TranslatableMessage[] getAllMessages() {
|
||||
return DynmapCitizensTranslatableMessage.values();
|
||||
return QuestsTranslatableMessage.values();
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
@ -2,18 +2,37 @@ package net.knarcraft.dynmapcitizens.handler.trait.quests;
|
||||
|
||||
import me.blackvein.quests.quests.IQuest;
|
||||
import me.blackvein.quests.quests.Requirements;
|
||||
import net.knarcraft.dynmapcitizens.DynmapCitizens;
|
||||
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 java.util.ArrayList;
|
||||
import java.util.List;
|
||||
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
|
||||
*/
|
||||
public class QuestRequirementsInfoGenerator {
|
||||
|
||||
private final IQuest quest;
|
||||
private final StringFormatter formatter;
|
||||
|
||||
/**
|
||||
* Instantiates a new quest requirement info generator
|
||||
@ -22,6 +41,7 @@ public class QuestRequirementsInfoGenerator {
|
||||
*/
|
||||
public QuestRequirementsInfoGenerator(IQuest quest) {
|
||||
this.quest = quest;
|
||||
formatter = DynmapCitizens.getFormatter();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -31,78 +51,122 @@ public class QuestRequirementsInfoGenerator {
|
||||
*/
|
||||
public String getQuestRequirementsInfo() {
|
||||
Requirements requirements = quest.getRequirements();
|
||||
StringBuilder requirementInfo = new StringBuilder();
|
||||
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) {
|
||||
requirementInfo.append("<li>").append(requirements.getQuestPoints()).append(" quest points</li>");
|
||||
}
|
||||
//Add info about quest point requirement
|
||||
replacer.add("{requirementQuestPoints}", requirements.getQuestPoints() > 0 ?
|
||||
formatter.replacePlaceholder(QUESTS_REQUIREMENTS_QUEST_POINTS,
|
||||
"{questPoints}", String.valueOf(requirements.getQuestPoints())) : "");
|
||||
|
||||
if (requirements.getExp() > 0) {
|
||||
requirementInfo.append("<li>").append(requirements.getExp()).append(" exp</li>");
|
||||
}
|
||||
//Add info about exp requirement
|
||||
replacer.add("{requirementExp}", requirements.getExp() > 0 ? formatter.replacePlaceholder(
|
||||
QUESTS_REQUIREMENTS_EXP, "{exp}", String.valueOf(requirements.getExp())) : "");
|
||||
|
||||
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>");
|
||||
}
|
||||
//Add info about blocking quests
|
||||
replacer.add("{requirementBlockedByQuests}", !requirements.getBlockQuests().isEmpty() ?
|
||||
getRequirementList(getQuestNames(requirements.getBlockQuests()),
|
||||
QUESTS_REQUIREMENTS_BLOCKED_BY_QUEST_FORMAT, "{blockingQuests}",
|
||||
QUESTS_REQUIREMENTS_BLOCKED_BY_QUEST_ITEM, "{questName}") : "");
|
||||
|
||||
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>");
|
||||
}
|
||||
//Add info about required quests
|
||||
replacer.add("{requirementRequiredQuests}", !requirements.getNeededQuests().isEmpty() ?
|
||||
getRequirementList(getQuestNames(requirements.getNeededQuests()),
|
||||
QUESTS_REQUIREMENTS_REQUIRED_QUEST_FORMAT, "{requiredQuests}",
|
||||
QUESTS_REQUIREMENTS_REQUIRED_QUEST_ITEM, "{questName}") : "");
|
||||
|
||||
if (!requirements.getItems().isEmpty()) {
|
||||
requirementInfo.append("<li>Required items:<ul>");
|
||||
for (ItemStack item : requirements.getItems()) {
|
||||
requirementInfo.append("<li>").append(QuestsHelper.getUpperCasedItemStackString(item)).append("</li>");
|
||||
}
|
||||
requirementInfo.append("</ul></li>");
|
||||
}
|
||||
//Add info about required items
|
||||
replacer.add("{requirementRequiredItems}", !requirements.getItems().isEmpty() ?
|
||||
getRequirementList(getItemNames(requirements.getItems()), QUESTS_REQUIREMENTS_REQUIRED_ITEM_FORMAT,
|
||||
"{requiredItems}", QUESTS_REQUIREMENTS_REQUIRED_ITEM_ITEM, "{itemName}") : "");
|
||||
|
||||
//Add info about required mcMMO skills
|
||||
if (!requirements.getMcmmoSkills().isEmpty()) {
|
||||
List<String> skills = requirements.getMcmmoSkills();
|
||||
List<Integer> amounts = requirements.getMcmmoAmounts();
|
||||
StringBuilder mcMMOSkillsBuilder = new StringBuilder();
|
||||
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>");
|
||||
mcMMOSkillsBuilder.append(formatter.replacePlaceholders(QUESTS_REQUIREMENTS_MC_MMO_SKILL, new String[]{
|
||||
"{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()) {
|
||||
requirementInfo.append("<li>Required permissions:<ul>");
|
||||
for (String permission : requirements.getPermissions()) {
|
||||
requirementInfo.append("<li>").append(permission).append("</li>");
|
||||
}
|
||||
requirementInfo.append("</ul></li>");
|
||||
}
|
||||
//Add info about required permissions
|
||||
replacer.add("{requirementPermissions}", !requirements.getPermissions().isEmpty() ?
|
||||
getRequirementList(requirements.getPermissions(), QUESTS_REQUIREMENTS_REQUIRED_PERMISSION_FORMAT,
|
||||
"{permissions}", QUESTS_REQUIREMENTS_REQUIRED_PERMISSION_ITEM, "{permission}") : "");
|
||||
|
||||
Map<String, Map<String, Object>> customRequirementPlugins = requirements.getCustomRequirements();
|
||||
StringBuilder customRequirementsBuilder = new StringBuilder();
|
||||
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
|
||||
// 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(" ");
|
||||
requirementInfo.append(customRequirementEntry.get(requirementDescription)).append("</li>");
|
||||
customRequirementsBuilder.append("<li>").append(requirementDescription).append(" ");
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user