| 1 | package net.minecraft.world.level.levelgen.structure;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.nbt.CompoundTag;
|
| 6 | import net.minecraft.world.level.LevelAccessor;
|
| 7 | import net.minecraft.world.level.levelgen.Heightmap;
|
| 8 | import net.minecraft.world.level.levelgen.structure.pieces.StructurePieceSerializationContext;
|
| 9 | import net.minecraft.world.level.levelgen.structure.pieces.StructurePieceType;
|
| 10 |
|
| 11 | public abstract class ScatteredFeaturePiece extends StructurePiece {
|
| 12 | protected final int width;
|
| 13 | protected final int height;
|
| 14 | protected final int depth;
|
| 15 | protected int heightPosition = -1;
|
| 16 |
|
| 17 | protected ScatteredFeaturePiece(
|
| 18 | final StructurePieceType type,
|
| 19 | final int west,
|
| 20 | final int floor,
|
| 21 | final int north,
|
| 22 | final int width,
|
| 23 | final int height,
|
| 24 | final int depth,
|
| 25 | final Direction direction
|
| 26 | ) {
|
| 27 | super(type, 0, StructurePiece.makeBoundingBox(west, floor, north, direction, width, height, depth));
|
| 28 | this.width = width;
|
| 29 | this.height = height;
|
| 30 | this.depth = depth;
|
| 31 | this.setOrientation(direction);
|
| 32 | }
|
| 33 |
|
| 34 | protected ScatteredFeaturePiece(final StructurePieceType type, final CompoundTag tag) {
|
| 35 | super(type, tag);
|
| 36 | this.width = tag.getIntOr("Width", 0);
|
| 37 | this.height = tag.getIntOr("Height", 0);
|
| 38 | this.depth = tag.getIntOr("Depth", 0);
|
| 39 | this.heightPosition = tag.getIntOr("HPos", 0);
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | protected void addAdditionalSaveData(final StructurePieceSerializationContext context, final CompoundTag tag) {
|
| 44 | tag.putInt("Width", this.width);
|
| 45 | tag.putInt("Height", this.height);
|
| 46 | tag.putInt("Depth", this.depth);
|
| 47 | tag.putInt("HPos", this.heightPosition);
|
| 48 | }
|
| 49 |
|
| 50 | protected boolean updateAverageGroundHeight(final LevelAccessor level, final BoundingBox chunkBB, final int offset) {
|
| 51 | if (this.heightPosition >= 0) {
|
| 52 | return true;
|
| 53 | }
|
| 54 |
|
| 55 | int total = 0;
|
| 56 | int count = 0;
|
| 57 | BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
|
| 58 |
|
| 59 | for (int z = this.boundingBox.minZ(); z <= this.boundingBox.maxZ(); z++) {
|
| 60 | for (int x = this.boundingBox.minX(); x <= this.boundingBox.maxX(); x++) {
|
| 61 | pos.set(x, 64, z);
|
| 62 | if (chunkBB.isInside(pos)) {
|
| 63 | total += level.getHeightmapPos(Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, pos).getY();
|
| 64 | count++;
|
| 65 | }
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | if (count == 0) {
|
| 70 | return false;
|
| 71 | }
|
| 72 |
|
| 73 | this.heightPosition = total / count;
|
| 74 | this.boundingBox.move(0, this.heightPosition - this.boundingBox.minY() + offset, 0);
|
| 75 | return true;
|
| 76 | }
|
| 77 |
|
| 78 | protected boolean updateHeightPositionToLowestGroundHeight(final LevelAccessor level, final int offset) {
|
| 79 | if (this.heightPosition >= 0) {
|
| 80 | return true;
|
| 81 | }
|
| 82 |
|
| 83 | int lowestGroundHeight = level.getMaxY() + 1;
|
| 84 | boolean foundPositionWithinBoundingBox = false;
|
| 85 | BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
|
| 86 |
|
| 87 | for (int z = this.boundingBox.minZ(); z <= this.boundingBox.maxZ(); z++) {
|
| 88 | for (int x = this.boundingBox.minX(); x <= this.boundingBox.maxX(); x++) {
|
| 89 | pos.set(x, 0, z);
|
| 90 | lowestGroundHeight = Math.min(lowestGroundHeight, level.getHeightmapPos(Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, pos).getY());
|
| 91 | foundPositionWithinBoundingBox = true;
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | if (!foundPositionWithinBoundingBox) {
|
| 96 | return false;
|
| 97 | }
|
| 98 |
|
| 99 | this.heightPosition = lowestGroundHeight;
|
| 100 | this.boundingBox.move(0, this.heightPosition - this.boundingBox.minY() + offset, 0);
|
| 101 | return true;
|
| 102 | }
|
| 103 | }
|
| 104 |
|