diff --git a/src/main/java/com/mojang/brigadier/CommandDispatcher.java b/src/main/java/com/mojang/brigadier/CommandDispatcher.java index 0ba62ab..46d6119 100644 --- a/src/main/java/com/mojang/brigadier/CommandDispatcher.java +++ b/src/main/java/com/mojang/brigadier/CommandDispatcher.java @@ -40,11 +40,15 @@ public class CommandDispatcher { } public int execute(String command, S source) throws CommandException { - CommandContext context = parseNodes(root, command, new CommandContextBuilder<>(source)); + CommandContext context = parse(command, source).build(); return context.getCommand().run(context); } - private CommandContext parseNodes(CommandNode node, String command, CommandContextBuilder contextBuilder) throws CommandException { + public CommandContextBuilder parse(String command, S source) throws CommandException { + return parseNodes(root, command, new CommandContextBuilder<>(source)); + } + + private CommandContextBuilder parseNodes(CommandNode node, String command, CommandContextBuilder contextBuilder) throws CommandException { CommandException exception = null; final S source = contextBuilder.getSource(); @@ -60,7 +64,7 @@ public class CommandDispatcher { context.withCommand(child.getCommand()); } if (remaining.isEmpty()) { - return context.build(); + return context; } else { return parseNodes(child, remaining.substring(1), context); } @@ -76,11 +80,11 @@ public class CommandDispatcher { throw ERROR_UNKNOWN_COMMAND.create(); } - return contextBuilder.build(); + return contextBuilder; } public String getUsage(String command, S source) throws CommandException { - CommandContext context = parseNodes(root, command, new CommandContextBuilder<>(source)); + CommandContext context = parseNodes(root, command, new CommandContextBuilder<>(source)).build(); CommandNode base = Iterables.getLast(context.getNodes().keySet()); List> children = base.getChildren().stream().filter(hasCommand).collect(Collectors.toList()); boolean optional = base.getCommand() != null; diff --git a/src/main/java/com/mojang/brigadier/context/CommandContextBuilder.java b/src/main/java/com/mojang/brigadier/context/CommandContextBuilder.java index 862327f..066451e 100644 --- a/src/main/java/com/mojang/brigadier/context/CommandContextBuilder.java +++ b/src/main/java/com/mojang/brigadier/context/CommandContextBuilder.java @@ -42,7 +42,7 @@ public class CommandContextBuilder { public CommandContextBuilder copy() { CommandContextBuilder copy = new CommandContextBuilder<>(source); copy.command = this.command; - copy.arguments.putAll(this.arguments); + this.arguments.forEach((k, v) -> copy.arguments.put(k, v.copy())); copy.nodes.putAll(this.nodes); return copy; }