Fix last context for forwarding commands - Closes #179

This commit is contained in:
Aikar
2019-02-11 20:10:46 -05:00
parent 1a12993a92
commit 6481f22ea4
3 changed files with 19 additions and 3 deletions
@@ -153,7 +153,7 @@ public abstract class BaseCommand {
/**
* The last operative context data of this command. This may be null if this command hasn't been run yet.
*/
@Nullable CommandOperationContext lastCommandOperationContext;
private final ThreadLocal<CommandOperationContext> lastCommandOperationContext = new ThreadLocal<>();
/**
* If a parent exists to this command, and it has a Subcommand annotation, prefix all subcommands in this class with this
*/
@@ -174,6 +174,16 @@ public abstract class BaseCommand {
this.commandName = cmd;
}
/**
* Returns a reference to the last used CommandOperationContext.
* This method is ThreadLocal, in that it can only be used on a thread that has executed a command
*
* @return
*/
public CommandOperationContext getLastCommandOperationContext() {
return lastCommandOperationContext.get();
}
/**
* Gets the root command name that the user actually typed
*
@@ -545,7 +555,7 @@ public abstract class BaseCommand {
Stack<CommandOperationContext> contexts = CommandManager.commandOperationContext.get();
CommandOperationContext context = this.manager.createCommandOperationContext(this, issuer, commandLabel, args, isAsync);
contexts.push(context);
lastCommandOperationContext = context;
lastCommandOperationContext.set(context);
execSubcommand = null;
execLabel = commandLabel;
origArgs = args;
@@ -47,6 +47,11 @@ public class ForwardingCommand extends BaseCommand {
return Collections.singletonList(regCommand);
}
@Override
public CommandOperationContext getLastCommandOperationContext() {
return command.getLastCommandOperationContext();
}
@Override
public Set<String> getRequiredPermissions() {
return command.getRequiredPermissions();
@@ -94,7 +94,8 @@ public class SpongeRootCommand implements CommandCallable, RootCommand {
private CommandResult executeSponge(CommandIssuer sender, String commandLabel, String[] args) {
BaseCommand cmd = execute(sender, commandLabel, args);
return ((SpongeCommandOperationContext) cmd.lastCommandOperationContext).getResult();
SpongeCommandOperationContext lastContext = (SpongeCommandOperationContext) cmd.getLastCommandOperationContext();
return lastContext != null ? lastContext.getResult() : CommandResult.success();
}
public void addChild(BaseCommand command) {