Class BaseCommandExecutor
java.lang.Object
com.ultikits.ultitools.abstracts.command.BaseCommandExecutor
- All Implemented Interfaces:
org.bukkit.command.CommandExecutor,org.bukkit.command.TabCompleter,org.bukkit.command.TabExecutor
Base command executor with improved architecture using Chain of Responsibility pattern.
This class provides a cleaner, more extensible command handling system.
使用责任链模式改进架构的基础命令执行器。 此类提供更清晰、更可扩展的命令处理系统。
- Since:
- 6.2.0
- Version:
- 2.0.0
- Author:
- wisdomme
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final CooldownValidatorprivate final UsageLockValidatorprivate static final Objectprivate final TypeParserRegistryprivate 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.protected Object[]buildMethodParams(CommandContext context, Method method) Builds the method parameters from the command context.private intcalculateMatchScore(String[] formatArgs, String[] actualArgs) Calculates a match score for format vs actual args.protected ValidatorChainCreates the default validator chain with standard validators.protected voidexecuteCommand(CommandContext context, Method method, Object[] params) Executes the command method.private StringExtract module name from the concrete command executor class.private StringextractParameterName(String formatArg) protected StringGets the help command string.Gets all registered command mappings.protected abstract voidhandleHelp(org.bukkit.command.CommandSender sender) Handles the help command.private booleanisParameter(String arg) private static booleanisSenderType(Class<?> paramType) protected MethodmatchMethod(String[] args) Matches arguments to a registered method.booleanonCommand(org.bukkit.command.CommandSender sender, org.bukkit.command.Command command, String alias, String[] args) onTabComplete(org.bukkit.command.CommandSender sender, org.bukkit.command.Command command, String alias, String[] args) parseParameters(String[] args, String format) Parses command arguments into named parameters.protected ObjectparseParameterValue(String[] values, Class<?> type) Parses a parameter value to the target type.voidremoveValidator(CommandValidator validator) Removes a validator from the chain.private ObjectresolveCmdParam(CommandContext context, Parameter parameter, Class<?> paramType) private ObjectresolveParameter(CommandContext context, Parameter parameter) private ObjectresolveSender(CommandContext context, Class<?> paramType) private voidScans methods for command mappings.Generates tab completion suggestions.protected booleanvalidateParameterCount(String[] args, String format, org.bukkit.command.CommandSender sender, org.bukkit.command.Command command) Validates the parameter count.
-
Field Details
-
mappings
-
validatorChain
-
cooldownValidator
-
lockValidator
-
parserRegistry
-
PARSE_FAILED_SENTINEL
-
-
Constructor Details
-
BaseCommandExecutor
public BaseCommandExecutor()Creates a new command executor with default validators. 使用默认验证器创建新的命令执行器。 -
BaseCommandExecutor
Creates a new command executor with a custom validator chain. 使用自定义验证器链创建新的命令执行器。- 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. 创建具有标准验证器的默认验证器链。 重写此方法以自定义验证管道。- Returns:
- the validator chain
-
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
Gets the help command string. 获取帮助命令字符串。- Returns:
- the help command
-
onCommand
public boolean onCommand(org.bukkit.command.CommandSender sender, org.bukkit.command.Command command, String alias, String[] args) - Specified by:
onCommandin interfaceorg.bukkit.command.CommandExecutor
-
executeCommand
Executes the command method. Supports both synchronous and asynchronous execution via @AsyncCommand or @RunAsync. 执行命令方法。 通过 @AsyncCommand 或 @RunAsync 支持同步和异步执行。- Parameters:
context- the command contextmethod- the method to executeparams- the method parameters
-
matchMethod
Matches arguments to a registered method. 将参数匹配到已注册的方法。- Parameters:
args- the command arguments- Returns:
- the matched method, or null if not found
-
calculateMatchScore
Calculates a match score for format vs actual args. 计算格式与实际参数的匹配分数。- Parameters:
formatArgs- the format argumentsactualArgs- the actual arguments- Returns:
- the match score (-1 if no match)
-
isParameter
-
parseParameters
Parses command arguments into named parameters. 将命令参数解析为命名参数。- Parameters:
args- the command argumentsformat- the command format- Returns:
- map of parameter names to values
-
extractParameterName
-
validateParameterCount
protected boolean validateParameterCount(String[] args, String format, org.bukkit.command.CommandSender sender, org.bukkit.command.Command command) Validates the parameter count. 验证参数数量。 -
buildMethodParams
Builds the method parameters from the command context. 从命令上下文构建方法参数。- Parameters:
context- the command contextmethod- the method to invoke- Returns:
- the method parameters, or null if parsing fails
-
resolveParameter
-
isSenderType
-
resolveSender
-
resolveCmdParam
-
parseParameterValue
Parses a parameter value to the target type. 将参数值解析为目标类型。- Parameters:
values- the string valuestype- the target type- Returns:
- the parsed value
- Throws:
TypeParseException
-
onTabComplete
@Nullable public List<String> onTabComplete(org.bukkit.command.CommandSender sender, org.bukkit.command.Command command, String alias, String[] args) - Specified by:
onTabCompletein interfaceorg.bukkit.command.TabCompleter
-
suggest
protected List<String> suggest(org.bukkit.entity.Player player, org.bukkit.command.Command command, String[] args) Generates tab completion suggestions. Override this method to provide custom suggestions. 生成 Tab 补全建议。 重写此方法以提供自定义建议。- Parameters:
player- the player requesting completioncommand- the commandargs- the current arguments- Returns:
- list of suggestions
-
getMappings
Gets all registered command mappings. 获取所有已注册的命令映射。- Returns:
- unmodifiable map of format to method
-
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
-
extractModuleName
Extract module name from the concrete command executor class. Uses the simple class name as a reasonable approximation.
-