Structure scatter

This commit is contained in:
paulevsGitch 2020-10-03 00:11:18 +03:00
parent 8dfb71478a
commit 8eb15c8d73
4 changed files with 95 additions and 91 deletions

View file

@ -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;
}
}