Fabric API

Fabric API

108M Downloads

[Question] Cannot generate trees in overworld

longfangsong opened this issue ยท 5 comments

commented

Sorry for disturbing. I'm trying to creating a mod with fabric and when to following the tutorial:trees, I met up with some problems.

I have the following code (most from the tutorial, based on the example mod project):

RichSaplingGenerator.java
public class RichSaplingGenerator extends SaplingGenerator {
  public RichSaplingGenerator() {
  }

  @Nullable
  @Override
  protected ConfiguredFeature<TreeFeatureConfig, ?> getTreeFeature(Random random, boolean bees) {
    return (ConfiguredFeature<TreeFeatureConfig, ?>) ExampleMod.TREE_RICH;
  }
}
RichSaplingBlock.java
public class RichSaplingBlock extends SaplingBlock {
    public RichSaplingBlock(SaplingGenerator generator, Settings settings) {
        super(generator, settings);
    }
}
ExampleMod.java
public class ExampleMod implements ModInitializer {
    public static ConfiguredFeature<?, ?> TREE_RICH = null;
    public static Block RICH_SAPLING = new RichSaplingBlock(
            new RichSaplingGenerator(),
            FabricBlockSettings.of(Material.PLANT).noCollision().ticksRandomly().breakInstantly());

    @Override
    public void onInitialize() {
        Registry.register(Registry.BLOCK, new Identifier("modid", "rich_sapling"), RICH_SAPLING);
        Registry.register(Registry.ITEM, new Identifier("modid", "rich_sapling"), new BlockItem(RICH_SAPLING, new Item.Settings().group(ItemGroup.MISC)));
        TREE_RICH = Feature.TREE
                // Configure the feature using the builder
                .configure(new TreeFeatureConfig.Builder(
                        new SimpleBlockStateProvider(Blocks.NETHERITE_BLOCK.getDefaultState()), // Trunk block provider
                        new StraightTrunkPlacer(8, 3, 0), // places a straight trunk
                        new SimpleBlockStateProvider(Blocks.DIAMOND_BLOCK.getDefaultState()), // Foliage block provider
                        new SimpleBlockStateProvider(RICH_SAPLING.getDefaultState()), // Sapling provider; used to determine what blocks the tree can generate on
                        new BlobFoliagePlacer(ConstantIntProvider.create(5), ConstantIntProvider.create(0), 3), // places leaves as a blob (radius, offset from trunk, height)
                        new TwoLayersFeatureSize(1, 0, 1) // The width of the tree at different layers; used to see how tall the tree can be without clipping into blocks
                ).build());
        // This code runs as soon as Minecraft is in a mod-load-ready state.
        // However, some things (like resources) may still be uninitialized.
        // Proceed with mild caution.
        RegistryKey<ConfiguredFeature<?, ?>> treeRich = RegistryKey.of(Registry.CONFIGURED_FEATURE_KEY, new Identifier("modid", "tree_rich"));

        Registry.register(BuiltinRegistries.CONFIGURED_FEATURE, treeRich.getValue(), TREE_RICH);

        // You should use the VEGETAL_DECORATION generation step for trees
        BiomeModifications.addFeature(BiomeSelectors.foundInOverworld(), GenerationStep.Feature.VEGETAL_DECORATION, treeRich);

        System.out.println("Hello Fabric world!");
    }
}

I found the feature is working, ie. I can put saplings and they will grow, the problem is that the tree does not generate in the overworld, and there're no exceptions reported.

Did I do something wrong?

commented

It can't work, because you haven't added any decorators. The normal way to do that (which is also how vanilla does it) is to create a second feature that uses the first feature, but has decorators for placement.

However, this is not the right place to ask for help with modding.

commented

Here's a simple example:
https://github.com/CamoMano/Vanilla-Enhanced/blob/master/src/main/java/com/vanillaenhanced/world/Features.java
Note the difference between the basic REDWOOD_TREE and the decorated REDWOOD_TREES that includes worldgen placement information.

commented

Here's a simple example:

https://github.com/CamoMano/Vanilla-Enhanced/blob/master/src/main/java/com/vanillaenhanced/world/Features.java

Note the difference between the basic REDWOOD_TREE and the decorated REDWOOD_TREES that includes worldgen placement information.

Why are you using ConfiguredFeatures.Decorators there? That's protected, and only it only has shortcuts anyway.

commented

Its not my code. It's just the first thing I found in recent updates on curseforge that had the tree/trees idiom in a simple example.
I didn't look at the details. :-)

commented

Thank you, adding decorators is working.