diff --git a/src/main/java/net/knarcraft/paidsigns/command/AddCommand.java b/src/main/java/net/knarcraft/paidsigns/command/AddCommand.java index 9d32486..b8f6915 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/AddCommand.java +++ b/src/main/java/net/knarcraft/paidsigns/command/AddCommand.java @@ -26,7 +26,7 @@ public class AddCommand extends TokenizedCommand { return false; } - String signName = arguments.get(0); + String signName = arguments.get(0).trim(); double cost; try { cost = Double.parseDouble(arguments.get(1)); diff --git a/src/main/java/net/knarcraft/paidsigns/command/AddConditionTabCompleter.java b/src/main/java/net/knarcraft/paidsigns/command/AddConditionTabCompleter.java index 352787b..be44805 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/AddConditionTabCompleter.java +++ b/src/main/java/net/knarcraft/paidsigns/command/AddConditionTabCompleter.java @@ -27,16 +27,18 @@ public class AddConditionTabCompleter extends TokenizedTabCompleter { if (lineIndices == null) { initializeValues(); } - if (argumentSize < 1) { - return TabCompleteHelper.getPaidSignNames(); - } else if (argumentSize < 2) { - return this.lineIndices; - } else if (argumentSize < 3) { - return stringsToMatch; - } else if (argumentSize < 4) { - return booleans; - } else if (argumentSize < 6) { - return optionStates; + if (argumentSize == 1) { + return TabCompleteHelper.filterMatchingStartsWith(TabCompleteHelper.getPaidSignNames(), arguments.get(0)); + } else if (argumentSize == 2) { + return TabCompleteHelper.filterMatchingStartsWith(this.lineIndices, arguments.get(1)); + } else if (argumentSize == 3) { + return TabCompleteHelper.filterMatchingStartsWith(stringsToMatch, arguments.get(2)); + } else if (argumentSize == 4) { + return TabCompleteHelper.filterMatchingStartsWith(booleans, arguments.get(3)); + } else if (argumentSize == 5) { + return TabCompleteHelper.filterMatchingStartsWith(optionStates, arguments.get(4)); + } else if (argumentSize == 6) { + return TabCompleteHelper.filterMatchingStartsWith(optionStates, arguments.get(5)); } return new ArrayList<>(); } diff --git a/src/main/java/net/knarcraft/paidsigns/command/AddTabCompleter.java b/src/main/java/net/knarcraft/paidsigns/command/AddTabCompleter.java index 2c29762..fa984c7 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/AddTabCompleter.java +++ b/src/main/java/net/knarcraft/paidsigns/command/AddTabCompleter.java @@ -35,14 +35,16 @@ public class AddTabCompleter extends TokenizedTabCompleter { loadAvailablePermissions(); } - if (argumentSize < 1) { - return names; - } else if (argumentSize < 2) { - return costs; - } else if (argumentSize < 3) { + if (argumentSize == 1) { + return TabCompleteHelper.filterMatchingStartsWith(names, arguments.get(0)); + } else if (argumentSize == 2) { + return TabCompleteHelper.filterMatchingStartsWith(costs, arguments.get(1)); + } else if (argumentSize == 3) { return tabCompletePermission(arguments.get(arguments.size() - 1)); - } else if (argumentSize < 5) { - return options; + } else if (argumentSize == 4) { + return TabCompleteHelper.filterMatchingStartsWith(options, arguments.get(3)); + } else if (argumentSize == 5) { + return TabCompleteHelper.filterMatchingStartsWith(options, arguments.get(4)); } return new ArrayList<>(); } diff --git a/src/main/java/net/knarcraft/paidsigns/command/ListTabCompleter.java b/src/main/java/net/knarcraft/paidsigns/command/ListTabCompleter.java index 1d39f94..7dc1afa 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/ListTabCompleter.java +++ b/src/main/java/net/knarcraft/paidsigns/command/ListTabCompleter.java @@ -20,22 +20,19 @@ public class ListTabCompleter extends TokenizedTabCompleter { @Override public List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) { - if (argumentSize < 1) { - return TabCompleteHelper.getPaidSignNames(); - } else if (argumentSize < 2) { + if (argumentSize == 1) { + return TabCompleteHelper.filterMatchingStartsWith(TabCompleteHelper.getPaidSignNames(), arguments.get(0)); + } else if (argumentSize == 2) { PaidSign sign = PaidSigns.getInstance().getSignManager().getPaidSign(arguments.get(0)); if (sign != null) { List availableConditions = new ArrayList<>(); for (Short signLine : sign.getConditions().keySet()) { availableConditions.add(String.valueOf(signLine + 1)); } - return availableConditions; - } else { - return TabCompleteHelper.getSignLines(); + return TabCompleteHelper.filterMatchingStartsWith(availableConditions, arguments.get(1)); } - } else { - return new ArrayList<>(); } + return new ArrayList<>(); } } diff --git a/src/main/java/net/knarcraft/paidsigns/command/RemoveConditionTabCompleter.java b/src/main/java/net/knarcraft/paidsigns/command/RemoveConditionTabCompleter.java index d3a5158..55161cf 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/RemoveConditionTabCompleter.java +++ b/src/main/java/net/knarcraft/paidsigns/command/RemoveConditionTabCompleter.java @@ -27,18 +27,16 @@ public class RemoveConditionTabCompleter extends TokenizedTabCompleter { initializeValues(); } - if (argumentSize < 1) { - return TabCompleteHelper.getPaidSignNames(); - } else if (argumentSize < 2) { + if (argumentSize == 1) { + return TabCompleteHelper.filterMatchingStartsWith(TabCompleteHelper.getPaidSignNames(), arguments.get(0)); + } else if (argumentSize == 2) { PaidSign sign = PaidSigns.getInstance().getSignManager().getPaidSign(arguments.get(0)); if (sign != null) { List availableConditions = new ArrayList<>(); for (Short signLine : sign.getConditions().keySet()) { availableConditions.add(String.valueOf(signLine + 1)); } - return availableConditions; - } else { - return TabCompleteHelper.getSignLines(); + return TabCompleteHelper.filterMatchingStartsWith(availableConditions, arguments.get(1)); } } return new ArrayList<>(); diff --git a/src/main/java/net/knarcraft/paidsigns/command/TokenizedTabCompleter.java b/src/main/java/net/knarcraft/paidsigns/command/TokenizedTabCompleter.java index d5b6b0a..5e258b2 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/TokenizedTabCompleter.java +++ b/src/main/java/net/knarcraft/paidsigns/command/TokenizedTabCompleter.java @@ -19,12 +19,16 @@ public class TokenizedTabCompleter implements TabCompleter { @Nullable @Override - public List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) { + public List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, + @NotNull String[] args) { arguments = Tokenizer.tokenize(String.join(" ", args)); if (args.length == 0) { argumentSize = 0; } else { - argumentSize = args[args.length - 1].isEmpty() ? arguments.size() : arguments.size() - 1; + if (args[args.length - 1].isEmpty()) { + arguments.add(""); + } + argumentSize = arguments.size(); } return null; } diff --git a/src/main/java/net/knarcraft/paidsigns/container/PaidSign.java b/src/main/java/net/knarcraft/paidsigns/container/PaidSign.java index 2caff93..2cc09b2 100644 --- a/src/main/java/net/knarcraft/paidsigns/container/PaidSign.java +++ b/src/main/java/net/knarcraft/paidsigns/container/PaidSign.java @@ -29,7 +29,7 @@ public class PaidSign { */ public PaidSign(String name, double cost, String permission, OptionState ignoreCase, OptionState ignoreColor) { if (name == null || name.trim().isBlank()) { - throw new IllegalArgumentException("Id cannot be empty"); + throw new IllegalArgumentException("Name cannot be empty"); } if (cost <= 0) { throw new IllegalArgumentException("Cost must be larger than 0"); diff --git a/src/main/java/net/knarcraft/paidsigns/utility/Tokenizer.java b/src/main/java/net/knarcraft/paidsigns/utility/Tokenizer.java index 5edb1b9..db5563b 100644 --- a/src/main/java/net/knarcraft/paidsigns/utility/Tokenizer.java +++ b/src/main/java/net/knarcraft/paidsigns/utility/Tokenizer.java @@ -15,10 +15,21 @@ public final class Tokenizer { /** * Tokenizes a string * - * @param input

A string.

- * @return

A list of tokens.

+ * @param input

A string

+ * @return

A list of tokens

*/ public static List tokenize(String input) { + return tokenize(input, true); + } + + /** + * Tokenizes a string + * + * @param input

A string

+ * @param allowEmptyQuotes

Whether to treat "" as a token

+ * @return

A list of tokens

+ */ + public static List tokenize(String input, boolean allowEmptyQuotes) { List tokens = new ArrayList<>(); boolean startedQuote = false; StringBuilder currentToken = new StringBuilder(); @@ -33,7 +44,7 @@ public final class Tokenizer { case '"': if (startedQuote) { //This quote signifies the end of the argument - if (isNotEmpty(currentToken)) { + if (allowEmptyQuotes || isNotEmpty(currentToken)) { tokens.add(currentToken.toString()); currentToken = new StringBuilder(); }