Adds some unfinished changes for improving blacksmith commands

This commit is contained in:
Kristian Knarvik 2022-09-05 13:51:41 +02:00
parent e1191dad7d
commit f3169c9255
4 changed files with 71 additions and 11 deletions

View File

@ -35,6 +35,12 @@ public class BlackSmithConfigCommand implements CommandExecutor {
"individual NPC. If you really want to change this, change it manually."); "individual NPC. If you really want to change this, change it manually.");
return false; 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)) { if (isSpecialCase(settings, commandName, args)) {
return true; return true;
@ -55,6 +61,18 @@ public class BlackSmithConfigCommand implements CommandExecutor {
return false; return false;
} }
/**
* Gets whether a command name matches a special case command
*
* @param commandName <p>The command specified</p>
* @return <p>True if the command is a special case</p>
*/
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 * Gets whether the command could be processed as one of the three special cases
* *

View File

@ -3,6 +3,7 @@ package net.knarcraft.blacksmith.command;
import net.knarcraft.blacksmith.config.GlobalSetting; import net.knarcraft.blacksmith.config.GlobalSetting;
import net.knarcraft.blacksmith.config.NPCSetting; import net.knarcraft.blacksmith.config.NPCSetting;
import net.knarcraft.blacksmith.util.TabCompleteValuesHelper; import net.knarcraft.blacksmith.util.TabCompleteValuesHelper;
import net.knarcraft.blacksmith.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;
@ -34,9 +35,9 @@ public class BlackSmithConfigTabCompleter implements TabCompleter {
for (GlobalSetting globalSetting : GlobalSetting.values()) { for (GlobalSetting globalSetting : GlobalSetting.values()) {
availableCommands.add(globalSetting.getCommandName()); availableCommands.add(globalSetting.getCommandName());
} }
return availableCommands; return TabCompletionHelper.filterMatchingContains(availableCommands, args[0]);
} else if (args.length == 2) { } else if (args.length == 2) {
return tabCompleteCommandValues(args[0]); return tabCompleteCommandValues(args[0], args[1]);
} }
return null; return null;
} }
@ -44,18 +45,24 @@ public class BlackSmithConfigTabCompleter implements TabCompleter {
/** /**
* Tab completes the values available for the given command * Tab completes the values available for the given command
* *
* @param commandName <p>The name of the used command</p> * @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> * @return <p>Some valid options for the command's argument</p>
*/ */
private List<String> tabCompleteCommandValues(String commandName) { private List<String> tabCompleteCommandValues(String commandName, String commandValue) {
if (commandName.equalsIgnoreCase("reload")) {
return new ArrayList<>();
}
for (GlobalSetting globalSetting : GlobalSetting.values()) { for (GlobalSetting globalSetting : GlobalSetting.values()) {
if (globalSetting.getCommandName().equalsIgnoreCase(commandName)) { if (globalSetting.getCommandName().equalsIgnoreCase(commandName)) {
return TabCompleteValuesHelper.getTabCompletions(globalSetting.getValueType()); return TabCompletionHelper.filterMatchingContains(TabCompleteValuesHelper.getTabCompletions(
globalSetting.getValueType()), commandValue);
} }
} }
for (NPCSetting npcSetting : NPCSetting.values()) { for (NPCSetting npcSetting : NPCSetting.values()) {
if (npcSetting.getCommandName().equalsIgnoreCase(commandName)) { if (npcSetting.getCommandName().equalsIgnoreCase(commandName)) {
return TabCompleteValuesHelper.getTabCompletions(npcSetting.getValueType()); return TabCompletionHelper.filterMatchingContains(TabCompleteValuesHelper.getTabCompletions(
npcSetting.getValueType()), commandValue);
} }
} }
return null; return null;

View File

@ -2,6 +2,7 @@ package net.knarcraft.blacksmith.command;
import net.knarcraft.blacksmith.config.NPCSetting; import net.knarcraft.blacksmith.config.NPCSetting;
import net.knarcraft.blacksmith.util.TabCompleteValuesHelper; import net.knarcraft.blacksmith.util.TabCompleteValuesHelper;
import net.knarcraft.blacksmith.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;
@ -29,10 +30,10 @@ public class BlackSmithEditTabCompleter implements TabCompleter {
if (!sender.hasPermission("blacksmith.edit")) { if (!sender.hasPermission("blacksmith.edit")) {
return new ArrayList<>(); return new ArrayList<>();
} }
return npcSettings; return TabCompletionHelper.filterMatchingContains(npcSettings, args[0]);
} else { } else {
if (npcSettings.contains(args[0]) && args.length <= 2) { if (npcSettings.contains(args[0]) && args.length <= 2) {
return tabCompleteCommandValues(args[0]); return tabCompleteCommandValues(args[0], args[1]);
} else { } else {
return new ArrayList<>(); return new ArrayList<>();
} }
@ -42,13 +43,15 @@ public class BlackSmithEditTabCompleter implements TabCompleter {
/** /**
* Tab completes the values available for the given command * Tab completes the values available for the given command
* *
* @param commandName <p>The name of the used command</p> * @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> * @return <p>Some valid options for the command's argument</p>
*/ */
private List<String> tabCompleteCommandValues(String commandName) { private List<String> tabCompleteCommandValues(String commandName, String commandValue) {
for (NPCSetting npcSetting : NPCSetting.values()) { for (NPCSetting npcSetting : NPCSetting.values()) {
if (npcSetting.getCommandName().equalsIgnoreCase(commandName)) { if (npcSetting.getCommandName().equalsIgnoreCase(commandName)) {
return TabCompleteValuesHelper.getTabCompletions(npcSetting.getValueType()); return TabCompletionHelper.filterMatchingContains(TabCompleteValuesHelper.getTabCompletions(
npcSetting.getValueType()), commandValue);
} }
} }
return null; return null;

View File

@ -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 <p>The values to filter</p>
* @param typedText <p>The text the player has started typing</p>
* @return <p>The given string values that contain the player's typed text</p>
*/
public static List<String> filterMatchingContains(List<String> values, String typedText) {
List<String> configValues = new ArrayList<>();
for (String value : values) {
if (value.toLowerCase().contains(typedText.toLowerCase())) {
configValues.add(value);
}
}
return configValues;
}
}