Added generic return type R to command

This commit is contained in:
Serena 2022-12-02 13:55:14 -05:00
parent d27e8f135d
commit 0515ff8b28
9 changed files with 22 additions and 23 deletions

View file

@ -7,8 +7,8 @@ import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
@FunctionalInterface
public interface Command<S> {
public interface Command<S, R> {
int SINGLE_SUCCESS = 1;
int run(CommandContext<S> context) throws CommandSyntaxException;
R run(CommandContext<S> context) throws CommandSyntaxException;
}

View file

@ -62,7 +62,7 @@ public class CommandDispatcher<S> {
return input != null && (input.getCommand() != null || input.getChildren().stream().anyMatch(hasCommand));
}
};
private ResultConsumer<S> consumer = (c, s, r) -> {
private ResultConsumer<S, Object> consumer = (c, s, r) -> {
};
/**
@ -104,7 +104,7 @@ public class CommandDispatcher<S> {
*
* @param consumer the new result consumer to be called
*/
public void setConsumer(final ResultConsumer<S> consumer) {
public void setConsumer(final ResultConsumer<S, Object> consumer) {
this.consumer = consumer;
}
@ -250,7 +250,7 @@ public class CommandDispatcher<S> {
}
}
} catch (final CommandSyntaxException ex) {
consumer.onCommandComplete(context, false, 0);
consumer.onCommandComplete(context, false, null);
if (!forked) {
throw ex;
}
@ -260,8 +260,7 @@ public class CommandDispatcher<S> {
} else if (context.getCommand() != null) {
foundCommand = true;
try {
final int value = context.getCommand().run(context);
result += value;
final Object value = context.getCommand().run(context);
consumer.onCommandComplete(context, true, value);
successfulForks++;
} catch (final CommandSyntaxException ex) {

View file

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

View file

@ -15,7 +15,7 @@ import java.util.function.Predicate;
public abstract class ArgumentBuilder<S, T extends ArgumentBuilder<S, T>> {
private final RootCommandNode<S> arguments = new RootCommandNode<>();
private Command<S> command;
private Command<S, ?> command;
private Predicate<S> requirement = s -> true;
private CommandNode<S> target;
private RedirectModifier<S> modifier = null;
@ -43,12 +43,12 @@ public abstract class ArgumentBuilder<S, T extends ArgumentBuilder<S, T>> {
return arguments.getChildren();
}
public T executes(final Command<S> command) {
public <R> T executes(final Command<S, R> command) {
this.command = command;
return getThis();
}
public Command<S> getCommand() {
public Command<S, ?> getCommand() {
return command;
}

View file

@ -28,7 +28,7 @@ public class CommandContext<S> {
private final S source;
private final String input;
private final Command<S> command;
private final Command<S, ?> command;
private final Map<String, ParsedArgument<S, ?>> arguments;
private final CommandNode<S> rootNode;
private final List<ParsedCommandNode<S>> nodes;
@ -37,7 +37,7 @@ public class CommandContext<S> {
private final RedirectModifier<S> modifier;
private final boolean forks;
public CommandContext(final S source, final String input, final Map<String, ParsedArgument<S, ?>> arguments, final Command<S> command, final CommandNode<S> rootNode, final List<ParsedCommandNode<S>> nodes, final StringRange range, final CommandContext<S> child, final RedirectModifier<S> modifier, boolean forks) {
public CommandContext(final S source, final String input, final Map<String, ParsedArgument<S, ?>> arguments, final Command<S, ?> command, final CommandNode<S> rootNode, final List<ParsedCommandNode<S>> nodes, final StringRange range, final CommandContext<S> child, final RedirectModifier<S> modifier, boolean forks) {
this.source = source;
this.input = input;
this.arguments = arguments;
@ -69,7 +69,7 @@ public class CommandContext<S> {
return result;
}
public Command<S> getCommand() {
public Command<S, ?> getCommand() {
return command;
}

View file

@ -19,7 +19,7 @@ public class CommandContextBuilder<S> {
private final List<ParsedCommandNode<S>> nodes = new ArrayList<>();
private final CommandDispatcher<S> dispatcher;
private S source;
private Command<S> command;
private Command<S, ?> command;
private CommandContextBuilder<S> child;
private StringRange range;
private RedirectModifier<S> modifier = null;
@ -54,7 +54,7 @@ public class CommandContextBuilder<S> {
return arguments;
}
public CommandContextBuilder<S> withCommand(final Command<S> command) {
public CommandContextBuilder<S> withCommand(final Command<S, ?> command) {
this.command = command;
return this;
}
@ -95,7 +95,7 @@ public class CommandContextBuilder<S> {
return result;
}
public Command<S> getCommand() {
public Command<S, ?> getCommand() {
return command;
}

View file

@ -28,7 +28,7 @@ public class ArgumentCommandNode<S, T> extends CommandNode<S> {
private final ArgumentType<T> type;
private final SuggestionProvider<S> customSuggestions;
public ArgumentCommandNode(final String name, final ArgumentType<T> type, final Command<S> command, final Predicate<S> requirement, final CommandNode<S> redirect, final RedirectModifier<S> modifier, final boolean forks, final SuggestionProvider<S> customSuggestions) {
public ArgumentCommandNode(final String name, final ArgumentType<T> type, final Command<S, ?> command, final Predicate<S> requirement, final CommandNode<S> redirect, final RedirectModifier<S> modifier, final boolean forks, final SuggestionProvider<S> customSuggestions) {
super(command, requirement, redirect, modifier, forks);
this.name = name;
this.type = type;

View file

@ -31,9 +31,9 @@ public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
private final CommandNode<S> redirect;
private final RedirectModifier<S> modifier;
private final boolean forks;
private Command<S> command;
private Command<S, ?> command;
protected CommandNode(final Command<S> command, final Predicate<S> requirement, final CommandNode<S> redirect, final RedirectModifier<S> modifier, final boolean forks) {
protected CommandNode(final Command<S, ?> command, final Predicate<S> requirement, final CommandNode<S> redirect, final RedirectModifier<S> modifier, final boolean forks) {
this.command = command;
this.requirement = requirement;
this.redirect = redirect;
@ -41,7 +41,7 @@ public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
this.forks = forks;
}
public Command<S> getCommand() {
public Command<S, ?> getCommand() {
return command;
}

View file

@ -24,7 +24,7 @@ public class LiteralCommandNode<S> extends CommandNode<S> {
private final String literal;
private final String literalLowerCase;
public LiteralCommandNode(final String literal, final Command<S> command, final Predicate<S> requirement, final CommandNode<S> redirect, final RedirectModifier<S> modifier, final boolean forks) {
public LiteralCommandNode(final String literal, final Command<S, ?> command, final Predicate<S> requirement, final CommandNode<S> redirect, final RedirectModifier<S> modifier, final boolean forks) {
super(command, requirement, redirect, modifier, forks);
this.literal = literal;
this.literalLowerCase = literal.toLowerCase(Locale.ROOT);