Added/optimized getInput methods to both builder and context

This commit is contained in:
Nathan Adams
2017-06-28 11:15:30 +02:00
parent 7d4dcc3e4b
commit e9a930c3b5
2 changed files with 20 additions and 11 deletions
@@ -1,28 +1,26 @@
package com.mojang.brigadier.context; package com.mojang.brigadier.context;
import com.google.common.base.Joiner;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.primitives.Primitives; import com.google.common.primitives.Primitives;
import com.mojang.brigadier.Command; import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.tree.CommandNode; import com.mojang.brigadier.tree.CommandNode;
import java.util.Map; import java.util.Map;
public class CommandContext<S> { public class CommandContext<S> {
private final Joiner JOINER = Joiner.on(CommandDispatcher.ARGUMENT_SEPARATOR);
private final S source; private final S source;
private final Map<String, ParsedArgument<?>> arguments; private final Map<String, ParsedArgument<?>> arguments;
private final Command<S> command; private final Command<S> command;
private final Map<CommandNode<S>, String> nodes; private final Map<CommandNode<S>, String> nodes;
private final String input;
public CommandContext(S source, Map<String, ParsedArgument<?>> arguments, Command<S> command, Map<CommandNode<S>, String> nodes) { public CommandContext(S source, Map<String, ParsedArgument<?>> arguments, Command<S> command, Map<CommandNode<S>, String> nodes, String input) {
this.source = source; this.source = source;
this.arguments = arguments; this.arguments = arguments;
this.command = command; this.command = command;
this.nodes = nodes; this.nodes = nodes;
this.input = input;
} }
public Command<S> getCommand() { public Command<S> getCommand() {
@@ -73,7 +71,7 @@ public class CommandContext<S> {
} }
public String getInput() { public String getInput() {
return JOINER.join(nodes.values()); return input;
} }
public Map<CommandNode<S>, String> getNodes() { public Map<CommandNode<S>, String> getNodes() {
@@ -81,8 +79,8 @@ public class CommandContext<S> {
} }
public CommandContext<S> copy() { public CommandContext<S> copy() {
Map<String, ParsedArgument<?>> arguments = Maps.newHashMap(); Map<String, ParsedArgument<?>> arguments = Maps.newLinkedHashMap();
this.arguments.forEach((k, v) -> arguments.put(k, v.copy())); 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);
} }
} }
@@ -2,6 +2,7 @@ package com.mojang.brigadier.context;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.mojang.brigadier.Command; import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.tree.CommandNode; import com.mojang.brigadier.tree.CommandNode;
import java.util.Map; import java.util.Map;
@@ -9,6 +10,7 @@ import java.util.Map;
public class CommandContextBuilder<S> { public class CommandContextBuilder<S> {
private final Map<String, ParsedArgument<?>> arguments = Maps.newHashMap(); private final Map<String, ParsedArgument<?>> arguments = Maps.newHashMap();
private final Map<CommandNode<S>, String> nodes = Maps.newLinkedHashMap(); private final Map<CommandNode<S>, String> nodes = Maps.newLinkedHashMap();
private final StringBuilder input = new StringBuilder();
private S source; private S source;
private Command<S> command; private Command<S> command;
@@ -40,19 +42,28 @@ public class CommandContextBuilder<S> {
} }
public CommandContextBuilder<S> withNode(CommandNode<S> node, String raw) { public CommandContextBuilder<S> withNode(CommandNode<S> 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; return this;
} }
public CommandContextBuilder<S> copy() { public CommandContextBuilder<S> copy() {
CommandContextBuilder<S> copy = new CommandContextBuilder<>(source); CommandContextBuilder<S> copy = new CommandContextBuilder<>(source);
copy.command = this.command; 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.nodes.putAll(this.nodes);
copy.input.append(input);
return copy; return copy;
} }
public String getInput() {
return input.toString();
}
public CommandContext<S> build() { public CommandContext<S> build() {
return new CommandContext<>(source, arguments, command, nodes); return new CommandContext<>(source, arguments, command, nodes, input.toString());
} }
} }