From e9a930c3b596ca5f45eb9ed9ada7eaffb875c002 Mon Sep 17 00:00:00 2001 From: Nathan Adams Date: Wed, 28 Jun 2017 11:15:30 +0200 Subject: [PATCH] Added/optimized getInput methods to both builder and context --- .../brigadier/context/CommandContext.java | 14 ++++++-------- .../context/CommandContextBuilder.java | 17 ++++++++++++++--- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/mojang/brigadier/context/CommandContext.java b/src/main/java/com/mojang/brigadier/context/CommandContext.java index 237755e..ef472d5 100644 --- a/src/main/java/com/mojang/brigadier/context/CommandContext.java +++ b/src/main/java/com/mojang/brigadier/context/CommandContext.java @@ -1,28 +1,26 @@ package com.mojang.brigadier.context; -import com.google.common.base.Joiner; import com.google.common.collect.Iterables; import com.google.common.collect.Maps; import com.google.common.primitives.Primitives; import com.mojang.brigadier.Command; -import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.tree.CommandNode; import java.util.Map; public class CommandContext { - private final Joiner JOINER = Joiner.on(CommandDispatcher.ARGUMENT_SEPARATOR); - private final S source; private final Map> arguments; private final Command command; private final Map, String> nodes; + private final String input; - public CommandContext(S source, Map> arguments, Command command, Map, String> nodes) { + public CommandContext(S source, Map> arguments, Command command, Map, String> nodes, String input) { this.source = source; this.arguments = arguments; this.command = command; this.nodes = nodes; + this.input = input; } public Command getCommand() { @@ -73,7 +71,7 @@ public class CommandContext { } public String getInput() { - return JOINER.join(nodes.values()); + return input; } public Map, String> getNodes() { @@ -81,8 +79,8 @@ public class CommandContext { } public CommandContext copy() { - Map> arguments = Maps.newHashMap(); + Map> arguments = Maps.newLinkedHashMap(); this.arguments.forEach((k, v) -> arguments.put(k, v.copy())); - return new CommandContext<>(source, arguments, command, nodes); + return new CommandContext<>(source, arguments, command, nodes, input); } } diff --git a/src/main/java/com/mojang/brigadier/context/CommandContextBuilder.java b/src/main/java/com/mojang/brigadier/context/CommandContextBuilder.java index 3ed4aa9..1455108 100644 --- a/src/main/java/com/mojang/brigadier/context/CommandContextBuilder.java +++ b/src/main/java/com/mojang/brigadier/context/CommandContextBuilder.java @@ -2,6 +2,7 @@ package com.mojang.brigadier.context; import com.google.common.collect.Maps; import com.mojang.brigadier.Command; +import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.tree.CommandNode; import java.util.Map; @@ -9,6 +10,7 @@ import java.util.Map; public class CommandContextBuilder { private final Map> arguments = Maps.newHashMap(); private final Map, String> nodes = Maps.newLinkedHashMap(); + private final StringBuilder input = new StringBuilder(); private S source; private Command command; @@ -40,19 +42,28 @@ public class CommandContextBuilder { } public CommandContextBuilder withNode(CommandNode node, String raw) { - this.nodes.put(node, raw); + if (!nodes.isEmpty()) { + input.append(CommandDispatcher.ARGUMENT_SEPARATOR); + } + nodes.put(node, raw); + input.append(raw); return this; } public CommandContextBuilder copy() { CommandContextBuilder copy = new CommandContextBuilder<>(source); copy.command = this.command; - this.arguments.forEach((k, v) -> copy.arguments.put(k, v.copy())); + arguments.forEach((k, v) -> copy.arguments.put(k, v.copy())); copy.nodes.putAll(this.nodes); + copy.input.append(input); return copy; } + public String getInput() { + return input.toString(); + } + public CommandContext build() { - return new CommandContext<>(source, arguments, command, nodes); + return new CommandContext<>(source, arguments, command, nodes, input.toString()); } }