From df2ee23a7c8201f0ea43a242ab931ac31ea2d781 Mon Sep 17 00:00:00 2001 From: themode Date: Sat, 13 Feb 2021 08:43:38 +0100 Subject: [PATCH] ArgumentLoop cleanup --- .../minestom/server/command/CommandManager.java | 14 ++++++++------ .../minestom/server/command/builder/NodeMaker.java | 5 ++++- .../command/builder/arguments/ArgumentLoop.java | 3 +-- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/main/java/net/minestom/server/command/CommandManager.java b/src/main/java/net/minestom/server/command/CommandManager.java index 6d85ab5c1..e872c9422 100644 --- a/src/main/java/net/minestom/server/command/CommandManager.java +++ b/src/main/java/net/minestom/server/command/CommandManager.java @@ -414,7 +414,7 @@ public final class CommandManager { DeclareCommandsPacket.Node literalNode = createMainNode(name, syntaxes.isEmpty()); - addCommandNameNode(literalNode, rootChildren, nodes); + final int literalNodeId = addCommandNameNode(literalNode, rootChildren, nodes); // Contains the arguments of the already-parsed syntaxes List[]> syntaxesArguments = new ArrayList<>(); @@ -434,7 +434,7 @@ public final class CommandManager { // Represent the children of the last node IntList argChildren = cmdChildren; - NodeMaker nodeMaker = new NodeMaker(lastNodes); + NodeMaker nodeMaker = new NodeMaker(lastNodes, literalNodeId); int lastArgumentNodeIndex = nodeMaker.getNodesCount(); final Argument[] arguments = syntax.getArguments(); @@ -525,10 +525,12 @@ public final class CommandManager { return literalNode; } - private void addCommandNameNode(@NotNull DeclareCommandsPacket.Node commandNode, - @NotNull IntList rootChildren, - @NotNull List nodes) { - rootChildren.add(nodes.size()); + private int addCommandNameNode(@NotNull DeclareCommandsPacket.Node commandNode, + @NotNull IntList rootChildren, + @NotNull List nodes) { + final int node = nodes.size(); + rootChildren.add(node); nodes.add(commandNode); + return node; } } 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 78761eba1..9493575b0 100644 --- a/src/main/java/net/minestom/server/command/builder/NodeMaker.java +++ b/src/main/java/net/minestom/server/command/builder/NodeMaker.java @@ -18,8 +18,11 @@ public class NodeMaker { private Rule rule; private int ruleCount; - public NodeMaker(@NotNull DeclareCommandsPacket.Node[] commandNodes){ + public NodeMaker(@NotNull DeclareCommandsPacket.Node[] commandNodes, int id) { addNodes(commandNodes); + for (DeclareCommandsPacket.Node node : commandNodes) { + this.nodeIdsMap.put(node, id); + } } public ConfiguredNodes getLatestConfiguredNodes() { diff --git a/src/main/java/net/minestom/server/command/builder/arguments/ArgumentLoop.java b/src/main/java/net/minestom/server/command/builder/arguments/ArgumentLoop.java index 4e470e2c6..0bbadee1c 100644 --- a/src/main/java/net/minestom/server/command/builder/arguments/ArgumentLoop.java +++ b/src/main/java/net/minestom/server/command/builder/arguments/ArgumentLoop.java @@ -66,12 +66,11 @@ public class ArgumentLoop extends Argument> { final int id = nodeMaker.getNodeIdsMap().getInt(latestNode); for (Argument argument : arguments) { - DeclareCommandsPacket.Node[] latestCache = nodeMaker.getLatestNodes(); argument.processNodes(nodeMaker, executable); NodeMaker.ConfiguredNodes configuredNodes = nodeMaker.getLatestConfiguredNodes(); // For the next loop argument to start at the same place - configuredNodes.getOptions().setPreviousNodes(latestCache); + configuredNodes.getOptions().setPreviousNodes(latestNodes); for (DeclareCommandsPacket.Node lastArgumentNode : configuredNodes.getNodes()) { lastArgumentNode.flags |= 0x08; lastArgumentNode.redirectedNode = id;