1.1 BlockItems

Now that we have changed the way that we register items and blocks, we can finally make our BlockItem for our block.

BlockItems are the version of the block that you can hold in your hand, and store in your inventory. Basically they are just items, but with a special ‘block’ model for rendering the cube in your hand, and the function to place the block in the world.

All we need to do is create a new instance of minecraft’s BlockItem and register it. The constructor is a bit different from Item’s, in that it also takes a Block

public BlockItem(Block blockIn, Item.Properties builder)

So we need to get a reference to our block, which we can do with

ModBlocks.EXAMPLE_ORE.get()

and we also need to create our Item.Properties which will be the same code as for our other items so far. The only other part we need is the registry name for using our setup function, we could just write the string again, but that would quickly get annoying to maintain and leave us open to issues. Instead we can get the name from the block

ModBlocks.EXAMPLE_ORE.get().getRegistryName()

If we put that all together in our ModEventSubscriber class in our onRegisterItems function, you should have something like this

@SubscribeEvent
public static void onRegisterItems(RegistryEvent.Register<Item> event) {
    event.getRegistry().registerAll(
        setup(new BlockItem(ModBlocks.EXAMPLE_BLOCK.get(), new Item.Properties().group(ModItemGroups.MOD_ITEM_GROUP)), ModBlocks.EXAMPLE_ORE.get().getRegistryName())
    );
}

Now you should be able to start the game, and find the block in your creative item’s tab, hold it in your hand, and use it like normal.

Missing texture house looks terrible!

You might notice that the block in flat on your toolbar, giant in your hand, and still has no texture anywhere. Let’s go ahead and fix that up next.

first we need to add a block folder to resources/assets/example_mod/models and create a file example_ore.json the format is the same as the model file for our item, except the parent is block/cube_all

{
  "parent": "block/cube_all",
  "textures": {
    "all": "example_mod:block/example_ore"
  }
}

Now we also need to add a example_ore.json file in our resources/assets/example_mod/models/items folder as well, this one will only references our block as it’s parent.

{
  "parent": "example_mod:block/example_ore"
}

Now our block is looking like other blocks in your inventory, and is smaller in your hand.

Previously the block looked more like this

Next we can go ahead and add the texture file, you can download the one I used from here or create your own 16×16 png texture. Then copy that texture file into resources/textures/block/example_ore.png we already made reference to that texture in our block’s model file earlier, so we don’t need to make any code changes to get the texture (partially) working.

As you can see the block is properly textured in our hand, and on the ground, but placed blocks are still untextured! The reason behind this is that blocks placed in the world have a blockstate, called meta in older versions of minecraft. This is used, for example, to set the color of wool blocks. In our case we don’t really need blockstates for this block, so we will set a blank blockstate. Create a new file at resources/assets/example_mod/blockstates/example_ore.json that should look like this

{
  "variants": {
    "": {
      "model": "example_mod:block/example_ore"
      }
  }
}

Later we will go over actually using blockstates, but for now just know that any block where you just want one texture you will need a blockstate just like that, but with a different model.

You can see my code at this point here github.com/cody-code-wy/1.15.2-Example-Mod/commit/9253a0

Now everything is textured, but my house has not improved. But if you try to break the block in survival you will find that nothing drops! In the next tutorial we will cover Block Loot, and make our block drop itself when mined.

Leave Comment

Your email address will not be published. Required fields are marked *