[Java] Let's create a mod for Minecraft 1.14.4 [2. Add block]

(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 a block. Adding blocks is similar to adding items, so it's ** easy **! Take the method of creating a class that manages blocks in the same way as for items.

\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"))
        );
    }
}

By the way, let's delete the registration description 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");
    //    }
    //}
    //Deleted so far
}

Now let's start the game. キャプチャ.PNG キャプチャ2.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 a block exists as a block as well as an item, so its registration Is also necessary.

A brief description of the code

The part to register the block


public class BlockList {
    //Declare and initialize the block as a member variable
    // Material.IRON specifies something like iron as a block property
    //The block ID to be registered with setRegistryName is set.
    // "example_block"Block ID in which the part is registered Lowercase
    public static Block ExampleBlock = new Block(Block.Properties.create(Material.IRON))
            .setRegistryName(new ResourceLocation(ExampleMod.MOD_ID, "example_block"));

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

    //Item registration
    //Since there is a BlockItem class, the argument to be registered as new with this is(Block, Item.Propaties)
    @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"))
        );
    }
}
___ After confirming that the block has been added safely, make detailed settings. Notice that the `block states` setting has been increased in addition to each item when it is an 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

blockstates\example_block.json


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

" MOD_ID: block / [model file name] " You can set the texture for each state of the block, but we will omit it here.

en_us.json


{
  "item.example_mod.example_ingot": "Example Ingot",
  "block.example_mod.example_block": "Example Block"
}

ja_jp.json


{
  "item.example_mod.example_ingot": "Example ingot",
  "block.example_mod.example_block": "Example block"
}

models\block\example_block.json


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

Specify a simple cube with " parent ":" block / cube_all ". Specify the texture on the entire surface with " all ".

models\item\example_block.json


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

Specify the block model file in " parent ".

Try launching the game again. キャプチャ.PNG キャプチャ.PNG

** Items have been added! ** **

development

Q. Even if I break it, it doesn't become an item? ** A. Let's set loottable. ** **

\src\main\resources
   ├ assets
   └ data
      └ example_mod
         └ loot_tables
            └ blocks
               └ example_block.json

example_block.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


// ...
   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 value related to the characteristics of the block. Accessors for those values are also defined. An example is shown below with reference to these.

BlockList.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"));
}

キャプチャ.PNG

Q. I want to add a function A. The commentary article is undecided due to the developmental content.

reference

Creating Minecraft 1.14.4 Forge Mod Part 4 [Adding Blocks] [Solved][1.14.2] Custom Blocks not dropping Items - Modder Support - Forge Forums

Next article

3. Add creative tab

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 [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 [1. Add items]
[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 [8. Add and generate ore]
[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 timed process with Java Timer! !!
Let's create a super-simple web framework in Java
[Java] Create a filter
[Java basics] Let's make a triangle with a for statement
[Java] Let's create a mod for Minecraft 1.14.4 [3. Add creative tab]
[No.003] Create an order list screen for the orderer
[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 [1. Add items]
[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]
Create a java method [Memo] [java11]
[Java] Create a temporary file
[Java twig] Create a parser combinator for recursive descent parsing 2
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!
[Java twig] Create a parser combinator for recursive descent parsing (also memoize)
Let's go with Watson Assistant (formerly Conversation) ⑤ Create a chatbot with Watson + Java + Slack
Create a java web application development environment with docker for mac part2
[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
[Azure] I tried to create a Java application for free-Web App creation- [Beginner]
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.
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
Try to create a bulletin board in Java
How to create pagination for a "kaminari" array
Let's create a custom tab view in SwiftUI 2.0
Create your own Android app for Java learning
Create Scala Seq from Java, make Scala Seq a Java List