Pass in source to evaluations of arguments

This commit is contained in:
Nathan Adams
2017-07-04 15:12:31 +02:00
parent 4af7274892
commit c1a159f75a
15 changed files with 96 additions and 83 deletions
@@ -6,7 +6,7 @@ import com.mojang.brigadier.exceptions.CommandException;
import java.util.Set;
public interface CommandArgumentType<T> {
ParsedArgument<T> parse(String command) throws CommandException;
<S> ParsedArgument<S, T> parse(String command) throws CommandException;
void listSuggestions(String command, Set<String> output);
}
@@ -42,7 +42,7 @@ public class IntegerArgumentType implements CommandArgumentType<Integer> {
}
@Override
public ParsedArgument<Integer> parse(String command) throws CommandException {
public <S> ParsedArgument<S, Integer> parse(String command) throws CommandException {
String raw = SPLITTER.split(command).iterator().next();
try {
@@ -39,7 +39,7 @@ public class StringArgumentType implements CommandArgumentType<String> {
}
@Override
public ParsedArgument<String> parse(String command) throws CommandException {
public <S> ParsedArgument<S, String> parse(String command) throws CommandException {
if (type == StringType.GREEDY_PHRASE) {
return new FixedParsedArgument<>(command, command);
} else if (type == StringType.SINGLE_WORLD) {
@@ -5,17 +5,18 @@ import com.google.common.collect.Maps;
import com.google.common.primitives.Primitives;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.tree.CommandNode;
import sun.security.x509.OIDMap;
import java.util.Map;
public class CommandContext<S> {
private final S source;
private final Map<String, ParsedArgument<?>> arguments;
private final Map<String, ParsedArgument<S, ?>> arguments;
private final Command<S> command;
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, String input) {
public CommandContext(S source, Map<String, ParsedArgument<S, ?>> arguments, Command<S> command, Map<CommandNode<S>, String> nodes, String input) {
this.source = source;
this.arguments = arguments;
this.command = command;
@@ -33,16 +34,17 @@ public class CommandContext<S> {
@SuppressWarnings("unchecked")
public <V> V getArgument(String name, Class<V> clazz) {
ParsedArgument<?> argument = arguments.get(name);
ParsedArgument<S, ?> argument = arguments.get(name);
if (argument == null) {
throw new IllegalArgumentException("No such argument '" + name + "' exists on this command");
}
if (Primitives.wrap(clazz).isAssignableFrom(argument.getResult().getClass())) {
return ((ParsedArgument<V>) argument).getResult();
final Object result = argument.getResult(source);
if (Primitives.wrap(clazz).isAssignableFrom(result.getClass())) {
return (V) result;
} else {
throw new IllegalArgumentException("Argument '" + name + "' is defined as " + argument.getResult().getClass().getSimpleName() + ", not " + clazz);
throw new IllegalArgumentException("Argument '" + name + "' is defined as " + result.getClass().getSimpleName() + ", not " + clazz);
}
}
@@ -79,7 +81,7 @@ public class CommandContext<S> {
}
public CommandContext<S> copy() {
Map<String, ParsedArgument<?>> arguments = Maps.newLinkedHashMap();
Map<String, ParsedArgument<S, ?>> arguments = Maps.newLinkedHashMap();
this.arguments.forEach((k, v) -> arguments.put(k, v.copy()));
return new CommandContext<>(source, arguments, command, nodes, input);
}
@@ -8,7 +8,7 @@ import com.mojang.brigadier.tree.CommandNode;
import java.util.Map;
public class CommandContextBuilder<S> {
private final Map<String, ParsedArgument<?>> arguments = Maps.newHashMap();
private final Map<String, ParsedArgument<S, ?>> arguments = Maps.newHashMap();
private final Map<CommandNode<S>, String> nodes = Maps.newLinkedHashMap();
private final StringBuilder input = new StringBuilder();
private S source;
@@ -27,12 +27,12 @@ public class CommandContextBuilder<S> {
return source;
}
public CommandContextBuilder<S> withArgument(String name, ParsedArgument<?> argument) {
public CommandContextBuilder<S> withArgument(String name, ParsedArgument<S, ?> argument) {
this.arguments.put(name, argument);
return this;
}
public Map<String, ParsedArgument<?>> getArguments() {
public Map<String, ParsedArgument<S, ?>> getArguments() {
return arguments;
}
@@ -1,14 +1,14 @@
package com.mojang.brigadier.context;
import java.util.function.Supplier;
import java.util.function.Function;
public class DynamicParsedArgument<T> implements ParsedArgument<T> {
public class DynamicParsedArgument<S, T> implements ParsedArgument<S, T> {
private final String raw;
private Supplier<T> supplier;
private Function<S, T> supplier;
private boolean evaluated;
private T result;
public DynamicParsedArgument(String raw, Supplier<T> supplier) {
public DynamicParsedArgument(String raw, Function<S, T> supplier) {
this.raw = raw;
this.supplier = supplier;
}
@@ -19,9 +19,9 @@ public class DynamicParsedArgument<T> implements ParsedArgument<T> {
}
@Override
public T getResult() {
public T getResult(S source) {
if (!evaluated) {
result = supplier.get();
result = supplier.apply(source);
evaluated = true;
}
return result;
@@ -48,7 +48,7 @@ public class DynamicParsedArgument<T> implements ParsedArgument<T> {
}
@Override
public ParsedArgument<T> copy() {
public ParsedArgument<S, T> copy() {
return new DynamicParsedArgument<>(raw, supplier);
}
}
@@ -1,6 +1,6 @@
package com.mojang.brigadier.context;
public class FixedParsedArgument<T> implements ParsedArgument<T> {
public class FixedParsedArgument<S, T> implements ParsedArgument<S, T> {
private final String raw;
private final T result;
@@ -15,7 +15,7 @@ public class FixedParsedArgument<T> implements ParsedArgument<T> {
}
@Override
public T getResult() {
public T getResult(S source) {
return result;
}
@@ -40,7 +40,7 @@ public class FixedParsedArgument<T> implements ParsedArgument<T> {
}
@Override
public ParsedArgument<T> copy() {
public ParsedArgument<S, T> copy() {
return new FixedParsedArgument<>(raw, result);
}
}
@@ -1,9 +1,9 @@
package com.mojang.brigadier.context;
public interface ParsedArgument<T> {
public interface ParsedArgument<S, T> {
String getRaw();
T getResult();
T getResult(S source);
ParsedArgument<T> copy();
ParsedArgument<S, T> copy();
}
@@ -2,7 +2,6 @@ package com.mojang.brigadier.tree;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.arguments.CommandArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.context.ParsedArgument;
@@ -44,7 +43,7 @@ public class ArgumentCommandNode<S, T> extends CommandNode<S> {
@Override
public String parse(String command, CommandContextBuilder<S> contextBuilder) throws CommandException {
ParsedArgument<T> parsed = type.parse(command);
ParsedArgument<S, T> parsed = type.parse(command);
int start = parsed.getRaw().length();
contextBuilder.withArgument(name, parsed);