跳到主要内容

42落叶器

代码

在tutorialmod.mixins.json中:

	"mixins": [
"ExampleMixin",
"TrunkPlacerTypeInvoker",
"FoliagePlacerTypeInvoker"
],

FoliagePlacerTypeInvoker类:

@Mixin(FoliagePlacerType.class)
public interface FoliagePlacerTypeInvoker {
@Invoker
static <P extends FoliagePlacer> FoliagePlacerType<P> callRegister(String id, Codec<P> codec) {
throw new IllegalStateException();
}
}

新增树叶生成结构:

public class ChestnutFoliagePlacer extends FoliagePlacer {
public static final Codec<ChestnutFoliagePlacer> CODEC = RecordCodecBuilder.create(chestnutFoliagePlacerInstance ->
fillFoliagePlacerFields(chestnutFoliagePlacerInstance).and(Codec.intRange(0, 12).fieldOf("height")
.forGetter(instance -> instance.height)).apply(chestnutFoliagePlacerInstance, ChestnutFoliagePlacer::new));
private final int height;

public ChestnutFoliagePlacer(IntProvider radius, IntProvider offset, int height) {
super(radius, offset);
this.height = height;
}

@Override
protected FoliagePlacerType<?> getType() {
return ModFoliagePlacerTypes.CHESTNUT_FOLIAGE_PLACER;
}

@Override
protected void generate(TestableWorld world, BlockPlacer placer, Random random, TreeFeatureConfig config, int trunkHeight,
TreeNode treeNode, int foliageHeight, int radius, int offset) {

// generateSquare(world, placer, random, config, treeNode.getCenter());
// radius on how many blocks it extends into x and z direction
// y how much offset in the y direction from treeNode.getCenter()
// y if it is dependent on i, also offsets each new layer in the y direction

generateSquare(world, placer, random, config, treeNode.getCenter().up(0), 2, 1, treeNode.isGiantTrunk());
generateSquare(world, placer, random, config, treeNode.getCenter().up(1), 2, 1, treeNode.isGiantTrunk());
generateSquare(world, placer, random, config, treeNode.getCenter().up(2), 2, 1, treeNode.isGiantTrunk());
}

@Override
public int getRandomHeight(Random random, int trunkHeight, TreeFeatureConfig config) {
return this.height;
}

@Override
protected boolean isInvalidForLeaves(Random random, int dx, int y, int dz, int radius, boolean giantTrunk) {
return false;
}
}

树种类:

public class ModFoliagePlacerTypes {
public static final FoliagePlacerType<?> CHESTNUT_FOLIAGE_PLACER = FoliagePlacerTypeInvoker.callRegister("chestnut_foliage_placer", ChestnutFoliagePlacer.CODEC);

public static void register() {
TutorialMod.LOGGER.info("Registering Foliage Placer for " + TutorialMod.MOD_ID);
}
}

修改配置ModConfiguredFeatures:

        register(context, CHESTNUT_KEY, Feature.TREE, new TreeFeatureConfig.Builder(
BlockStateProvider.of(ModBlocks.CHESTNUT_LOG),
new ChestnutTrunkPlacer(5, 4, 3),

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

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

在TutorialMod中:

        ModFoliagePlacerTypes.register();

效果

image-20240825213937845