Skip to content

CommandBase

Wittawat Manha edited this page Sep 8, 2020 · 1 revision

CommandBase

interface CommandBase : CommandExecutor, TabCompleter
interface PlayerCommandBase : CommandBase

this class used to create command with automatic on-tab-complete

Features

  • can implement CommandExecutor and TabCompleter in single class
  • PlayerCommandBase will automatic filter command sender
  • auto register to KotlinPlugin

implementation

class MyCommand(
    override val plugin: KotlinPlugin
) : CommandBase {
    override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<String>): Boolean {
        sender.sendMessage("this my command is running")
        return true
    }

    // if tab complete is need player name this is optional
    override fun onTabComplete(sender: CommandSender, command: Command, alias: String, args: Array<String>): MutableList<String>? {
        return when(args.size){
            // if args is empty or having 1 argument
            0, 1 -> mutableListOf("abc","def")
            // if args have size >= 2
            else -> null
        }
    }
}

// below command won't work when used in console
class MyPlayerCommand(
    override val plugin: KotlinPlugin
) : PlayerCommandBase {
    override fun onCommand(player: Player, command: Command, label: String, args: Array<String>): Boolean {
        // console won't able to call this
        sender.sendMessage("your uuid is ${player.uniqueId}")
        return true
    }

    // if tab complete is need player name this is optional
    override fun onTabComplete(player: Player, command: Command, alias: String, args: Array<String>): MutableList<String>? {
        return when(args.size){
            // if args is empty or having 1 argument
            0, 1 -> mutableListOf(player.name)
            // if args have size >= 2
            else -> null
        }
    }
}

// to register a plugin
overide fun onEnable() {
    // required `KotlinPlugin`
    MyCommand(this).register()

    MyPlayerCommand(this).register()
}

Clone this wiki locally