Add Sponge CommandResult API/Context and move/add operation context

This commit is contained in:
Aikar
2017-07-03 02:35:34 -05:00
parent 6243202fe9
commit f900a739df
9 changed files with 162 additions and 40 deletions
@@ -23,10 +23,14 @@
package co.aikar.commands;
import co.aikar.commands.contexts.CommandResultSupplier;
@SuppressWarnings("WeakerAccess")
public class SpongeCommandContexts extends CommandContexts<SpongeCommandExecutionContext> {
public SpongeCommandContexts(final SpongeCommandManager manager) {
super(manager);
registerIssuerOnlyContext(CommandResultSupplier.class, c -> new CommandResultSupplier());
}
}
@@ -153,4 +153,15 @@ public class SpongeCommandManager extends CommandManager {
}
}
}
@Override
public CommandOperationContext createCommandOperationContext(BaseCommand command, CommandIssuer issuer, String commandLabel, String[] args) {
return new SpongeCommandOperationContext(
this,
issuer,
command,
commandLabel,
args
);
}
}
@@ -0,0 +1,18 @@
package co.aikar.commands;
import org.spongepowered.api.command.CommandResult;
public class SpongeCommandOperationContext extends CommandOperationContext {
private CommandResult result = CommandResult.success();
SpongeCommandOperationContext(CommandManager manager, CommandIssuer issuer, BaseCommand command, String commandLabel, String[] args) {
super(manager, issuer, command, commandLabel, args);
}
public CommandResult getResult() {
return result;
}
public void setResult(CommandResult result) {
this.result = result;
}
}
@@ -24,6 +24,7 @@
package co.aikar.commands;
import co.aikar.commands.apachecommonslang.ApacheCommonsLangUtil;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.api.command.CommandCallable;
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
@@ -62,37 +63,34 @@ public class SpongeRootCommand implements CommandCallable, RootCommand {
}
@Override
public CommandResult process(CommandSource source, String arguments) throws CommandException {
public CommandResult process(@NotNull CommandSource source, @NotNull String arguments) throws CommandException {
String[] args = arguments.isEmpty() ? new String[0] : arguments.split(" ");
if(this.execute(new SpongeCommandIssuer(manager, source), this.name, args)) {
return CommandResult.success();
}
return CommandResult.empty();
return this.execute(new SpongeCommandIssuer(manager, source), this.name, args);
}
@Override
public List<String> getSuggestions(CommandSource source, String arguments, @Nullable Location<World> location) throws CommandException {
public List<String> getSuggestions(@NotNull CommandSource source, @NotNull String arguments, @Nullable Location<World> location) throws CommandException {
String[] args = arguments.isEmpty() ? new String[0] : arguments.split(" ");
return tabComplete(new SpongeCommandIssuer(manager, source), this.name, args);
}
@Override
public boolean testPermission(CommandSource source) {
public boolean testPermission(@NotNull CommandSource source) {
return this.defCommand.hasPermission(source);
}
@Override
public Optional<Text> getShortDescription(CommandSource source) {
public Optional<Text> getShortDescription(@NotNull CommandSource source) {
return Optional.empty();
}
@Override
public Optional<Text> getHelp(CommandSource source) {
public Optional<Text> getHelp(@NotNull CommandSource source) {
return Optional.empty();
}
@Override
public Text getUsage(CommandSource source) {
public Text getUsage(@NotNull CommandSource source) {
return Text.of();
}
@@ -102,18 +100,19 @@ public class SpongeRootCommand implements CommandCallable, RootCommand {
return new ArrayList<>(completions);
}
private boolean execute(CommandIssuer sender, String commandLabel, String[] args) {
private CommandResult execute(CommandIssuer sender, String commandLabel, String[] args) {
BaseCommand cmd = this.defCommand;
for (int i = args.length; i >= 0; i--) {
String checkSub = ApacheCommonsLangUtil.join(args, " ", 0, i).toLowerCase();
BaseCommand subHandler = this.subCommands.get(checkSub);
if (subHandler != null) {
subHandler.execute(sender, commandLabel, args);
return false;
cmd = subHandler;
break;
}
}
this.defCommand.execute(sender, commandLabel, args);
return false;
cmd.execute(sender, commandLabel, args);
return ((SpongeCommandOperationContext) cmd.lastCommandOperationContext).getResult();
}
public void addChild(BaseCommand command) {
@@ -0,0 +1,19 @@
package co.aikar.commands.contexts;
import co.aikar.commands.CommandManager;
import co.aikar.commands.SpongeCommandOperationContext;
import org.spongepowered.api.command.CommandResult;
import java.util.function.Consumer;
public class CommandResultSupplier implements Consumer<CommandResult> {
public CommandResultSupplier() {
}
@Override
public void accept(CommandResult commandResult) {
SpongeCommandOperationContext context = (SpongeCommandOperationContext) CommandManager.getCurrentCommandOperationContext();
context.setResult(commandResult);
}
}