Split ParsedArgument into fixed or dynamic, added copy methods for caching contexts
This commit is contained in:
@@ -3,6 +3,7 @@ package com.mojang.brigadier.arguments;
|
||||
import com.google.common.base.Splitter;
|
||||
import com.mojang.brigadier.CommandDispatcher;
|
||||
import com.mojang.brigadier.context.CommandContext;
|
||||
import com.mojang.brigadier.context.FixedParsedArgument;
|
||||
import com.mojang.brigadier.context.ParsedArgument;
|
||||
import com.mojang.brigadier.exceptions.CommandException;
|
||||
import com.mojang.brigadier.exceptions.ParameterizedCommandExceptionType;
|
||||
@@ -54,7 +55,7 @@ public class IntegerArgumentType implements CommandArgumentType<Integer> {
|
||||
throw ERROR_TOO_BIG.create(value, maximum);
|
||||
}
|
||||
|
||||
return new ParsedArgument<>(raw, value);
|
||||
return new FixedParsedArgument<>(raw, value);
|
||||
} catch (NumberFormatException ignored) {
|
||||
throw ERROR_NOT_A_NUMBER.create(raw);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ 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;
|
||||
@@ -78,4 +79,10 @@ public class CommandContext<S> {
|
||||
public Map<CommandNode<S>, String> getNodes() {
|
||||
return nodes;
|
||||
}
|
||||
|
||||
public CommandContext<S> copy() {
|
||||
Map<String, ParsedArgument<?>> arguments = Maps.newHashMap();
|
||||
this.arguments.forEach((k, v) -> arguments.put(k, v.copy()));
|
||||
return new CommandContext<>(source, arguments, command, nodes);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.mojang.brigadier.context;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class DynamicParsedArgument<T> implements ParsedArgument<T> {
|
||||
private final String raw;
|
||||
private Supplier<T> supplier;
|
||||
private boolean evaluated;
|
||||
private T result;
|
||||
|
||||
public DynamicParsedArgument(String raw, Supplier<T> supplier) {
|
||||
this.raw = raw;
|
||||
this.supplier = supplier;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRaw() {
|
||||
return raw;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getResult() {
|
||||
if (!evaluated) {
|
||||
result = supplier.get();
|
||||
evaluated = true;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof DynamicParsedArgument)) return false;
|
||||
|
||||
DynamicParsedArgument that = (DynamicParsedArgument) o;
|
||||
|
||||
if (!raw.equals(that.raw)) return false;
|
||||
if (!supplier.equals(that.supplier)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = raw.hashCode();
|
||||
result = 31 * result + supplier.hashCode();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ParsedArgument<T> copy() {
|
||||
return new DynamicParsedArgument<>(raw, supplier);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.mojang.brigadier.context;
|
||||
|
||||
public class FixedParsedArgument<T> implements ParsedArgument<T> {
|
||||
private final String raw;
|
||||
private final T result;
|
||||
|
||||
public FixedParsedArgument(String raw, T result) {
|
||||
this.raw = raw;
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRaw() {
|
||||
return raw;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getResult() {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof FixedParsedArgument)) return false;
|
||||
|
||||
FixedParsedArgument that = (FixedParsedArgument) o;
|
||||
|
||||
if (!raw.equals(that.raw)) return false;
|
||||
if (!result.equals(that.result)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = raw.hashCode();
|
||||
result = 31 * result + this.result.hashCode();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ParsedArgument<T> copy() {
|
||||
return new FixedParsedArgument<>(raw, result);
|
||||
}
|
||||
}
|
||||
@@ -1,39 +1,9 @@
|
||||
package com.mojang.brigadier.context;
|
||||
|
||||
public class ParsedArgument<T> {
|
||||
private final String raw;
|
||||
private final T result;
|
||||
public interface ParsedArgument<T> {
|
||||
String getRaw();
|
||||
|
||||
public ParsedArgument(String raw, T result) {
|
||||
this.raw = raw;
|
||||
this.result = result;
|
||||
}
|
||||
T getResult();
|
||||
|
||||
public String getRaw() {
|
||||
return raw;
|
||||
}
|
||||
|
||||
public T getResult() {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof ParsedArgument)) return false;
|
||||
|
||||
ParsedArgument that = (ParsedArgument) o;
|
||||
|
||||
if (!raw.equals(that.raw)) return false;
|
||||
if (!result.equals(that.result)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result1 = raw.hashCode();
|
||||
result1 = 31 * result1 + result.hashCode();
|
||||
return result1;
|
||||
}
|
||||
ParsedArgument<T> copy();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user