Fixes various tab completion problems

This commit is contained in:
Kristian Knarvik 2022-02-27 14:28:46 +01:00
parent 91d477b45a
commit 3b7268d2ff
8 changed files with 52 additions and 38 deletions

View File

@ -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));

View File

@ -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<>();
}

View File

@ -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<>();
}

View File

@ -20,22 +20,19 @@ public class ListTabCompleter extends TokenizedTabCompleter {
@Override
public List<String> 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<String> 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<>();
}
}
}

View File

@ -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<String> 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<>();

View File

@ -19,12 +19,16 @@ public class TokenizedTabCompleter implements TabCompleter {
@Nullable
@Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
public List<String> 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;
}

View File

@ -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");

View File

@ -15,10 +15,21 @@ public final class Tokenizer {
/**
* Tokenizes a string
*
* @param input <p>A string.</p>
* @return <p>A list of tokens.</p>
* @param input <p>A string</p>
* @return <p>A list of tokens</p>
*/
public static List<String> tokenize(String input) {
return tokenize(input, true);
}
/**
* Tokenizes a string
*
* @param input <p>A string</p>
* @param allowEmptyQuotes <p>Whether to treat "" as a token</p>
* @return <p>A list of tokens</p>
*/
public static List<String> tokenize(String input, boolean allowEmptyQuotes) {
List<String> 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();
}