-
Notifications
You must be signed in to change notification settings - Fork 20
Command Fail
Command fail
is similar to a return
but with a Message to a Player or with a code execution lambda.
myPlugin.simpleCommand("warp") {
val player = sender as? Player ?: fail("Please, use this command just in game")
val arg0 = args.getOrNull(0) ?: fail("Please add the warp name")
val warp = warpManager.findWarp(arg0) ?: fail("This warp was not found")
warp.teleport(player)
}
The fail
extension throws an exception called CommandFailException
that is catch by the Command framework. This means that when you call fail
will block your code, but the Exception will be treated by the CommandDSL sending message to the Player if was one to send.
- Null safety:
val player: Player = sender as? Player ?: fail("Please, use this command just in game")
- Small if statements:
if(!player.hasPermission("myplugin.permission")) fail("You don't have permission")
- And much more.
This is usage on Command Arguments for not continue your code if the Player pass an invalid argument, making your code cleaner.
The CommandFailException has a boolean argument argMissing
usage by the optional
argument block for handle if the CommandFailException was throw because of an argument missing by the player. This is something that I will not use, but, is good to know how it works.
More about Command Arguments.