一年前的今天我们制作了1.12.2的作物教程:Minecraft 1.12.2模组开发(十五) 种植作物
我们本次在1.18.2中实现一个农作物。
1.在blocks包中新建一个我们的作物类BlockHerb。
BlockHerb.java
package com.joy187.re8joymod.blocks;
import com.joy187.re8joymod.init.ItemInit;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.CarrotBlock;
import net.minecraft.world.level.block.CropBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
public class BlockHerb extends CropBlock{
//每个阶段我们的作物的高低
private static final VoxelShape[] SHAPE_BY_AGE = new VoxelShape[]{
Block.box(0.0D, 0.0D, 0.0D, 16.0D, 2.0D, 16.0D),
Block.box(0.0D, 0.0D, 0.0D, 16.0D, 4.0D, 16.0D),
Block.box(0.0D, 0.0D, 0.0D, 16.0D, 6.0D, 16.0D),
Block.box(0.0D, 0.0D, 0.0D, 16.0D, 8.0D, 16.0D),
Block.box(0.0D, 0.0D, 0.0D, 16.0D, 10.0D, 16.0D),
Block.box(0.0D, 0.0D, 0.0D, 16.0D, 12.0D, 16.0D),
Block.box(0.0D, 0.0D, 0.0D, 16.0D, 14.0D, 16.0D),
Block.box(0.0D, 0.0D, 0.0D, 16.0D, 16.0D, 16.0D)};
public BlockHerb(Properties builder)
{
super(builder);
}
//该作物对应哪个物品
protected ItemLike getBaseSeedId() {
return ItemInit.HERB.get();
}
public VoxelShape getShape(BlockState state, BlockGetter blockgetter, BlockPos blockpos, CollisionContext contex) {
return SHAPE_BY_AGE[state.getValue(this.getAgeProperty())];
}
}
将我们的作物类在BlockInit中进行注册
public static RegistryObject<Block> HERB_BLOCK = BLOCKS.register("herb_block",
()-> new CropBlock(BlockBehaviour.Properties.of(Material.PLANT).noCollission().randomTicks().instabreak().sound(SoundType.CROP)));
2.在ItemInit中声明一个我们的作物物品:
//该作物对应我们的作物方块,可以定义吃了作物之后的回复值(像胡萝卜、土豆一样)
public static final RegistryObject<Item> HERB = ITEMS.register("herb1",
()-> new ItemNameBlockItem(BlockInit.HERB_BLOCK.get(),new Item.Properties()
.tab(Main.TUTORIAL_TAB).food(new FoodProperties.Builder().nutrition(2).saturationMod(0.5F).alwaysEat().build())
));
3.在项目主类的clientSetup()中添加我们的方块渲染模式
public Main() {
IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus();
ItemInit.ITEMS.register(bus);
BlockInit.BLOCKS.register(bus);
EntityInit.ENTITY_TYPES.register(bus);
EffectInit.EFFECTS.register(bus);
PotionInit.POTIONS.register(bus);
SoundInit.SOUNDS.register(bus);
//将我们的clientSetup函数进行注册
bus.addListener(this::clientSetup);
MinecraftForge.EVENT_BUS.register(this);
}
private void clientSetup(final FMLClientSetupEvent event) {
//EntityRenderers.register(EntityInit.DUND1.get(), RenderDund::new);
ItemBlockRenderTypes.setRenderLayer(BlockInit.EBONY_LEAVES.get(), RenderType.cutout());
//添加这个,可以让我们的作物像小麦的生长图像一样
ItemBlockRenderTypes.setRenderLayer(BlockInit.HERB_BLOCK.get(), RenderType.cutout());
MenuScreens.register(MenuInit.VIRUS_GENERATOR_MENU.get(), VirusGeneratorScreen::new);
}
4.代码部分结束,进入资源包制作
1.在resources\assets\你的modid\lang
中的en_us.json
添加我们作物的方块和物品的名称:
"block.re8joymod.herb_block":"Herb",
"item.re8joymod.herb1":"Herb",
2.在blockstates中新建一个我们的作物.json文件,用来指定每一阶段的生长状态
herb_block.json
{
"variants": {
"age=0": {
"model": "re8joymod:block/herb_block_stage0"
},
"age=1": {
"model": "re8joymod:block/herb_block_stage1"
},
"age=2": {
"model": "re8joymod:block/herb_block_stage2"
},
"age=3": {
"model": "re8joymod:block/herb_block_stage3"
},
"age=4": {
"model": "re8joymod:block/herb_block_stage4"
},
"age=5": {
"model": "re8joymod:block/herb_block_stage5"
},
"age=6": {
"model": "re8joymod:block/herb_block_stage6"
},
"age=7": {
"model": "re8joymod:block/herb_block_stage7"
}
}
}
3.由于我们定义了8个阶段的作物生长流程,所以需要制作8个这样的方块模型文件,在models/block
中新建八个文件
每一个都写成这种形式:
herb_block_stage0.json
{
"parent": "minecraft:block/crop",
"textures": {
"crop": "re8joymod:block/herb_block_stage0"
}
}
4.在models/item
中新建一个我们的物品模型文件
herb1.json
{
"parent": "item/generated",
"textures": {
"layer0": "re8joymod:items/herb1"
}
}
5.在textures/block
中添加我们作物不同阶段的贴图:
6.在textures/item
中添加我们作物的手持贴图:
7.在resources\data\你的modid\loot_tables\blocks
下新建一个我们的作物战利品文件,指明我们破坏了作物后掉落什么:
herb_block.json
{
"type": "minecraft:block",
"pools": [
{
"rolls": 1.0,
"entries": [
{
"type": "minecraft:alternatives",
"children": [
{
"type": "minecraft:item",
"conditions": [
{
"condition": "minecraft:block_state_property",
//对应我们的作物方块,到达最终阶段的掉落物
"block": "re8joymod:herb_block",
"properties": {
"age": "7"
}
}
],
"name": "re8joymod:herb1"
},
{
//掉落我们的作物物品
"type": "minecraft:item",
"name": "re8joymod:herb1"
}
]
}
]
}
],
"functions": [
{
"function": "minecraft:explosion_decay"
}
]
}
5.保存所有文件 -> 进入游戏调试
#### 各种生长状态都显示正常。