-
Notifications
You must be signed in to change notification settings - Fork 20
Command Arguments
Gabriel Souza edited this page Jan 4, 2020
·
18 revisions
Arguments is an extension function for Executor that parsers the arguments passed by the Player/Console in the command.
It's important you read Command Fail first because the Arguments Extensions works with it.
val PLAYER_MISSING_PARAMETER = "Missing player parameter!".color(ChatColor.RED)
// return null if player is not online
fun Executor<*>.playerOrNull(
index: Int,
argMissing: BaseComponent = PLAYER_MISSING_PARAMETER
): Player? = string(index, argMissing).let { Bukkit.getPlayerExact(it) }
-
index
is argument position that the player should inform (start in zero, likeargs
array) -
argMissing
is the message to send the Player/Console if the argument was not informed.
myPlugin.simpleCommand("isOnline") {
val target: Player? = playerOrNull(index = 0)
if(target != null) {
sender.msg("This player is online")
} else {
sender.msg("This player is offline")
}
}
All arguments are required to player add in the command, if the Player/Sender does not add the argument, the command will fail sending the argMissing
message to the Player/Sender.
Because of that, we have the optional
extension for handle when we want an optional argument.
inline fun <T> Executor<*>.optional(block: () -> T): T?
Usage
This command will send the message Welcome to my server!!
to the Sender or to a Player if he adds
myPlugin.simpleCommand("welcome") {
val target: Player? = optional {
player(index = 0, notOnline = "This player is not online".color(ChatColor.RED))
}
val message = "Welcome to my server!!"
if(target != null) {
target.msg(message)
} else {
sender.msg(message)
}
}
val PLAYER_MISSING_PARAMETER = "Missing player parameter!".color(ChatColor.RED)
val PLAYER_NOT_ONLINE = "The player specified is not online.".color(ChatColor.RED)
fun Executor<*>.player(
index: Int,
argMissing: BaseComponent = PLAYER_MISSING_PARAMETER,
notOnline: BaseComponent = PLAYER_NOT_ONLINE
): Player = playerOrNull(index, argMissing) ?: fail(notOnline)
All arguments can be found on command/arguments folder.