[Java] [Java] Let’s create Minecraft Mod 1.16.1 [Add Block]

4 minute read

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

Top article: Introduction Previous article: Add Item Next article:

Add block

Add a block. I changed the writing method a little from 1.14.4 (it does not mean that the implementation method changed due to the version upgrade).

Block registration

\src\main\java\jp\koteko\liveinwater\
   ├ block
   │ └ Blocks.java
   ├ item
   └ LiveInWater.java

Blocks.java


package jp.koteko.liveinwater.block;

import jp.koteko.liveinwater.LiveInWater;
import net.minecraft.block.AbstractBlock;
import net.minecraft.block.Block;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.util.ArrayList;
import java.util.List;

@Mod.EventBusSubscriber(modid = LiveInWater.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD)
public class Blocks {
    public static List<Block> blockList = new ArrayList<Block>();
    public static Block WATERTREE_ROOT_BLOCK = register("watertree_root_block", new Block(AbstractBlock.Properties.create(Material.WOOD).hardnessAndResistance(2.5F).sound(SoundType.WOOD)));

    private static Block register(String key, Block blockIn){
        blockList.add(blockIn);
        return blockIn.setRegistryName(LiveInWater.MOD_ID, key);
    }

    @SubscribeEvent
    public static void registerBlocks(RegistryEvent.Register<Block> event) {
        for (Block block: blockList) {
            event.getRegistry().register(block);
        }
    }
}

Last time Do the same as item registration for blocks. At the same time as declaring and initializing, add items to List and do register() all the items in the list in a for loop. If it is just a non-functional block, it is an instance of the Block class. An instance of AbstractBlock.Properties is given to the argument of the constructor.

Item registration

\src\main\java\jp\koteko\liveinwater\
   ├ block
   ├ item
   │ └ Items.java
   └ LiveInWater.java

Items.java


package jp.koteko.liveinwater.item;

import jp.koteko.liveinwater.LiveInWater;
import jp.koteko.liveinwater.block.Blocks;
import net.minecraft.block.Block;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.util.ArrayList;
import java.util.List;

@Mod.EventBusSubscriber(modid = LiveInWater.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD)
public class Items {
    public static List<Item> itemList = new ArrayList<Item>();
    public static final Item WATERTREE_ROOT = register("watertree_root", new Item((new Item.Properties()).group(ItemGroup.MATERIALS)));
    public static final Item WATERTREE_ROOT_BLOCK = register("watertree_root_block", Blocks.WATERTREE_ROOT_BLOCK, ItemGroup.BUILDING_BLOCKS);

    private static Item register(String key, Item itemIn) {
        itemList.add(itemIn);
        return itemIn.setRegistryName(LiveInWater.MOD_ID, key);
    }
    private static Item register(String key, Block blockIn, ItemGroup itemGroupIn) {
        return register(key, new BlockItem(blockIn, (new Item.Properties()).group(itemGroupIn)));
    }

    @SubscribeEvent
    public static void registerItems(RegistryEvent.Register<Item> event) {
        for (Item item: itemList) {
            event.getRegistry().register(item);
        }
    }
}

Blocks also exist as items at the same time, so register items as well. Made it easier to register block items by overloading the register method (defining the same name method with different arguments). If you pass the registered name, block, item group, it will create a BlockItem with the group set and throw it in the item’s register. BlockItem is a subclass of Item, and blocks as items are instances of this class.

resource settings

\src\main\resources
   └ assets
      └ liveinwater
         ├ blockstates
         │ └ watertree_root_block.json
         ├ lang
         │ └ en_us.json
         │ └ ja_jp.json
         ├ models
         │ ├ block
         │ │ └ watertree_root_block.json
         │ └ item
         │ └ watertree_root_block.json
         └ textures
            ├ block
            │ └ watertree_root_block.png
            └ item
{
  "item.liveinwater.watertree_root_block": "WaterTree Root"
}
{
  "item.liveinwater.watertree_root_block": "Watertree root"
}
{
  "variants": {
    "": {"model": "liveinwater:block/watertree_root_block"}
  }
}

"model": "[MOD_ID]/[model file path]" This file is edited when the model changes depending on the state of the block (for example, a block having a direction or a block to be connected such as a fence), but this is the case when it is unnecessary.

{
  "parent": "block/cube_all",
  "textures": {
    "all": "liveinwater:block/watertree_root_block"
  }
}

[MOD_ID]:[texture file path] parent specifies block/cube_all. This is a simple cube model, which reflects the same texture on all sides.

{
  "parent": "liveinwater:block/watertree_root_block"
}

For block items, specify the block model file in parent.

watertree_root_block.png Create and place a texture.## Route table settings Minecraft 1.9 and later introduced a route table as a mechanism for managing drops. Block drop is also set using this.

\src\main\resources
   ├ assets
   └ data
      └ liveinwater
         └ loot_tables
            └ blocks
               └ watertree_root_block.json

Place blocks\watertree_root_block.json in the data\liveinwater folder prepared in Previous article.

watertree_root_block.json


{
  "type": "minecraft:block",
  "pools": [
    {
      "rolls": 1,
      "conditions": [
        {
          "condition": "minecraft:match_tool",
          "predicate": {
            "enchantments": [
              {
                "enchantment": "minecraft:silk_touch",
                "levels": {
                  "min": 1
                }
              }
            ]
          }
        }
      ],
      "entries": [
        {
          "type": "minecraft:item",
          "name": "liveinwater:watertree_root_block"
        }
      ]
    },
    {
      "rolls": 1,
      "conditions": [
        {
          "condition": "minecraft:inverted",
          "term": {
            "condition": "minecraft:match_tool",
            "predicate": {
              "enchantments": [
                {
                  "enchantment": "minecraft:silk_touch",
                  "levels": {
                    "min": 1
                  }
                }
              ]
            }
          }
        }
      ],
      "entries": [
        {
          "type": "minecraft:item",
          "functions": [
            {
              "function": "minecraft:apply_bonus",
              "enchantment": "minecraft:fortune",
              "formula": "minecraft:binomial_with_bonus_count",
              "parameters": {
                "extra": 3,
                "probability": 0.5714286
              }
            }
          ],
          "name": "liveinwater:watertree_root"
        }
      ]
    },
    {
      "rolls": 1,
      "conditions": [
        {
          "condition": "minecraft:inverted",
          "term": {
            "condition": "minecraft:match_tool",
            "predicate": {
              "enchantments": [
                {
                  "enchantment": "minecraft:silk_touch",
                  "levels": {
                    "min": 1
                  }
                }
              ]
            }
          }
        }
      ],
      "entries": [
        {
          "type": "minecraft:item",
          "name": "minecraft:dirt"
        }
      ]
    }
  ]
}

An example is “If you destroy it with the Silk Touch tool, the block itself (watertree_root_block) is dropped, otherwise, one dirt block (dirt) and an item (watertree_root) are randomly dropped”. Detailed explanation is given to the reference page. (I tried variously, but there is a duplicate description and there is a possibility that it is redundant writing.)

The minimum configuration looks like this:
#### **` Minimal route table that drops the block itself`** ```json { "type": "minecraft:block", "pools": [ { "rolls": 1, "entries": [ { "type": "minecraft:item", "name": "[MOD_ID]:[ITEM_NAME]" } ] } ] } ``` `type` selects what the route table is about. Each pool given to `pools` is sequentially adapted, and the result is selected from each entry given to `entries` by the lottery of `rolls` times. Specified to return the `name` of the `type` in the entry.

Confirmation

Launch the game and check. Capture.PNG We were able to confirm that the block and its item were added, that the display was correct, and that the block itself was silk-touched and the soil and item were dropped by non-silk-touch.

Reference

Create [Java]Minecraft Mod 1.14.4 [2. Add Block] Minecraft 1.14.4 Forge Mod Part 4 [Add Block] [Route Table-Minecraft Wiki](https://minecraft-ja.gamepedia.com/%E3%83%AB%E3%83%BC%E3%83%88%E3%83%86%E3%83%BC(%E3%83%96%E3%83%AB) loot_tables_1.14.md · GitHub

Next article