From 84da72d79828aaa7b50db5727b11aa786995de0f Mon Sep 17 00:00:00 2001 From: themode Date: Fri, 12 Feb 2021 00:00:44 +0100 Subject: [PATCH] Fix command aliases + cleanup --- .../server/command/CommandManager.java | 45 ++++++++++--------- .../server/command/builder/NodeMaker.java | 8 ++++ 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/src/main/java/net/minestom/server/command/CommandManager.java b/src/main/java/net/minestom/server/command/CommandManager.java index bde69833e..90adda252 100644 --- a/src/main/java/net/minestom/server/command/CommandManager.java +++ b/src/main/java/net/minestom/server/command/CommandManager.java @@ -320,19 +320,22 @@ public final class CommandManager { final Collection syntaxes = command.getSyntaxes(); // Create command for main name - int mainNodeIndex = createCommand(player, nodes, cmdChildren, command.getName(), syntaxes, rootChildren); + final int mainNodeIndex = createCommand(player, nodes, cmdChildren, + command.getName(), syntaxes, rootChildren); // Use redirection to hook aliases with the command - if (command.getAliases() == null) + final String[] aliases = command.getAliases(); + if (aliases == null) continue; - for (String alias : command.getAliases()) { - DeclareCommandsPacket.Node node = new DeclareCommandsPacket.Node(); - node.flags = DeclareCommandsPacket.getFlag(DeclareCommandsPacket.NodeType.LITERAL, + for (String alias : aliases) { + DeclareCommandsPacket.Node aliasNode = new DeclareCommandsPacket.Node(); + aliasNode.flags = DeclareCommandsPacket.getFlag(DeclareCommandsPacket.NodeType.LITERAL, false, true, false); - node.name = alias; - node.redirectedNode = mainNodeIndex; - nodes.add(node); + aliasNode.name = alias; + aliasNode.redirectedNode = mainNodeIndex; + + addCommandNameNode(aliasNode, rootChildren, nodes); } } @@ -379,8 +382,7 @@ public final class CommandManager { literalNode.name = name; literalNode.children = new int[]{nodes.size() - 1}; - rootChildren.add(nodes.size()); - nodes.add(literalNode); + addCommandNameNode(literalNode, rootChildren, nodes); } // Add root node children @@ -412,8 +414,7 @@ public final class CommandManager { DeclareCommandsPacket.Node literalNode = createMainNode(name, syntaxes.isEmpty()); - rootChildren.add(nodes.size()); - nodes.add(literalNode); + addCommandNameNode(literalNode, rootChildren, nodes); // Contains the arguments of the already-parsed syntaxes List[]> syntaxesArguments = new ArrayList<>(); @@ -427,15 +428,14 @@ public final class CommandManager { continue; } - NodeMaker nodeMaker = new NodeMaker(); - // Represent the last nodes computed in the last iteration DeclareCommandsPacket.Node[] lastNodes = new DeclareCommandsPacket.Node[]{literalNode}; // Represent the children of the last node IntList argChildren = cmdChildren; - int lastArgumentNodeIndex = 0; + NodeMaker nodeMaker = new NodeMaker(lastNodes); + int lastArgumentNodeIndex = nodeMaker.getNodesCount(); final Argument[] arguments = syntax.getArguments(); for (int i = 0; i < arguments.length; i++) { @@ -508,12 +508,8 @@ public final class CommandManager { syntaxesArguments.add(arguments); } - final int[] children = ArrayUtils.toArray(cmdChildren); - //System.out.println("test " + children.length + " : " + children[0]); - literalNode.children = children; - if (children.length > 0) { - literalNode.redirectedNode = children[0]; - } + + literalNode.children = ArrayUtils.toArray(cmdChildren); return nodes.indexOf(literalNode); @@ -527,4 +523,11 @@ public final class CommandManager { return literalNode; } + + private void addCommandNameNode(@NotNull DeclareCommandsPacket.Node commandNode, + @NotNull IntList rootChildren, + @NotNull List nodes) { + rootChildren.add(nodes.size()); + nodes.add(commandNode); + } } diff --git a/src/main/java/net/minestom/server/command/builder/NodeMaker.java b/src/main/java/net/minestom/server/command/builder/NodeMaker.java index 90baa14a5..78761eba1 100644 --- a/src/main/java/net/minestom/server/command/builder/NodeMaker.java +++ b/src/main/java/net/minestom/server/command/builder/NodeMaker.java @@ -18,6 +18,10 @@ public class NodeMaker { private Rule rule; private int ruleCount; + public NodeMaker(@NotNull DeclareCommandsPacket.Node[] commandNodes){ + addNodes(commandNodes); + } + public ConfiguredNodes getLatestConfiguredNodes() { if (configuredNodes.isEmpty()) return null; @@ -29,6 +33,10 @@ public class NodeMaker { return configuredNodes != null ? configuredNodes.nodes : null; } + public int getNodesCount() { + return nodes.size(); + } + public void addNodes(@NotNull DeclareCommandsPacket.Node[] nodes) { Options options = null; if (rule != null) {