(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 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. 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.
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"))
);
}
}
\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.
** Items have been added! ** **
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"));
}
Q. I want to add a function A. The commentary article is undecided due to the developmental content.
Creating Minecraft 1.14.4 Forge Mod Part 4 [Adding Blocks] [Solved][1.14.2] Custom Blocks not dropping Items - Modder Support - Forge Forums