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

import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.loot.BuiltInLootTables;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.levelgen.Heightmap;
import net.minecraft.world.level.ChunkPos;
import java.util.Random;
import net.minecraft.world.level.chunk.ChunkGenerator;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureManager;
import net.minecraft.world.level.levelgen.feature.StructurePieceType;
import net.minecraft.core.BlockPos;

public class BuriedTreasurePieces {
    public static class BuriedTreasurePiece extends StructurePiece {
        public BuriedTreasurePiece(final BlockPos fk) {
            super(StructurePieceType.BURIED_TREASURE_PIECE, 0);
            this.boundingBox = new BoundingBox(fk.getX(), fk.getY(), fk.getZ(), fk.getX(), fk.getY(), fk.getZ());
        }
        
        public BuriedTreasurePiece(final StructureManager cmo, final CompoundTag jt) {
            super(StructurePieceType.BURIED_TREASURE_PIECE, jt);
        }
        
        @Override
        protected void addAdditionalSaveData(final CompoundTag jt) {
        }
        
        @Override
        public boolean postProcess(final LevelAccessor bjx, final ChunkGenerator<?> caa, final Random random, final BoundingBox clb, final ChunkPos bjh) {
            final int integer7 = bjx.getHeight(Heightmap.Types.OCEAN_FLOOR_WG, this.boundingBox.x0, this.boundingBox.z0);
            final BlockPos.MutableBlockPos a8 = new BlockPos.MutableBlockPos(this.boundingBox.x0, integer7, this.boundingBox.z0);
            while (a8.getY() > 0) {
                final BlockState byj9 = bjx.getBlockState(a8);
                final BlockState byj10 = bjx.getBlockState(a8.below());
                if (byj10 == Blocks.SANDSTONE.defaultBlockState() || byj10 == Blocks.STONE.defaultBlockState() || byj10 == Blocks.ANDESITE.defaultBlockState() || byj10 == Blocks.GRANITE.defaultBlockState() || byj10 == Blocks.DIORITE.defaultBlockState()) {
                    final BlockState byj11 = (byj9.isAir() || this.isLiquid(byj9)) ? Blocks.SAND.defaultBlockState() : byj9;
                    for (final Direction fp15 : Direction.values()) {
                        final BlockPos fk16 = a8.relative(fp15);
                        final BlockState byj12 = bjx.getBlockState(fk16);
                        if (byj12.isAir() || this.isLiquid(byj12)) {
                            final BlockPos fk17 = fk16.below();
                            final BlockState byj13 = bjx.getBlockState(fk17);
                            if ((byj13.isAir() || this.isLiquid(byj13)) && fp15 != Direction.UP) {
                                bjx.setBlock(fk16, byj10, 3);
                            }
                            else {
                                bjx.setBlock(fk16, byj11, 3);
                            }
                        }
                    }
                    this.boundingBox = new BoundingBox(a8.getX(), a8.getY(), a8.getZ(), a8.getX(), a8.getY(), a8.getZ());
                    return this.createChest(bjx, clb, random, a8, BuiltInLootTables.BURIED_TREASURE, null);
                }
                a8.move(0, -1, 0);
            }
            return false;
        }
        
        private boolean isLiquid(final BlockState byj) {
            return byj == Blocks.WATER.defaultBlockState() || byj == Blocks.LAVA.defaultBlockState();
        }
    }
}
