跳到主要内容

20自定义作物

代码

自定义番茄庄稼:

public class TomatoCropBlock extends CropBlock {
public static final int MAX_AGE = 5;
public static final IntProperty AGE = Properties.AGE_5;

public TomatoCropBlock(Settings settings) {
super(settings);
}

@Override
protected ItemConvertible getSeedsItem() {
return ModItems.TOMATO_SEEDS;
}

@Override
public IntProperty getAgeProperty() {
return AGE;
}

@Override
public int getMaxAge() {
return MAX_AGE;
}

@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(AGE);
}
}

在ModItems中注册物品

public static final Item TOMATO_SEEDS = registerItem("tomato_seeds",
new AliasedBlockItem(ModBlocks.TOMATO_CROP, new FabricItemSettings()));

在ModBlocks中注册块

public static final Block TOMATO_CROP = Registry.register(Registries.BLOCK, new Identifier(TutorialMod.MOD_ID, "tomato_crop"),
new TomatoCropBlock(FabricBlockSettings.copyOf(Blocks.WHEAT)));

掉落物生成器中添加ModLootTableProvider:

BlockStatePropertyLootCondition.Builder builder = BlockStatePropertyLootCondition.builder(ModBlocks.TOMATO_CROP).properties(StatePredicate.Builder.create()
.exactMatch(TomatoCropBlock.AGE, 5));
addDrop(ModBlocks.TOMATO_CROP, cropDrops(ModBlocks.TOMATO_CROP, ModItems.TOMATO, ModItems.TOMATO_SEEDS, builder));

ModModelProvider中添加:

blockStateModelGenerator.registerCrop(ModBlocks.TOMATO_CROP, TomatoCropBlock.AGE, 0, 1, 2, 3, 4, 5);

在TutorialModClient中添加:

public class TutorialModClient implements ClientModInitializer {
@Override
public void onInitializeClient() {
BlockRenderLayerMap.INSTANCE.putBlock(ModBlocks.RUBY_DOOR, RenderLayer.getCutout());
BlockRenderLayerMap.INSTANCE.putBlock(ModBlocks.RUBY_TRAPDOOR, RenderLayer.getCutout());

BlockRenderLayerMap.INSTANCE.putBlock(ModBlocks.TOMATO_CROP, RenderLayer.getCutout());
}
}

除此之外,还需要注意添加番茄各阶段的图片,以及番茄的翻译

效果

西红柿种植的不同阶段

image-20240824233128602

收获掉落物:

image-20240824233157986