diff --git a/LauncherCore/src/main/java/pro/gravit/utils/command/Command.java b/LauncherCore/src/main/java/pro/gravit/utils/command/Command.java index ebeb96dc..10d03a4f 100644 --- a/LauncherCore/src/main/java/pro/gravit/utils/command/Command.java +++ b/LauncherCore/src/main/java/pro/gravit/utils/command/Command.java @@ -1,14 +1,12 @@ package pro.gravit.utils.command; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; +import java.util.*; import org.jline.reader.Candidate; import pro.gravit.utils.helper.VerifyHelper; public abstract class Command { - + public Map childCommands = new HashMap<>(); protected static String parseUsername(String username) throws CommandException { try { @@ -39,20 +37,31 @@ public Candidate buildCandidate(CommandHandler.Category category, String command public List complete(List words, int wordIndex, String word) { - return new ArrayList<>(); + if(wordIndex == 0) + { + List candidates = new ArrayList<>(); + childCommands.forEach((k,v) -> { + if(k.startsWith(word)) + { + candidates.add(new Candidate(k)); + } + }); + return candidates; + } + else + { + Command cmd = childCommands.get(words.get(0)); + if(cmd == null) return new ArrayList<>(); + return cmd.complete(words.subList(1, words.size()), wordIndex - 1, word); + } } - public List subCommandComplete(String word, List commands) + public void invokeSubcommands(String... args) throws Exception { - List candidates = new ArrayList<>(); - for(String s : commands) - { - if(word.startsWith(s)) - { - candidates.add(new Candidate(s)); - } - } - return candidates; + verifyArgs(args, 1); + Command command = childCommands.get(args[0]); + if(command == null) throw new CommandException(String.format("Unknown sub command: '%s'", args[0])); + command.invoke(Arrays.copyOfRange(args, 1, args.length)); } diff --git a/LauncherCore/src/main/java/pro/gravit/utils/command/JLineCommandHandler.java b/LauncherCore/src/main/java/pro/gravit/utils/command/JLineCommandHandler.java index a795c571..25767e33 100644 --- a/LauncherCore/src/main/java/pro/gravit/utils/command/JLineCommandHandler.java +++ b/LauncherCore/src/main/java/pro/gravit/utils/command/JLineCommandHandler.java @@ -1,6 +1,7 @@ package pro.gravit.utils.command; import java.io.IOException; +import java.util.Arrays; import java.util.List; import org.jline.reader.Candidate; @@ -47,7 +48,8 @@ public void complete(LineReader reader, ParsedLine line, List candida else { Command target = findCommand(line.words().get(0)); - List candidates1 = target.complete(line.words(), line.wordIndex(), completeWord); + List words = line.words(); + List candidates1 = target.complete(words.subList(1, words.size()), line.wordIndex() - 1, completeWord); candidates.addAll(candidates1); } } diff --git a/LauncherCore/src/main/java/pro/gravit/utils/command/SubCommand.java b/LauncherCore/src/main/java/pro/gravit/utils/command/SubCommand.java new file mode 100644 index 00000000..54bd51fe --- /dev/null +++ b/LauncherCore/src/main/java/pro/gravit/utils/command/SubCommand.java @@ -0,0 +1,13 @@ +package pro.gravit.utils.command; + +public abstract class SubCommand extends Command { + @Override + public String getArgsDescription() { + return null; + } + + @Override + public String getUsageDescription() { + return null; + } +}