项目结构
src
├── main
│ ├── java
│ │ ├── com
│ │ │ ├── XXXYJade17
│ │ │ │ └── TestMod
│ │ │ │ ├── Registry.java //注册类
│ │ │ │ └── TestMod.java //Mod主程序
│ └── resources
│ ├── assets
│ │ ├── testmod
│ │ │ ├── lang
│ │ │ │ ├── en_us.json // 英文语言文件
│ │ │ │ └── zh_cn.json // 中文语言文件
│ │ │ ├── models
│ │ │ │ ├── block
│ │ │ │ │ ├── example_block_01.json
│ │ │ │ │ └── example_block_02.json
│ │ │ │ └── item
│ │ │ │ ├── example_block_01.json
│ │ │ │ ├── example_block_02.json
│ │ │ │ ├── example_item_01.json
│ │ │ │ └── example_item_02.json
│ │ │ └── textures
│ │ │ │ ├── block
│ │ │ │ │ ├── example_block_01.png
│ │ │ │ │ └── example_block_02.png
│ │ │ │ └── item
│ │ │ │ ├── example_item_01.png
│ │ │ │ └── example_item_02.png
一.创建延迟注册容器
//Block方块容器
private static final DeferredRegister<Block> BLOCKS =
DeferredRegister.create(Registries.BLOCK, TestMod.MODID);
//Item物品容器
private static final DeferredRegister<Item> ITEMS =
DeferredRegister.create(Registries.ITEM, TestMod.MODID);
二.创建注册物品,这里提供两种方式
1.基础注册方式(Supplier)
//Block
private static final Supplier<Block> EXAMPLE_BLOCK_01 =
BLOCKS.register("example_block_01",
() -> new Block(BlockBehaviour.Properties.of()));
//Item
private static final Supplier<Item> EXAMPLE_ITEM_01 =
ITEMS.register("example_item_01",
() -> new Item(new Item.Properties()));
2.现代注册方式(DeferredHolder)
//Block
private static final DeferredHolder<Block, Block> EXAMPLE_BLOCK_02 =
BLOCKS.register("example_block_02",
() -> new Block(BlockBehaviour.Properties.of()));
//Item
private static final DeferredHolder<Item, Item> EXAMPLE_ITEM_02 =
ITEMS.register("example_item_02",
() -> new Item(new Item.Properties()));
*三.如果想要注册Block还需要注册对应的BlockItem
同样有两种方式
//使用Supplier
private static final Supplier<Item> EXAMPLE_BLOCK_01_ITEM =
ITEMS.register("example_block_01",
() -> new BlockItem(EXAMPLE_BLOCK_01.get(),
new Item.Properties()));
//使用DeferredHolder
private static final DeferredHolder<Item,Item> EXAMPLE_BLOCK_02_ITEM =
ITEMS.register("example_block_02",
() -> new BlockItem(EXAMPLE_BLOCK_02.get(),
new Item.Properties()));
四.创建注册函数,将注册器加到总线
public static void register(IEventBus bus){
BLOCKS.register(bus);
ITEMS.register(bus);
}
然后在主程序中引用即可
public TestMod(IEventBus bus, ModContainer container) {
Registry.register(bus);
}
五.添加语言文件
先在lang包中添加语言文件en_us.json,一般项目自带
{
"block.testmod.example_block_01": "Example Block 01",
"block.testmod.example_block_02": "Example Block 02",
"item.testmod.example_item_01": "Example Item 01",
"item.testmod.example_item_02": "Example Item 02"
}
想要中文文件,再添加一个zh_cn.json即可
{
"block.testmod.example_block_01": "示例方块01",
"block.testmod.example_block_02": "示例方块02",
"item.testmod.example_item_01": "示例物品01",
"item.testmod.example_item_02": "示例物品02"
}
六.添加模型和材质文件
先手动创建目录
├─resources
├── assets
│ ├── testmod
│ │ ├── lang
│ │ ├── models
│ │ │ ├── block
│ │ │ └── item
│ │ └── textures
│ │ │ ├── block
│ │ │ └── item
- Item
{
"parent": "minecraft:item/generated",
"textures": {
"layer0": "testmod:item/example_item_01"
}
}
- Block
先配置Block,这里采用六面同材质,根据需求可以自己更改
{
"parent": "block/cube_all",
"textures": {
"all": "testmod:block/example_block_01"
}
}
之后还需要在 model/Item 里再添加BlockItem,文件名同block
{
"parent": "testmod:block/example_block_01",
}
附完整代码
注册类
import net.minecraft.core.registries.Registries;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.registries.DeferredHolder;
import net.neoforged.neoforge.registries.DeferredRegister;
import java.util.function.Supplier;
public class Registry {
//延迟注册器
public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(Registries.BLOCK, TestMod.MODID);
public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(Registries.ITEM, TestMod.MODID);
//BLOCK
public static final Supplier<Block> EXAMPLE_BLOCK_01 = BLOCKS.register("example_block_01",
() -> new Block(BlockBehaviour.Properties.of()));
public static final DeferredHolder<Block, Block> EXAMPLE_BLOCK_02 = BLOCKS.register("example_block_02",
() -> new Block(BlockBehaviour.Properties.of()));
//BLOCK_ITEM
public static final Supplier<Item> EXAMPLE_BLOCK_01_ITEM = ITEMS.register("example_block_01",
() -> new BlockItem(EXAMPLE_BLOCK_01.get(), new Item.Properties()));
public static final DeferredHolder<Item,Item> EXAMPLE_BLOCK_02_ITEM = ITEMS.register("example_block_02",
() -> new BlockItem(EXAMPLE_BLOCK_02.get(), new Item.Properties()));
//ITEM
public static final Supplier<Item> EXAMPLE_ITEM_01= ITEMS.register("example_item_01",
() -> new Item(new Item.Properties()));
public static final DeferredHolder<Item, Item> EXAMPLE_ITEM_02 = ITEMS.register("example_item_02",
() -> new Item(new Item.Properties()));
//BLOCK STATE
public static final Supplier<Block> STATE_EXAMPLE_BLOCK = BLOCKS.register("state_example_block",
() -> new StateExampleBlock(BlockBehaviour.Properties.of()));
public static final Supplier<Item> STATE_EXAMPLE_BLOCK_ITEM = ITEMS.register("state_example_block",
() -> new BlockItem(STATE_EXAMPLE_BLOCK.get(), new Item.Properties()));
//注册方法
public static void register(IEventBus bus){
BLOCKS.register(bus);
ITEMS.register(bus);
ModCreativeTab.register(bus);
}
}
主程序
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.ModContainer;
import net.neoforged.fml.common.Mod;
@Mod(TestMod.MODID)
public class TestMod {
public static final String MODID = "testmod";
public TestMod(IEventBus bus, ModContainer container) {
Registry.register(bus);
}
}