diff --git a/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java b/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java index afe4d35..48d6a87 100644 --- a/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java +++ b/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java @@ -19,6 +19,7 @@ import net.knarcraft.blacksmith.listener.PlayerListener; import net.knarcraft.blacksmith.manager.EconomyManager; import net.knarcraft.blacksmith.trait.BlacksmithTrait; import net.knarcraft.knarlib.formatting.StringFormatter; +import net.knarcraft.knarlib.formatting.TranslatableMessage; import net.knarcraft.knarlib.formatting.TranslatableTimeUnit; import net.knarcraft.knarlib.formatting.Translator; import net.knarcraft.knarlib.util.UpdateChecker; @@ -73,6 +74,16 @@ public class BlacksmithPlugin extends JavaPlugin { return instance; } + /** + * Gets the translation of the given translatable message + * + * @param translatableMessage
The message to translate
+ * @returnThe message, in the language specified in the configuration
+ */ + public static @NotNull String translate(TranslatableMessage translatableMessage) { + return BlacksmithPlugin.getTranslator().getTranslatedMessage(translatableMessage); + } + /** * Gets settings for the blacksmith plugin's blacksmiths * diff --git a/src/main/java/net/knarcraft/blacksmith/command/EditCommand.java b/src/main/java/net/knarcraft/blacksmith/command/EditCommand.java new file mode 100644 index 0000000..d02c00e --- /dev/null +++ b/src/main/java/net/knarcraft/blacksmith/command/EditCommand.java @@ -0,0 +1,171 @@ +package net.knarcraft.blacksmith.command; + +import net.citizensnpcs.api.CitizensAPI; +import net.citizensnpcs.api.npc.NPC; +import net.knarcraft.blacksmith.BlacksmithPlugin; +import net.knarcraft.blacksmith.config.Setting; +import net.knarcraft.blacksmith.config.SettingValueType; +import net.knarcraft.blacksmith.config.Settings; +import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage; +import net.knarcraft.blacksmith.trait.CustomTrait; +import net.knarcraft.blacksmith.util.InputParsingHelper; +import net.knarcraft.blacksmith.util.TypeValidationHelper; +import net.md_5.bungee.api.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Arrays; +import java.util.logging.Level; + +import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getCurrentValueMessage; +import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getValueChangedMessage; + +/** + * A generic implementation of the edit command + * + * @paramThe type of trait to edit
+ * @paramThe type of setting the trait uses
+ */ +public abstract class EditCommandThe input given by the user
+ * @returnThe corresponding setting, or null if not recognized
+ */ + public abstract L getSetting(String input); + + /** + * Gets the global settings to use + * + * @returnThe global settings
+ */ + public abstract @NotNull SettingsThe trait belonging to the selected NPC
+ * @param settingThe NPC setting to change
+ * @param globalSettingsThe global settings to get default values from
+ * @param newValueThe value to change the setting to
+ * @param senderThe command sender to notify about results
+ * @returnTrue if everything went successfully
+ */ + private boolean displayOrChangeNPCSetting(@NotNull CustomTraitThe trait of the NPC to get the value from
+ * @param settingThe NPC setting to see the value of
+ * @param globalSettingsThe global settings to get default values from
+ * @param senderThe command sender to display the value to
+ */ + private void displayNPCSetting(@NotNull CustomTraitThe blacksmith trait belonging to the selected NPC
- * @param blacksmithSettingThe NPC setting to change
- * @param newValueThe value to change the setting to
- * @param senderThe command sender to notify about results
- * @returnTrue if everything went successfully
- */ - private boolean displayOrChangeNPCSetting(@NotNull BlacksmithTrait blacksmithTrait, - @NotNull BlacksmithSetting blacksmithSetting, - @Nullable String newValue, - @NotNull CommandSender sender) { - if (newValue == null) { - //Display the current value of the setting - displayNPCSetting(blacksmithTrait, blacksmithSetting, sender); - } else { - //If an empty value or null, clear the value instead of changing it - if (InputParsingHelper.isEmpty(newValue)) { - newValue = null; - } else { - //Abort if an invalid value is given - boolean isValidType = TypeValidationHelper.isValid(blacksmithSetting.getValueType(), newValue, sender); - if (!isValidType) { - return false; - } - newValue = ChatColor.translateAlternateColorCodes('&', newValue); - } - - //Change the setting - blacksmithTrait.getSettings().changeValue(blacksmithSetting, newValue); - BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, - getValueChangedMessage(blacksmithSetting.getCommandName(), String.valueOf(newValue))); - //Save the changes immediately to prevent data loss on server crash - CitizensAPI.getNPCRegistry().saveToStore(); - } - return true; - } - - /** - * Displays the current value of the given NPC setting - * - * @param blacksmithTraitThe blacksmith trait of the NPC to get the value from
- * @param blacksmithSettingThe NPC setting to see the value of
- * @param senderThe command sender to display the value to
- */ - private void displayNPCSetting(@NotNull BlacksmithTrait blacksmithTrait, - @NotNull BlacksmithSetting blacksmithSetting, @NotNull CommandSender sender) { - String rawValue = String.valueOf(blacksmithTrait.getSettings().getRawValue(blacksmithSetting)); - if (InputParsingHelper.isEmpty(rawValue)) { - //Display the default value, if no custom value has been specified - rawValue = String.valueOf(BlacksmithPlugin.getInstance().getGlobalBlacksmithSettings().getRawValue( - blacksmithSetting)); - BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, - getCurrentValueMessage(blacksmithSetting.getCommandName(), rawValue)); - } else { - //Add a marker if the value has been customized - String marker = BlacksmithPlugin.getTranslator().getTranslatedMessage( - BlacksmithTranslatableMessage.SETTING_OVERRIDDEN_MARKER); - BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, - getCurrentValueMessage(blacksmithSetting.getCommandName(), rawValue) + marker); - } - if (blacksmithSetting.getPath().startsWith("defaults.messages")) { - sender.sendMessage(BlacksmithTranslatableMessage.getRawValueMessage( - rawValue.replace(ChatColor.COLOR_CHAR, '&'))); - } + @Override + public @NotNull SettingsThe name of the used command
+ * @param commandValueThe command value used to filter tab-completions
+ * @returnSome valid options for the command's argument
+ */ + private @Nullable ListThe message to display
*/ public static String getRawValueMessage(String rawValue) { - return StringFormatter.replacePlaceholder(BlacksmithPlugin.getTranslator().getTranslatedMessage( + return StringFormatter.replacePlaceholder(BlacksmithPlugin.translate( BlacksmithTranslatableMessage.RAW_VALUE), "{rawValue}", rawValue); } @@ -156,7 +156,7 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage { * @returnThe string to display to a user
*/ public static String getValueChangedMessage(String setting, String newValue) { - return StringFormatter.replacePlaceholders(BlacksmithPlugin.getTranslator().getTranslatedMessage( + return StringFormatter.replacePlaceholders(BlacksmithPlugin.translate( BlacksmithTranslatableMessage.VALUE_CHANGED), new String[]{"{setting}", "{newValue}"}, new String[]{setting, newValue}); } @@ -171,7 +171,7 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage { * @returnThe string to display to a user
*/ public static String getItemValueChangedMessage(String setting, ItemType itemType, String item, String newValue) { - return StringFormatter.replacePlaceholders(BlacksmithPlugin.getTranslator().getTranslatedMessage( + return StringFormatter.replacePlaceholders(BlacksmithPlugin.translate( BlacksmithTranslatableMessage.VALUE_FOR_ITEM_CHANGED), new String[]{"{setting}", "{itemType}", "{item}", "{newValue}"}, new String[]{setting, itemType.getItemTypeName(), item, newValue}); @@ -185,7 +185,7 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage { * @returnThe string to display to a user
*/ public static String getCurrentValueMessage(String setting, String currentValue) { - return StringFormatter.replacePlaceholders(BlacksmithPlugin.getTranslator().getTranslatedMessage( + return StringFormatter.replacePlaceholders(BlacksmithPlugin.translate( BlacksmithTranslatableMessage.CURRENT_VALUE), new String[]{"{setting}", "{currentValue}"}, new String[]{setting, currentValue}); } @@ -200,7 +200,7 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage { * @returnThe string to display to a user
*/ public static String getItemCurrentValueMessage(String setting, ItemType itemType, String item, String currentValue) { - return StringFormatter.replacePlaceholders(BlacksmithPlugin.getTranslator().getTranslatedMessage( + return StringFormatter.replacePlaceholders(BlacksmithPlugin.translate( BlacksmithTranslatableMessage.CURRENT_VALUE_FOR_ITEM), new String[]{"{setting}", "{itemType}", "{item}", "{currentValue}"}, new String[]{setting, itemType.getItemTypeName(), item, currentValue}); diff --git a/src/main/java/net/knarcraft/blacksmith/formatting/ItemType.java b/src/main/java/net/knarcraft/blacksmith/formatting/ItemType.java index c0d24d4..235754a 100644 --- a/src/main/java/net/knarcraft/blacksmith/formatting/ItemType.java +++ b/src/main/java/net/knarcraft/blacksmith/formatting/ItemType.java @@ -17,10 +17,8 @@ public enum ItemType { */ public String getItemTypeName() { return switch (this) { - case MATERIAL -> BlacksmithPlugin.getTranslator().getTranslatedMessage( - BlacksmithTranslatableMessage.ITEM_TYPE_MATERIAL); - case ENCHANTMENT -> BlacksmithPlugin.getTranslator().getTranslatedMessage( - BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT); + case MATERIAL -> BlacksmithPlugin.translate(BlacksmithTranslatableMessage.ITEM_TYPE_MATERIAL); + case ENCHANTMENT -> BlacksmithPlugin.translate(BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT); }; } diff --git a/src/main/java/net/knarcraft/blacksmith/formatting/TimeFormatter.java b/src/main/java/net/knarcraft/blacksmith/formatting/TimeFormatter.java index 4936ead..51a625c 100644 --- a/src/main/java/net/knarcraft/blacksmith/formatting/TimeFormatter.java +++ b/src/main/java/net/knarcraft/blacksmith/formatting/TimeFormatter.java @@ -53,7 +53,7 @@ public final class TimeFormatter { * @returnText describing the time interval
*/ private static String getMessageFromInterval(TimeInterval interval) { - String text = BlacksmithPlugin.getTranslator().getTranslatedMessage(BlacksmithTranslatableMessage.valueOf(interval.name())); + String text = BlacksmithPlugin.translate(BlacksmithTranslatableMessage.valueOf(interval.name())); //Choose a random entry if a comma-separated list is provided if (text.contains(",")) { diff --git a/src/main/java/net/knarcraft/blacksmith/trait/BlacksmithTrait.java b/src/main/java/net/knarcraft/blacksmith/trait/BlacksmithTrait.java index 2df69b1..33acc87 100644 --- a/src/main/java/net/knarcraft/blacksmith/trait/BlacksmithTrait.java +++ b/src/main/java/net/knarcraft/blacksmith/trait/BlacksmithTrait.java @@ -3,6 +3,7 @@ package net.knarcraft.blacksmith.trait; import net.citizensnpcs.api.util.DataKey; import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.blacksmith.config.blacksmith.BlacksmithNPCSettings; +import net.knarcraft.blacksmith.config.blacksmith.BlacksmithSetting; import net.knarcraft.blacksmith.manager.EconomyManager; import net.knarcraft.blacksmith.util.ItemHelper; import net.knarcraft.knarlib.formatting.StringFormatter; @@ -19,7 +20,7 @@ import static net.knarcraft.blacksmith.formatting.BlacksmithStringFormatter.send /** * The class representing the Blacksmith NPC trait */ -public class BlacksmithTrait extends CustomTrait { +public class BlacksmithTrait extends CustomTraitThe trait settings to use
*/ - protected void setTraitSettings(TraitSettings config) { + protected void setTraitSettings(TraitSettingsThe settings used by this trait
+ */ + public @Nullable Settings