[Java] Let's create a mod for Minecraft 1.16.1 [Add block]

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

First article: Introduction Previous article: Add Item Next article:

Add block

Add a block. I changed the writing style a little from 1.14.4 (it does not mean that the implementation method has 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);
        }
    }
}

Do the same for blocking as the item registration in Last time. Add items to List at the same time as declaration / initialization, andregister ()all items in the list with a for loop. If it is just a non-functional block, it will be an instance of the Block class. An instance of ʻAbstractBlock.Properties` is given as an 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);
        }
    }
}

Since the block also exists as an item at the same time, register the item as well. Overloaded the register method (defining a method with the same name with different arguments) to make it easier to register block items. If you pass the registered name, block, and item group, it will create a BlockItem with the group set in it and throw it to the item's register. BlockItem is a subclass of ʻItem`, and the block as an item is an instance of this class.

resources 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

lang\en_us.json


{
  "item.liveinwater.watertree_root_block": "WaterTree Root"
}

lang\ja_jp.json


{
  "item.liveinwater.watertree_root_block": "Water tree root"
}

blockstates\watertree_root_block.json


{
  "variants": {
    "": { "model": "liveinwater:block/watertree_root_block" }
  }
}

" model ":" [MOD_ID] / [model file path] " If you want to change the model depending on the state of the block (for example, a block with a direction or a connecting block such as a fence), edit this file, but if you do not need it, it looks like this.

models\block\watertree_root_block.json


{
  "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 that reflects the same texture over the entire surface.

models\item\watertree_root_block.json


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

For the block item, specify the model file of the block in parent.

watertree_root_block.png Create and place a texture.

Route table settings

In Minecraft 1.9 and later, the route table was introduced as a mechanism to manage drops. Block drops are 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, drop the block itself ( watertree_root_block), otherwise drop one soil block (dirt) and an item ( watertree_root) at random. " Detailed explanation will be given on the reference page. (I tried various things, but there is a duplicate description and it may be redundant.)

The minimum configuration looks like this.

Minimum configuration route table to drop the block itself


{
  "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 applied in sequence, and the result is selected from each entry given to ʻentries by the lottery of rollstimes. Specified to returnname of type` in the entry.

Verification

Start the game and check. キャプチャ.PNG It was confirmed that the block and its item could be added, the display was correct, and the block itself was dropped with silk touch and the soil and item were dropped with non-silk touch.

reference

[Java] Let's create a mod for Minecraft 1.14.4 [2. Add a block] Creating Minecraft 1.14.4 Forge Mod Part 4 [Adding Blocks] [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

Recommended Posts

[Java] Let's create a mod for Minecraft 1.14.4 [2. Add block]
[Java] Let's create a mod for Minecraft 1.16.1 [Add block]
[Java] Let's create a mod for Minecraft 1.14.4 [4. Add tools]
[Java] Let's create a mod for Minecraft 1.14.4 [5. Add armor]
[Java] Let's create a mod for Minecraft 1.14.4 [7. Add progress]
[Java] Let's create a mod for Minecraft 1.14.4 [6. Add recipe]
[Java] Let's create a mod for Minecraft 1.16.1 [Add item]
[Java] Let's create a mod for Minecraft 1.14.4 [3. Add creative tab]
[Java] Let's create a mod for Minecraft 1.14.4 [Introduction]
[Java] Let's create a mod for Minecraft 1.16.1 [Introduction]
[Java] Let's create a mod for Minecraft 1.14.4 [99. Mod output]
[Java] Let's create a mod for Minecraft 1.16.1 [Add and generate trees]
[Java] Let's create a mod for Minecraft 1.14.4 [9. Add and generate trees]
[Java] Let's create a mod for Minecraft 1.14.4 [8. Add and generate ore]
[Java] Let's create a mod for Minecraft 1.14.4 [0. Basic file]
[Java] Let's create a mod for Minecraft 1.14.4 [Extra edition]
[Java] Let's create a mod for Minecraft 1.16.1 [Basic file]
Let's create a Java development environment (updating)
Let's create a super-simple web framework in Java
[Java] Create a filter
Create a java method [Memo] [java11]
[Java] Create a temporary file
How to create a lightweight container image for Java apps
Create a MOB using the Minecraft Java Mythicmobs plugin | Preparation 1
Let's create a TODO application in Java 4 Implementation of posting function
How to sign a Minecraft MOD
Let's create a TODO application in Java 6 Implementation of search function
Let's create a TODO application in Java 8 Implementation of editing function
Create a Java project using Eclipse
[Java] How to create a folder
Let's create a TODO application in Java 1 Brief explanation of MVC
Let's create a TODO application in Java 5 Switch the display of TODO
Create a fluentd server for testing
Let's install Docker on Windows 10 and create a verification environment for CentOS 8!
Let's go with Watson Assistant (formerly Conversation) ⑤ Create a chatbot with Watson + Java + Slack
[Java] Create and apply a slide master
How to create a Maven repository for 2020
Create a TODO app in Java 7 Create Header
[Rails] Let's create a super simple Rails API
[Java] Let's make a DB access library!
Let's make a calculator application with Java ~ Create a display area in the window
Java (add2)
Java (add)
Let's create a versatile file storage (?) Operation library by abstracting file storage / acquisition in Java
I made a Diff tool for Java files
How to create a database for H2 Database anywhere
A story about Java 11 support for Web services
Create a CSR with extended information in Java
Create a simple bulletin board with Java + MySQL
Let's create a REST API using WildFly Swarm.
[Windows] [IntelliJ] [Java] [Tomcat] Create a Tomcat9 environment with IntelliJ
Create a Lambda Container Image based on Java 15
java I tried to break a simple block
[Java] Create something like a product search API
Let's create a RESTful email sending service + client
Let's create a custom tab view in SwiftUI 2.0
[Java] Create a collection with only one element
Create your own Android app for Java learning
Create Scala Seq from Java, make Scala Seq a Java List
Create an extension for Burp. ~ Simply add tab ~
Create a tool for name identification in Salesforce