Adding a custom item
Overview
So far, we've created a 'custom' item in the sense that we've changed the amount of hunger filled up by the baguette from 5 to 6. But what if we want to, for example, be able to right click entities with the baguette to set them on fire? There's no inbuilt way to do this like there was with food. We'll have to write some code to do this for us.
The baguette flamethrower
To illustrate this, let's create a new 'baguette flamethrower' item.
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 can create a custom BaguetteFlamethrower
class. All Pylon item classes must extend PylonItem.
Create a new file BaguetteFlamethrower.java
and add the following:
BaguetteFlamethrower.java | |
---|---|
We now want to do something whenever the player right clicks on a block while holding the baguette flamethrower. In order to do this, we can implement PylonItemEntityInteractor interface. This is a builtin Pylon interface with one method: onUsedToRightClickEntity
.
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!