Adding an item
Overview
Our addon so far has only one class: MyAddon
(or whatever you renamed it to). This class extends JavaPlugin and implements PylonAddon. There are some comments inside the class to explain what each part does - have a read through and try to understand how it works. Our addon doesn't actually do anything yet though - so let's add a new item!
Let's create a baguette, which fills 6 hunger bars, to start with.
To create a simple item, we only need two things: a key for the item, and an item stack.
Adding the item
Creating a key
NamespacedKeys are how Pylon identifies custom items, blocks, researches, entities, and more.
What are NamespaceKeys and why are we using them?
A key is just a simple piece of text, like pylonbase:copper_dust
, which allows Pylon to uniquely identify your item. This is very similar to how vanilla Minecraft items have IDs. Why don't we just use copper_dust
as the key? Well, what if two addons add an item called copper_dust
? We won't be able to tell which one is which! To fix this, Pylon uses NamespacedKeys, which just means we take a string and your addon's name, and put them together - for example, my_addon:copper_dust
.
To create a new NamespacedKey called 'baguette', we can do the following (inside onEnable
):
Creating the item stack
The second thing we need is an actual item. We'll use ItemStackBuilder for this.
ItemStackBuilder contains several different methods to help you create ItemStacks. For example, you can use .set(<component>, <value>)
to set some of the item's values, like enchantments, whether the item is unbreakable, and so on.
Whenever you're creating a Pylon item, make sure you use ItemStackBuilder.pylonItem(<material>, <key>)
. There are others ways to create ItemStacks, but do not use these to create Pylon items.
Why use ItemStackBuilder.pylonItem
, and not any of the other ways to create an ItemStack?
Under the hood, Pylon stores item keys in PersistentDataContainers, or PDCs. When you call ItemStackBuilder.pylonItem
and supply a key, that key is written to the item's PDC automatically. If you supply your own item stack, its PDC won't contain the item's key, and Pylon won't be able to differentiate that item with a regular Minecraft item.
ItemStackBuilder also sets the name and lore of the item to the default translation keys (which will be explained later in this tutorial).
To create a baguette, you can do as follows:
Data components
Data components are just a way to specify some information about an item - like 'this is a food and it restores 6 hunger', or 'this is a pickaxe and it breaks blocks at XYZ speed'. You can see a full list here
Registering the item
Next, we need to register our item with Pylon. This means we need to pass two things: the item stack, and the class that should be used to represent the item. We'll cover how to make your own item classes in the next chapter, but for now, you can use the default PylonItem class:
Adding the item to the guide.
Finally, we want to add our item to the Pylon guide. Let's add it to the 'food' section.
Putting it all together
Here's the complete code: