今天我们在1.18.2版本下制作属于自己的方块和物品
1.新建init文件夹 -> init包中新建BlockInit.java和ItemInit.java
![cr0.jpg]()
BlockInit.java
```
package com.joy187.re8joymod.init;
import java.util.function.Function;
import com.google.common.base.Supplier;
import com.joy187.re8joymod.Main;
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.SoundType;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.level.material.MaterialColor;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
public class BlockInit {
//注册机
public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS,
Main.MOD_ID);
public static final DeferredRegister<Item> ITEMS = ItemInit.ITEMS;
//定义一个属于我们的方块
public static final RegistryObject<Block> EXAMPLE_BLOCK = register("example_block",
() -> new Block(BlockBehaviour.Properties.of(Material.METAL, MaterialColor.COLOR_PURPLE).strength(3.0f)
.sound(SoundType.METAL).requiresCorrectToolForDrops()),
object -> () -> new BlockItem(object.get(), new Item.Properties().tab(Main.TUTORIAL_TAB)));
private static <T extends Block> RegistryObject<T> registerBlock(final String name,
final Supplier<? extends T> block) {
return BLOCKS.register(name, block);
}
private static <T extends Block> RegistryObject<T> register(final String name, final Supplier<? extends T> block,
Function<RegistryObject<T>, Supplier<? extends Item>> item) {
RegistryObject<T> obj = registerBlock(name, block);
ITEMS.register(name, item.apply(obj));
return obj;
}
}
```
ItemInit.java
```
package com.joy187.re8joymod.init;
import com.google.common.base.Supplier;
import com.joy187.re8joymod.Main;
import com.joy187.re8joymod.items.ItemPreAttack;
import net.minecraft.world.item.Item;
import net.minecraftforge.common.ForgeSpawnEggItem;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
public class ItemInit {
//注册机
public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS,
Main.MOD_ID);
//定义一个属于我们自己的物品
public static final RegistryObject<Item> EXAMPLE_ITEM = register("example_item",
() -> new Item(new Item.Properties().tab(Main.TUTORIAL_TAB)));
private static <T extends Item> RegistryObject<T> register(final String name, final Supplier<T> item) {
return ITEMS.register(name, item);
}
}
```
2.在Main文件中添加初始化信息
Main.java
```
package com.joy187.re8joymod;
import com.joy187.re8joymod.entity.EntityRe8Dimi;
import com.joy187.re8joymod.init.BlockInit;
import com.joy187.re8joymod.init.EntityInit;
import com.joy187.re8joymod.init.ItemInit;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.EntityAttributeCreationEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
@Mod("re8joymod")
public class Main {
public static final String MOD_ID = "re8joymod";
public static final CreativeModeTab TUTORIAL_TAB = new CreativeModeTab(MOD_ID) {
@Override
@OnlyIn(Dist.CLIENT)
public ItemStack makeIcon() {
return new ItemStack(BlockInit.EXAMPLE_BLOCK.get());
}
};
public Main() {
IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus();
//添加物品,方块的初始化信息
ItemInit.ITEMS.register(bus);
BlockInit.BLOCKS.register(bus);
MinecraftForge.EVENT_BUS.register(this);
}
public static void setup() {
IEventBus bus = MinecraftForge.EVENT_BUS;
}
}
```
3.在resources/assets/模组名称/下新建一格blockstates文件夹 -> 在该文件夹中新建方块名.json文件(名称与BlockInit.java中你自定义的方块名称保持一致)
![cr1.jpg]()
example_block.json
```
{
"variants": {
"": {
"model": "re8joymod:block/example_block"
}
}
}
```
同样的新建models文件夹 -> 在models中新建block和item两个文件夹
![cr2.jpg]()
在block中新建方块名.json文件(名称与BlockInit.java中你自定义的方块名称保持一致)
example_block.json(指明你方块的贴图)
```
{
"parent": "block/cube_all",
"textures": {
"all": "re8joymod:block/example_block"
}
}
```
在item中新建物品名.json文件(名称与ItemInit.java中你自定义的方块名称保持一致)
example_item.json
```
{
"parent": "item/generated",
"textures": {
"layer0": "re8joymod:item/example_item"
}
}
```
注:方块也是特殊的物品,所以要为方块新建一个属于它的.json文件:
example_block.json
```
{
"parent": "re8joymod:block/example_block"
}
```
同样的新建textures文件夹 -> 在textures中新建block和item两个文件夹
分别将我们的方块贴图和物品贴图放入文件夹中:
![cr3.jpg]()
4.在resources/data文件夹下新建2个文件夹,一个叫minecraft,另一个是你的模组名称(以re8joymod为例)
在minecraft文件夹中新建 -> tags文件夹 -> tags包中新建blocks文件夹 -> blocks包中新建mineable文件夹 ->在mineable中新建 pickaxe.json(定义方块是否可挖掘)
pickaxe.json
```
{
"replace": false,
"values": [
"re8joymod:example_block" //这里面的方块都是可以被挖掘的
]
}
```
在re8joymod中新建loot\_tables文件夹 -> 在loot\_tables中新建blocks文件夹 -> 在blocks文件夹中新建 方块名.json(名称与BlockInit.java中你自定义的方块名称保持一致)
example_block.json(挖掉之后方块的掉落物)
```
{
"type": "minecraft:block",
"pools": [
{
"rolls": 1.0,
"entries": [
{
"type": "minecraft:item",
"name": "re8joymod:example_block" //掉落物格式:模组名称:物品、方块的名称,原版默认是minecraft:xxx
}
]
}
]
}
```
5.保存所有文件,刷新项目,启动游戏
![cr5.jpg]()
OK,完美!
|
|