Allow consuming of command results

This commit is contained in:
Nathan Adams 2017-11-08 16:05:38 +01:00
parent be0090873b
commit e97dd6bb79
3 changed files with 32 additions and 7 deletions

View file

@ -3,7 +3,7 @@ import groovy.io.FileType
apply plugin: 'java-library' apply plugin: 'java-library'
apply plugin: 'maven' apply plugin: 'maven'
version = '0.1.5' version = '0.1.6'
group = 'com.mojang' group = 'com.mojang'
task wrapper(type: Wrapper) { task wrapper(type: Wrapper) {

View file

@ -6,6 +6,7 @@ import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.context.CommandContextBuilder; import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.context.StringRange; import com.mojang.brigadier.context.StringRange;
import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.CommandSyntaxException;
@ -47,6 +48,8 @@ public class CommandDispatcher<S> {
return input != null && (input.getCommand() != null || input.getChildren().stream().anyMatch(hasCommand)); return input != null && (input.getCommand() != null || input.getChildren().stream().anyMatch(hasCommand));
} }
}; };
private ResultConsumer<S> consumer = (c, s, r) -> {
};
public CommandDispatcher(final RootCommandNode<S> root) { public CommandDispatcher(final RootCommandNode<S> root) {
this.root = root; this.root = root;
@ -62,6 +65,10 @@ public class CommandDispatcher<S> {
return build; return build;
} }
public void setConsumer(final ResultConsumer<S> consumer) {
this.consumer = consumer;
}
public int execute(final String input, final S source) throws CommandSyntaxException { public int execute(final String input, final S source) throws CommandSyntaxException {
final ParseResults<S> parse = parse(input, source); final ParseResults<S> parse = parse(input, source);
return execute(parse); return execute(parse);
@ -84,26 +91,36 @@ public class CommandDispatcher<S> {
contexts.add(parse.getContext()); contexts.add(parse.getContext());
while (!contexts.isEmpty()) { while (!contexts.isEmpty()) {
final CommandContextBuilder<S> context = contexts.removeLast(); final CommandContextBuilder<S> builder = contexts.removeLast();
final CommandContextBuilder<S> child = context.getChild(); final CommandContextBuilder<S> child = builder.getChild();
final CommandContext<S> context = builder.build();
if (child != null) { if (child != null) {
if (!child.getNodes().isEmpty()) { if (!child.getNodes().isEmpty()) {
final RedirectModifier<S> modifier = Iterators.getLast(context.getNodes().keySet().iterator()).getRedirectModifier(); final RedirectModifier<S> modifier = Iterators.getLast(builder.getNodes().keySet().iterator()).getRedirectModifier();
final Collection<S> results = modifier.apply(context.build()); final Collection<S> results = modifier.apply(context);
if (results.isEmpty()) { if (results.isEmpty()) {
consumer.onCommandComplete(context, false, 0);
return 0; return 0;
} }
for (final S source : results) { for (final S source : results) {
contexts.add(child.copy().withSource(source)); contexts.add(child.copy().withSource(source));
} }
} }
} else if (context.getCommand() != null) { } else if (builder.getCommand() != null) {
foundCommand = true; foundCommand = true;
result += context.getCommand().run(context.build()); try {
final int value = builder.getCommand().run(context);
result += value;
consumer.onCommandComplete(context, true, value);
} catch (final CommandSyntaxException ex) {
consumer.onCommandComplete(context, false, 0);
throw ex;
}
} }
} }
if (!foundCommand) { if (!foundCommand) {
consumer.onCommandComplete(parse.getContext().build(), false, 0);
throw ERROR_UNKNOWN_COMMAND.createWithContext(parse.getReader()); throw ERROR_UNKNOWN_COMMAND.createWithContext(parse.getReader());
} }

View file

@ -0,0 +1,8 @@
package com.mojang.brigadier;
import com.mojang.brigadier.context.CommandContext;
@FunctionalInterface
public interface ResultConsumer<S> {
void onCommandComplete(CommandContext<S> context, boolean success, int result);
}