How to register an ItemBlock in Minecraft Forge?

4.2k views Asked by At

So I'm trying to make a custom block in my mod. I tried to register it, but found out that the API has changed and I had no idea how to register my block. After some research, I found out that you need to register the Block AND the ItemBlock. So I tried making an ItemBlock out of my Block using this code:

public ItemBlock itemBlock = new ItemBlock(this);

This code is inside my Block class. Then I tried registering the block:

event.getRegistry().register(new myBlockInMyMod());
event.getRegistry().register(new myBlockInMyMod().itemBlock);

I get this error code on IntelliJ:

register (net.minecraft.block.Block) in IForgeRegistry cannot be applied to net.minecraft.item.ItemBlock

Am I doing anything wrong here? Because the register method doesn't seem to accept ItemBlocks :/

1

There are 1 answers

1
Draco18s no longer trusts SE On BEST ANSWER

Ok, you have two problems:

  1. That's not how you get an item block (or at least, what you're doing is a bad idea)
  2. You need to register the ItemBlock in the Registry.Register<Item> event, not the Register<Block> event, as they are different registries.

The first problem is easy to fix:

new ItemBlock(block_reference_variable)

However, you don't currently have a reference to your block. You instantiate it and shove it directly into the registry, never holding onto a reference to refer to later. Most people create static fields in their main mod class.

You can, if you want, store a reference to the ItemBlock as you are, but when you create a new instance of your block, that ItemBlock has no bearing on the block you registered. They're different objects. You must store a reference to your block somewhere.

The second problem requires you write a second event handler method, only you change <Block> to <Item>: The Blocks.REGISTERY cannot accept Items, you need to wait for the Items.REGISTRY to fire its registration event.