Skip to content

Creating an item

mineLdiver edited this page Jul 26, 2023 · 5 revisions

As discussed in registries page, there are several ways to register modded content.

But, let's go over the less manual one.

Creating a listener

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) {
        
    }
}

Instantiating an item

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.

Item properties

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");

Localization

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.