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!

Adding Item textures

You're probably annoyed with the black & purple missing texture, so we'll fix that now.

Registering a texture for an item requires an item model .json file and a texture image. You're going to need to add these to your resource directory; the direct path of each is:

  Item model: //resources/assets/my-mod/models/item/fabric_item.json//
  Item texture: //resources/assets/my-mod/textures/item/fabric_item.png//
  

Note that my-mod is your modid. If you registered your item properly in the first step, your game will also complain about a missing texture file in a fashion similar to this:

  [Server-Worker-1/WARN]: Unable to load model: 'my-mod:fabric_item#inventory' referenced from: my-mod:fabric_item#inventory: java.io.FileNotFoundException: my-mod:models/item/fabric_item.json
  

It conveniently tells you exactly where to put your item model file, so this is good for when you can't figure it out yourself.

As for the actual contents of your item json file, a basic template is as follows:

{
  "parent": "item/generated",
  "textures": {
    "layer0": "my-mod:item/fabric_item"
  }
}

The parent of your item changes how it's rendered in the hand and comes in useful for things like block items in the inventory. textures/layer0 is the location of your image file.

If you set everything up correctly, you should be met with a textured item:

tutorial/items.1550444876.txt.gz · Last modified: 2019/02/17 23:07 by draylar