Custom Item Interactions
This page covers how to breathe life into your custom items. While basic items are great for simple resources or food, you will often want your items to interact dynamically with the world—like a sword that sets enemies on fire, or a tool that triggers magic spells.
To achieve this, you must extend the base Item class and override its provided event methods.
- ActionResult
ActionResultdecides whether or not the underlying server event should be cancelled; it can be used to prevent vanilla damage, stop block breaking, and more.
ActionResult.PASS-> Allows the event to proceed un-interrupted.
ActionResult.CANCEL-> Cancels the event behind the scenes (event.setCancelled(true)).
Overridable Events
The Item class provides a wide array of overridable events so you can add precise functionality to your items.
Method | Information | Return |
|---|---|---|
| Called when the player mines a block. |
|
| Called when the player hits an entity. |
|
| Called when the player Right-Clicks a block or an entity. |
|
| Called when the player uses an item (Right-Clicks air, eats food, etc). |
|
| Called every tick while the item is inside the player's inventory. |
|
| Called when the item's position within the player's inventory changes. |
|
| Called when the player clicks the item inside an inventory GUI. |
|
| Called when the player drops the item. |
|
| Called when the player picks up the item. |
|
| Called when the player swaps the item from/into their offhand. |
|
| Called when the item is placed inside an anvil. |
|
| Called when a player crafts this item. |
|
Example: The onHit event
Let's say you want to make a specialized weapon that sets the target on fire when hit. Instead of registering it inline, you would create a custom Item class.
First, we set up the item's base stats in the constructor:
Then, we override the onHit event inside that same class and inflict fire on the target entity:
As usual, register the item in your main registry class and launch the server. As you can see, hitting an entity successfully sets it ablaze.