Adding Custom Items

At this point, I assume you are able to build your mod and load it into minecraft. If not, go back and read up on Setting up the Environment.

In this chapter, we will add our own custom copper item into minecraft:

copper item

We will work off of the example mod source code that we set up from before, but the instructions should be generic enough for you to follow along with your own projects as well.

Playing around with ForgeRegistries

Minecraft keeps track of all things from items to blocks, from biomes to dimensions, using registries. We will, however, not touch these in-game registries directly. Instead, forge offers a convenient layer of abstraction called ForgeRegistries which is what we will be interacting with.

We will create a RegistryHandler class to register our item.

new class

package com.example.examplemod;

import ...;

public class RegistryHandler {
    // create DeferredRegister object
    public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, ExampleMod.MODID);

    public static void init() {
        // attach DeferredRegister to the event bus
        ITEMS.register(FMLJavaModLoadingContext.get().getModEventBus());
    }

    // register item
    public static final RegistryObject<Item> COPPER = ITEMS.register("copper", () ->
            new Item(
                    new Item.Properties().group(ItemGroup.MATERIALS)
            )
    );
}

Let me break down the code a bit and explain what's going on.

First, we created a DeferredRegister object that wraps around the ForgeRegistries.ITEMS registry. This objects helps us register things at the right time when the game loads.

Then, we supply our DeferredRegister with the event bus in the init method. This is done to make sure our object knows when various stages of loading are happening.

Lastly, we register our item using the register method while providing both an item name and a function that will be used to create the actual item.

As you can see, our item is would be an instance of the Item class. When creating this instance, we also have to pass in an Item.Properties object that defines various properties of the object. In this case, we defined the group of our item to be MATERIALS.

Now, we will make a few edits to the ExampleMod class and tell it to use our RegistryHandler.

package com.example.examplemod;

import ...;

import java.util.stream.Collectors;

// The value here should match an entry in the META-INF/mods.toml file
+ @Mod(ExampleMod.MODID)
public class ExampleMod
{
    // Directly reference a log4j logger.
    private static final Logger LOGGER = LogManager.getLogger();
+   public static final String MODID = "examplemod";

    public ExampleMod() {
        // Register the setup method for modloading
        FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup);
        // Register the enqueueIMC method for modloading
        FMLJavaModLoadingContext.get().getModEventBus().addListener(this::enqueueIMC);
        // Register the processIMC method for modloading
        FMLJavaModLoadingContext.get().getModEventBus().addListener(this::processIMC);
        // Register the doClientStuff method for modloading
        FMLJavaModLoadingContext.get().getModEventBus().addListener(this::doClientStuff);

        // Register ourselves for server and other game events we are interested in
        MinecraftForge.EVENT_BUS.register(this);

+       RegistryHandler.init();
    }

    ...
}

+ indicate modified lines

With these changes in place, we should be able to register our copper item.

Defining texture, model, and lang translation

Moving on, we need to give our item a look, a shape, and a name.

To achieve this, we need to create a few nested folders and files under src/main/resources. In the end, the file structure should look like this.

src/main/resources
├── assets
│   └── examplemod
│       ├── lang
│       │   └── en_us.json*
│       ├── models
│       │   └── item
│       │       └── copper.json*
│       └── textures
│           └── items
│               └── copper.png*
├── META-INF
│   └── mods.toml
└── pack.mcmeta

filenames with a * next to them are ones added or modified


copper.png will just be the 16x16 item texture.

copper.json will be a model file defining the shape of our item. In this case, the most basic one will be enough

models/item/copper.json

{
  "parent": "item/generated",
  "textures": {
    "layer0": "examplemod:items/copper"
  }
}

en_us.json will contain the English name of our item which would just be Copper.

en_us.json

{
  "item.examplemod.copper": "Copper"
}

Result

With a proper registry and all the necessary info to display the item, we should be able to see our copper item in game 🔥

copper item in game

results matching ""

    No results matching ""