Class BaseCommandExecutor
- Direct Known Subclasses:
CloudLoginCommand, PluginInstallCommands, UltiToolsCommands
The default validator chain applies four validators in order: sender type (driven by the
class-level @CmdTarget, overridable per method), permission and operator requirement
(driven by @CmdExecutor), cooldown (applied when the mapped invocation completes,
whether it returns normally or throws, but not when dispatch is refused before invocation), and
usage locking (acquired and released around the call in a try-finally). Brigadier matches
and parses the command; the typed invocation then runs this executor's validator chain and
its existing synchronous or asynchronous body. Subclasses implement handleHelp(CommandSender).
Validator release at unload (case 48, @since 6.3.0). CooldownValidator and
UsageLockValidator register themselves with PlayerCacheManager at their first
dispatch. When this executor is torn down (module unload), the framework validators of the
chain it dispatches through -- and its two field validators -- are released (unregistered, and
a lock validator's locks cleared) unless another loaded executor that has dispatched still uses
them; the last such executor's unload releases them, in whatever order the executors unload. A
sharer that had not dispatched yet holds no state through them and simply registers them again
on its first dispatch.
- Since:
- 6.2.0
- Version:
- 2.0.0
- Author:
- wisdomme
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate intprivate final Objectprivate CommandWaitBudgetprivate booleanprivate booleanResolved seconds of this executor's config-bound@CmdCDannotations, keyed byCooldownValidator.bindingKey, and their load-time value sources (#531).private final CooldownValidatorprivate static final List<WeakReference<BaseCommandExecutor>> Executors that have dispatched at least once and have not been torn down (case 48): the sharers an unloading executor must not release a validator from under.private static final Objectprivate booleanWhether this executor is inDISPATCHED_EXECUTORS; written under its lock.private final UsageLockValidatorprivate final ValidatorChain -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new command executor with default validators.BaseCommandExecutor(ValidatorChain validatorChain) Creates a new command executor with a custom validator chain. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddValidator(CommandValidator validator) Adds a custom validator to the chain.private static voidarmTimeoutWatcher(CommandContext context, AsyncCommand asyncCommand, AtomicBoolean reported, AtomicReference<TaskHandle> timeoutWatcher) Arms the delayed watcher that reports a timeout message to the sender if the command body has not already completed by then.final voidWaits once for accepted asynchronous invocations and their completion hooks, using the module drain's remaining allowance or the shared server-stop allowance (ten seconds total).(package private) final voidbeginAsyncUnload(CommandWaitBudget budget) protected Object[]buildMethodParams(CommandContext context, Method method) Builds the mapped method's arguments from already typed values.private static voidcancelTimeoutWatcher(AtomicReference<TaskHandle> timeoutWatcher) private static voidcompleteAll(CommandContext context, List<CommandValidator> ranValidators, boolean commandSucceeded) Calls every validator'sonCompletein chain order.protected ValidatorChainCreates the default validator chain with standard validators.private static StringRenders a throwable for the user in a form that is never"null".private voiddispatchAsyncCommand(CommandContext context, AsyncCommand asyncCommand, Runnable runnable, AtomicBoolean reported, AtomicReference<TaskHandle> timeoutWatcher, List<CommandValidator> ranValidators) Sends the optional "processing" message, dispatchesrunnableasynchronously exactly once, and arms the timeout watcher if configured.private static voiddispatchScheduled(CommandContext context, List<CommandValidator> ranValidators, Runnable schedule) Hands a body to the scheduler; if the scheduler refuses it (the plugin is disabling, for example), the body never runs and so never reaches itsonCompletehooks, so the validators that passed are told of the refusal instead (#568) before the exception leaves.protected voidexecuteCommand(CommandContext context, Method method, Object[] params, ValidatorChain.ChainValidationResult validationResult) Executes the command method.private voidfinishAsyncCommand(AtomicBoolean finished) protected StringGets the help command string.Gets all registered command mappings.final booleanhandleGatedHelp(CommandContext context) Runs the sender-type and permission validators, then shows help if they pass.protected abstract voidhandleHelp(org.bukkit.command.CommandSender sender) Handles the help command.final StringGives the compiler this executor's help literal.private voidinvokeCommandBody(CommandContext context, Method method, Object[] params, List<CommandValidator> ranValidators, TriggerContext triggerCtx, AtomicBoolean reported, AtomicReference<TaskHandle> timeoutWatcher) Runs the matched command method plus its surrounding bookkeeping (audit-user context, post-action validator hooks, error reporting, the WIRE-12 completion flag) -- the body of theRunnableexecuteCommand(CommandContext, Method, Object[], ValidatorChain.ChainValidationResult)schedules.final booleaninvokeTyped(CommandContext context) Runs the validation and invocation stage after Brigadier has produced typed arguments.private static booleanisUsedByAnotherExecutor(CommandValidator validator, List<BaseCommandExecutor> others) Whether one ofothersstill dispatches throughvalidator(the same instance).private voidRecords this executor inDISPATCHED_EXECUTORSat its first dispatch, so an unloading executor sharing a validator with it leaves that validator registered (case 48).private static List<BaseCommandExecutor> Removesleavingand every cleared reference fromDISPATCHED_EXECUTORSand returns the executors that remain.private static voidnotifyRefused(CommandContext context, ValidatorChain.ChainValidationResult validationResult) Tells each validator that passed for this dispatch that it was refused before the mapped method ran, in chain order, so a validator that acquired something invalidate--UsageLockValidator's lock -- releases it (#568).private static voidnotifyRefusedAndRethrow(CommandContext context, ValidatorChain.ChainValidationResult validationResult, RuntimeException cause) notifyRefused(CommandContext, ValidatorChain.ChainValidationResult)for a dispatch leaving bycause: every hook still runs, and a hook that throws is attached tocauseas suppressed rather than replacing it.private TextprocessingText(String key) voidremoveValidator(CommandValidator validator) Removes a validator from the chain.private static TaskContextreplyContext(org.bukkit.command.CommandSender sender) private voidreportCommandExecutionError(CommandContext context, Method method, Exception e, TriggerContext triggerCtx) Logs the exception and best-effort reports it toErrorReportCollector, then attempts the user-facing failure message for a command method that threw.private voidScans methods for command mappings.(package private) static voidsendValidationFailure(org.bukkit.command.CommandSender sender, Text text, String legacyMessage) private voidsubmitAsyncCommand(Runnable body) Optional completion fallback for parameters with no explicit suggestion source.suggestions(org.bukkit.command.CommandSender sender, String label, String[] args) Invokes the completion hook without a Bukkit command object.private voidReleases the self-registering validators this executor dispatched through fromPlayerCacheManagertracking and bulk-clears everyUsageLockValidator's locks when this executor is torn down (module unload).private void
-
Field Details
-
mappings
-
invalidProcessingKeys
-
asyncCommandsMonitor
-
asyncCommands
private int asyncCommands -
asyncUnloading
private boolean asyncUnloading -
asyncUnloadWaitFinished
private boolean asyncUnloadWaitFinished -
asyncUnloadBudget
-
validatorChain
-
configBoundCooldownSeconds
Resolved seconds of this executor's config-bound@CmdCDannotations, keyed byCooldownValidator.bindingKey, and their load-time value sources (#531). Kept on the executor -- the exact owner of a binding -- so the state lives and dies with it and nothing shared (such as aCooldownValidatorseveral executors use) has to release it on any exit path. Replaced as whole immutable maps; written on the main thread, read on dispatch threads. Reached only throughConfigBoundCooldownState. -
configBoundCooldownSources
-
cooldownValidator
-
lockValidator
-
DISPATCHED_EXECUTORS
Executors that have dispatched at least once and have not been torn down (case 48): the sharers an unloading executor must not release a validator from under. Held weakly so the registry never keeps a module's executor -- or its class loader -- reachable; an entry joins at the executor's first dispatch and leaves at its teardown, which also prunes cleared references. Guarded byDISPATCHED_EXECUTORS_LOCK. -
DISPATCHED_EXECUTORS_LOCK
-
dispatchedExecutorMember
private volatile boolean dispatchedExecutorMemberWhether this executor is inDISPATCHED_EXECUTORS; written under its lock.
-
-
Constructor Details
-
BaseCommandExecutor
public BaseCommandExecutor()Creates a new command executor with default validators.The chain's framework validators are released at this executor's unload unless another loaded executor that has dispatched still uses them (one built over
getValidatorChain(), for example); a sharer that had not dispatched simply registers them again on its first dispatch (case 48). -
BaseCommandExecutor
Creates a new command executor with a custom validator chain.This is the other half of the construction surface
createDefaultValidatorChain()warns about: a chain supplied directly here is subject to the exact same load-time contract. If any@CmdMappingmethod on this class -- or the class itself -- declares@CmdCDwhilevalidatorChainholds noCooldownValidator, or declares@UsageLimit(any scope butNONE) while it holds noUsageLockValidator, the module is refused at load, naming this class and the offending mapping method (SILENT-11 / D-01, D-04; enforced byPluginManager.validateCommandExecutorContract). There is no opt-out: Phase 3 D-08's module-granularity isolation is the accepted escape hatch -- only this module fails to load, every other module still starts.At this executor's unload, the
CooldownValidator/UsageLockValidatorinstances invalidatorChainare released -- unregistered fromPlayerCacheManager, a lock validator's locks cleared -- unless another loaded executor that has dispatched still uses them, as when several executors or modules share one chain. A sharer that had not dispatched simply registers them again on its first dispatch (case 48).- Parameters:
validatorChain- the custom validator chain
-
-
Method Details
-
createDefaultValidatorChain
Creates the default validator chain with standard validators. Override this method to customize the validation pipeline.An override that drops
CooldownValidatororUsageLockValidatorfrom the chain while a mapping on this class -- or the class itself -- declares@CmdCDor@UsageLimit(any scope butNONE) respectively IS refused at load, naming the offending class and, when known, the offending mapping method (SILENT-11 / D-01, D-04; enforced byPluginManager.validateCommandExecutorContract, hooked at the end of container assembly, before any command reaches Bukkit). There is no opt-out: no system property, config key, or annotation attribute disables this check. Phase 3 D-08's module-granularity isolation is the accepted escape hatch instead -- only the offending module fails to load, every other module still starts.- Returns:
- the validator chain
-
unregisterValidatorsFromPlayerCache
private void unregisterValidatorsFromPlayerCache()Releases the self-registering validators this executor dispatched through fromPlayerCacheManagertracking and bulk-clears everyUsageLockValidator's locks when this executor is torn down (module unload).The validators released are those of the chain this executor dispatches through -- a chain passed to
BaseCommandExecutor(ValidatorChain)or returned by an overriddencreateDefaultValidatorChain()-- together withcooldownValidator/lockValidator, taken as one identity set (case 48). Before, only the two fields were released; a custom chain that did not contain them left its own validators, which register themselves withPlayerCacheManagerat their first dispatch, strongly held there after the module unloaded -- one more per module update, and a module's own validator subclass pinned its old class loader. OnlyCooldownValidatorandUsageLockValidator(and their subclasses) register themselves, so only they are released.A candidate that another loaded executor still uses is left untouched: still registered, its locks held, its cooldowns enforced and its quit cleanup running. "Still uses" means an executor that has dispatched at least once, has not been torn down, is still reachable and whose current chain contains the candidate by identity (
DISPATCHED_EXECUTORS). The last such executor's unload releases it. Release goes through each validator'sreleasePlayerCacheRegistration(), which also re-arms its lazy registration, so a sharer that had not dispatched yet -- or a later executor over the same chain -- registers it again on its next dispatch.Reached through the framework's EXISTING
@PreDestroycontainer lifecycle --PluginManager.unregister(plugin)already callsplugin.getContext().close(), which invokes every singleton bean's@PreDestroymethods -- rather than a second, purpose-built unload hook (GEN-08, D-03). These validators are registered withPlayerCacheManagerSEPARATELY from this executor bean itself (their sweepable state lives on them, not on this class), so the pre-existing per-beanplayerCacheManager.unregisterBean(bean)loop already inPluginManager.unregister(...)-- which only ever sees the beans a plugin's own container constructed -- never reaches them on its own; this hook is what does.UsageLockValidator.clearAllLocks()has no per-player analogue and, unlikeUsageLockValidator.clearPlayerLocks(java.util.UUID)/CooldownValidator.clearCooldowns(java.util.UUID), no quit-path call site either (GEN-08) -- module unload is the correct point to bulk-release whatever locks a released lock validator still held at teardown.- Since:
- 6.3.0
-
beginAsyncUnload
-
awaitAsyncCommands
@Internal public final void awaitAsyncCommands()Waits once for accepted asynchronous invocations and their completion hooks, using the module drain's remaining allowance or the shared server-stop allowance (ten seconds total). Only actual command waiting consumes that allowance. Never cancels or interrupts a body. Managers call this before releasing module resources or the framework scheduler; the container hook also covers executors owned by a directly constructed container.- Since:
- 7.0.0
-
waitForAsyncCommands
private void waitForAsyncCommands() -
submitAsyncCommand
-
finishAsyncCommand
-
joinDispatchedExecutors
private void joinDispatchedExecutors()Records this executor inDISPATCHED_EXECUTORSat its first dispatch, so an unloading executor sharing a validator with it leaves that validator registered (case 48). -
leaveDispatchedExecutors
Removesleavingand every cleared reference fromDISPATCHED_EXECUTORSand returns the executors that remain. The lock is released before any caller reads another executor's chain. -
isUsedByAnotherExecutor
private static boolean isUsedByAnotherExecutor(CommandValidator validator, List<BaseCommandExecutor> others) Whether one ofothersstill dispatches throughvalidator(the same instance). -
scanCommandMappings
private void scanCommandMappings()Scans methods for command mappings. -
handleHelp
protected abstract void handleHelp(org.bukkit.command.CommandSender sender) Handles the help command. Override to provide custom help.- Parameters:
sender- the command sender
-
getHelpCommand
-
invokeTyped
Runs the validation and invocation stage after Brigadier has produced typed arguments. This is an internal bridge, not a second command matching entry point.- Parameters:
context- the complete, typed invocation context- Returns:
- true after handling the invocation
-
helpCommand
Gives the compiler this executor's help literal.- Returns:
- the help literal
-
suggestions
@Nullable @Internal public final List<String> suggestions(org.bukkit.command.CommandSender sender, String label, String[] args) Invokes the completion hook without a Bukkit command object.- Parameters:
sender- the requesting senderlabel- the command labelargs- the input arguments- Returns:
- custom suggestions, or null to retain native argument suggestions
-
notifyRefused
private static void notifyRefused(CommandContext context, ValidatorChain.ChainValidationResult validationResult) Tells each validator that passed for this dispatch that it was refused before the mapped method ran, in chain order, so a validator that acquired something invalidate--UsageLockValidator's lock -- releases it (#568). Driven from the chain's own passed-validator list, likeonComplete, never from a named field. -
notifyRefusedAndRethrow
private static void notifyRefusedAndRethrow(CommandContext context, ValidatorChain.ChainValidationResult validationResult, RuntimeException cause) notifyRefused(CommandContext, ValidatorChain.ChainValidationResult)for a dispatch leaving bycause: every hook still runs, and a hook that throws is attached tocauseas suppressed rather than replacing it. -
executeCommand
protected void executeCommand(CommandContext context, Method method, Object[] params, ValidatorChain.ChainValidationResult validationResult) Executes the command method. Supports both synchronous and asynchronous execution via @AsyncCommand or @RunAsync.A synchronous body runs at once, inside the dispatch, when this is called on the server's primary thread -- which is where Bukkit dispatches every command a player, the console, a command block or the panel sends -- and is handed to
global().runonly when it is not (as of 6.3.0, #541; before, every synchronous body was deferred one tick). An@AsyncCommand/@RunAsyncbody always runs asynchronously.- Parameters:
context- the command contextmethod- the method to executeparams- the method parametersvalidationResult- the chain result produced byinvokeTyped's validation step for THIS invocation; itsValidatorChain.ChainValidationResult.getPassedValidators()is the sole source that drives each validator'sCommandValidator.onComplete(CommandContext, boolean)call -- see D-01. Never recomputed here: re-validating would run every validator twice and re-trigger their gates (e.g. a second lock acquisition attempt).
-
dispatchScheduled
private static void dispatchScheduled(CommandContext context, List<CommandValidator> ranValidators, Runnable schedule) Hands a body to the scheduler; if the scheduler refuses it (the plugin is disabling, for example), the body never runs and so never reaches itsonCompletehooks, so the validators that passed are told of the refusal instead (#568) before the exception leaves. -
invokeCommandBody
private void invokeCommandBody(CommandContext context, Method method, Object[] params, List<CommandValidator> ranValidators, TriggerContext triggerCtx, AtomicBoolean reported, AtomicReference<TaskHandle> timeoutWatcher) Runs the matched command method plus its surrounding bookkeeping (audit-user context, post-action validator hooks, error reporting, the WIRE-12 completion flag) -- the body of theRunnableexecuteCommand(CommandContext, Method, Object[], ValidatorChain.ChainValidationResult)schedules.Split out of
executeCommandpurely to bring the enclosing method's NPathComplexity back under threshold; the control flow below, and therefore the runtime behaviour, is unchanged from before the extraction (refactor only, per D-01/D-02/T-02-REP design notes that used to live on the anonymousrun()this replaces).- Since:
- 6.3.0
-
completeAll
private static void completeAll(CommandContext context, List<CommandValidator> ranValidators, boolean commandSucceeded) Calls every validator'sonCompletein chain order. Each runs even when an earlier one throws, so one validator's failing hook cannot keep a laterUsageLockValidatorfrom releasing its lock (#568); the first exception is rethrown afterwards, later ones suppressed. -
handleGatedHelp
Runs the sender-type and permission validators, then shows help if they pass.Before 6.3.0 the help short-circuit sat at the very top of the old Bukkit executor, ahead of the
CommandContextbeing built, so it bypassed the entire validator chain (#381). Two consequences were measured on a real server: a console received the full help of a@CmdTarget(PLAYER)command -- including, in one module, an admin section gated only onhasPermission, which a console always satisfies -- and modules had begun working around the absent guarantee in incompatible ways. One testedsender instanceof Playerand silently printed nothing for a console; another printed everything. Neither is wrong against a contract that promised nothing.Cooldown and usage-limit are deliberately not applied here. Help is not the guarded action: withholding an explanation because the command itself is on cooldown would be perverse, and
UsageLockValidatoracquires a lock that only the invocation path releases. This is a choice, stated so it cannot later be mistaken for the same oversight in a different place.Applicability is consulted, same as the normal dispatch path (#413). Before invoking a sender-type or permission validator here,
ValidatorChain.isApplicable(CommandValidator, CommandContext)is called -- the exact same static methodValidatorChain.validate(CommandContext)/#validateAllcall internally, not a second, independent copy of the one-line condition it wraps (WR-02). A validator that reports it does not apply to this context is skipped, not invoked and happened to pass. This does not weaken enforcement: a validator that DOES apply and denies still denies help, exactly as before.- Parameters:
context- the command context, already built- Returns:
truealways, indicating that the help invocation was handled- Since:
- 6.3.0
-
reportCommandExecutionError
private void reportCommandExecutionError(CommandContext context, Method method, Exception e, TriggerContext triggerCtx) Logs the exception and best-effort reports it toErrorReportCollector, then attempts the user-facing failure message for a command method that threw. Split out ofinvokeCommandBody(CommandContext, Method, Object[], List, TriggerContext, AtomicBoolean, AtomicReference)for the same NPathComplexity reason as that extraction.- Since:
- 6.3.0
-
sendValidationFailure
-
processingText
-
describe
Renders a throwable for the user in a form that is never"null".Every command body is invoked reflectively, so anything it throws arrives wrapped in an
InvocationTargetException, whose owngetMessage()isnullby construction -- the detail belongs to the cause. Reporting the wrapper's message therefore appended a literal"null"to the error prefix for every command failure, sending the user to look for a null-pointer bug that was not there and hiding the real one in the server log (#385).The cause's own message can also be null --
NullPointerExceptionthrown by a bare dereference is the common case -- so the class's simple name is used as the fallback. It is a poor message, but"NullPointerException"tells the reader more than"null"does, and the full stack trace is one line below in the log either way.- Parameters:
cause- the unwrapped throwable- Returns:
- its message, or its simple class name when the message is null or blank
- Since:
- 6.3.0
-
dispatchAsyncCommand
private void dispatchAsyncCommand(CommandContext context, AsyncCommand asyncCommand, Runnable runnable, AtomicBoolean reported, AtomicReference<TaskHandle> timeoutWatcher, List<CommandValidator> ranValidators) Sends the optional "processing" message, dispatchesrunnableasynchronously exactly once, and arms the timeout watcher if configured. Split out ofexecuteCommand(CommandContext, Method, Object[], ValidatorChain.ChainValidationResult)for the same NPathComplexity reason asinvokeCommandBody(CommandContext, Method, Object[], List, TriggerContext, AtomicBoolean, AtomicReference)'s extraction.- Since:
- 6.3.0
-
armTimeoutWatcher
private static void armTimeoutWatcher(CommandContext context, AsyncCommand asyncCommand, AtomicBoolean reported, AtomicReference<TaskHandle> timeoutWatcher) Arms the delayed watcher that reports a timeout message to the sender if the command body has not already completed by then. Split out ofdispatchAsyncCommand(CommandContext, AsyncCommand, Runnable, AtomicBoolean, AtomicReference, List)for the same NPathComplexity reason as its sibling extractions.- Since:
- 6.3.0
-
replyContext
-
cancelTimeoutWatcher
-
buildMethodParams
Builds the mapped method's arguments from already typed values.- Parameters:
context- the invocation contextmethod- the mapped method- Returns:
- the arguments, or null when an override refuses the invocation
-
suggest
@Nullable protected List<String> suggest(org.bukkit.command.CommandSender sender, String label, String[] args) Optional completion fallback for parameters with no explicit suggestion source. Literal suggestions are always supplied by Brigadier.- Parameters:
sender- the requesting senderlabel- the command labelargs- the current input including its partial token- Returns:
- custom suggestions, or null for native argument suggestions
-
getMappings
-
addValidator
Adds a custom validator to the chain.- Parameters:
validator- the validator to add
-
removeValidator
Removes a validator from the chain.- Parameters:
validator- the validator to remove
-