Store start & end on parsed arguments
This commit is contained in:
parent
152c0b09d1
commit
42631c29b7
4 changed files with 37 additions and 15 deletions
|
@ -1,16 +1,28 @@
|
|||
package com.mojang.brigadier.context;
|
||||
|
||||
import com.mojang.brigadier.ImmutableStringReader;
|
||||
|
||||
public class ParsedArgument<S, T> {
|
||||
private final String raw;
|
||||
private final int start;
|
||||
private final int end;
|
||||
private final T result;
|
||||
|
||||
public ParsedArgument(final String raw, final T result) {
|
||||
this.raw = raw;
|
||||
public ParsedArgument(final int start, final int end, final T result) {
|
||||
this.start = start;
|
||||
this.end = end;
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
public String getRaw() {
|
||||
return raw;
|
||||
public String getRaw(final ImmutableStringReader reader) {
|
||||
return reader.getString().substring(start, end);
|
||||
}
|
||||
|
||||
public int getStart() {
|
||||
return start;
|
||||
}
|
||||
|
||||
public int getEnd() {
|
||||
return end;
|
||||
}
|
||||
|
||||
public T getResult() {
|
||||
|
@ -24,7 +36,8 @@ public class ParsedArgument<S, T> {
|
|||
|
||||
final ParsedArgument that = (ParsedArgument) o;
|
||||
|
||||
if (!raw.equals(that.raw)) return false;
|
||||
if (start != that.start) return false;
|
||||
if (end != that.end) return false;
|
||||
if (!result.equals(that.result)) return false;
|
||||
|
||||
return true;
|
||||
|
@ -32,7 +45,8 @@ public class ParsedArgument<S, T> {
|
|||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = raw.hashCode();
|
||||
int result = start;
|
||||
result = 31 * result + end;
|
||||
result = 31 * result + this.result.hashCode();
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -54,10 +54,10 @@ public class ArgumentCommandNode<S, T> extends CommandNode<S> {
|
|||
public void parse(final StringReader reader, final CommandContextBuilder<S> contextBuilder) throws CommandException {
|
||||
final int start = reader.getCursor();
|
||||
final T result = type.parse(reader, contextBuilder);
|
||||
final ParsedArgument<S, T> parsed = new ParsedArgument<>(reader.getString().substring(start, reader.getCursor()), result);
|
||||
final ParsedArgument<S, T> parsed = new ParsedArgument<>(start, reader.getCursor(), result);
|
||||
|
||||
contextBuilder.withArgument(name, parsed);
|
||||
contextBuilder.withNode(this, parsed.getRaw());
|
||||
contextBuilder.withNode(this, parsed.getRaw(reader));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -37,13 +37,13 @@ public class CommandContextTest {
|
|||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testGetArgument_wrongType() throws Exception {
|
||||
final CommandContext<Object> context = builder.withArgument("foo", new ParsedArgument<>("123", 123)).build();
|
||||
final CommandContext<Object> context = builder.withArgument("foo", new ParsedArgument<>(0, 1, 123)).build();
|
||||
context.getArgument("foo", String.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetArgument() throws Exception {
|
||||
final CommandContext<Object> context = builder.withArgument("foo", new ParsedArgument<>("123", 123)).build();
|
||||
final CommandContext<Object> context = builder.withArgument("foo", new ParsedArgument<>(0, 1, 123)).build();
|
||||
assertThat(context.getArgument("foo", int.class), is(123));
|
||||
}
|
||||
|
||||
|
@ -65,7 +65,7 @@ public class CommandContextTest {
|
|||
.addEqualityGroup(new CommandContextBuilder<>(dispatcher, otherSource).build(), new CommandContextBuilder<>(dispatcher, otherSource).build())
|
||||
.addEqualityGroup(new CommandContextBuilder<>(dispatcher, source).withCommand(command).build(), new CommandContextBuilder<>(dispatcher, source).withCommand(command).build())
|
||||
.addEqualityGroup(new CommandContextBuilder<>(dispatcher, source).withCommand(otherCommand).build(), new CommandContextBuilder<>(dispatcher, source).withCommand(otherCommand).build())
|
||||
.addEqualityGroup(new CommandContextBuilder<>(dispatcher, source).withArgument("foo", new ParsedArgument<>("123", 123)).build(), new CommandContextBuilder<>(dispatcher, source).withArgument("foo", new ParsedArgument<>("123", 123)).build())
|
||||
.addEqualityGroup(new CommandContextBuilder<>(dispatcher, source).withArgument("foo", new ParsedArgument<>(0, 1, 123)).build(), new CommandContextBuilder<>(dispatcher, source).withArgument("foo", new ParsedArgument<>(0, 1, 123)).build())
|
||||
.addEqualityGroup(new CommandContextBuilder<>(dispatcher, source).withNode(node, "foo").withNode(otherNode, "bar").build(), new CommandContextBuilder<>(dispatcher, source).withNode(node, "foo").withNode(otherNode, "bar").build())
|
||||
.addEqualityGroup(new CommandContextBuilder<>(dispatcher, source).withNode(otherNode, "bar").withNode(node, "foo").build(), new CommandContextBuilder<>(dispatcher, source).withNode(otherNode, "bar").withNode(node, "foo").build())
|
||||
.testEquals();
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.mojang.brigadier.context;
|
||||
|
||||
import com.google.common.testing.EqualsTester;
|
||||
import com.mojang.brigadier.StringReader;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
@ -11,9 +12,16 @@ public class ParsedArgumentTest {
|
|||
@Test
|
||||
public void testEquals() throws Exception {
|
||||
new EqualsTester()
|
||||
.addEqualityGroup(new ParsedArgument<>("foo", "bar"), new ParsedArgument<>("foo", "bar"))
|
||||
.addEqualityGroup(new ParsedArgument<>("bar", "baz"), new ParsedArgument<>("bar", "baz"))
|
||||
.addEqualityGroup(new ParsedArgument<>("foo", "baz"), new ParsedArgument<>("foo", "baz"))
|
||||
.addEqualityGroup(new ParsedArgument<>(0, 3, "bar"), new ParsedArgument<>(0, 3, "bar"))
|
||||
.addEqualityGroup(new ParsedArgument<>(3, 6, "baz"), new ParsedArgument<>(3, 6, "baz"))
|
||||
.addEqualityGroup(new ParsedArgument<>(6, 9, "baz"), new ParsedArgument<>(6, 9, "baz"))
|
||||
.testEquals();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRaw() throws Exception {
|
||||
final StringReader reader = new StringReader("0123456789");
|
||||
final ParsedArgument<Object, String> argument = new ParsedArgument<>(2, 5, "");
|
||||
assertThat(argument.getRaw(reader), equalTo("234"));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue