From f3169c925543f9fa63b01ed9c2ea6c1651ef1a4b Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Mon, 5 Sep 2022 13:51:41 +0200 Subject: [PATCH] Adds some unfinished changes for improving blacksmith commands --- .../command/BlackSmithConfigCommand.java | 18 +++++++++++ .../command/BlackSmithConfigTabCompleter.java | 19 +++++++---- .../command/BlackSmithEditTabCompleter.java | 13 +++++--- .../blacksmith/util/TabCompletionHelper.java | 32 +++++++++++++++++++ 4 files changed, 71 insertions(+), 11 deletions(-) create mode 100644 src/main/java/net/knarcraft/blacksmith/util/TabCompletionHelper.java diff --git a/src/main/java/net/knarcraft/blacksmith/command/BlackSmithConfigCommand.java b/src/main/java/net/knarcraft/blacksmith/command/BlackSmithConfigCommand.java index 4a907b4..90c9eaa 100644 --- a/src/main/java/net/knarcraft/blacksmith/command/BlackSmithConfigCommand.java +++ b/src/main/java/net/knarcraft/blacksmith/command/BlackSmithConfigCommand.java @@ -35,6 +35,12 @@ public class BlackSmithConfigCommand implements CommandExecutor { "individual NPC. If you really want to change this, change it manually."); return false; } + + if (args.length == 1) { + //TODO: Display the current value of the setting + } else if (args.length == 2 && isSpecialCase(commandName)) { + //TODO: Display the current value for the specified setting and material + } if (isSpecialCase(settings, commandName, args)) { return true; @@ -55,6 +61,18 @@ public class BlackSmithConfigCommand implements CommandExecutor { return false; } + /** + * Gets whether a command name matches a special case command + * + * @param commandName

The command specified

+ * @return

True if the command is a special case

+ */ + private boolean isSpecialCase(String commandName) { + return commandName.equalsIgnoreCase(GlobalSetting.BASE_PRICE.getCommandName()) || + commandName.equalsIgnoreCase(GlobalSetting.PRICE_PER_DURABILITY_POINT.getCommandName()) || + commandName.equalsIgnoreCase(GlobalSetting.ENCHANTMENT_COST.getCommandName()); + } + /** * Gets whether the command could be processed as one of the three special cases * diff --git a/src/main/java/net/knarcraft/blacksmith/command/BlackSmithConfigTabCompleter.java b/src/main/java/net/knarcraft/blacksmith/command/BlackSmithConfigTabCompleter.java index d11b1ff..e4898e6 100644 --- a/src/main/java/net/knarcraft/blacksmith/command/BlackSmithConfigTabCompleter.java +++ b/src/main/java/net/knarcraft/blacksmith/command/BlackSmithConfigTabCompleter.java @@ -3,6 +3,7 @@ package net.knarcraft.blacksmith.command; import net.knarcraft.blacksmith.config.GlobalSetting; import net.knarcraft.blacksmith.config.NPCSetting; import net.knarcraft.blacksmith.util.TabCompleteValuesHelper; +import net.knarcraft.blacksmith.util.TabCompletionHelper; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabCompleter; @@ -34,9 +35,9 @@ public class BlackSmithConfigTabCompleter implements TabCompleter { for (GlobalSetting globalSetting : GlobalSetting.values()) { availableCommands.add(globalSetting.getCommandName()); } - return availableCommands; + return TabCompletionHelper.filterMatchingContains(availableCommands, args[0]); } else if (args.length == 2) { - return tabCompleteCommandValues(args[0]); + return tabCompleteCommandValues(args[0], args[1]); } return null; } @@ -44,18 +45,24 @@ public class BlackSmithConfigTabCompleter implements TabCompleter { /** * Tab completes the values available for the given command * - * @param commandName

The name of the used command

+ * @param commandName

The name of the used command

+ * @param commandValue

The command value used to filter tab-completions

* @return

Some valid options for the command's argument

*/ - private List tabCompleteCommandValues(String commandName) { + private List tabCompleteCommandValues(String commandName, String commandValue) { + if (commandName.equalsIgnoreCase("reload")) { + return new ArrayList<>(); + } for (GlobalSetting globalSetting : GlobalSetting.values()) { if (globalSetting.getCommandName().equalsIgnoreCase(commandName)) { - return TabCompleteValuesHelper.getTabCompletions(globalSetting.getValueType()); + return TabCompletionHelper.filterMatchingContains(TabCompleteValuesHelper.getTabCompletions( + globalSetting.getValueType()), commandValue); } } for (NPCSetting npcSetting : NPCSetting.values()) { if (npcSetting.getCommandName().equalsIgnoreCase(commandName)) { - return TabCompleteValuesHelper.getTabCompletions(npcSetting.getValueType()); + return TabCompletionHelper.filterMatchingContains(TabCompleteValuesHelper.getTabCompletions( + npcSetting.getValueType()), commandValue); } } return null; diff --git a/src/main/java/net/knarcraft/blacksmith/command/BlackSmithEditTabCompleter.java b/src/main/java/net/knarcraft/blacksmith/command/BlackSmithEditTabCompleter.java index 7bb9464..ea4e004 100644 --- a/src/main/java/net/knarcraft/blacksmith/command/BlackSmithEditTabCompleter.java +++ b/src/main/java/net/knarcraft/blacksmith/command/BlackSmithEditTabCompleter.java @@ -2,6 +2,7 @@ package net.knarcraft.blacksmith.command; import net.knarcraft.blacksmith.config.NPCSetting; import net.knarcraft.blacksmith.util.TabCompleteValuesHelper; +import net.knarcraft.blacksmith.util.TabCompletionHelper; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabCompleter; @@ -29,10 +30,10 @@ public class BlackSmithEditTabCompleter implements TabCompleter { if (!sender.hasPermission("blacksmith.edit")) { return new ArrayList<>(); } - return npcSettings; + return TabCompletionHelper.filterMatchingContains(npcSettings, args[0]); } else { if (npcSettings.contains(args[0]) && args.length <= 2) { - return tabCompleteCommandValues(args[0]); + return tabCompleteCommandValues(args[0], args[1]); } else { return new ArrayList<>(); } @@ -42,13 +43,15 @@ public class BlackSmithEditTabCompleter implements TabCompleter { /** * Tab completes the values available for the given command * - * @param commandName

The name of the used command

+ * @param commandName

The name of the used command

+ * @param commandValue

The command value used to filter tab-completions

* @return

Some valid options for the command's argument

*/ - private List tabCompleteCommandValues(String commandName) { + private List tabCompleteCommandValues(String commandName, String commandValue) { for (NPCSetting npcSetting : NPCSetting.values()) { if (npcSetting.getCommandName().equalsIgnoreCase(commandName)) { - return TabCompleteValuesHelper.getTabCompletions(npcSetting.getValueType()); + return TabCompletionHelper.filterMatchingContains(TabCompleteValuesHelper.getTabCompletions( + npcSetting.getValueType()), commandValue); } } return null; diff --git a/src/main/java/net/knarcraft/blacksmith/util/TabCompletionHelper.java b/src/main/java/net/knarcraft/blacksmith/util/TabCompletionHelper.java new file mode 100644 index 0000000..2eb21f7 --- /dev/null +++ b/src/main/java/net/knarcraft/blacksmith/util/TabCompletionHelper.java @@ -0,0 +1,32 @@ +package net.knarcraft.blacksmith.util; + +import java.util.ArrayList; +import java.util.List; + +/** + * A helper class for tab complation + */ +public final class TabCompletionHelper { + + private TabCompletionHelper() { + + } + + /** + * Finds tab complete values that contain the typed text + * + * @param values

The values to filter

+ * @param typedText

The text the player has started typing

+ * @return

The given string values that contain the player's typed text

+ */ + public static List filterMatchingContains(List values, String typedText) { + List configValues = new ArrayList<>(); + for (String value : values) { + if (value.toLowerCase().contains(typedText.toLowerCase())) { + configValues.add(value); + } + } + return configValues; + } + +}