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 jt) {
        final int integer2 = jt.getInt("xPos");
        final int integer3 = jt.getInt("zPos");
        final OldLevelChunk a4 = new OldLevelChunk(integer2, integer3);
        a4.blocks = jt.getByteArray("Blocks");
        a4.data = new OldDataLayer(jt.getByteArray("Data"), 7);
        a4.skyLight = new OldDataLayer(jt.getByteArray("SkyLight"), 7);
        a4.blockLight = new OldDataLayer(jt.getByteArray("BlockLight"), 7);
        a4.heightmap = jt.getByteArray("HeightMap");
        a4.terrainPopulated = jt.getBoolean("TerrainPopulated");
        a4.entities = jt.getList("Entities", 10);
        a4.blockEntities = jt.getList("TileEntities", 10);
        a4.blockTicks = jt.getList("TileTicks", 10);
        try {
            a4.lastUpdated = jt.getLong("LastUpdate");
        }
        catch (ClassCastException classCastException5) {
            a4.lastUpdated = jt.getInt("LastUpdate");
        }
        return a4;
    }
    
    public static void convertToAnvilFormat(final OldLevelChunk a, final CompoundTag jt, final BiomeSource bkw) {
        jt.putInt("xPos", a.x);
        jt.putInt("zPos", a.z);
        jt.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];
        }
        jt.putIntArray("HeightMap", arr4);
        jt.putBoolean("TerrainPopulated", a.terrainPopulated);
        final ListTag jz5 = 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 caf9 = new DataLayer();
                final DataLayer caf10 = new DataLayer();
                final DataLayer caf11 = 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);
                            caf9.set(integer11, integer12, integer13, a.data.get(integer11, integer12 + (integer6 << 4), integer13));
                            caf10.set(integer11, integer12, integer13, a.skyLight.get(integer11, integer12 + (integer6 << 4), integer13));
                            caf11.set(integer11, integer12, integer13, a.blockLight.get(integer11, integer12 + (integer6 << 4), integer13));
                        }
                    }
                }
                final CompoundTag jt2 = new CompoundTag();
                jt2.putByte("Y", (byte)(integer6 & 0xFF));
                jt2.putByteArray("Blocks", arr5);
                jt2.putByteArray("Data", caf9.getData());
                jt2.putByteArray("SkyLight", caf10.getData());
                jt2.putByteArray("BlockLight", caf11.getData());
                ((AbstractList<CompoundTag>)jz5).add(jt2);
            }
        }
        jt.put("Sections", jz5);
        jt.putIntArray("Biomes", new ChunkBiomeContainer(new ChunkPos(a.x, a.z), bkw).writeBiomes());
        jt.put("Entities", a.entities);
        jt.put("TileEntities", a.blockEntities);
        if (a.blockTicks != null) {
            jt.put("TileTicks", a.blockTicks);
        }
        jt.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;
        }
    }
}
