Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve arguments after redirects #138

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/com/mojang/brigadier/CommandDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public int execute(final ParseResults<S> parse) throws CommandSyntaxException {
if (next == null) {
next = new ArrayList<>(1);
}
next.add(child.copyFor(context.getSource()));
next.add(child.copyFor(context.getSource()).copyWithArgumentsOf(context));
} else {
try {
final Collection<S> results = modifier.apply(context);
Expand All @@ -245,7 +245,7 @@ public int execute(final ParseResults<S> parse) throws CommandSyntaxException {
next = new ArrayList<>(results.size());
}
for (final S source : results) {
next.add(child.copyFor(source));
next.add(child.copyFor(source).copyWithArgumentsOf(context));
}
} else {
foundCommand = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ public CommandContext<S> copyFor(final S source) {
return new CommandContext<>(source, input, arguments, command, rootNode, nodes, range, child, modifier, forks);
}

public CommandContext<S> copyWithArgumentsOf(CommandContext<S> context) {
Map<String, ParsedArgument<S, ?>> newArguments = new HashMap<>();
newArguments.putAll(context.arguments);
newArguments.putAll(this.arguments);
return new CommandContext<>(source, input, newArguments, command, rootNode, nodes, range, child, modifier, forks);
}

public CommandContext<S> getChild() {
return child;
}
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/com/mojang/brigadier/context/CommandContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,34 @@ public void testGetArgument() throws Exception {
assertThat(context.getArgument("foo", int.class), is(123));
}

@Test
public void testCopyArgumentsFills() throws Exception {
final CommandContext<Object> originalContext = new CommandContextBuilder<>(dispatcher, source, rootNode, 0).withArgument("foo", new ParsedArgument<>(0, 1, 123)).build("123");
final CommandContext<Object> childContext = new CommandContextBuilder<>(dispatcher, source, rootNode, 0).build("123");

final CommandContext<Object> copiedContext = childContext.copyWithArgumentsOf(originalContext);
assertThat(copiedContext.getArgument("foo", int.class), is(123));
}

@Test
public void testCopyArgumentsOverrides() throws Exception {
final CommandContext<Object> originalContext = new CommandContextBuilder<>(dispatcher, source, rootNode, 0).withArgument("foo", new ParsedArgument<>(0, 1, 123)).build("123");
final CommandContext<Object> childContext = new CommandContextBuilder<>(dispatcher, source, rootNode, 0).withArgument("foo", new ParsedArgument<>(0, 1, "123")).build("123");

final CommandContext<Object> copiedContext = childContext.copyWithArgumentsOf(originalContext);
assertThat(copiedContext.getArgument("foo", String.class), is("123"));
}

@Test
public void testCopyArgumentsMerges() throws Exception {
final CommandContext<Object> originalContext = new CommandContextBuilder<>(dispatcher, source, rootNode, 0).withArgument("foo", new ParsedArgument<>(0, 1, 123)).build("123");
final CommandContext<Object> childContext = new CommandContextBuilder<>(dispatcher, source, rootNode, 0).withArgument("bar", new ParsedArgument<>(0, 1, "123")).build("123");

final CommandContext<Object> copiedContext = childContext.copyWithArgumentsOf(originalContext);
assertThat(copiedContext.getArgument("foo", int.class), is(123));
assertThat(copiedContext.getArgument("bar", String.class), is("123"));
}

@Test
public void testSource() throws Exception {
assertThat(builder.build("").getSource(), is(source));
Expand Down