    001package net.minecraft.world.chunk.storage;
    002
    003import net.minecraft.nbt.NBTTagCompound;
    004import net.minecraft.nbt.NBTTagList;
    005import net.minecraft.world.biome.WorldChunkManager;
    006import net.minecraft.world.chunk.NibbleArray;
    007
    008public class ChunkLoader
    009{
    010    public static AnvilConverterData load(NBTTagCompound par0NBTTagCompound)
    011    {
    012        int i = par0NBTTagCompound.getInteger("xPos");
    013        int j = par0NBTTagCompound.getInteger("zPos");
    014        AnvilConverterData anvilconverterdata = new AnvilConverterData(i, j);
    015        anvilconverterdata.blocks = par0NBTTagCompound.getByteArray("Blocks");
    016        anvilconverterdata.data = new NibbleArrayReader(par0NBTTagCompound.getByteArray("Data"), 7);
    017        anvilconverterdata.skyLight = new NibbleArrayReader(par0NBTTagCompound.getByteArray("SkyLight"), 7);
    018        anvilconverterdata.blockLight = new NibbleArrayReader(par0NBTTagCompound.getByteArray("BlockLight"), 7);
    019        anvilconverterdata.heightmap = par0NBTTagCompound.getByteArray("HeightMap");
    020        anvilconverterdata.terrainPopulated = par0NBTTagCompound.getBoolean("TerrainPopulated");
    021        anvilconverterdata.entities = par0NBTTagCompound.getTagList("Entities");
    022        anvilconverterdata.tileEntities = par0NBTTagCompound.getTagList("TileEntities");
    023        anvilconverterdata.tileTicks = par0NBTTagCompound.getTagList("TileTicks");
    024
    025        try
    026        {
    027            anvilconverterdata.lastUpdated = par0NBTTagCompound.getLong("LastUpdate");
    028        }
    029        catch (ClassCastException classcastexception)
    030        {
    031            anvilconverterdata.lastUpdated = (long)par0NBTTagCompound.getInteger("LastUpdate");
    032        }
    033
    034        return anvilconverterdata;
    035    }
    036
    037    public static void convertToAnvilFormat(AnvilConverterData par0AnvilConverterData, NBTTagCompound par1NBTTagCompound, WorldChunkManager par2WorldChunkManager)
    038    {
    039        par1NBTTagCompound.setInteger("xPos", par0AnvilConverterData.x);
    040        par1NBTTagCompound.setInteger("zPos", par0AnvilConverterData.z);
    041        par1NBTTagCompound.setLong("LastUpdate", par0AnvilConverterData.lastUpdated);
    042        int[] aint = new int[par0AnvilConverterData.heightmap.length];
    043
    044        for (int i = 0; i < par0AnvilConverterData.heightmap.length; ++i)
    045        {
    046            aint[i] = par0AnvilConverterData.heightmap[i];
    047        }
    048
    049        par1NBTTagCompound.setIntArray("HeightMap", aint);
    050        par1NBTTagCompound.setBoolean("TerrainPopulated", par0AnvilConverterData.terrainPopulated);
    051        NBTTagList nbttaglist = new NBTTagList("Sections");
    052        int j;
    053
    054        for (int k = 0; k < 8; ++k)
    055        {
    056            boolean flag = true;
    057
    058            for (j = 0; j < 16 && flag; ++j)
    059            {
    060                int l = 0;
    061
    062                while (l < 16 && flag)
    063                {
    064                    int i1 = 0;
    065
    066                    while (true)
    067                    {
    068                        if (i1 < 16)
    069                        {
    070                            int j1 = j << 11 | i1 << 7 | l + (k << 4);
    071                            byte b0 = par0AnvilConverterData.blocks[j1];
    072
    073                            if (b0 == 0)
    074                            {
    075                                ++i1;
    076                                continue;
    077                            }
    078
    079                            flag = false;
    080                        }
    081
    082                        ++l;
    083                        break;
    084                    }
    085                }
    086            }
    087
    088            if (!flag)
    089            {
    090                byte[] abyte = new byte[4096];
    091                NibbleArray nibblearray = new NibbleArray(abyte.length, 4);
    092                NibbleArray nibblearray1 = new NibbleArray(abyte.length, 4);
    093                NibbleArray nibblearray2 = new NibbleArray(abyte.length, 4);
    094
    095                for (int k1 = 0; k1 < 16; ++k1)
    096                {
    097                    for (int l1 = 0; l1 < 16; ++l1)
    098                    {
    099                        for (int i2 = 0; i2 < 16; ++i2)
    100                        {
    101                            int j2 = k1 << 11 | i2 << 7 | l1 + (k << 4);
    102                            byte b1 = par0AnvilConverterData.blocks[j2];
    103                            abyte[l1 << 8 | i2 << 4 | k1] = (byte)(b1 & 255);
    104                            nibblearray.set(k1, l1, i2, par0AnvilConverterData.data.get(k1, l1 + (k << 4), i2));
    105                            nibblearray1.set(k1, l1, i2, par0AnvilConverterData.skyLight.get(k1, l1 + (k << 4), i2));
    106                            nibblearray2.set(k1, l1, i2, par0AnvilConverterData.blockLight.get(k1, l1 + (k << 4), i2));
    107                        }
    108                    }
    109                }
    110
    111                NBTTagCompound nbttagcompound1 = new NBTTagCompound();
    112                nbttagcompound1.setByte("Y", (byte)(k & 255));
    113                nbttagcompound1.setByteArray("Blocks", abyte);
    114                nbttagcompound1.setByteArray("Data", nibblearray.data);
    115                nbttagcompound1.setByteArray("SkyLight", nibblearray1.data);
    116                nbttagcompound1.setByteArray("BlockLight", nibblearray2.data);
    117                nbttaglist.appendTag(nbttagcompound1);
    118            }
    119        }
    120
    121        par1NBTTagCompound.setTag("Sections", nbttaglist);
    122        byte[] abyte1 = new byte[256];
    123
    124        for (int k2 = 0; k2 < 16; ++k2)
    125        {
    126            for (j = 0; j < 16; ++j)
    127            {
    128                abyte1[j << 4 | k2] = (byte)(par2WorldChunkManager.getBiomeGenAt(par0AnvilConverterData.x << 4 | k2, par0AnvilConverterData.z << 4 | j).biomeID & 255);
    129            }
    130        }
    131
    132        par1NBTTagCompound.setByteArray("Biomes", abyte1);
    133        par1NBTTagCompound.setTag("Entities", par0AnvilConverterData.entities);
    134        par1NBTTagCompound.setTag("TileEntities", par0AnvilConverterData.tileEntities);
    135
    136        if (par0AnvilConverterData.tileTicks != null)
    137        {
    138            par1NBTTagCompound.setTag("TileTicks", par0AnvilConverterData.tileTicks);
    139        }
    140    }
    141}