-
Notifications
You must be signed in to change notification settings - Fork 21
Creating an item
As discussed in registries page, there are several ways to register modded content.
But, let's go over the less manual one.
To register an item during ItemRegistryEvent
, we need to create an event listener and add it to fabric.mod.json
fabric.mod.json
{
"entrypoints": {
"stationapi:event_bus": [
"me.mymod.item.MyItems"
]
}
}
MyItems.java
public class MyItems {
@EventListener
void registerItems(ItemRegistryEvent event) {
}
}
Now, to actually instantiate an item, we'll use one of the Template*
classes StationAPI provides to avoid some manual registry code.
public class MyItems {
@Entrypoint.ModID
final ModID modId = Null.get();
public static ItemBase myItem;
@EventListener
void registerItems(ItemRegistryEvent event) {
myItem = new TemplateItemBase(modId.id("my_item"));
}
}
Unlike vanilla Minecraft and some other modding APIs, we use string-based identifiers instead of integer IDs.
And, as you can see, we obtain an identifier using modId.id("my_item")
method. This is equivalent to Identifier.of("my_modid:my_item")
, which is a more manual way of doing it.
We can give the item some custom properties, such as max stack size or a translation key.
For example:
myItem = new TemplateItemBase(modId.id("my_item")).setMaxStackSize(32).setTranslationKey(modId, "myItem");
To give the translation key a meaningful, localized name, we need to create a lang file under src/main/resources/assets/my_modid/stationapi/lang/
folder. By default, and without any extra localization mods, only en_US.lang
and stats_US.lang
are used.
After the file is created, we need to add the translation key, like this:
item.myItem.name=My Item
Notice that we didn't add my_modid
anywhere, though we did use it in .setTranslationKey()
method. That's because StationAPI itself prepends modid in automatically discovered lang files.