Skip to content

Item Events

Devin Fritz edited this page Nov 27, 2025 · 1 revision

Item Events

Introduction

Items can have events outside of inventories, for example clicking them. This brings an issue though, because listeners of these kind can not persist a server restart, as the memory will be lost. Therefore KIA introduces a Behavior system, which can preserve actions over a runtime, by using a registry. The system works as follows:

KBehavior Identifier -> Global Registry -> Add it to item -> Stored as NBT

Therefore when an item is clicked the following happens:

Item Clicked -> Check if NBT has any identifiers -> Lookup KBehavior in global registry -> Execute it

To build an behavior you can use one of the helper functions:

Building KBehaviors

For the following this example behavior is used:

object DiceBehavior {
    val behavior = kItemBehavior(KIdentifier(KIA.plugin, "dice")) {
        if (!it.action.isRightClick) return@kItemBehavior
        val player = it.player
        val rolled = (1..6).random()
        player.sendMessage("You rolled a $rolled!")
    }
}

Registering KBehaviors

To register a behavior the registry can be used like the following: KBehaviorRegistry.register(behavior)

KIA also offers some helper function to make this easier:

registerBehaviors {
    +DiceBehavior.behavior
}

You can have the behaviors in objects for example, or build a module structure, and register them at your plugin boot time.

Adding Behaviors to your items

addBehavior(DiceBehavior.behavior)

Clone this wiki locally