From 8eb15c8d739dbae96b48ceea2ed71e2d16b38b6a Mon Sep 17 00:00:00 2001 From: paulevsGitch Date: Sat, 3 Oct 2020 00:11:18 +0300 Subject: [PATCH] Structure scatter --- .../world/features/DoublePlantFeature.java | 65 +++++-------------- .../features/MossyGlowshroomFeature.java | 4 ++ .../world/features/ScatterFeature.java | 58 +++++++++++++++++ .../world/features/SinglePlantFeature.java | 59 +++++------------ 4 files changed, 95 insertions(+), 91 deletions(-) create mode 100644 src/main/java/ru/betterend/world/features/ScatterFeature.java diff --git a/src/main/java/ru/betterend/world/features/DoublePlantFeature.java b/src/main/java/ru/betterend/world/features/DoublePlantFeature.java index adae8e95..a88fbcb4 100644 --- a/src/main/java/ru/betterend/world/features/DoublePlantFeature.java +++ b/src/main/java/ru/betterend/world/features/DoublePlantFeature.java @@ -5,68 +5,39 @@ import java.util.Random; import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.BlockPos.Mutable; import net.minecraft.world.StructureWorldAccess; -import net.minecraft.world.gen.chunk.ChunkGenerator; -import net.minecraft.world.gen.feature.DefaultFeatureConfig; import ru.betterend.blocks.basis.BlockDoublePlant; -import ru.betterend.registry.BlockTagRegistry; import ru.betterend.util.BlocksHelper; import ru.betterend.util.MHelper; -public class DoublePlantFeature extends DefaultFeature { - private static final Mutable POS = new Mutable(); +public class DoublePlantFeature extends ScatterFeature { private final Block smallPlant; private final Block largePlant; - private final int radius; + private Block plant; public DoublePlantFeature(Block smallPlant, Block largePlant, int radius) { + super(radius); this.smallPlant = smallPlant; this.largePlant = largePlant; - this.radius = radius; } @Override - public boolean generate(StructureWorldAccess world, ChunkGenerator chunkGenerator, Random random, BlockPos blockPos, DefaultFeatureConfig featureConfig) { - blockPos = getPosOnSurface(world, blockPos); - - if (blockPos.getY() < 5) { - return false; + public boolean canGenerate(StructureWorldAccess world, Random random, BlockPos center, BlockPos blockPos, float radius) { + float d = MHelper.length(center.getX() - blockPos.getX(), center.getZ() - blockPos.getZ()) / radius * 0.6F + random.nextFloat() * 0.4F; + plant = d < 0.5F ? largePlant : smallPlant; + return plant.canPlaceAt(plant.getDefaultState(), world, blockPos); + } + + @Override + public void generate(StructureWorldAccess world, Random random, BlockPos blockPos) { + if (plant instanceof BlockDoublePlant) { + int rot = random.nextInt(4); + BlockState state = plant.getDefaultState().with(BlockDoublePlant.ROTATION, rot); + BlocksHelper.setWithoutUpdate(world, blockPos, state); + BlocksHelper.setWithoutUpdate(world, blockPos.up(), state.with(BlockDoublePlant.TOP, true)); } - if (!world.getBlockState(blockPos.down()).isIn(BlockTagRegistry.END_GROUND)) { - return false; + else { + BlocksHelper.setWithoutUpdate(world, blockPos, plant); } - - float r = MHelper.randRange(radius * 0.5F, radius, random); - int count = MHelper.floor(r * r * MHelper.randRange(1.5F, 3F, random)); - Block block; - for (int i = 0; i < count; i++) { - float pr = r * (float) Math.sqrt(random.nextFloat()); - float theta = random.nextFloat() * MHelper.PI2; - float x = pr * (float) Math.cos(theta); - float z = pr * (float) Math.sin(theta); - - POS.set(blockPos.getX() + x, blockPos.getY() + 5, blockPos.getZ() + z); - int down = BlocksHelper.downRay(world, POS, 16); - if (down > 10) continue; - POS.setY(POS.getY() - down); - - float d = MHelper.length(x, z) / r * 0.6F + random.nextFloat() * 0.4F; - block = d < 0.5F ? largePlant : smallPlant; - - if (block.canPlaceAt(block.getDefaultState(), world, POS)) { - if (block instanceof BlockDoublePlant) { - int rot = random.nextInt(4); - BlockState state = block.getDefaultState().with(BlockDoublePlant.ROTATION, rot); - BlocksHelper.setWithoutUpdate(world, POS, state); - BlocksHelper.setWithoutUpdate(world, POS.up(), state.with(BlockDoublePlant.TOP, true)); - } - else { - BlocksHelper.setWithoutUpdate(world, POS, block); - } - } - } - - return true; } } diff --git a/src/main/java/ru/betterend/world/features/MossyGlowshroomFeature.java b/src/main/java/ru/betterend/world/features/MossyGlowshroomFeature.java index e8a44faf..d5549e73 100644 --- a/src/main/java/ru/betterend/world/features/MossyGlowshroomFeature.java +++ b/src/main/java/ru/betterend/world/features/MossyGlowshroomFeature.java @@ -7,6 +7,7 @@ import java.util.function.Function; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; +import net.minecraft.block.Material; import net.minecraft.client.util.math.Vector3f; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; @@ -154,6 +155,9 @@ public class MossyGlowshroomFeature extends DefaultFeature { if (state.getBlock() != Blocks.END_STONE && state.isIn(BlockTagRegistry.END_GROUND)) { return true; } + if (state.getMaterial().equals(Material.PLANT)) { + return true; + } return state.getMaterial().isReplaceable(); }; diff --git a/src/main/java/ru/betterend/world/features/ScatterFeature.java b/src/main/java/ru/betterend/world/features/ScatterFeature.java new file mode 100644 index 00000000..7e76cf5b --- /dev/null +++ b/src/main/java/ru/betterend/world/features/ScatterFeature.java @@ -0,0 +1,58 @@ +package ru.betterend.world.features; + +import java.util.Random; + +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.BlockPos.Mutable; +import net.minecraft.world.StructureWorldAccess; +import net.minecraft.world.gen.chunk.ChunkGenerator; +import net.minecraft.world.gen.feature.DefaultFeatureConfig; +import ru.betterend.registry.BlockTagRegistry; +import ru.betterend.util.BlocksHelper; +import ru.betterend.util.MHelper; + +public abstract class ScatterFeature extends DefaultFeature { + private static final Mutable POS = new Mutable(); + private final int radius; + + public ScatterFeature(int radius) { + this.radius = radius; + } + + public abstract boolean canGenerate(StructureWorldAccess world, Random random, BlockPos center, BlockPos blockPos, float radius); + + public abstract void generate(StructureWorldAccess world, Random random, BlockPos blockPos); + + + @Override + public boolean generate(StructureWorldAccess world, ChunkGenerator chunkGenerator, Random random, BlockPos center, DefaultFeatureConfig featureConfig) { + center = getPosOnSurface(world, center); + + if (center.getY() < 5) { + return false; + } + if (!world.getBlockState(center.down()).isIn(BlockTagRegistry.END_GROUND)) { + return false; + } + + float r = MHelper.randRange(radius * 0.5F, radius, random); + int count = MHelper.floor(r * r * MHelper.randRange(1.5F, 3F, random)); + for (int i = 0; i < count; i++) { + float pr = r * (float) Math.sqrt(random.nextFloat()); + float theta = random.nextFloat() * MHelper.PI2; + float x = pr * (float) Math.cos(theta); + float z = pr * (float) Math.sin(theta); + + POS.set(center.getX() + x, center.getY() + 5, center.getZ() + z); + int down = BlocksHelper.downRay(world, POS, 16); + if (down > 10) continue; + POS.setY(POS.getY() - down); + + if (canGenerate(world, random, center, POS, r)) { + generate(world, random, POS); + } + } + + return true; + } +} diff --git a/src/main/java/ru/betterend/world/features/SinglePlantFeature.java b/src/main/java/ru/betterend/world/features/SinglePlantFeature.java index 47331a95..7d4d34aa 100644 --- a/src/main/java/ru/betterend/world/features/SinglePlantFeature.java +++ b/src/main/java/ru/betterend/world/features/SinglePlantFeature.java @@ -5,62 +5,33 @@ import java.util.Random; import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.BlockPos.Mutable; import net.minecraft.world.StructureWorldAccess; -import net.minecraft.world.gen.chunk.ChunkGenerator; -import net.minecraft.world.gen.feature.DefaultFeatureConfig; import ru.betterend.blocks.basis.BlockDoublePlant; -import ru.betterend.registry.BlockTagRegistry; import ru.betterend.util.BlocksHelper; -import ru.betterend.util.MHelper; -public class SinglePlantFeature extends DefaultFeature { - private static final Mutable POS = new Mutable(); +public class SinglePlantFeature extends ScatterFeature { private final Block plant; - private final int radius; public SinglePlantFeature(Block plant, int radius) { + super(radius); this.plant = plant; - this.radius = radius; } @Override - public boolean generate(StructureWorldAccess world, ChunkGenerator chunkGenerator, Random random, BlockPos blockPos, DefaultFeatureConfig featureConfig) { - blockPos = getPosOnSurface(world, blockPos); - - if (blockPos.getY() < 5) { - return false; + public boolean canGenerate(StructureWorldAccess world, Random random, BlockPos center, BlockPos blockPos, float radius) { + return plant.canPlaceAt(plant.getDefaultState(), world, blockPos); + } + + @Override + public void generate(StructureWorldAccess world, Random random, BlockPos blockPos) { + if (plant instanceof BlockDoublePlant) { + int rot = random.nextInt(4); + BlockState state = plant.getDefaultState().with(BlockDoublePlant.ROTATION, rot); + BlocksHelper.setWithoutUpdate(world, blockPos, state); + BlocksHelper.setWithoutUpdate(world, blockPos.up(), state.with(BlockDoublePlant.TOP, true)); } - if (!world.getBlockState(blockPos.down()).isIn(BlockTagRegistry.END_GROUND)) { - return false; + else { + BlocksHelper.setWithoutUpdate(world, blockPos, plant); } - - float r = MHelper.randRange(radius * 0.5F, radius, random); - int count = MHelper.floor(r * r * MHelper.randRange(1.5F, 3F, random)); - for (int i = 0; i < count; i++) { - float pr = r * (float) Math.sqrt(random.nextFloat()); - float theta = random.nextFloat() * MHelper.PI2; - float x = pr * (float) Math.cos(theta); - float z = pr * (float) Math.sin(theta); - - POS.set(blockPos.getX() + x, blockPos.getY() + 5, blockPos.getZ() + z); - int down = BlocksHelper.downRay(world, POS, 16); - if (down > 10) continue; - POS.setY(POS.getY() - down); - - if (plant.canPlaceAt(plant.getDefaultState(), world, POS)) { - if (plant instanceof BlockDoublePlant) { - int rot = random.nextInt(4); - BlockState state = plant.getDefaultState().with(BlockDoublePlant.ROTATION, rot); - BlocksHelper.setWithoutUpdate(world, POS, state); - BlocksHelper.setWithoutUpdate(world, POS.up(), state.with(BlockDoublePlant.TOP, true)); - } - else { - BlocksHelper.setWithoutUpdate(world, POS, plant); - } - } - } - - return true; } }