Cleanup command manager aliases handling

This commit is contained in:
Luck 2020-05-09 19:04:50 +01:00
parent f281927578
commit bd9ae9dbd5
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B

View File

@ -154,7 +154,7 @@ public class CommandManager {
} }
private CommandResult execute(Sender sender, String label, List<String> arguments) { private CommandResult execute(Sender sender, String label, List<String> arguments) {
handleRewrites(arguments, true); applyConvenienceAliases(arguments, true);
// Handle no arguments // Handle no arguments
if (arguments.isEmpty() || (arguments.size() == 1 && arguments.get(0).trim().isEmpty())) { if (arguments.isEmpty() || (arguments.size() == 1 && arguments.get(0).trim().isEmpty())) {
@ -211,8 +211,7 @@ public class CommandManager {
} }
public List<String> tabCompleteCommand(Sender sender, List<String> arguments) { public List<String> tabCompleteCommand(Sender sender, List<String> arguments) {
// we rewrite tab completions too! applyConvenienceAliases(arguments, false);
handleRewrites(arguments, false);
final List<Command<?>> mains = this.mainCommands.values().stream() final List<Command<?>> mains = this.mainCommands.values().stream()
.filter(Command::shouldDisplay) .filter(Command::shouldDisplay)
@ -255,75 +254,67 @@ public class CommandManager {
} }
/** /**
* Handles aliases * Applies "convenience" aliases to the given cmd line arguments.
* *
* @param args the current args list * @param args the current args list
* @param rewriteLastArgument if the last argument should be rewritten - this is false when the method is called on tab completions * @param rewriteLastArgument if the last argument should be rewritten -
* this is false when the method is called on tab completions
*/ */
private static void handleRewrites(List<String> args, boolean rewriteLastArgument) { private static void applyConvenienceAliases(List<String> args, boolean rewriteLastArgument) {
// Provide aliases // '/lp u' --> '/lp user' etc
// ^ ^^^^
if (args.size() >= 1 && (rewriteLastArgument || args.size() >= 2)) { if (args.size() >= 1 && (rewriteLastArgument || args.size() >= 2)) {
String arg0 = args.get(0); replaceArgs(args, 0, arg -> {
if (arg0.equalsIgnoreCase("u")) { switch (arg) {
args.remove(0); case "u": return "user";
args.add(0, "user"); case "g": return "group";
} else if (arg0.equalsIgnoreCase("g")) { case "t": return "track";
args.remove(0); case "i": return "info";
args.add(0, "group"); default: return null;
} else if (arg0.equalsIgnoreCase("t")) {
args.remove(0);
args.add(0, "track");
} else if (arg0.equalsIgnoreCase("i")) {
args.remove(0);
args.add(0, "info");
} }
});
} }
// '/lp user Luck p set --> /lp user Luck permission set' etc
// ^ ^^^^^^^^^^
if (args.size() >= 3 && (rewriteLastArgument || args.size() >= 4)) { if (args.size() >= 3 && (rewriteLastArgument || args.size() >= 4)) {
if (!args.get(0).equalsIgnoreCase("user") && !args.get(0).equalsIgnoreCase("group")) { String arg0 = args.get(0).toLowerCase();
return; if (arg0.equals("user") || arg0.equals("group")) {
} replaceArgs(args, 2, arg -> {
switch (arg) {
String s = args.get(2).toLowerCase();
switch (s) {
// Provide aliases
case "p": case "p":
case "perm": case "perm":
args.remove(2); return "permission";
args.add(2, "permission");
break;
case "g": case "g":
case "group": case "group":
args.remove(2); return "parent";
args.add(2, "parent"); case "m": return "meta";
case "m": case "i": return "info";
args.remove(2); case "e": return "editor";
args.add(2, "meta"); default: return null;
break; }
case "i": });
args.remove(2);
args.add(2, "info"); // '/lp user Luck permission i' --> '/lp user Luck permission info' etc
break; // ^ ^^^^
case "e": if (args.size() >= 4 && (rewriteLastArgument || args.size() >= 5)) {
args.remove(2); String arg2 = args.get(2).toLowerCase();
args.add(2, "editor"); if (arg2.equals("permission") || arg2.equals("parent") || arg2.equals("meta")) {
break; replaceArgs(args, 3, arg -> arg.equals("i") ? "info" : null);
default: }
break;
} }
// /lp user Luck permission i ==> /lp user Luck permission info
boolean lazyInfo = (
args.size() >= 4 && (rewriteLastArgument || args.size() >= 5) &&
(args.get(2).equalsIgnoreCase("permission") || args.get(2).equalsIgnoreCase("parent") || args.get(2).equalsIgnoreCase("meta")) &&
(args.get(3).equalsIgnoreCase("i"))
);
if (lazyInfo) {
args.remove(3);
args.add(3, "info");
} }
} }
} }
private static void replaceArgs(List<String> args, int i, Function<String, String> rewrites) {
String arg = args.get(i).toLowerCase();
String rewrite = rewrites.apply(arg);
if (rewrite != null) {
args.remove(i);
args.add(0, rewrite);
}
}
} }