From 3d74e74b9eeec8740f073ba4f05da980988c7670 Mon Sep 17 00:00:00 2001 From: LeoDog896 Date: Tue, 9 Feb 2021 09:25:18 -0500 Subject: [PATCH] Use redirection for aliases instead of repetition --- .gitignore | 3 ++- .../minestom/server/command/CommandManager.java | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index be3b46bfa..f7e4a1cff 100644 --- a/.gitignore +++ b/.gitignore @@ -55,4 +55,5 @@ gradle-app.setting /extensions/ # When compiling we get a docs folder -/docs \ No newline at end of file +/docs +.mixin.out/ diff --git a/src/main/java/net/minestom/server/command/CommandManager.java b/src/main/java/net/minestom/server/command/CommandManager.java index 7486ae0f4..2dad50fd4 100644 --- a/src/main/java/net/minestom/server/command/CommandManager.java +++ b/src/main/java/net/minestom/server/command/CommandManager.java @@ -312,11 +312,15 @@ public final class CommandManager { final Collection syntaxes = command.getSyntaxes(); // Create command for main name - createCommand(player, nodes, cmdChildren, command.getName(), syntaxes, rootChildren); + int mainNodeIndex = createCommand(player, nodes, cmdChildren, command.getName(), syntaxes, rootChildren); - // Repeat that for all aliases. + // Use redirection to hook aliases with the command for (String alias : command.getAliases()) { - createCommand(player, nodes, cmdChildren, alias, syntaxes, rootChildren); + DeclareCommandsPacket.Node node = new DeclareCommandsPacket.Node(); + node.flags = getFlag(NodeType.LITERAL, false, true, false); + node.name = alias; + node.redirectedNode = mainNodeIndex; + nodes.add(node); } } @@ -386,8 +390,10 @@ public final class CommandManager { * @param name the name of the command (or the alias) * @param syntaxes the syntaxes of the command * @param rootChildren the children of the main node (all commands name) + * + * @return The index of the main node for alias redirection */ - private void createCommand(@NotNull CommandSender sender, + private int createCommand(@NotNull CommandSender sender, @NotNull List nodes, @NotNull IntList cmdChildren, @NotNull String name, @@ -493,6 +499,9 @@ public final class CommandManager { if (children.length > 0) { literalNode.redirectedNode = children[0]; } + + return nodes.indexOf(literalNode); + } @NotNull