    001    package net.minecraft.world.storage;
    002
    003    import java.io.DataInputStream;
    004    import java.io.DataOutputStream;
    005    import java.io.File;
    006    import java.io.FileInputStream;
    007    import java.io.FileOutputStream;
    008    import java.util.ArrayList;
    009    import java.util.HashMap;
    010    import java.util.Iterator;
    011    import java.util.List;
    012    import java.util.Map;
    013    import net.minecraft.nbt.CompressedStreamTools;
    014    import net.minecraft.nbt.NBTBase;
    015    import net.minecraft.nbt.NBTTagCompound;
    016    import net.minecraft.nbt.NBTTagShort;
    017    import net.minecraft.world.WorldSavedData;
    018
    019    public class MapStorage
    020    {
    021        private ISaveHandler saveHandler;
    022
    023        /** Map of item data String id to loaded MapDataBases */
    024        private Map loadedDataMap = new HashMap();
    025
    026        /** List of loaded MapDataBases. */
    027        private List loadedDataList = new ArrayList();
    028
    029        /**
    030         * Map of MapDataBase id String prefixes ('map' etc) to max known unique Short id (the 0 part etc) for that prefix
    031         */
    032        private Map idCounts = new HashMap();
    033
    034        public MapStorage(ISaveHandler par1ISaveHandler)
    035        {
    036            this.saveHandler = par1ISaveHandler;
    037            this.loadIdCounts();
    038        }
    039
    040        /**
    041         * Loads an existing MapDataBase corresponding to the given String id from disk, instantiating the given Class, or
    042         * returns null if none such file exists. args: Class to instantiate, String dataid
    043         */
    044        public WorldSavedData loadData(Class par1Class, String par2Str)
    045        {
    046            WorldSavedData var3 = (WorldSavedData)this.loadedDataMap.get(par2Str);
    047
    048            if (var3 != null)
    049            {
    050                return var3;
    051            }
    052            else
    053            {
    054                if (this.saveHandler != null)
    055                {
    056                    try
    057                    {
    058                        File var4 = this.saveHandler.getMapFileFromName(par2Str);
    059
    060                        if (var4 != null && var4.exists())
    061                        {
    062                            try
    063                            {
    064                                var3 = (WorldSavedData)par1Class.getConstructor(new Class[] {String.class}).newInstance(new Object[] {par2Str});
    065                            }
    066                            catch (Exception var7)
    067                            {
    068                                throw new RuntimeException("Failed to instantiate " + par1Class.toString(), var7);
    069                            }
    070
    071                            FileInputStream var5 = new FileInputStream(var4);
    072                            NBTTagCompound var6 = CompressedStreamTools.readCompressed(var5);
    073                            var5.close();
    074                            var3.readFromNBT(var6.getCompoundTag("data"));
    075                        }
    076                    }
    077                    catch (Exception var8)
    078                    {
    079                        var8.printStackTrace();
    080                    }
    081                }
    082
    083                if (var3 != null)
    084                {
    085                    this.loadedDataMap.put(par2Str, var3);
    086                    this.loadedDataList.add(var3);
    087                }
    088
    089                return var3;
    090            }
    091        }
    092
    093        /**
    094         * Assigns the given String id to the given MapDataBase, removing any existing ones of the same id.
    095         */
    096        public void setData(String par1Str, WorldSavedData par2WorldSavedData)
    097        {
    098            if (par2WorldSavedData == null)
    099            {
    100                throw new RuntimeException("Can\'t set null data");
    101            }
    102            else
    103            {
    104                if (this.loadedDataMap.containsKey(par1Str))
    105                {
    106                    this.loadedDataList.remove(this.loadedDataMap.remove(par1Str));
    107                }
    108
    109                this.loadedDataMap.put(par1Str, par2WorldSavedData);
    110                this.loadedDataList.add(par2WorldSavedData);
    111            }
    112        }
    113
    114        /**
    115         * Saves all dirty loaded MapDataBases to disk.
    116         */
    117        public void saveAllData()
    118        {
    119            for (int var1 = 0; var1 < this.loadedDataList.size(); ++var1)
    120            {
    121                WorldSavedData var2 = (WorldSavedData)this.loadedDataList.get(var1);
    122
    123                if (var2.isDirty())
    124                {
    125                    this.saveData(var2);
    126                    var2.setDirty(false);
    127                }
    128            }
    129        }
    130
    131        /**
    132         * Saves the given MapDataBase to disk.
    133         */
    134        private void saveData(WorldSavedData par1WorldSavedData)
    135        {
    136            if (this.saveHandler != null)
    137            {
    138                try
    139                {
    140                    File var2 = this.saveHandler.getMapFileFromName(par1WorldSavedData.mapName);
    141
    142                    if (var2 != null)
    143                    {
    144                        NBTTagCompound var3 = new NBTTagCompound();
    145                        par1WorldSavedData.writeToNBT(var3);
    146                        NBTTagCompound var4 = new NBTTagCompound();
    147                        var4.setCompoundTag("data", var3);
    148                        FileOutputStream var5 = new FileOutputStream(var2);
    149                        CompressedStreamTools.writeCompressed(var4, var5);
    150                        var5.close();
    151                    }
    152                }
    153                catch (Exception var6)
    154                {
    155                    var6.printStackTrace();
    156                }
    157            }
    158        }
    159
    160        /**
    161         * Loads the idCounts Map from the 'idcounts' file.
    162         */
    163        private void loadIdCounts()
    164        {
    165            try
    166            {
    167                this.idCounts.clear();
    168
    169                if (this.saveHandler == null)
    170                {
    171                    return;
    172                }
    173
    174                File var1 = this.saveHandler.getMapFileFromName("idcounts");
    175
    176                if (var1 != null && var1.exists())
    177                {
    178                    DataInputStream var2 = new DataInputStream(new FileInputStream(var1));
    179                    NBTTagCompound var3 = CompressedStreamTools.read(var2);
    180                    var2.close();
    181                    Iterator var4 = var3.getTags().iterator();
    182
    183                    while (var4.hasNext())
    184                    {
    185                        NBTBase var5 = (NBTBase)var4.next();
    186
    187                        if (var5 instanceof NBTTagShort)
    188                        {
    189                            NBTTagShort var6 = (NBTTagShort)var5;
    190                            String var7 = var6.getName();
    191                            short var8 = var6.data;
    192                            this.idCounts.put(var7, Short.valueOf(var8));
    193                        }
    194                    }
    195                }
    196            }
    197            catch (Exception var9)
    198            {
    199                var9.printStackTrace();
    200            }
    201        }
    202
    203        /**
    204         * Returns an unique new data id for the given prefix and saves the idCounts map to the 'idcounts' file.
    205         */
    206        public int getUniqueDataId(String par1Str)
    207        {
    208            Short var2 = (Short)this.idCounts.get(par1Str);
    209
    210            if (var2 == null)
    211            {
    212                var2 = Short.valueOf((short)0);
    213            }
    214            else
    215            {
    216                var2 = Short.valueOf((short)(var2.shortValue() + 1));
    217            }
    218
    219            this.idCounts.put(par1Str, var2);
    220
    221            if (this.saveHandler == null)
    222            {
    223                return var2.shortValue();
    224            }
    225            else
    226            {
    227                try
    228                {
    229                    File var3 = this.saveHandler.getMapFileFromName("idcounts");
    230
    231                    if (var3 != null)
    232                    {
    233                        NBTTagCompound var4 = new NBTTagCompound();
    234                        Iterator var5 = this.idCounts.keySet().iterator();
    235
    236                        while (var5.hasNext())
    237                        {
    238                            String var6 = (String)var5.next();
    239                            short var7 = ((Short)this.idCounts.get(var6)).shortValue();
    240                            var4.setShort(var6, var7);
    241                        }
    242
    243                        DataOutputStream var9 = new DataOutputStream(new FileOutputStream(var3));
    244                        CompressedStreamTools.write(var4, var9);
    245                        var9.close();
    246                    }
    247                }
    248                catch (Exception var8)
    249                {
    250                    var8.printStackTrace();
    251                }
    252
    253                return var2.shortValue();
    254            }
    255        }
    256    }