package net.minecraft.world.level.chunk.storage;

import java.util.AbstractList;
import net.minecraft.world.level.chunk.ChunkBiomeContainer;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.nbt.Tag;
import net.minecraft.world.level.chunk.DataLayer;
import net.minecraft.nbt.ListTag;
import net.minecraft.world.level.biome.BiomeSource;
import net.minecraft.world.level.chunk.OldDataLayer;
import net.minecraft.nbt.CompoundTag;

public class OldChunkStorage {
    public static OldLevelChunk load(final CompoundTag le) {
        final int integer2 = le.getInt("xPos");
        final int integer3 = le.getInt("zPos");
        final OldLevelChunk a4 = new OldLevelChunk(integer2, integer3);
        a4.blocks = le.getByteArray("Blocks");
        a4.data = new OldDataLayer(le.getByteArray("Data"), 7);
        a4.skyLight = new OldDataLayer(le.getByteArray("SkyLight"), 7);
        a4.blockLight = new OldDataLayer(le.getByteArray("BlockLight"), 7);
        a4.heightmap = le.getByteArray("HeightMap");
        a4.terrainPopulated = le.getBoolean("TerrainPopulated");
        a4.entities = le.getList("Entities", 10);
        a4.blockEntities = le.getList("TileEntities", 10);
        a4.blockTicks = le.getList("TileTicks", 10);
        try {
            a4.lastUpdated = le.getLong("LastUpdate");
        }
        catch (ClassCastException classCastException5) {
            a4.lastUpdated = le.getInt("LastUpdate");
        }
        return a4;
    }
    
    public static void convertToAnvilFormat(final OldLevelChunk a, final CompoundTag le, final BiomeSource brh) {
        le.putInt("xPos", a.x);
        le.putInt("zPos", a.z);
        le.putLong("LastUpdate", a.lastUpdated);
        final int[] arr4 = new int[a.heightmap.length];
        for (int integer5 = 0; integer5 < a.heightmap.length; ++integer5) {
            arr4[integer5] = a.heightmap[integer5];
        }
        le.putIntArray("HeightMap", arr4);
        le.putBoolean("TerrainPopulated", a.terrainPopulated);
        final ListTag lk5 = new ListTag();
        for (int integer6 = 0; integer6 < 8; ++integer6) {
            boolean boolean7 = true;
            for (int integer7 = 0; integer7 < 16 && boolean7; ++integer7) {
                for (int integer8 = 0; integer8 < 16 && boolean7; ++integer8) {
                    for (int integer9 = 0; integer9 < 16; ++integer9) {
                        final int integer10 = integer7 << 11 | integer9 << 7 | integer8 + (integer6 << 4);
                        final int integer11 = a.blocks[integer10];
                        if (integer11 != 0) {
                            boolean7 = false;
                            break;
                        }
                    }
                }
            }
            if (!boolean7) {
                final byte[] arr5 = new byte[4096];
                final DataLayer chd9 = new DataLayer();
                final DataLayer chd10 = new DataLayer();
                final DataLayer chd11 = new DataLayer();
                for (int integer11 = 0; integer11 < 16; ++integer11) {
                    for (int integer12 = 0; integer12 < 16; ++integer12) {
                        for (int integer13 = 0; integer13 < 16; ++integer13) {
                            final int integer14 = integer11 << 11 | integer13 << 7 | integer12 + (integer6 << 4);
                            final int integer15 = a.blocks[integer14];
                            arr5[integer12 << 8 | integer13 << 4 | integer11] = (byte)(integer15 & 0xFF);
                            chd9.set(integer11, integer12, integer13, a.data.get(integer11, integer12 + (integer6 << 4), integer13));
                            chd10.set(integer11, integer12, integer13, a.skyLight.get(integer11, integer12 + (integer6 << 4), integer13));
                            chd11.set(integer11, integer12, integer13, a.blockLight.get(integer11, integer12 + (integer6 << 4), integer13));
                        }
                    }
                }
                final CompoundTag le2 = new CompoundTag();
                le2.putByte("Y", (byte)(integer6 & 0xFF));
                le2.putByteArray("Blocks", arr5);
                le2.putByteArray("Data", chd9.getData());
                le2.putByteArray("SkyLight", chd10.getData());
                le2.putByteArray("BlockLight", chd11.getData());
                ((AbstractList<CompoundTag>)lk5).add(le2);
            }
        }
        le.put("Sections", lk5);
        le.putIntArray("Biomes", new ChunkBiomeContainer(new ChunkPos(a.x, a.z), brh).writeBiomes());
        le.put("Entities", a.entities);
        le.put("TileEntities", a.blockEntities);
        if (a.blockTicks != null) {
            le.put("TileTicks", a.blockTicks);
        }
        le.putBoolean("convertedFromAlphaFormat", true);
    }
    
    public static class OldLevelChunk {
        public long lastUpdated;
        public boolean terrainPopulated;
        public byte[] heightmap;
        public OldDataLayer blockLight;
        public OldDataLayer skyLight;
        public OldDataLayer data;
        public byte[] blocks;
        public ListTag entities;
        public ListTag blockEntities;
        public ListTag blockTicks;
        public final int x;
        public final int z;
        
        public OldLevelChunk(final int integer1, final int integer2) {
            this.x = integer1;
            this.z = integer2;
        }
    }
}
