Adding a custom item
Overview
This section describes how to attach custom behaviour to an item. We'll demonstrate this by creating a baguette flamethrower which sets entities on fire when they are right clicked.
The baguette flamethrower
Creating a 'normal item'
Let's start by making a 'normal' item, like we did in the previous chapter.
| en.yml | |
|---|---|
Creating a custom item class
Next, we'll add the code to set entities on fire.
In order to do this, we must create a custom BaguetteFlamethrower class. All Pylon item classes must extend PylonItem.
Create a new file BaguetteFlamethrower.java and add the following:
Create a new file BaguetteFlamethrower.kt and add the following:
| BaguetteFlamethrower.kt | |
|---|---|
We now want to run some code when the player right clicks on an entity while holding the baguette flamethrower. In order to do this, we need to implement a Pylon item interface.
Pylon item interfaces are just interfaces that add different behaviours to items, like running some code when the item is used.
Here, we need to implement the PylonItemEntityInteractor interface. This interface has one method: onUsedToRightClickEntity.
!!! note You can find a full list of item interfaces here.
| BaguetteFlamethrower | |
|---|---|
Using the custom item class
In order to use this class, we now need to specify that the baguette flamethrower should use it instead of the default PylonItem class.
Change your PylonItem.register(...) line to the following:
And that's it! Now, try running the server. When you right click an entity with the baguette flamethrower, you should set it on fire for 40 ticks!