Your First Item and Challenge
Your First Item
I encourage you to design your own items instead of copying mine, but if you are not interested in creating your own artwork, all my items will be provided to you.
First, make a new folder inside your mod folder. In other words, if you mod is named tutorialcraft, you should be putting your new folder in com.example.tutorialcraft.
NOTE: Feel free to change the name of the com and example folders.
Name your new folder items.
Within your items folder, create the class ModItems.java. This will be a central registry class to keep track of all the items your mod wants to add.
Inside your ModItems class, create a DeferredRegister for your Forge Items:
1
2
public static final DeferredRegister<Item> ITEMS =
DeferredRegister.create(ForgeRegistries.ITEMS, TutorialCraft.MOD_ID);
What is a DeferredRegister?
A DeferredRegister is Forge’s helper to add “things” to its registry. Right now, we are using it to add items.
Specifically, it allows us to make new items and then add them to the registry.
Right now, ForgeRegistries.ITEMS holds all the built-in items before we’ve added our mod. This is where we will add all of our own items to be in the game.
We make it static so that there’s only one copy shared by the whole mod, and final so it never gets replaced.
Making the Item Variable
Each item is a RegistryObject, which is Forge’s wrapper around anything that you register into the game.
Here is an example to add a Ruby item into the game:
1
2
3
public static final RegistryObject<Item> RUBY = ITEMS.register(
"ruby", // The name of the item
() -> new Item(new Item.Properties())); // Passing the Supplier
What does the second line do?
Forge wants us to pass a Supplier to create an item rather than just the item itself. Additionally, items have many different properties. Calling Item.Properties() calls all of the default properties of a typical item.
You are able to override the default properties by chaining functions. We will discuss that more later.
Registering Using the ModBus
In TutorialCraft.java, there is modEventBus.
What is modEventBus?
- An event bus that is dedicated for startup events of a mod
- This makes it useful to register things into the bus
In the last section, we added an item into the register to define them. This portion is covering how to connect your items to the game’s startup process so they actually appear in Minecraft.
In ModItems.java, add a function to register our ITEMS in a startup event:
1
2
3
4
// Add this somewhere within the ModItems class
public static void register(IEventBus eventBus) {
ITEMS.register(eventBus);
}
Next, go back to TutorialCraft.java. Register the ModBus with the new items inside of the constructor:
1
2
3
4
5
public TutorialCraft(FMLJavaModLoadingContext context)
{
IEventBus modEventBus = context.getModEventBus();
ModItems.register(modEventBus); // <- Add this line
Adding the Texture
To add the item texture, we will first need to put the actual PNG of it in the correct spot.
You should have a resources folder in main / src.
Add subdirectories to the resources folder so that your file tree looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
src/
└─ main/
└─ resources/
└─ assets/
└─ tutorialcraft/
├─ lang/
│ └─ en_us.json
├─ models/
│ └─ item/
│ └─ ruby.json
└─ textures/
└─ item/
└─ ruby.png
If you haven’t turned off flattening for your file tree, I recommend you do it here. Instructions on how to do so is covered at the beginning of the tutorial.
Navigate to ruby.json and add:
1
2
3
4
5
6
{
"parent": "item/generated",
"textures": {
"layer0": "tutorialcraft:item/ruby"
}
}
What this means:
"parent" : "item/generated"tells the game to use the built in item/generated model for this item, which is Mojang’s default 2D flat item model (used for things like minerals, food, enchanted books, etc.). It tells Minecraft to render this as a flat texture that always faces the player, with optional multiple texture layers."layer0": "tutorialcraft:item/ruby"tells the game that the base texture layer is the ruby item in our mod
Next, we will add to the en_us.json file. This is what Minecraft uses to translate the items’ keys within the mod to readable text. Specifically, this file is for English (USA). Feel free to add more languages, such as French using fr_fr.json, etc.
1
2
3
{
"item.tutorialcraft.ruby": "Ruby"
}
Now when look at this item in-game, its name will display as whatever you entered here.
Lastly, download ruby.png and move it to resources/assets/tutorialcraft/textures/item/.
Add Your Item to the Menu
Last but not least, we need to add our item to the creative menu.
We have a function called addCreative in TutorialCraft.java that does exactly that:
1
2
3
4
5
6
7
private void addCreative(BuildCreativeModeTabContentsEvent event)
{
// just adding this to the Ingredients tab in the creative menu
if (event.getTabKey() == CreativeModeTabs.INGREDIENTS) {
event.accept(ModItems.RUBY);
}
}
Congrats!
You have now created your own item!
There are a lot of moving components, but thankfully, you won’t have to repeat the setup processes for your additional items here on out.
Challenge Item
Time to try this on your own!
CHALLENGE: Make a Sapphire item with the following features:
- It needs to be fire resistant
- Stacks of Sapphires can only go up to 16
- Set its durability to 500
Here is the PNG for it.
Try this on your own, using documentation for help. View the hint below if you need it. Additionally, my solution is included if you get completely stuck.
HINT: Remember the method chaining pattern we used when making the Ruby? Look at the Forge item properties documentation and chain the modifier methods together!
SOLUTION
1
2
3
4
5
6
7
// Use chaining to edit the properties of each item
public static final RegistryObject<Item> SAPPHIRE = ITEMS.register("sapphire",
() -> new Item(new Item.Properties()
.fireResistant()
.stacksTo(16)
.durability(500))
);
Start Up the Game!
Choose the runClient configuration and run the game. Check inside of the Ingredients tab – you should see both a Sapphire and a Ruby there.