From f5d8cd2f5343651d4e1fd8729746695b25a30ba4 Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Sun, 3 May 2020 00:04:55 +0100 Subject: [PATCH] Rework rubber tree generation --- .../techreborn/config/TechRebornConfig.java | 15 ++--- .../techreborn/world/RubberTreeDecorator.java | 38 +++++++++++++ .../techreborn/world/RubberTreeFeature.java | 56 +++++++++++-------- .../java/techreborn/world/WorldGenerator.java | 12 ++-- 4 files changed, 84 insertions(+), 37 deletions(-) create mode 100644 src/main/java/techreborn/world/RubberTreeDecorator.java diff --git a/src/main/java/techreborn/config/TechRebornConfig.java b/src/main/java/techreborn/config/TechRebornConfig.java index 5d95eaddc..f09d8feae 100644 --- a/src/main/java/techreborn/config/TechRebornConfig.java +++ b/src/main/java/techreborn/config/TechRebornConfig.java @@ -731,15 +731,12 @@ public class TechRebornConfig { @Config(config = "world", category = "ore", key = "tungstenVeinSize", comment = "Amount of Tungsten Ores per vein.") public static int tungstenVeinSize = 6; - @Config(config = "world", category = "rubber_tree", key = "RubberTreeChance", comment = "Chance to spawn rubber tree") - public static float RubberTreeChance = 0.1F; + @Config(config = "world", category = "rubber_tree", key = "rubberTreeChance", comment = "Chance to spawn rubber tree") + public static int rubberTreeChance = 50; - @Config(config = "world", category = "rubber_tree", key = "RubberTreeCount", comment = "Amount of trees to spawn in successful case") - public static int RubberTreeCount = 1; + @Config(config = "world", category = "rubber_tree", key = "rubberTreeBaseHeight", comment = "Basic height for not-spire part of rubber tree") + public static int rubberTreeBaseHeight = 6; - @Config(config = "world", category = "rubber_tree", key = "RubberTreeBaseHeight", comment = "Basic height for not-spire part of rubber tree") - public static int RubberTreeBaseHeight = 6; - - @Config(config = "world", category = "rubber_tree", key = "RubberTreeSpireHeight", comment = "Height of spire of rubber tree") - public static int RubberTreeSpireHeight = 3; + @Config(config = "world", category = "rubber_tree", key = "rubberTreeSpireHeight", comment = "Height of spire of rubber tree") + public static int rubberTreeSpireHeight = 3; } diff --git a/src/main/java/techreborn/world/RubberTreeDecorator.java b/src/main/java/techreborn/world/RubberTreeDecorator.java new file mode 100644 index 000000000..9f3d6ff3a --- /dev/null +++ b/src/main/java/techreborn/world/RubberTreeDecorator.java @@ -0,0 +1,38 @@ +package techreborn.world; + +import com.mojang.datafixers.Dynamic; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.Heightmap; +import net.minecraft.world.IWorld; +import net.minecraft.world.gen.chunk.ChunkGenerator; +import net.minecraft.world.gen.chunk.ChunkGeneratorConfig; +import net.minecraft.world.gen.decorator.ChanceDecoratorConfig; +import net.minecraft.world.gen.decorator.Decorator; + +import java.util.Random; +import java.util.function.Function; +import java.util.stream.IntStream; +import java.util.stream.Stream; + +// Big thanks to SuperCoder7979 for this +public class RubberTreeDecorator extends Decorator { + public RubberTreeDecorator(Function, ? extends ChanceDecoratorConfig> configDeserializer) { + super(configDeserializer); + } + + @Override + public Stream getPositions(IWorld world, ChunkGenerator generator, Random random, ChanceDecoratorConfig config, BlockPos pos) { + // Generate tree clusters randomly + if (random.nextInt(config.chance) == 0) { + // Generate 4 - 8 trees + int treeCount = 4 + random.nextInt(5); + return IntStream.range(0, treeCount).mapToObj((i) -> { + int x = random.nextInt(16) + pos.getX(); + int z = random.nextInt(16) + pos.getZ(); + int y = world.getTopY(Heightmap.Type.MOTION_BLOCKING, x, z); + return new BlockPos(x, y, z); + }); + } + return Stream.empty(); + } +} \ No newline at end of file diff --git a/src/main/java/techreborn/world/RubberTreeFeature.java b/src/main/java/techreborn/world/RubberTreeFeature.java index aef05a22b..baf84a890 100644 --- a/src/main/java/techreborn/world/RubberTreeFeature.java +++ b/src/main/java/techreborn/world/RubberTreeFeature.java @@ -26,12 +26,13 @@ package techreborn.world; import com.mojang.datafixers.Dynamic; import net.minecraft.block.BlockState; -import net.minecraft.util.math.BlockBox; import net.minecraft.util.math.BlockPos; import net.minecraft.world.ModifiableTestableWorld; import net.minecraft.world.gen.feature.AbstractTreeFeature; import net.minecraft.world.gen.feature.TreeFeatureConfig; +import net.minecraft.world.gen.foliage.BlobFoliagePlacer; +import net.minecraft.world.gen.foliage.FoliagePlacerType; import techreborn.config.TechRebornConfig; import techreborn.init.TRContent; @@ -49,32 +50,41 @@ public class RubberTreeFeature extends AbstractTreeFeature { super(configFactory); } -// @Override -// public boolean generate(ModifiableTestableWorld world, Random random, BlockPos blockPos, Set set, Set set2, BlockBox blockBox, TreeFeatureConfig branchedTreeFeatureConfig) { -// if(super.generate(world, random, blockPos, set, set2, blockBox, branchedTreeFeatureConfig)) { -// spawnSpike(world, blockPos); -// return true; -// } -// return false; -// } - private void spawnSpike(ModifiableTestableWorld world, BlockPos pos) { - final int startScan = pos.getY(); - BlockPos topPos = null; + public static class FoliagePlacer extends BlobFoliagePlacer { - //Limit the scan to 15 blocks - while(topPos == null && pos.getY() - startScan < 15) { - pos = pos.up(); - if(world.testBlockState(pos, BlockState::isAir)) { - topPos = pos; + public FoliagePlacer(int radius, int randomRadius, int offset, int randomOffset, int height) { + super(radius, randomRadius, offset, randomOffset, height); + } + + @Override + protected void generate(ModifiableTestableWorld world, Random random, TreeFeatureConfig config, int trunkHeight, TreeNode treeNode, int foliageHeight, int radius, Set leaves, int i) { + super.generate(world, random, config, trunkHeight, treeNode, foliageHeight, radius, leaves, i); + + spawnSpike(world, treeNode.getCenter()); + } + + private void spawnSpike(ModifiableTestableWorld world, BlockPos pos) { + final int startScan = pos.getY(); + BlockPos topPos = null; + + //Limit the scan to 15 blocks + while(topPos == null && pos.getY() - startScan < 15) { + pos = pos.up(); + if(world.testBlockState(pos, BlockState::isAir)) { + topPos = pos; + } + } + + if(topPos == null) return; + + for (int i = 0; i < TechRebornConfig.rubberTreeSpireHeight; i++) { + world.setBlockState(pos.up(i), TRContent.RUBBER_LEAVES.getDefaultState(), 19); } } - - if(topPos == null) return; - - for (int i = 0; i < TechRebornConfig.RubberTreeSpireHeight; i++) { - setBlockState(world, pos.up(i), TRContent.RUBBER_LEAVES.getDefaultState()); - } } + + + } diff --git a/src/main/java/techreborn/world/WorldGenerator.java b/src/main/java/techreborn/world/WorldGenerator.java index 743b940c1..b2899e9dd 100644 --- a/src/main/java/techreborn/world/WorldGenerator.java +++ b/src/main/java/techreborn/world/WorldGenerator.java @@ -32,7 +32,7 @@ import net.minecraft.util.registry.Registry; import net.minecraft.world.biome.Biome; import net.minecraft.world.biome.Biome.Category; import net.minecraft.world.gen.GenerationStep; -import net.minecraft.world.gen.decorator.CountExtraChanceDecoratorConfig; +import net.minecraft.world.gen.decorator.ChanceDecoratorConfig; import net.minecraft.world.gen.decorator.Decorator; import net.minecraft.world.gen.decorator.RangeDecoratorConfig; import net.minecraft.world.gen.feature.Feature; @@ -61,6 +61,7 @@ import java.util.List; public class WorldGenerator { public static Feature RUBBER_TREE_FEATURE; + public static RubberTreeDecorator RUBBER_TREE_DECORATOR; public static TreeFeatureConfig RUBBER_TREE_CONFIG; @@ -79,6 +80,7 @@ public class WorldGenerator { private static void setupTrees() { RUBBER_TREE_FEATURE = Registry.register(Registry.FEATURE, new Identifier("techreborn:rubber_tree"), new RubberTreeFeature(TreeFeatureConfig::deserialize)); + RUBBER_TREE_DECORATOR = Registry.register(Registry.DECORATOR, new Identifier("techreborn:rubber_tree"), new RubberTreeDecorator(ChanceDecoratorConfig::deserialize)); WeightedBlockStateProvider logProvider = new WeightedBlockStateProvider(); logProvider.addState(TRContent.RUBBER_LOG.getDefaultState(), 10); @@ -94,8 +96,8 @@ public class WorldGenerator { RUBBER_TREE_CONFIG = new TreeFeatureConfig.Builder( logProvider, new SimpleBlockStateProvider(TRContent.RUBBER_LEAVES.getDefaultState()), - new BlobFoliagePlacer(2, 0, 0, 0, 3), - new StraightTrunkPlacer(TechRebornConfig.RubberTreeBaseHeight, 3, 0), + new RubberTreeFeature.FoliagePlacer(2, 0, 0, 0, 3), + new StraightTrunkPlacer(TechRebornConfig.rubberTreeBaseHeight, 3, 0), new TwoLayersFeatureSize(1, 0, 1) ).build(); } @@ -162,8 +164,8 @@ public class WorldGenerator { if (biome.getCategory() == Category.FOREST || biome.getCategory() == Category.TAIGA || biome.getCategory() == Category.SWAMP) { biome.addFeature(GenerationStep.Feature.VEGETAL_DECORATION, RUBBER_TREE_FEATURE.configure(RUBBER_TREE_CONFIG) - .createDecoratedFeature(Decorator.COUNT_EXTRA_HEIGHTMAP - .configure(new CountExtraChanceDecoratorConfig(biome.getCategory() == Category.SWAMP ? 1 : 0, TechRebornConfig.RubberTreeChance, TechRebornConfig.RubberTreeCount)) + .createDecoratedFeature(RUBBER_TREE_DECORATOR + .configure(new ChanceDecoratorConfig(biome.getCategory() == Category.SWAMP ? TechRebornConfig.rubberTreeChance / 3 : TechRebornConfig.rubberTreeChance)) ) ); }