User Tools

Site Tools


tutorial:items

This is an old revision of the document!


Adding an Item

Introduction

One of the first things you'll want to do with Fabric is registering a new item.

You're going to need to create an Item object, register it, and give it a texture.

Creating an Item

To create your first item, create a new class that overrides Item. We'll name our item class 'FabricItem'. Create a standard constructor; your class should now look like this:

public class FabricItem extends Item
{
  public FabricItem()
  {
      super();
  }
}

Your super() call will require an Item.Settings object. This is used to set properties such as the creative inventory category, durability, and stack count. We're just going to tell our item to go in the Misc section:

super(new Item.Settings().itemGroup(ItemGroup.MISC));

Registering an Item

Just like most additions you make in Fabric, you'll need to register your new item to the Registry. You can call the registry with Registry.register. The first argument is the registry type, such as Registry.ITEM or Registry.BLOCK. The second argument is the name of your addition– remember to append it with your namespace! In most cases, the last argument is an instance of what you are registering.

First, we'll create an instance of our item at the top of our main class. It's useful to keep an instance so you can reference and access it later from different classes:

public class ExampleMod implements ModInitializer
{
    // an instance of our new item
    public static FabricItem fabricItem = new FabricItem();
    [...]
}

Second, register your item in your onInitialize method. You can also register your item in any other class, as long as it's called during initialization.

public class ExampleMod implements ModInitializer
{
    // an instance of our new item
    public static FabricItem fabricItem = new FabricItem();
    @Override
    public void onInitialize()
    {
	Registry.register(Registry.ITEM, "my-mod:fabric_item", fabricItem);
    }
}

At this point, if you start your Minecraft game, you will see your new item. Pretty easy!

tutorial/items.1550443970.txt.gz · Last modified: 2019/02/17 22:52 by draylar