跳到主要内容

39自定义树

代码

新建一个worldgenerater:ChestnutSaplingGenerator

public class ChestnutSaplingGenerator extends SaplingGenerator {
@Nullable
@Override
protected RegistryKey<ConfiguredFeature<?, ?>> getTreeFeature(Random random, boolean bees) {
return ModConfiguredFeatures.CHESTNUT_KEY;
}
}

ModConfiguredFeatures配置:

   public static final RegistryKey<ConfiguredFeature<?, ?>> CHESTNUT_KEY = registerKey("chestnut");

public static void boostrap(Registerable<ConfiguredFeature<?, ?>> context) {
register(context, CHESTNUT_KEY, Feature.TREE, new TreeFeatureConfig.Builder(
BlockStateProvider.of(ModBlocks.CHESTNUT_LOG),
new StraightTrunkPlacer(5, 4, 3),

BlockStateProvider.of(ModBlocks.CHESTNUT_LEAVES),
new BlobFoliagePlacer(ConstantIntProvider.create(2), ConstantIntProvider.create(1), 2),

new TwoLayersFeatureSize(1, 0, 2)).build());
}

新建一个Block:

public static final Block CHESTNUT_SAPLING = registerBlock("chestnut_sapling",
new SaplingBlock(new ChestnutSaplingGenerator(), FabricBlockSettings.copyOf(Blocks.OAK_SAPLING)));

添加到物品组中:

entries.add(ModBlocks.CHESTNUT_SAPLING);

配置数据生成ModLootTableProvider:

addDrop(ModBlocks.CHESTNUT_SAPLING);

addDrop(ModBlocks.CHESTNUT_LEAVES, leavesDrops(ModBlocks.CHESTNUT_LEAVES, ModBlocks.CHESTNUT_SAPLING, 0.0025f));

ModModelProvider:

blockStateModelGenerator.registerTintableCross(ModBlocks.CHESTNUT_SAPLING, BlockStateModelGenerator.TintType.NOT_TINTED);

启动类:

BlockRenderLayerMap.INSTANCE.putBlock(ModBlocks.CHESTNUT_SAPLING, RenderLayer.getCutout());

效果

image-20240825212123211