Structure scatter
This commit is contained in:
parent
8dfb71478a
commit
8eb15c8d73
4 changed files with 95 additions and 91 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
};
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue