[Java] [Java]Let’s create Minecraft Mod 1.14.4 [2. Add Block]

4 minute read

(This article is one of a series of commentary articles)

First article: Introduction Previous Article: 1. Add Items Next article: 3. Add Creative Tab

Add block

Add blocks. Adding blocks is similar to adding items, so it’s easy! In the same way for items, we will create a class that manages blocks.

\src\main\java\jp\koteko\example_mod\
   ├ ExampleMod.java
   └ lists
      ├ BlockList.java
      └ ItemList.java

BlockList.java


package jp.koteko.example_mod.lists;

import jp.koteko.example_mod.ExampleMod;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber(modid = ExampleMod.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD)
public class BlockList {
    public static Block ExampleBlock = new Block(Block.Properties.create(Material.IRON))
            .setRegistryName(new ResourceLocation(ExampleMod.MOD_ID, "example_block"));

    @SubscribeEvent
    public static void registerBlocks(RegistryEvent.Register<Block> event) {
        event.getRegistry().registerAll(
                ExampleBlock
        );
    }

    @SubscribeEvent
    public static void registerBlockItems(RegistryEvent.Register<Item> event) {
        event.getRegistry().registerAll(
                new BlockItem(ExampleBlock, new Item.Properties().group(ItemGroup.BUILDING_BLOCKS))
                        .setRegistryName(new ResourceLocation(ExampleMod.MOD_ID, "example_block"))
        );
    }
}

While you’re at it, let’s erase the registration statement that was prepared from the beginning in the main file.

ExampleMod.java


//...
public class ExampleMod
{
    //...

    // delete from here
    //@Mod.EventBusSubscriber(bus=Mod.EventBusSubscriber.Bus.MOD)
    //public static class RegistryEvents {
    // @SubscribeEvent
    // public static void onBlocksRegistry(final RegistryEvent.Register<Block> blockRegistryEvent) {
    // // register a new block here
    // LOGGER.info("HELLO from Register Block");
    //}
    //}
    // delete here
}

Now let’s start the game. Capture.PNG Capture2.PNG Blocks have been added!

As you can see from the code, adding a block is basically the same as adding an item, but one thing to note is that the block exists as an item at the same time as a block, so register it Is also necessary.

A brief description of the code
#### **` Block registration part`** ```java public class BlockList { // Declare and initialize the block as a member variable // Material.IRON specifies something like iron as the property of the block // Set the block ID to register with setRegistryName //Block ID in which the "example_block" part is registered Lowercase public static Block ExampleBlock = new Block(Block.Properties.create(Material.IRON)) .setRegistryName(new ResourceLocation(ExampleMod.MOD_ID, "example_block")); // Register block @SubscribeEvent public static void registerBlocks(RegistryEvent.Register event) { event.getRegistry().registerAll( ExampleBlock ); } // Item registration // Since there is a BlockItem class, new and register with this argument is (Block, Item.Propaties) @SubscribeEvent public static void registerBlockItems(RegistryEvent.Register event) { event.getRegistry().registerAll( new BlockItem(ExampleBlock, new Item.Properties().group(ItemGroup.BUILDING_BLOCKS)) .setRegistryName(new ResourceLocation(ExampleMod.MOD_ID, "example_block")) ); } } ``` </div></details> ___ After confirming that the block has been added safely, we will make detailed settings. Note that the setting of `blockstates` has been increased in addition to each item at the time of item. ``` \src\main\resources └ assets └ example_mod ├ blockstates │ └ example_block.json ├ lang │ └ en_us.json │ └ ja_jp.json ├ models │ ├ block │ │ └ example_block.json │ └ item │ └ example_block.json └ textures ├ blocks │ └ example_block.png └ items ``` ```json:blockstates\example_block.json { "variants": { "": {"model": "example_mod:block/example_block"} } } ``` `"MOD_ID:block/[model file name]"` Texture can be set for each state value (state) of the block, but it is omitted here. #### **`en_us.json`** ```json { "item.example_mod.example_ingot": "Example Ingot", "block.example_mod.example_block": "Example Block" } ``` #### **`ja_jp.json`** ```json { "item.example_mod.example_ingot": "Example ingot", "block.example_mod.example_block": "Example block" } ``` ```json:models\block\example_block.json { "parent": "block/cube_all", "textures": { "all": "example_mod:blocks/example_block" } } ``` Specify a simple cube by `"parent":"block/cube_all"`. Specify the texture on the entire surface with `"all"`. ```json:models\item\example_block.json { "parent": "example_mod:block/example_block" } ```Specify the block model file in `"parent"`. Try to start the game again. ![Capture.PNG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/256627/042ae98c-5fc8-42eb-9a10-34e1f22cc4c5.png) ![Capture.PNG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/256627/dc6bd7a3-af02-25d3-ba3d-99360e8b5a99.png) **Items have been added! ** # Development Q. Even if I break it, it doesn't become an item? **A. Set the loottable. ** ``` \src\main\resources ├ assets └ data └ example_mod └ loot_tables └ blocks └ example_block.json ``` #### **`example_block.json`** ```json { "type": "minecraft:block", "pools": [ { "rolls": 1, "entries": [ { "type": "minecraft:item", "name": "example_mod:example_block" } ] } ] } ``` Q. I want to use it as a light source Q. I want to set the destruction tool **A. Observe `net.minecraft.block.Block`. ** #### **`Block.java`** ```java // ... public static class Properties { private Material material; private MaterialColor mapColor; private boolean blocksMovement = true; private SoundType soundType = SoundType.STONE; private int lightValue; private float resistance; private float hardness; // ... // ... public Block.Properties doesNotBlockMovement() { this.blocksMovement = false; return this; } public Block.Properties slipperiness(float slipperinessIn) { this.slipperiness = slipperinessIn; return this; } public Block.Properties sound(SoundType soundTypeIn) { this.soundType = soundTypeIn; return this; } public Block.Properties lightValue(int lightValueIn) { this.lightValue = lightValueIn; return this; } public Block.Properties hardnessAndResistance(float hardnessIn, float resistanceIn) { this.hardness = hardnessIn; this.resistance = Math.max(0.0F, resistanceIn); return this; } // ... ``` The argument `Block.Properties` given to the constructor holds the values related to the block properties. It also defines accessors for those values. An example is shown below with reference to these. #### **`BlockList.java`** ```java public class BlockList { public static Block ExampleBlock = new Block( Block.Properties.create(Material.IRON) .hardnessAndResistance(2.0f, 3.0f) .lightValue(15) .harvestLevel(3) .harvestTool(ToolType.SHOVEL)) .setRegistryName(new ResourceLocation(ExampleMod.MOD_ID, "example_block")); } ``` ![Capture.PNG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/256627/0a2c10c2-bb98-e725-c67c-71256c5ca701.png) Q. I want to add a function A. The commentary is undecided due to its evolutionary content. # Reference [Minecraft 1.14.4 Forge Mod Part 4 [Add Block]](https://qiita.com/Hiroya_W/items/5b14f397bc52ce87255f) [[Solved][1.14.2] Custom Blocks not dropping Items-Modder Support-Forge Forums](https://forums.minecraftforge.net/topic/71815-solved1142-custom-blocks-not-dropping-items/) # Next article [3. Add Creative Tab](https://qiita.com/koteko/items/1d7a7fb2ff98bebce939)