package net.minecraft.world.level.levelgen.structure;

import net.minecraft.world.level.levelgen.Heightmap;
import net.minecraft.core.Vec3i;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.core.Direction;
import java.util.Random;
import net.minecraft.world.level.levelgen.feature.StructurePieceType;

public abstract class ScatteredFeaturePiece extends StructurePiece {
    protected final int width;
    protected final int height;
    protected final int depth;
    protected int heightPosition;
    
    protected ScatteredFeaturePiece(final StructurePieceType cfm, final Random random, final int integer3, final int integer4, final int integer5, final int integer6, final int integer7, final int integer8) {
        super(cfm, 0);
        this.heightPosition = -1;
        this.width = integer6;
        this.height = integer7;
        this.depth = integer8;
        this.setOrientation(Direction.Plane.HORIZONTAL.getRandomDirection(random));
        if (this.getOrientation().getAxis() == Direction.Axis.Z) {
            this.boundingBox = new BoundingBox(integer3, integer4, integer5, integer3 + integer6 - 1, integer4 + integer7 - 1, integer5 + integer8 - 1);
        }
        else {
            this.boundingBox = new BoundingBox(integer3, integer4, integer5, integer3 + integer8 - 1, integer4 + integer7 - 1, integer5 + integer6 - 1);
        }
    }
    
    protected ScatteredFeaturePiece(final StructurePieceType cfm, final CompoundTag jt) {
        super(cfm, jt);
        this.heightPosition = -1;
        this.width = jt.getInt("Width");
        this.height = jt.getInt("Height");
        this.depth = jt.getInt("Depth");
        this.heightPosition = jt.getInt("HPos");
    }
    
    @Override
    protected void addAdditionalSaveData(final CompoundTag jt) {
        jt.putInt("Width", this.width);
        jt.putInt("Height", this.height);
        jt.putInt("Depth", this.depth);
        jt.putInt("HPos", this.heightPosition);
    }
    
    protected boolean updateAverageGroundHeight(final LevelAccessor bjx, final BoundingBox clb, final int integer) {
        if (this.heightPosition >= 0) {
            return true;
        }
        int integer2 = 0;
        int integer3 = 0;
        final BlockPos.MutableBlockPos a7 = new BlockPos.MutableBlockPos();
        for (int integer4 = this.boundingBox.z0; integer4 <= this.boundingBox.z1; ++integer4) {
            for (int integer5 = this.boundingBox.x0; integer5 <= this.boundingBox.x1; ++integer5) {
                a7.set(integer5, 64, integer4);
                if (clb.isInside(a7)) {
                    integer2 += bjx.getHeightmapPos(Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, a7).getY();
                    ++integer3;
                }
            }
        }
        if (integer3 == 0) {
            return false;
        }
        this.heightPosition = integer2 / integer3;
        this.boundingBox.move(0, this.heightPosition - this.boundingBox.y0 + integer, 0);
        return true;
    }
}
