Fixed structure features and code style

This commit is contained in:
paulevsGitch 2021-07-10 16:07:44 +03:00
parent d431f2555c
commit 5a9365e2bb
153 changed files with 2304 additions and 2459 deletions

View file

@ -8,7 +8,7 @@ import java.lang.reflect.Field;
public class BCLDecorators {
public static final ConfiguredDecorator<?> HEIGHTMAP_SQUARE;
private static final ConfiguredDecorator<?> getDecorator(Field[] fields, int index) {
try {
return (ConfiguredDecorator<?>) fields[index].get(null);
@ -18,7 +18,7 @@ public class BCLDecorators {
return null;
}
}
static {
Class<?>[] classes = Features.class.getDeclaredClasses();
Field[] fields = classes[1].getDeclaredFields(); // Decorators class

View file

@ -2,9 +2,7 @@ package ru.bclib.world.features;
import net.minecraft.core.Registry;
import net.minecraft.data.BuiltinRegistries;
import net.minecraft.data.worldgen.Features;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.levelgen.GenerationStep;
@ -15,7 +13,6 @@ import net.minecraft.world.level.levelgen.feature.configurations.CountConfigurat
import net.minecraft.world.level.levelgen.feature.configurations.FeatureConfiguration;
import net.minecraft.world.level.levelgen.feature.configurations.NoneFeatureConfiguration;
import net.minecraft.world.level.levelgen.feature.configurations.OreConfiguration;
import net.minecraft.world.level.levelgen.feature.configurations.RangeDecoratorConfiguration;
import net.minecraft.world.level.levelgen.placement.ChanceDecoratorConfiguration;
import net.minecraft.world.level.levelgen.placement.FeatureDecorator;
import net.minecraft.world.level.levelgen.structure.templatesystem.BlockMatchTest;
@ -28,7 +25,7 @@ public class BCLFeature {
private ConfiguredFeature<?, ?> featureConfigured;
private GenerationStep.Decoration featureStep;
private Feature<?> feature;
public BCLFeature(Feature<?> feature, ConfiguredFeature<?, ?> configuredFeature, GenerationStep.Decoration featureStep) {
this.featureConfigured = configuredFeature;
this.featureStep = featureStep;
@ -59,10 +56,7 @@ public class BCLFeature {
public static BCLFeature makeOreFeature(ResourceLocation id, Block blockOre, int veins, int veinSize, int offset, int minY, int maxY) {
OreConfiguration featureConfig = new OreConfiguration(new BlockMatchTest(Blocks.END_STONE), blockOre.defaultBlockState(), veinSize);
OreConfiguration config = new OreConfiguration(ANY_TERRAIN, blockOre.defaultBlockState(), 33);
ConfiguredFeature<?, ?> oreFeature = Feature.ORE.configured(featureConfig)
.rangeUniform(VerticalAnchor.absolute(minY), VerticalAnchor.absolute(maxY))
.squared()
.count(veins);
ConfiguredFeature<?, ?> oreFeature = Feature.ORE.configured(featureConfig).rangeUniform(VerticalAnchor.absolute(minY), VerticalAnchor.absolute(maxY)).squared().count(veins);
return new BCLFeature(Feature.ORE, Registry.register(BuiltinRegistries.CONFIGURED_FEATURE, id, oreFeature), GenerationStep.Decoration.UNDERGROUND_ORES);
}
@ -89,11 +83,11 @@ public class BCLFeature {
public Feature<?> getFeature() {
return feature;
}
public ConfiguredFeature<?, ?> getFeatureConfigured() {
return featureConfigured;
}
public GenerationStep.Decoration getFeatureStep() {
return featureStep;
}

View file

@ -1,8 +1,5 @@
package ru.bclib.world.features;
import java.util.List;
import java.util.Random;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.WorldGenLevel;
import net.minecraft.world.level.block.Mirror;
@ -11,6 +8,9 @@ import net.minecraft.world.level.levelgen.structure.templatesystem.StructurePlac
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
import ru.bclib.util.StructureHelper;
import java.util.List;
import java.util.Random;
public class ListFeature extends NBTStructureFeature {
private final List<StructureInfo> list;
private StructureInfo selected;
@ -24,29 +24,29 @@ public class ListFeature extends NBTStructureFeature {
selected = list.get(random.nextInt(list.size()));
return selected.getStructure();
}
@Override
protected boolean canSpawn(WorldGenLevel world, BlockPos pos, Random random) {
int cx = pos.getX() >> 4;
int cz = pos.getZ() >> 4;
return ((cx + cz) & 1) == 0 && pos.getY() > 58;// && world.getBlockState(pos.below()).is(EndTags.GEN_TERRAIN);
}
@Override
protected Rotation getRotation(WorldGenLevel world, BlockPos pos, Random random) {
return Rotation.getRandom(random);
}
@Override
protected Mirror getMirror(WorldGenLevel world, BlockPos pos, Random random) {
return Mirror.values()[random.nextInt(3)];
}
@Override
protected int getYOffset(StructureTemplate structure, WorldGenLevel world, BlockPos pos, Random random) {
return selected.offsetY;
}
@Override
protected TerrainMerge getTerrainMerge(WorldGenLevel world, BlockPos pos, Random random) {
return selected.terrainMerge;

View file

@ -1,9 +1,5 @@
package ru.bclib.world.features;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;
import net.minecraft.core.BlockPos;
import net.minecraft.core.BlockPos.MutableBlockPos;
import net.minecraft.core.Direction;
@ -17,36 +13,38 @@ import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.block.Mirror;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.chunk.ChunkGenerator;
import net.minecraft.world.level.levelgen.feature.FeaturePlaceContext;
import net.minecraft.world.level.levelgen.feature.configurations.NoneFeatureConfiguration;
import net.minecraft.world.level.levelgen.structure.BoundingBox;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructurePlaceSettings;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
import net.minecraft.world.level.levelgen.surfacebuilders.SurfaceBuilderConfiguration;
import net.minecraft.world.phys.Vec3;
import ru.bclib.api.BiomeAPI;
import ru.bclib.api.TagAPI;
import ru.bclib.util.BlocksHelper;
import ru.bclib.world.processors.DestructionStructureProcessor;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;
public abstract class NBTStructureFeature extends DefaultFeature {
protected static final DestructionStructureProcessor DESTRUCTION = new DestructionStructureProcessor();
protected abstract StructureTemplate getStructure(WorldGenLevel world, BlockPos pos, Random random);
protected abstract boolean canSpawn(WorldGenLevel world, BlockPos pos, Random random);
protected abstract Rotation getRotation(WorldGenLevel world, BlockPos pos, Random random);
protected abstract Mirror getMirror(WorldGenLevel world, BlockPos pos, Random random);
protected abstract int getYOffset(StructureTemplate structure, WorldGenLevel world, BlockPos pos, Random random);
protected abstract TerrainMerge getTerrainMerge(WorldGenLevel world, BlockPos pos, Random random);
protected abstract void addStructureData(StructurePlaceSettings data);
protected BlockPos getGround(WorldGenLevel world, BlockPos center) {
Biome biome = world.getBiome(center);
ResourceLocation id = BiomeAPI.getBiomeID(biome);
@ -59,7 +57,7 @@ public abstract class NBTStructureFeature extends DefaultFeature {
return new BlockPos(center.getX(), y, center.getZ());
}
}
protected int getAverageY(WorldGenLevel world, BlockPos center) {
int y = getYOnSurface(world, center.getX(), center.getZ());
y += getYOnSurface(world, center.getX() - 2, center.getZ() - 2);
@ -68,7 +66,7 @@ public abstract class NBTStructureFeature extends DefaultFeature {
y += getYOnSurface(world, center.getX() + 2, center.getZ() + 2);
return y / 5;
}
protected int getAverageYWG(WorldGenLevel world, BlockPos center) {
int y = getYOnSurfaceWG(world, center.getX(), center.getZ());
y += getYOnSurfaceWG(world, center.getX() - 2, center.getZ() - 2);
@ -77,33 +75,33 @@ public abstract class NBTStructureFeature extends DefaultFeature {
y += getYOnSurfaceWG(world, center.getX() + 2, center.getZ() + 2);
return y / 5;
}
@Override
public boolean place(FeaturePlaceContext<NoneFeatureConfiguration> context) {
WorldGenLevel world = context.level();
Random random = context.random();
BlockPos center = context.origin();
center = new BlockPos(((center.getX() >> 4) << 4) | 8, 128, ((center.getZ() >> 4) << 4) | 8);
center = getGround(world, center);
if (!canSpawn(world, center, random)) {
return false;
}
int posY = center.getY() + 1;
StructureTemplate structure = getStructure(world, center, random);
Rotation rotation = getRotation(world, center, random);
Mirror mirror = getMirror(world, center, random);
BlockPos offset = StructureTemplate.transform(new BlockPos(structure.getSize()), mirror, rotation, BlockPos.ZERO);
center = center.offset(0, getYOffset(structure, world, center, random) + 0.5, 0);
BoundingBox bounds = makeBox(center);
StructurePlaceSettings placementData = new StructurePlaceSettings().setRotation(rotation).setMirror(mirror).setBoundingBox(bounds);
addStructureData(placementData);
center = center.offset(-offset.getX() * 0.5, 0, -offset.getZ() * 0.5);
structure.placeInWorld(world, center, center, placementData, random, 4);
TerrainMerge merge = getTerrainMerge(world, center, random);
int x1 = center.getX();
int z1 = center.getZ();
@ -111,19 +109,19 @@ public abstract class NBTStructureFeature extends DefaultFeature {
int z2 = z1 + offset.getZ();
if (merge != TerrainMerge.NONE) {
MutableBlockPos mut = new MutableBlockPos();
if (x2 < x1) {
int a = x1;
x1 = x2;
x2 = a;
}
if (z2 < z1) {
int a = z1;
z1 = z2;
z2 = a;
}
int surfMax = posY - 1;
for (int x = x1; x <= x2; x++) {
mut.setX(x);
@ -149,8 +147,7 @@ public abstract class NBTStructureFeature extends DefaultFeature {
else {
if (stateSt.is(TagAPI.END_GROUND) && state.getMaterial().isSolidBlocking()) {
if (merge == TerrainMerge.SURFACE) {
SurfaceBuilderConfiguration config = world.getBiome(mut).getGenerationSettings()
.getSurfaceBuilderConfig();
SurfaceBuilderConfiguration config = world.getBiome(mut).getGenerationSettings().getSurfaceBuilderConfig();
BlocksHelper.setWithoutUpdate(world, mut, config.getUnderMaterial());
}
else {
@ -165,10 +162,10 @@ public abstract class NBTStructureFeature extends DefaultFeature {
}
}
//BlocksHelper.fixBlocks(world, new BlockPos(x1, center.getY(), z1), new BlockPos(x2, center.getY() + offset.getY(), z2));
return true;
}
protected BoundingBox makeBox(BlockPos pos) {
int sx = ((pos.getX() >> 4) << 4) - 16;
int sz = ((pos.getZ() >> 4) << 4) - 16;
@ -176,35 +173,34 @@ public abstract class NBTStructureFeature extends DefaultFeature {
int ez = sz + 47;
return BoundingBox.fromCorners(new Vec3i(sx, 0, sz), new Vec3i(ex, 255, ez));
}
protected static StructureTemplate readStructure(ResourceLocation resource) {
String ns = resource.getNamespace();
String nm = resource.getPath();
try {
InputStream inputstream = MinecraftServer.class
.getResourceAsStream("/data/" + ns + "/structures/" + nm + ".nbt");
InputStream inputstream = MinecraftServer.class.getResourceAsStream("/data/" + ns + "/structures/" + nm + ".nbt");
return readStructureFromStream(inputstream);
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
private static StructureTemplate readStructureFromStream(InputStream stream) throws IOException {
CompoundTag nbttagcompound = NbtIo.readCompressed(stream);
StructureTemplate template = new StructureTemplate();
template.load(nbttagcompound);
return template;
}
public static enum TerrainMerge {
NONE, SURFACE, OBJECT;
public static TerrainMerge getFromString(String type) {
if (type.equals("surface")) {
return SURFACE;