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

import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.LevelAccessor;
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.StructureFeatureManager;
import net.minecraft.world.level.WorldGenLevel;
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 fu) {
            super(StructurePieceType.BURIED_TREASURE_PIECE, 0);
            this.boundingBox = new BoundingBox(fu.getX(), fu.getY(), fu.getZ(), fu.getX(), fu.getY(), fu.getZ());
        }
        
        public BuriedTreasurePiece(final StructureManager cva, final CompoundTag le) {
            super(StructurePieceType.BURIED_TREASURE_PIECE, le);
        }
        
        @Override
        protected void addAdditionalSaveData(final CompoundTag le) {
        }
        
        @Override
        public boolean postProcess(final WorldGenLevel bqu, final StructureFeatureManager bqq, final ChunkGenerator cha, final Random random, final BoundingBox ctd, final ChunkPos bph, final BlockPos fu) {
            final int integer9 = bqu.getHeight(Heightmap.Types.OCEAN_FLOOR_WG, this.boundingBox.x0, this.boundingBox.z0);
            final BlockPos.MutableBlockPos a10 = new BlockPos.MutableBlockPos(this.boundingBox.x0, integer9, this.boundingBox.z0);
            while (a10.getY() > 0) {
                final BlockState cfj11 = bqu.getBlockState(a10);
                final BlockState cfj12 = bqu.getBlockState(a10.below());
                if (cfj12 == Blocks.SANDSTONE.defaultBlockState() || cfj12 == Blocks.STONE.defaultBlockState() || cfj12 == Blocks.ANDESITE.defaultBlockState() || cfj12 == Blocks.GRANITE.defaultBlockState() || cfj12 == Blocks.DIORITE.defaultBlockState()) {
                    final BlockState cfj13 = (cfj11.isAir() || this.isLiquid(cfj11)) ? Blocks.SAND.defaultBlockState() : cfj11;
                    for (final Direction fz17 : Direction.values()) {
                        final BlockPos fu2 = a10.relative(fz17);
                        final BlockState cfj14 = bqu.getBlockState(fu2);
                        if (cfj14.isAir() || this.isLiquid(cfj14)) {
                            final BlockPos fu3 = fu2.below();
                            final BlockState cfj15 = bqu.getBlockState(fu3);
                            if ((cfj15.isAir() || this.isLiquid(cfj15)) && fz17 != Direction.UP) {
                                bqu.setBlock(fu2, cfj12, 3);
                            }
                            else {
                                bqu.setBlock(fu2, cfj13, 3);
                            }
                        }
                    }
                    this.boundingBox = new BoundingBox(a10.getX(), a10.getY(), a10.getZ(), a10.getX(), a10.getY(), a10.getZ());
                    return this.createChest(bqu, ctd, random, a10, BuiltInLootTables.BURIED_TREASURE, null);
                }
                a10.move(0, -1, 0);
            }
            return false;
        }
        
        private boolean isLiquid(final BlockState cfj) {
            return cfj == Blocks.WATER.defaultBlockState() || cfj == Blocks.LAVA.defaultBlockState();
        }
    }
}
