Events

As you'll know if you're transitioning from something like spigot or paper, events are in almost every single plugin, and events in bullet are as easy as ever. In this short page about events, we'll go over some events like the PlayerJoinEvent and BlockPlaceEvent.

If you haven't already, you'll need to setup a server and development environment, you can read Getting Started if you need too.

Basic usage

All plugin classes should override the registerEvents() function to listen for events. Here's a quick example on how one might register a PlayerJoinEvent event

override fun registerEvents() {
    EventManager.register(PlayerJoinEvent::class.java) { e ->
        println("Player ${e.player.username} joined the game!")
    }
}

Unlike Bukkit's @EventHandler annotations and registration boilerplate, BulletMC uses kotlins modern features to reduce any verbosity and increase clarity. registerEvents() gets called right after onEnable() is called.

Anatomy of an Event

Every event in BulletMC inherits from the base Event class

open class Event {
    var isCancelled: Boolean = false
}

You can listen to any event extending Event. Some events like PlayerPreJoinEvent or BlockPlaceEvent, can be cancelled to prevent any further processing of them.

Firing Events

For the internal workings of BulletMC, the server fires events using

EventManager.fire(PlayerJoinEvent(player))

This will notify all registered listeners in the order they were added.


Common events

Event
Description

PlayerJoinEvent

Fired when a player joins the game

PlayerQuitEvent

Fired when a player leaves

PlayerChatEvent

Fired when a player sends a message

BlockPlaceEvent

Fired when a player places a block

BlockBreakEvent

Fired when a player starts breaking a block (and when fully broken)

PlayerSneakEvent

Fired when a player starts/stops sneaking

PlayerMoveEvent

Fired when a player changes location (including pitch/yaw)

PlayerInteractEntityEvent

Fired when a player is interacting with an entity

... and more, you can see the full list of events in com.aznos.events


Example: Canceling chat

EventManager.register(PlayerChatEvent::class.java) { e ->
    if(e.message.contains("badword", ignoreCase = true)) {
        println("Blocked message from ${e.username.username}")
        event.isCancelled = true
    }
}

Creating custom events

Bullet offers the ability to create your own events, simply define a data class like this

data class PlayerDanceEvent(val player: Player, val danceType: String) : Event()

Then fire it with

EventManager.fire(PlayerDanceEvent(player, "twerk"))

And listen for it in your plugin just like any built-in event.

Last updated