Implements the scrapper edit command
This commit is contained in:
		| @@ -19,6 +19,7 @@ import net.knarcraft.blacksmith.listener.PlayerListener; | |||||||
| import net.knarcraft.blacksmith.manager.EconomyManager; | import net.knarcraft.blacksmith.manager.EconomyManager; | ||||||
| import net.knarcraft.blacksmith.trait.BlacksmithTrait; | import net.knarcraft.blacksmith.trait.BlacksmithTrait; | ||||||
| import net.knarcraft.knarlib.formatting.StringFormatter; | import net.knarcraft.knarlib.formatting.StringFormatter; | ||||||
|  | import net.knarcraft.knarlib.formatting.TranslatableMessage; | ||||||
| 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 net.knarcraft.knarlib.util.UpdateChecker; | import net.knarcraft.knarlib.util.UpdateChecker; | ||||||
| @@ -73,6 +74,16 @@ public class BlacksmithPlugin extends JavaPlugin { | |||||||
|         return instance; |         return instance; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * Gets the translation of the given translatable message | ||||||
|  |      * | ||||||
|  |      * @param translatableMessage <p>The message to translate</p> | ||||||
|  |      * @return <p>The message, in the language specified in the configuration</p> | ||||||
|  |      */ | ||||||
|  |     public static @NotNull String translate(TranslatableMessage translatableMessage) { | ||||||
|  |         return BlacksmithPlugin.getTranslator().getTranslatedMessage(translatableMessage); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * Gets settings for the blacksmith plugin's blacksmiths |      * Gets settings for the blacksmith plugin's blacksmiths | ||||||
|      * |      * | ||||||
|   | |||||||
							
								
								
									
										171
									
								
								src/main/java/net/knarcraft/blacksmith/command/EditCommand.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										171
									
								
								src/main/java/net/knarcraft/blacksmith/command/EditCommand.java
									
									
									
									
									
										Normal file
									
								
							| @@ -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 | ||||||
|  |  * | ||||||
|  |  * @param <K> <p>The type of trait to edit</p> | ||||||
|  |  * @param <L> <p>The type of setting the trait uses</p> | ||||||
|  |  */ | ||||||
|  | public abstract class EditCommand<K extends CustomTrait<L>, L extends Setting> implements CommandExecutor { | ||||||
|  |  | ||||||
|  |     protected Class<K> traitClass; | ||||||
|  |  | ||||||
|  |     public EditCommand(Class<K> traitClass) { | ||||||
|  |         this.traitClass = traitClass; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * Gets the setting corresponding to the given input | ||||||
|  |      * | ||||||
|  |      * @param input <p>The input given by the user</p> | ||||||
|  |      * @return <p>The corresponding setting, or null if not recognized</p> | ||||||
|  |      */ | ||||||
|  |     public abstract L getSetting(String input); | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * Gets the global settings to use | ||||||
|  |      * | ||||||
|  |      * @return <p>The global settings</p> | ||||||
|  |      */ | ||||||
|  |     public abstract @NotNull Settings<L> getGlobalSettings(); | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String s, | ||||||
|  |                              @NotNull String[] args) { | ||||||
|  |         NPC npc = CitizensAPI.getDefaultNPCSelector().getSelected(sender); | ||||||
|  |         if (npc == null || !npc.hasTrait(traitClass)) { | ||||||
|  |             BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender, | ||||||
|  |                     BlacksmithTranslatableMessage.NO_NPC_SELECTED); | ||||||
|  |             return true; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         if (args.length < 1) { | ||||||
|  |             return false; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         K trait = npc.getTraitNullable(traitClass); | ||||||
|  |         if (trait == null) { | ||||||
|  |             return false; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         L setting = getSetting(args[0]); | ||||||
|  |         if (setting != null) { | ||||||
|  |             String newValue = args.length < 2 ? null : args[1]; | ||||||
|  |             //This makes sure all arguments are treated as a sentence | ||||||
|  |             if (setting.getValueType() == SettingValueType.STRING && args.length > 2) { | ||||||
|  |                 newValue = String.join(" ", Arrays.asList(args).subList(1, args.length)); | ||||||
|  |             } | ||||||
|  |             Settings<L> settings = getGlobalSettings(); | ||||||
|  |             return displayOrChangeNPCSetting(trait, setting, settings, newValue, sender); | ||||||
|  |         } else { | ||||||
|  |             return false; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * Changes the given NPC setting, or displays the current value if a new value isn't specified | ||||||
|  |      * | ||||||
|  |      * @param trait          <p>The trait belonging to the selected NPC</p> | ||||||
|  |      * @param setting        <p>The NPC setting to change</p> | ||||||
|  |      * @param globalSettings <p>The global settings to get default values from</p> | ||||||
|  |      * @param newValue       <p>The value to change the setting to</p> | ||||||
|  |      * @param sender         <p>The command sender to notify about results</p> | ||||||
|  |      * @return <p>True if everything went successfully</p> | ||||||
|  |      */ | ||||||
|  |     private boolean displayOrChangeNPCSetting(@NotNull CustomTrait<L> trait, | ||||||
|  |                                               @NotNull L setting, | ||||||
|  |                                               @NotNull Settings<L> globalSettings, | ||||||
|  |                                               @Nullable String newValue, | ||||||
|  |                                               @NotNull CommandSender sender) { | ||||||
|  |         if (newValue == null) { | ||||||
|  |             //Display the current value of the setting | ||||||
|  |             displayNPCSetting(trait, setting, globalSettings, 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(setting.getValueType(), newValue, sender); | ||||||
|  |                 if (!isValidType) { | ||||||
|  |                     return false; | ||||||
|  |                 } | ||||||
|  |                 newValue = ChatColor.translateAlternateColorCodes('&', newValue); | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             //Change the setting | ||||||
|  |             Settings<L> settings = trait.getTraitSettings(); | ||||||
|  |             if (settings == null) { | ||||||
|  |                 BlacksmithPlugin.getInstance().getLogger().log(Level.SEVERE, "Settings for a CustomTrait has not " + | ||||||
|  |                         "been initialized! Please inform the developer!"); | ||||||
|  |                 return false; | ||||||
|  |             } | ||||||
|  |             settings.changeValue(setting, newValue); | ||||||
|  |             BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, | ||||||
|  |                     getValueChangedMessage(setting.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 trait          <p>The trait of the NPC to get the value from</p> | ||||||
|  |      * @param setting        <p>The NPC setting to see the value of</p> | ||||||
|  |      * @param globalSettings <p>The global settings to get default values from</p> | ||||||
|  |      * @param sender         <p>The command sender to display the value to</p> | ||||||
|  |      */ | ||||||
|  |     private void displayNPCSetting(@NotNull CustomTrait<L> trait, | ||||||
|  |                                    @NotNull L setting, | ||||||
|  |                                    @NotNull Settings<L> globalSettings, | ||||||
|  |                                    @NotNull CommandSender sender) { | ||||||
|  |         Settings<L> settings = trait.getTraitSettings(); | ||||||
|  |         if (settings == null) { | ||||||
|  |             BlacksmithPlugin.getInstance().getLogger().log(Level.SEVERE, "Settings for a CustomTrait has not " + | ||||||
|  |                     "been initialized! Please inform the developer!"); | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|  |         String rawValue = String.valueOf(settings.getRawValue(setting)); | ||||||
|  |         if (InputParsingHelper.isEmpty(rawValue)) { | ||||||
|  |             //Display the default value, if no custom value has been specified | ||||||
|  |             rawValue = String.valueOf(globalSettings.getRawValue(setting)); | ||||||
|  |             BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, | ||||||
|  |                     getCurrentValueMessage(setting.getCommandName(), rawValue)); | ||||||
|  |         } else { | ||||||
|  |             //Add a marker if the value has been customized | ||||||
|  |             String marker = BlacksmithPlugin.translate(BlacksmithTranslatableMessage.SETTING_OVERRIDDEN_MARKER); | ||||||
|  |             BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, | ||||||
|  |                     getCurrentValueMessage(setting.getCommandName(), rawValue) + marker); | ||||||
|  |         } | ||||||
|  |         if (setting.isMessage()) { | ||||||
|  |             sender.sendMessage(BlacksmithTranslatableMessage.getRawValueMessage( | ||||||
|  |                     rawValue.replace(ChatColor.COLOR_CHAR, '&'))); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  | } | ||||||
| @@ -57,7 +57,7 @@ public class PresetCommand implements CommandExecutor { | |||||||
|             materialNames.add(material.name()); |             materialNames.add(material.name()); | ||||||
|         } |         } | ||||||
|         BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, StringFormatter.replacePlaceholder( |         BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, StringFormatter.replacePlaceholder( | ||||||
|                 BlacksmithPlugin.getTranslator().getTranslatedMessage(BlacksmithTranslatableMessage.PRESET_MATERIALS), |                 BlacksmithPlugin.translate(BlacksmithTranslatableMessage.PRESET_MATERIALS), | ||||||
|                 "{materials}", String.join(", ", materialNames))); |                 "{materials}", String.join(", ", materialNames))); | ||||||
|         return true; |         return true; | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -1,126 +1,32 @@ | |||||||
| package net.knarcraft.blacksmith.command.blacksmith; | package net.knarcraft.blacksmith.command.blacksmith; | ||||||
|  |  | ||||||
| import net.citizensnpcs.api.CitizensAPI; |  | ||||||
| import net.citizensnpcs.api.npc.NPC; |  | ||||||
| import net.knarcraft.blacksmith.BlacksmithPlugin; | import net.knarcraft.blacksmith.BlacksmithPlugin; | ||||||
| import net.knarcraft.blacksmith.config.SettingValueType; | import net.knarcraft.blacksmith.command.EditCommand; | ||||||
|  | import net.knarcraft.blacksmith.config.Settings; | ||||||
| import net.knarcraft.blacksmith.config.blacksmith.BlacksmithSetting; | import net.knarcraft.blacksmith.config.blacksmith.BlacksmithSetting; | ||||||
| import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage; |  | ||||||
| import net.knarcraft.blacksmith.trait.BlacksmithTrait; | import net.knarcraft.blacksmith.trait.BlacksmithTrait; | ||||||
| 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.NotNull; | ||||||
| import org.jetbrains.annotations.Nullable; |  | ||||||
|  |  | ||||||
| import java.util.Arrays; |  | ||||||
|  |  | ||||||
| import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getCurrentValueMessage; |  | ||||||
| import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getValueChangedMessage; |  | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * The main command used for blacksmith editing |  * The main command used for blacksmith editing | ||||||
|  */ |  */ | ||||||
| public class BlackSmithEditCommand implements CommandExecutor { | public class BlackSmithEditCommand extends EditCommand<BlacksmithTrait, BlacksmithSetting> { | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * Instantiates a new blacksmith edit command | ||||||
|  |      */ | ||||||
|  |     public BlackSmithEditCommand() { | ||||||
|  |         super(BlacksmithTrait.class); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, |     public BlacksmithSetting getSetting(String input) { | ||||||
|                              @NotNull String[] args) { |         return BlacksmithSetting.getSetting(input); | ||||||
|         NPC npc = CitizensAPI.getDefaultNPCSelector().getSelected(sender); |  | ||||||
|         if (npc == null || !npc.hasTrait(BlacksmithTrait.class)) { |  | ||||||
|             BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender, |  | ||||||
|                     BlacksmithTranslatableMessage.NO_NPC_SELECTED); |  | ||||||
|             return true; |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|         if (args.length < 1) { |     @Override | ||||||
|             return false; |     public @NotNull Settings<BlacksmithSetting> getGlobalSettings() { | ||||||
|         } |         return BlacksmithPlugin.getInstance().getGlobalBlacksmithSettings(); | ||||||
|  |  | ||||||
|         BlacksmithTrait blacksmithTrait = npc.getTraitNullable(BlacksmithTrait.class); |  | ||||||
|  |  | ||||||
|         BlacksmithSetting setting = BlacksmithSetting.getSetting(args[0]); |  | ||||||
|         if (setting != null) { |  | ||||||
|             String newValue = args.length < 2 ? null : args[1]; |  | ||||||
|             //This makes sure all arguments are treated as a sentence |  | ||||||
|             if (setting.getValueType() == SettingValueType.STRING && args.length > 2) { |  | ||||||
|                 newValue = String.join(" ", Arrays.asList(args).subList(1, args.length)); |  | ||||||
|             } |  | ||||||
|             return displayOrChangeNPCSetting(blacksmithTrait, setting, newValue, sender); |  | ||||||
|         } else { |  | ||||||
|             return false; |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /** |  | ||||||
|      * Changes the given NPC setting, or displays the current value if a new value isn't specified |  | ||||||
|      * |  | ||||||
|      * @param blacksmithTrait   <p>The blacksmith trait belonging to the selected NPC</p> |  | ||||||
|      * @param blacksmithSetting <p>The NPC setting to change</p> |  | ||||||
|      * @param newValue          <p>The value to change the setting to</p> |  | ||||||
|      * @param sender            <p>The command sender to notify about results</p> |  | ||||||
|      * @return <p>True if everything went successfully</p> |  | ||||||
|      */ |  | ||||||
|     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 blacksmithTrait   <p>The blacksmith trait of the NPC to get the value from</p> |  | ||||||
|      * @param blacksmithSetting <p>The NPC setting to see the value of</p> |  | ||||||
|      * @param sender            <p>The command sender to display the value to</p> |  | ||||||
|      */ |  | ||||||
|     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, '&'))); |  | ||||||
|         } |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,15 +1,32 @@ | |||||||
| package net.knarcraft.blacksmith.command.scrapper; | package net.knarcraft.blacksmith.command.scrapper; | ||||||
|  |  | ||||||
| import org.apache.commons.lang.NotImplementedException; | import net.knarcraft.blacksmith.BlacksmithPlugin; | ||||||
| import org.bukkit.command.Command; | import net.knarcraft.blacksmith.command.EditCommand; | ||||||
| import org.bukkit.command.CommandExecutor; | import net.knarcraft.blacksmith.config.Settings; | ||||||
| import org.bukkit.command.CommandSender; | import net.knarcraft.blacksmith.config.scrapper.ScrapperSetting; | ||||||
|  | import net.knarcraft.blacksmith.trait.ScrapperTrait; | ||||||
| import org.jetbrains.annotations.NotNull; | import org.jetbrains.annotations.NotNull; | ||||||
|  |  | ||||||
| public class ScrapperEditCommand implements CommandExecutor { | /** | ||||||
|     @Override |  * The main command used for scrapper editing | ||||||
|     public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, |  */ | ||||||
|                              @NotNull String[] strings) { | public class ScrapperEditCommand extends EditCommand<ScrapperTrait, ScrapperSetting> { | ||||||
|         throw new NotImplementedException(); |  | ||||||
|  |     /** | ||||||
|  |      * Instantiates a new scrapper edit command | ||||||
|  |      */ | ||||||
|  |     public ScrapperEditCommand() { | ||||||
|  |         super(ScrapperTrait.class); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public ScrapperSetting getSetting(String input) { | ||||||
|  |         return ScrapperSetting.getSetting(input); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public @NotNull Settings<ScrapperSetting> getGlobalSettings() { | ||||||
|  |         return BlacksmithPlugin.getInstance().getGlobalScrapperSettings(); | ||||||
|  |     } | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,18 +1,62 @@ | |||||||
| package net.knarcraft.blacksmith.command.scrapper; | package net.knarcraft.blacksmith.command.scrapper; | ||||||
|  |  | ||||||
| import org.apache.commons.lang.NotImplementedException; | import net.knarcraft.blacksmith.config.scrapper.ScrapperSetting; | ||||||
|  | import net.knarcraft.blacksmith.util.TabCompleteValuesHelper; | ||||||
|  | import net.knarcraft.knarlib.util.TabCompletionHelper; | ||||||
| import org.bukkit.command.Command; | import org.bukkit.command.Command; | ||||||
| import org.bukkit.command.CommandSender; | import org.bukkit.command.CommandSender; | ||||||
| import org.bukkit.command.TabCompleter; | import org.bukkit.command.TabCompleter; | ||||||
| import org.jetbrains.annotations.NotNull; | import org.jetbrains.annotations.NotNull; | ||||||
| import org.jetbrains.annotations.Nullable; | import org.jetbrains.annotations.Nullable; | ||||||
|  |  | ||||||
|  | import java.util.ArrayList; | ||||||
| import java.util.List; | import java.util.List; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * The tab completer for the scrapper editing command | ||||||
|  |  */ | ||||||
| public class ScrapperEditTabCompleter implements TabCompleter { | public class ScrapperEditTabCompleter implements TabCompleter { | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public @Nullable List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, |     public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, | ||||||
|                                                 @NotNull String s, @NotNull String[] strings) { |                                                 @NotNull String s, @NotNull String[] args) { | ||||||
|         throw new NotImplementedException(); |         if (!sender.hasPermission("blacksmith.edit")) { | ||||||
|  |             return new ArrayList<>(); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |         List<String> npcSettings = new ArrayList<>(); | ||||||
|  |         for (ScrapperSetting setting : ScrapperSetting.values()) { | ||||||
|  |             if (setting.isPerNPC()) { | ||||||
|  |                 npcSettings.add(setting.getCommandName()); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         if (args.length == 1) { | ||||||
|  |             return TabCompletionHelper.filterMatchingContains(npcSettings, args[0]); | ||||||
|  |         } else { | ||||||
|  |             if (npcSettings.contains(args[0]) && args.length == 2) { | ||||||
|  |                 return tabCompleteCommandValues(args[0], args[1]); | ||||||
|  |             } else { | ||||||
|  |                 return new ArrayList<>(); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * Tab completes the values available for the given command | ||||||
|  |      * | ||||||
|  |      * @param commandName  <p>The name of the used command</p> | ||||||
|  |      * @param commandValue <p>The command value used to filter tab-completions</p> | ||||||
|  |      * @return <p>Some valid options for the command's argument</p> | ||||||
|  |      */ | ||||||
|  |     private @Nullable List<String> tabCompleteCommandValues(@NotNull String commandName, @NotNull String commandValue) { | ||||||
|  |         ScrapperSetting setting = ScrapperSetting.getSetting(commandName); | ||||||
|  |         if (setting != null) { | ||||||
|  |             return TabCompletionHelper.filterMatchingContains(TabCompleteValuesHelper.getTabCompletions( | ||||||
|  |                     setting.getValueType()), commandValue); | ||||||
|  |         } else { | ||||||
|  |             return null; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,6 +1,6 @@ | |||||||
| package net.knarcraft.blacksmith.config; | package net.knarcraft.blacksmith.config; | ||||||
|  |  | ||||||
| public interface TraitSettings { | public interface TraitSettings<K extends Setting> extends Settings<K> { | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * Gets whether to disable the action cool-down |      * Gets whether to disable the action cool-down | ||||||
|   | |||||||
| @@ -3,7 +3,6 @@ package net.knarcraft.blacksmith.config.blacksmith; | |||||||
| import net.citizensnpcs.api.util.DataKey; | import net.citizensnpcs.api.util.DataKey; | ||||||
| import net.knarcraft.blacksmith.BlacksmithPlugin; | import net.knarcraft.blacksmith.BlacksmithPlugin; | ||||||
| import net.knarcraft.blacksmith.config.SettingValueType; | import net.knarcraft.blacksmith.config.SettingValueType; | ||||||
| import net.knarcraft.blacksmith.config.Settings; |  | ||||||
| import net.knarcraft.blacksmith.config.SmithPreset; | import net.knarcraft.blacksmith.config.SmithPreset; | ||||||
| import net.knarcraft.blacksmith.config.TraitSettings; | import net.knarcraft.blacksmith.config.TraitSettings; | ||||||
| import net.knarcraft.blacksmith.util.ConfigHelper; | import net.knarcraft.blacksmith.util.ConfigHelper; | ||||||
| @@ -24,7 +23,7 @@ import java.util.logging.Level; | |||||||
| /** | /** | ||||||
|  * A class which keeps track of all Blacksmith settings/config values for one NPC |  * A class which keeps track of all Blacksmith settings/config values for one NPC | ||||||
|  */ |  */ | ||||||
| public class BlacksmithNPCSettings implements TraitSettings, Settings<BlacksmithSetting> { | public class BlacksmithNPCSettings implements TraitSettings<BlacksmithSetting> { | ||||||
|  |  | ||||||
|     private final List<Material> reforgeAbleItems = new ArrayList<>(); |     private final List<Material> reforgeAbleItems = new ArrayList<>(); | ||||||
|     private final List<Enchantment> enchantmentBlocklist = new ArrayList<>(); |     private final List<Enchantment> enchantmentBlocklist = new ArrayList<>(); | ||||||
|   | |||||||
| @@ -2,14 +2,13 @@ package net.knarcraft.blacksmith.config.scrapper; | |||||||
|  |  | ||||||
| import net.citizensnpcs.api.util.DataKey; | import net.citizensnpcs.api.util.DataKey; | ||||||
| import net.knarcraft.blacksmith.config.SettingValueType; | import net.knarcraft.blacksmith.config.SettingValueType; | ||||||
| import net.knarcraft.blacksmith.config.Settings; |  | ||||||
| import net.knarcraft.blacksmith.config.TraitSettings; | import net.knarcraft.blacksmith.config.TraitSettings; | ||||||
| import net.knarcraft.blacksmith.util.ConfigHelper; | import net.knarcraft.blacksmith.util.ConfigHelper; | ||||||
|  |  | ||||||
| import java.util.HashMap; | import java.util.HashMap; | ||||||
| import java.util.Map; | import java.util.Map; | ||||||
|  |  | ||||||
| public class ScrapperNPCSettings implements TraitSettings, Settings<ScrapperSetting> { | public class ScrapperNPCSettings implements TraitSettings<ScrapperSetting> { | ||||||
|     private final Map<ScrapperSetting, Object> currentValues = new HashMap<>(); |     private final Map<ScrapperSetting, Object> currentValues = new HashMap<>(); | ||||||
|     private final GlobalScrapperSettings globalScrapperSettings; |     private final GlobalScrapperSettings globalScrapperSettings; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -144,7 +144,7 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage { | |||||||
|      * @return <p>The message to display</p> |      * @return <p>The message to display</p> | ||||||
|      */ |      */ | ||||||
|     public static String getRawValueMessage(String rawValue) { |     public static String getRawValueMessage(String rawValue) { | ||||||
|         return StringFormatter.replacePlaceholder(BlacksmithPlugin.getTranslator().getTranslatedMessage( |         return StringFormatter.replacePlaceholder(BlacksmithPlugin.translate( | ||||||
|                 BlacksmithTranslatableMessage.RAW_VALUE), "{rawValue}", rawValue); |                 BlacksmithTranslatableMessage.RAW_VALUE), "{rawValue}", rawValue); | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -156,7 +156,7 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage { | |||||||
|      * @return <p>The string to display to a user</p> |      * @return <p>The string to display to a user</p> | ||||||
|      */ |      */ | ||||||
|     public static String getValueChangedMessage(String setting, String newValue) { |     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}"}, |                         BlacksmithTranslatableMessage.VALUE_CHANGED), new String[]{"{setting}", "{newValue}"}, | ||||||
|                 new String[]{setting, newValue}); |                 new String[]{setting, newValue}); | ||||||
|     } |     } | ||||||
| @@ -171,7 +171,7 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage { | |||||||
|      * @return <p>The string to display to a user</p> |      * @return <p>The string to display to a user</p> | ||||||
|      */ |      */ | ||||||
|     public static String getItemValueChangedMessage(String setting, ItemType itemType, String item, String newValue) { |     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), |                         BlacksmithTranslatableMessage.VALUE_FOR_ITEM_CHANGED), | ||||||
|                 new String[]{"{setting}", "{itemType}", "{item}", "{newValue}"}, |                 new String[]{"{setting}", "{itemType}", "{item}", "{newValue}"}, | ||||||
|                 new String[]{setting, itemType.getItemTypeName(), item, newValue}); |                 new String[]{setting, itemType.getItemTypeName(), item, newValue}); | ||||||
| @@ -185,7 +185,7 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage { | |||||||
|      * @return <p>The string to display to a user</p> |      * @return <p>The string to display to a user</p> | ||||||
|      */ |      */ | ||||||
|     public static String getCurrentValueMessage(String setting, String currentValue) { |     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}"}, |                         BlacksmithTranslatableMessage.CURRENT_VALUE), new String[]{"{setting}", "{currentValue}"}, | ||||||
|                 new String[]{setting, currentValue}); |                 new String[]{setting, currentValue}); | ||||||
|     } |     } | ||||||
| @@ -200,7 +200,7 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage { | |||||||
|      * @return <p>The string to display to a user</p> |      * @return <p>The string to display to a user</p> | ||||||
|      */ |      */ | ||||||
|     public static String getItemCurrentValueMessage(String setting, ItemType itemType, String item, String currentValue) { |     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), |                         BlacksmithTranslatableMessage.CURRENT_VALUE_FOR_ITEM), | ||||||
|                 new String[]{"{setting}", "{itemType}", "{item}", "{currentValue}"}, |                 new String[]{"{setting}", "{itemType}", "{item}", "{currentValue}"}, | ||||||
|                 new String[]{setting, itemType.getItemTypeName(), item, currentValue}); |                 new String[]{setting, itemType.getItemTypeName(), item, currentValue}); | ||||||
|   | |||||||
| @@ -17,10 +17,8 @@ public enum ItemType { | |||||||
|      */ |      */ | ||||||
|     public String getItemTypeName() { |     public String getItemTypeName() { | ||||||
|         return switch (this) { |         return switch (this) { | ||||||
|             case MATERIAL -> BlacksmithPlugin.getTranslator().getTranslatedMessage( |             case MATERIAL -> BlacksmithPlugin.translate(BlacksmithTranslatableMessage.ITEM_TYPE_MATERIAL); | ||||||
|                     BlacksmithTranslatableMessage.ITEM_TYPE_MATERIAL); |             case ENCHANTMENT -> BlacksmithPlugin.translate(BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT); | ||||||
|             case ENCHANTMENT -> BlacksmithPlugin.getTranslator().getTranslatedMessage( |  | ||||||
|                     BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT); |  | ||||||
|         }; |         }; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -53,7 +53,7 @@ public final class TimeFormatter { | |||||||
|      * @return <p>Text describing the time interval</p> |      * @return <p>Text describing the time interval</p> | ||||||
|      */ |      */ | ||||||
|     private static String getMessageFromInterval(TimeInterval 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 |         //Choose a random entry if a comma-separated list is provided | ||||||
|         if (text.contains(",")) { |         if (text.contains(",")) { | ||||||
|   | |||||||
| @@ -3,6 +3,7 @@ package net.knarcraft.blacksmith.trait; | |||||||
| import net.citizensnpcs.api.util.DataKey; | import net.citizensnpcs.api.util.DataKey; | ||||||
| import net.knarcraft.blacksmith.BlacksmithPlugin; | import net.knarcraft.blacksmith.BlacksmithPlugin; | ||||||
| import net.knarcraft.blacksmith.config.blacksmith.BlacksmithNPCSettings; | import net.knarcraft.blacksmith.config.blacksmith.BlacksmithNPCSettings; | ||||||
|  | import net.knarcraft.blacksmith.config.blacksmith.BlacksmithSetting; | ||||||
| import net.knarcraft.blacksmith.manager.EconomyManager; | import net.knarcraft.blacksmith.manager.EconomyManager; | ||||||
| import net.knarcraft.blacksmith.util.ItemHelper; | import net.knarcraft.blacksmith.util.ItemHelper; | ||||||
| import net.knarcraft.knarlib.formatting.StringFormatter; | 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 |  * The class representing the Blacksmith NPC trait | ||||||
|  */ |  */ | ||||||
| public class BlacksmithTrait extends CustomTrait { | public class BlacksmithTrait extends CustomTrait<BlacksmithSetting> { | ||||||
|     private final BlacksmithNPCSettings config; |     private final BlacksmithNPCSettings config; | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|   | |||||||
| @@ -2,6 +2,8 @@ package net.knarcraft.blacksmith.trait; | |||||||
|  |  | ||||||
| import net.citizensnpcs.api.npc.NPC; | import net.citizensnpcs.api.npc.NPC; | ||||||
| import net.citizensnpcs.api.trait.Trait; | import net.citizensnpcs.api.trait.Trait; | ||||||
|  | import net.knarcraft.blacksmith.config.Setting; | ||||||
|  | import net.knarcraft.blacksmith.config.Settings; | ||||||
| import net.knarcraft.blacksmith.config.TraitSettings; | import net.knarcraft.blacksmith.config.TraitSettings; | ||||||
| import net.knarcraft.blacksmith.formatting.TimeFormatter; | import net.knarcraft.blacksmith.formatting.TimeFormatter; | ||||||
| import net.knarcraft.blacksmith.manager.EconomyManager; | import net.knarcraft.blacksmith.manager.EconomyManager; | ||||||
| @@ -10,6 +12,7 @@ import org.bukkit.entity.LivingEntity; | |||||||
| import org.bukkit.entity.Player; | import org.bukkit.entity.Player; | ||||||
| import org.bukkit.inventory.ItemStack; | import org.bukkit.inventory.ItemStack; | ||||||
| import org.jetbrains.annotations.NotNull; | import org.jetbrains.annotations.NotNull; | ||||||
|  | import org.jetbrains.annotations.Nullable; | ||||||
|  |  | ||||||
| import java.util.Calendar; | import java.util.Calendar; | ||||||
| import java.util.HashMap; | import java.util.HashMap; | ||||||
| @@ -22,10 +25,10 @@ import static net.knarcraft.blacksmith.formatting.BlacksmithStringFormatter.send | |||||||
| /** | /** | ||||||
|  * A custom trait that utilizes sessions |  * A custom trait that utilizes sessions | ||||||
|  */ |  */ | ||||||
| public abstract class CustomTrait extends Trait { | public abstract class CustomTrait<K extends Setting> extends Trait { | ||||||
|  |  | ||||||
|     protected Session session; |     protected Session session; | ||||||
|     protected TraitSettings config; |     protected TraitSettings<K> config; | ||||||
|     protected final Map<UUID, Calendar> coolDowns = new HashMap<>(); |     protected final Map<UUID, Calendar> coolDowns = new HashMap<>(); | ||||||
|     protected long currentSessionStartTime = System.currentTimeMillis(); |     protected long currentSessionStartTime = System.currentTimeMillis(); | ||||||
|  |  | ||||||
| @@ -43,10 +46,19 @@ public abstract class CustomTrait extends Trait { | |||||||
|      * |      * | ||||||
|      * @param config <p>The trait settings to use</p> |      * @param config <p>The trait settings to use</p> | ||||||
|      */ |      */ | ||||||
|     protected void setTraitSettings(TraitSettings config) { |     protected void setTraitSettings(TraitSettings<K> config) { | ||||||
|         this.config = config; |         this.config = config; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * Gets the settings used by this trait | ||||||
|  |      * | ||||||
|  |      * @return <p>The settings used by this trait</p> | ||||||
|  |      */ | ||||||
|  |     public @Nullable Settings<K> getTraitSettings() { | ||||||
|  |         return this.config; | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * Starts a new session, and prepares to repair the player's item |      * Starts a new session, and prepares to repair the player's item | ||||||
|      * |      * | ||||||
|   | |||||||
| @@ -3,6 +3,7 @@ package net.knarcraft.blacksmith.trait; | |||||||
| import net.citizensnpcs.api.util.DataKey; | import net.citizensnpcs.api.util.DataKey; | ||||||
| import net.knarcraft.blacksmith.BlacksmithPlugin; | import net.knarcraft.blacksmith.BlacksmithPlugin; | ||||||
| import net.knarcraft.blacksmith.config.scrapper.ScrapperNPCSettings; | import net.knarcraft.blacksmith.config.scrapper.ScrapperNPCSettings; | ||||||
|  | import net.knarcraft.blacksmith.config.scrapper.ScrapperSetting; | ||||||
| import org.apache.commons.lang.NotImplementedException; | import org.apache.commons.lang.NotImplementedException; | ||||||
| import org.bukkit.Bukkit; | import org.bukkit.Bukkit; | ||||||
| import org.bukkit.entity.Player; | import org.bukkit.entity.Player; | ||||||
| @@ -11,7 +12,7 @@ import org.jetbrains.annotations.NotNull; | |||||||
| /** | /** | ||||||
|  * The class representing a scrapper NPC trait |  * The class representing a scrapper NPC trait | ||||||
|  */ |  */ | ||||||
| public class ScrapperTrait extends CustomTrait { | public class ScrapperTrait extends CustomTrait<ScrapperSetting> { | ||||||
|  |  | ||||||
|     //TODO: A scrapper will take items and turn them into the base ingredients |     //TODO: A scrapper will take items and turn them into the base ingredients | ||||||
|     //TODO: If an item is enchanted, give an appropriate amount of exp |     //TODO: If an item is enchanted, give an appropriate amount of exp | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user