diff --git a/core/src/main/java/co/aikar/commands/BaseCommand.java b/core/src/main/java/co/aikar/commands/BaseCommand.java index 7fc7afad..e6735d80 100644 --- a/core/src/main/java/co/aikar/commands/BaseCommand.java +++ b/core/src/main/java/co/aikar/commands/BaseCommand.java @@ -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 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 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; diff --git a/core/src/main/java/co/aikar/commands/ForwardingCommand.java b/core/src/main/java/co/aikar/commands/ForwardingCommand.java index 37273999..3045c4eb 100644 --- a/core/src/main/java/co/aikar/commands/ForwardingCommand.java +++ b/core/src/main/java/co/aikar/commands/ForwardingCommand.java @@ -47,6 +47,11 @@ public class ForwardingCommand extends BaseCommand { return Collections.singletonList(regCommand); } + @Override + public CommandOperationContext getLastCommandOperationContext() { + return command.getLastCommandOperationContext(); + } + @Override public Set getRequiredPermissions() { return command.getRequiredPermissions(); diff --git a/sponge/src/main/java/co/aikar/commands/SpongeRootCommand.java b/sponge/src/main/java/co/aikar/commands/SpongeRootCommand.java index 768c0085..4d22237c 100644 --- a/sponge/src/main/java/co/aikar/commands/SpongeRootCommand.java +++ b/sponge/src/main/java/co/aikar/commands/SpongeRootCommand.java @@ -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) {