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

import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.levelgen.Heightmap;
import net.minecraft.core.Vec3i;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.chunk.ChunkGenerator;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.storage.loot.BuiltInLootTables;
import net.minecraft.world.level.block.entity.ChestBlockEntity;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureProcessor;
import net.minecraft.world.level.levelgen.structure.templatesystem.BlockIgnoreProcessor;
import net.minecraft.world.level.block.Mirror;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructurePlaceSettings;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.levelgen.feature.StructurePieceType;
import com.google.common.collect.ImmutableMap;
import net.minecraft.world.level.levelgen.feature.configurations.NoneFeatureConfiguration;
import java.util.Random;
import java.util.List;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureManager;
import net.minecraft.core.BlockPos;
import java.util.Map;
import net.minecraft.resources.ResourceLocation;

public class IglooPieces {
    private static final ResourceLocation STRUCTURE_LOCATION_IGLOO;
    private static final ResourceLocation STRUCTURE_LOCATION_LADDER;
    private static final ResourceLocation STRUCTURE_LOCATION_LABORATORY;
    private static final Map<ResourceLocation, BlockPos> PIVOTS;
    private static final Map<ResourceLocation, BlockPos> OFFSETS;
    
    public static void addPieces(final StructureManager cmo, final BlockPos fk, final Rotation btu, final List<StructurePiece> list, final Random random, final NoneFeatureConfiguration cgv) {
        if (random.nextDouble() < 0.5) {
            final int integer7 = random.nextInt(8) + 4;
            list.add(new IglooPiece(cmo, IglooPieces.STRUCTURE_LOCATION_LABORATORY, fk, btu, integer7 * 3));
            for (int integer8 = 0; integer8 < integer7 - 1; ++integer8) {
                list.add(new IglooPiece(cmo, IglooPieces.STRUCTURE_LOCATION_LADDER, fk, btu, integer8 * 3));
            }
        }
        list.add(new IglooPiece(cmo, IglooPieces.STRUCTURE_LOCATION_IGLOO, fk, btu, 0));
    }
    
    static {
        STRUCTURE_LOCATION_IGLOO = new ResourceLocation("igloo/top");
        STRUCTURE_LOCATION_LADDER = new ResourceLocation("igloo/middle");
        STRUCTURE_LOCATION_LABORATORY = new ResourceLocation("igloo/bottom");
        PIVOTS = ImmutableMap.<ResourceLocation, BlockPos>of(IglooPieces.STRUCTURE_LOCATION_IGLOO, new BlockPos(3, 5, 5), IglooPieces.STRUCTURE_LOCATION_LADDER, new BlockPos(1, 3, 1), IglooPieces.STRUCTURE_LOCATION_LABORATORY, new BlockPos(3, 6, 7));
        OFFSETS = ImmutableMap.<ResourceLocation, BlockPos>of(IglooPieces.STRUCTURE_LOCATION_IGLOO, BlockPos.ZERO, IglooPieces.STRUCTURE_LOCATION_LADDER, new BlockPos(2, -3, 4), IglooPieces.STRUCTURE_LOCATION_LABORATORY, new BlockPos(0, -3, -2));
    }
    
    public static class IglooPiece extends TemplateStructurePiece {
        private final ResourceLocation templateLocation;
        private final Rotation rotation;
        
        public IglooPiece(final StructureManager cmo, final ResourceLocation sm, final BlockPos fk, final Rotation btu, final int integer) {
            super(StructurePieceType.IGLOO, 0);
            this.templateLocation = sm;
            final BlockPos fk2 = IglooPieces.OFFSETS.get(sm);
            this.templatePosition = fk.offset(fk2.getX(), fk2.getY() - integer, fk2.getZ());
            this.rotation = btu;
            this.loadTemplate(cmo);
        }
        
        public IglooPiece(final StructureManager cmo, final CompoundTag jt) {
            super(StructurePieceType.IGLOO, jt);
            this.templateLocation = new ResourceLocation(jt.getString("Template"));
            this.rotation = Rotation.valueOf(jt.getString("Rot"));
            this.loadTemplate(cmo);
        }
        
        private void loadTemplate(final StructureManager cmo) {
            final StructureTemplate cms3 = cmo.getOrCreate(this.templateLocation);
            final StructurePlaceSettings cmp4 = new StructurePlaceSettings().setRotation(this.rotation).setMirror(Mirror.NONE).setRotationPivot(IglooPieces.PIVOTS.get(this.templateLocation)).addProcessor(BlockIgnoreProcessor.STRUCTURE_BLOCK);
            this.setup(cms3, this.templatePosition, cmp4);
        }
        
        @Override
        protected void addAdditionalSaveData(final CompoundTag jt) {
            super.addAdditionalSaveData(jt);
            jt.putString("Template", this.templateLocation.toString());
            jt.putString("Rot", this.rotation.name());
        }
        
        @Override
        protected void handleDataMarker(final String string, final BlockPos fk, final LevelAccessor bjx, final Random random, final BoundingBox clb) {
            if (!"chest".equals(string)) {
                return;
            }
            bjx.setBlock(fk, Blocks.AIR.defaultBlockState(), 3);
            final BlockEntity bwl7 = bjx.getBlockEntity(fk.below());
            if (bwl7 instanceof ChestBlockEntity) {
                ((ChestBlockEntity)bwl7).setLootTable(BuiltInLootTables.IGLOO_CHEST, random.nextLong());
            }
        }
        
        @Override
        public boolean postProcess(final LevelAccessor bjx, final ChunkGenerator<?> caa, final Random random, final BoundingBox clb, final ChunkPos bjh) {
            final StructurePlaceSettings cmp7 = new StructurePlaceSettings().setRotation(this.rotation).setMirror(Mirror.NONE).setRotationPivot(IglooPieces.PIVOTS.get(this.templateLocation)).addProcessor(BlockIgnoreProcessor.STRUCTURE_BLOCK);
            final BlockPos fk8 = IglooPieces.OFFSETS.get(this.templateLocation);
            final BlockPos fk9 = this.templatePosition.offset(StructureTemplate.calculateRelativePosition(cmp7, new BlockPos(3 - fk8.getX(), 0, 0 - fk8.getZ())));
            final int integer10 = bjx.getHeight(Heightmap.Types.WORLD_SURFACE_WG, fk9.getX(), fk9.getZ());
            final BlockPos fk10 = this.templatePosition;
            this.templatePosition = this.templatePosition.offset(0, integer10 - 90 - 1, 0);
            final boolean boolean12 = super.postProcess(bjx, caa, random, clb, bjh);
            if (this.templateLocation.equals(IglooPieces.STRUCTURE_LOCATION_IGLOO)) {
                final BlockPos fk11 = this.templatePosition.offset(StructureTemplate.calculateRelativePosition(cmp7, new BlockPos(3, 0, 5)));
                final BlockState byj14 = bjx.getBlockState(fk11.below());
                if (!byj14.isAir() && byj14.getBlock() != Blocks.LADDER) {
                    bjx.setBlock(fk11, Blocks.SNOW_BLOCK.defaultBlockState(), 3);
                }
            }
            this.templatePosition = fk10;
            return boolean12;
        }
    }
}
