VitalCommand

abstract class VitalCommand<CS : Any> : VitalHasInfo

Use this class to define your own custom command. A command defines n-amount of entry points for user-input, that can perform actions and produce a result.

Each command may have n-amount of arguments, each argument may also have n-amount of argument-exception-handlers, mapped to them to handle exceptions during argument execution.

You can also define global exception handlers, which can handle exceptions globally, not tied to any specific argument.

Each argument is defined as a function using the VitalCommand.Arg annotation. An argument may be a list of strings separated by spaces, containing the hard-coded static arguments, and variable ones with either %NAME% or syntax.

Each argument handler supports automatic parameter injection for the following parameters:

  • CommandSender or Player

  • String for the actual executed argument

  • VitalCommand.Arg for the matched command argument annotation, you have defined within your class.

  • Array of String for arguments, that have defined variables using either %NAME% or .

Any command related operation is executed in the following order:

  1. Look for and execute (if found) argument function based on the user-input.

  2. If not exception occurs during this command execution, return.

  3. If an exception occurs during execution within the argument, look for and execute (if found) an argument exception handler matching the executed argument and exception type.

  4. If no argument exception handler was found during argument execution, look for and execute (if found) a global exception handler for the thrown exception type.

  5. If no global exception handler was found for the exception type, fall back to onCommandError.

@VitalCommand.Info(
name = "mycommand",
description = "description"
)
public class MyCommand extends VitalCommand.Spigot {
// This will define a valid argument a player or command sender can execute
@Arg(name = "static %dynamic% <dynamic> arg")
public ReturnState onSomeArgument(CommandSender sender, String executedArg, Arg commandArg, String[] values) {
// If the command sender executes "/mycommand static 123 456 arg", this function will be called.
// the "values" parameter will have the following values: ["123", "456"]
return ReturnState.SUCCESS;
}
}

Inheritors

Types

Link copied to clipboard
@Component
@Target(allowedTargets = [AnnotationTarget.CLASS])
annotation class Advice(val commandSenderClass: KClass<out Any>)

Marks the annotated class to be used as a global command exception handler. This class may define multiple VitalCommand.GlobalExceptionHandler annotated functions to handle exceptions during command execution.

Link copied to clipboard
@Target(allowedTargets = [AnnotationTarget.CLASS])
annotation class Arg(val name: String = "", val permission: String = "", val playerOnly: Boolean = false)

Defines the argument of a VitalCommand.ArgHandler annotated function.

Link copied to clipboard
@Target(allowedTargets = [AnnotationTarget.FUNCTION])
annotation class ArgExceptionHandler(val arg: String, val type: KClass<out Throwable>)

Marks the annotated function as a valid entry-point for when exception occur during the execution of a VitalCommand.ArgHandler annotated function.

Link copied to clipboard
data class ArgExceptionHandlerContext(val handlerMethod: Method, val commandSenderIndex: Int?, val executedArgIndex: Int?, val commandArgIndex: Int?, val exceptionIndex: Int?)

Defines the context required to execute a VitalCommand.ArgExceptionHandler annotated function.

Link copied to clipboard
@Target(allowedTargets = [AnnotationTarget.FUNCTION])
annotation class ArgHandler(val arg: VitalCommand.Arg = Arg())

Marks the annotated function as a valid argument entry-point within a VitalCommand.

Link copied to clipboard
data class ArgHandlerContext(val handlerMethod: Method, val commandSenderIndex: Int?, val executedArgIndex: Int?, val commandArgIndex: Int?, val valuesIndex: Int?)

Defines the context required to execute a VitalCommand.ArgHandler annotated function.

Link copied to clipboard
Link copied to clipboard
object Companion
Link copied to clipboard
@Target(allowedTargets = [AnnotationTarget.FUNCTION])
annotation class GlobalExceptionHandler(val type: KClass<out Throwable>)

Marks the annotated function as a global exception handler. This function will be called when an exception is thrown within the execution of an VitalCommand.ArgHandler annotated function, and is not caught by an underlying VitalCommand.ArgExceptionHandler annotated function.

Link copied to clipboard
data class GlobalExceptionHandlerContext(val adviceInstance: Any, val handlerMethod: Method, val commandSenderIndex: Int?, val executedArgIndex: Int?, val commandArgIndex: Int?, val valuesIndex: Int?, val exceptionIndex: Int?)

Defines the context required to execute a VitalCommand.GlobalExceptionHandler annotated function.

Link copied to clipboard
@Component
@Target(allowedTargets = [AnnotationTarget.CLASS])
annotation class Info(val name: String, val description: String = "A Vital Command", val aliases: Array<String> = [], val usage: String = "", val permission: String = "", val playerOnly: Boolean = true)

Defines the metadata info for a VitalCommand.

Link copied to clipboard

Defines the possible VitalCommand return states of a VitalCommand.ArgHandler annotated function.

Link copied to clipboard
Link copied to clipboard
data class TabCompletionContext(val completions: MutableList<String>, val playerNames: List<String>)

Defines the context required to execute tab completion on a VitalCommand.

Properties

Link copied to clipboard

The arguments of the command, mapped from Pattern to VitalCommand.Arg.

Link copied to clipboard
Link copied to clipboard

Functions

Link copied to clipboard
fun execute(sender: CS, args: Array<String>)

Internal function used to execute this command as the given sender and args. Any mapped arg handlers that matches the given command execution will be called using executeArgExceptionHandlerMethod.

Link copied to clipboard
abstract fun getAllPlayerNames(): List<String>

Internal function used to get the names of all currently connected players.

Link copied to clipboard
open fun <T : Annotation> getInfo(type: Class<T>): T
Link copied to clipboard

Internal function used to get all arg exception handler functions annotated with ArgExceptionHandler and map them to their appropriate Arg and specified Annotation. This function returns a Map of all mapped arg exception handlers and will later be used during command execution to correctly call arg exception handlers when an exception occurs during command execution.

Link copied to clipboard

Internal function used to get the internal arg handler context for all functions annotated with ArgHandler. This arg handler context will later be used during command execution to call the annotated arg handler function with the correct parameters.

Link copied to clipboard

Internal function used to Get all functions annotated with ArgHandler and map them to their given Arg.name as a Pattern. These mapped args are later used during command execution to find out which arg was actually executed by a command sender.

Link copied to clipboard
abstract fun hasPermission(commandSender: CS, permission: String): Boolean

Internal function used to check if the given commandSender has the given permission.

Link copied to clipboard
abstract fun isPlayer(commandSender: CS): Boolean

Internal function used to check if the given commandSender is a player.

Link copied to clipboard
fun tabComplete(sender: CS, args: Array<String>): List<String>

Internal function used to provide automatic tab-completions for the given command execution. Additionally, implementers can add their own tab-completions by overriding the onCommandTabComplete lifecycle function.