diff --git a/SkyblockCore-Common/src/main/java/me/illusion/skyblockcore/common/command/data/builder/SimpleSkyblockCommandBuilder.java b/SkyblockCore-Common/src/main/java/me/illusion/skyblockcore/common/command/data/builder/SimpleSkyblockCommandBuilder.java index e145773..e68edd8 100644 --- a/SkyblockCore-Common/src/main/java/me/illusion/skyblockcore/common/command/data/builder/SimpleSkyblockCommandBuilder.java +++ b/SkyblockCore-Common/src/main/java/me/illusion/skyblockcore/common/command/data/builder/SimpleSkyblockCommandBuilder.java @@ -26,13 +26,13 @@ public class SimpleSkyblockCommandBuilder implements private SkyblockCommandHandler handler; private String permission; - public SimpleSkyblockCommandBuilder(AbstractSkyblockCommandManager manager, String name, Class audienceClass, List arguments) { + public SimpleSkyblockCommandBuilder(AbstractSkyblockCommandManager manager, String name, Class audienceClass, List arguments, + String permission) { this.manager = manager; this.name = name; this.audienceClass = audienceClass; this.arguments = arguments; - - arguments.add(new LiteralArgument(name)); + this.permission = permission; } public SimpleSkyblockCommandBuilder(AbstractSkyblockCommandManager manager, String name, Class audienceClass) { @@ -40,7 +40,8 @@ public SimpleSkyblockCommandBuilder(AbstractSkyblockCommandManager manager, Stri manager, name, audienceClass, - new LinkedList<>() + new LinkedList<>(), + null ); } @@ -64,12 +65,17 @@ public SkyblockCommandBuilder permission(String permission) { @Override public SkyblockCommandBuilder audience(Class audience) { - return new SimpleSkyblockCommandBuilder<>(manager, name, audience, arguments); + return new SimpleSkyblockCommandBuilder<>(manager, name, audience, arguments, permission); } @Override public SkyblockCommand build() { - SkyblockCommand command = new SimpleSkyblockCommand<>(arguments, handler, audienceClass, permission); + List realArguments = new LinkedList<>(); + + realArguments.add(new LiteralArgument(name)); + realArguments.addAll(arguments); + + SkyblockCommand command = new SimpleSkyblockCommand<>(realArguments, handler, audienceClass, permission); manager.registerCommand(command); return command; diff --git a/SkyblockCore-Common/src/main/java/me/illusion/skyblockcore/common/command/manager/AbstractSkyblockCommandManager.java b/SkyblockCore-Common/src/main/java/me/illusion/skyblockcore/common/command/manager/AbstractSkyblockCommandManager.java index 3a3a091..cf0c98f 100644 --- a/SkyblockCore-Common/src/main/java/me/illusion/skyblockcore/common/command/manager/AbstractSkyblockCommandManager.java +++ b/SkyblockCore-Common/src/main/java/me/illusion/skyblockcore/common/command/manager/AbstractSkyblockCommandManager.java @@ -10,6 +10,7 @@ import me.illusion.skyblockcore.common.command.node.CommandNode; import me.illusion.skyblockcore.common.command.structure.CommandTree; import me.illusion.skyblockcore.common.command.structure.CommandTree.TargetResult; +import me.illusion.skyblockcore.common.config.SkyblockMessagesFile; import me.illusion.skyblockcore.common.platform.SkyblockPlatform; /** @@ -44,8 +45,10 @@ protected void handle(SkyblockAudience audience, String label, String[] args) { protected void handle(SkyblockAudience audience, String input) { TargetResult result = commandTree.getTargetNode(input); + SkyblockMessagesFile messages = platform.getMessagesFile(); if (result == null) { + messages.sendMessage(audience, "invalid-command"); return; } @@ -64,12 +67,12 @@ protected void handle(SkyblockAudience audience, St boolean hasPermission = permission == null || audience.hasPermission(permission); if (!hasPermission) { - platform.getMessagesFile().sendMessage(audience, "no-permission"); + messages.sendMessage(audience, "no-permission"); return; } if (!audienceClass.isAssignableFrom(audience.getClass())) { - platform.getMessagesFile().sendMessage(audience, "invalid-audience"); + messages.sendMessage(audience, "invalid-audience"); return; } @@ -77,11 +80,7 @@ protected void handle(SkyblockAudience audience, St } protected List tabComplete(SkyblockAudience audience, String label, String[] args) { - return tabComplete(audience, createInput(label, args)); - } - - protected List tabComplete(SkyblockAudience audience, String input) { - return commandTree.tabComplete(audience, input); + return commandTree.tabComplete(audience, label, args); } public abstract void registerRoot(String name); diff --git a/SkyblockCore-Common/src/main/java/me/illusion/skyblockcore/common/command/structure/CommandTree.java b/SkyblockCore-Common/src/main/java/me/illusion/skyblockcore/common/command/structure/CommandTree.java index f1a9519..d67583c 100644 --- a/SkyblockCore-Common/src/main/java/me/illusion/skyblockcore/common/command/structure/CommandTree.java +++ b/SkyblockCore-Common/src/main/java/me/illusion/skyblockcore/common/command/structure/CommandTree.java @@ -52,12 +52,19 @@ public TargetResult getTargetNode(String fullInput) { return null; } + if (node.getChildren().isEmpty() && fullInput.equalsIgnoreCase(node.getName())) { + return new TargetResult(node, new MutatingCommandContext(fullInput)); + } + CommandNode target = node; MutatingCommandContext context = new MutatingCommandContext(fullInput); + boolean valid = false; + for (int index = 1; index < split.length; index++) { String word = split[index]; List children = target.getChildren(); + boolean isLast = index == split.length - 1; if (children == null) { return null; @@ -70,6 +77,10 @@ public TargetResult getTargetNode(String fullInput) { if (context.addArgument(word, argument)) { child = nodeChild; + + if (isLast && child.getChildren().isEmpty()) { + valid = true; + } break; } } @@ -81,18 +92,23 @@ public TargetResult getTargetNode(String fullInput) { target = child; } + if (!valid) { + return null; + } + return new TargetResult(target, context); } /** * Tab completes a command. * @param audience The audience to tab complete for. - * @param fullInput The full input of the command. + * @param commandName The name of the command. + * @param split The argument array. * @return The tab completions. */ - public List tabComplete(SkyblockAudience audience, String fullInput) { - String[] split = fullInput.split(" "); - CommandNode node = getRoot(split[0]); + public List tabComplete(SkyblockAudience audience, String commandName, String[] split) { + CommandNode node = getRoot(commandName); + String fullInput = commandName + " " + String.join(" ", split); if (node == null) { return Collections.emptyList(); @@ -103,7 +119,7 @@ public List tabComplete(SkyblockAudience audience, String fullInput) { MutatingCommandContext context = new MutatingCommandContext(fullInput); - for (int index = 1; index < split.length; index++) { + for (int index = 0; index < split.length; index++) { String s = split[index]; List children = target.getChildren(); @@ -127,7 +143,9 @@ public List tabComplete(SkyblockAudience audience, String fullInput) { CommandArgument argument = child.getArgument(); - if (!audience.hasPermission(child.getPermission())) { + String permission = child.getPermission(); + + if (permission != null && !audience.hasPermission(child.getPermission())) { continue; } diff --git a/SkyblockCore-Spigot/src/main/java/me/illusion/skyblockcore/spigot/command/SkyblockBukkitCommandManager.java b/SkyblockCore-Spigot/src/main/java/me/illusion/skyblockcore/spigot/command/SkyblockBukkitCommandManager.java index e355c26..df5e8da 100644 --- a/SkyblockCore-Spigot/src/main/java/me/illusion/skyblockcore/spigot/command/SkyblockBukkitCommandManager.java +++ b/SkyblockCore-Spigot/src/main/java/me/illusion/skyblockcore/spigot/command/SkyblockBukkitCommandManager.java @@ -115,7 +115,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command @Override public List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) { if (sender instanceof ConsoleCommandSender) { - return tabComplete(new SkyblockBukkitConsoleAudience(), s); + return tabComplete(new SkyblockBukkitConsoleAudience(), s, strings); } if (!(sender instanceof Player player)) { @@ -123,7 +123,7 @@ public List onTabComplete(@NotNull CommandSender sender, @NotNull Comman } SkyblockAudience audience = platform.getPlayerManager().getPlayer(player.getUniqueId()); - return tabComplete(audience, s); + return tabComplete(audience, s, strings); } } }