Commands & listeners
SporeAPI can register your commands and listeners for you automatically, so you don't have to do it by hand in onEnable.
For this to work, your main class needs to extend SporePlugin (see Getting started).
Commands
Put @RegisterCommand on a command class and SporeAPI finds and registers it. It looks at the class and sends it to the right framework, so you don't pick:
- a class with Cloud annotations goes to Cloud (built into
sporeapi-paper) - a class extending
SporeCommandgoes to ACF (needssporeapi-acf, see ACF commands)
The same routing applies to SporeModule.getCommands() and registerCommand(command) in your main class.
Cloud commands
@RegisterCommand
class HealCommand {
@Command("heal [target]")
@Permission("myplugin.heal")
fun heal(sender: Player, @Argument("target") target: Player?) {
(target ?: sender).health = 20.0
}
}
Player is injected for you - a console sender gets "Only players can run this command." instead of a crash.
If you need to configure the manager (custom parsers, Brigadier settings, suggestions), override setupCloud in your main class, and use the cloudCommandManager property when something wants the manager itself.
override fun setupCloud(manager: SporeCloudCommandManager) {
CommandSuggestions.register(manager.manager)
}
ACF commands
ACF (Aikar's Command Framework) lives in its own module for older projects. Use eu.sporedev:sporeapi-acf instead of sporeapi-paper (it includes paper), and extend SporeAcfPlugin instead of SporePlugin:
class MyPlugin : SporeAcfPlugin() {
override fun setupACF(manager: PaperCommandManager) {
super.setupACF(manager) // keeps the SporeAPI styled help/error messages
// your contexts and completions
}
}
@RegisterCommand
class TestCommand : SporeCommand() {
@CommandAlias("test")
fun onTest(sender: CommandSender) {
sender.sendMessage("It works!")
}
}
Cloud commands keep working next to ACF ones in the same plugin. The ACF manager is available as commandManager.
Listeners
Same idea, but for a normal Bukkit Listener. Just add @RegisterListener and it gets registered automatically.
@RegisterListener
class JoinListener : Listener {
@EventHandler
fun onJoin(event: PlayerJoinEvent) {
event.player.sendMessage("Welcome!".green())
}
}
This also works with Kotlin objects, so you can use a singleton instead of a normal class if you prefer.