| 1 | package net.minecraft.world.storage;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.google.common.collect.Maps;
|
| 5 | import java.io.DataInputStream;
|
| 6 | import java.io.DataOutputStream;
|
| 7 | import java.io.File;
|
| 8 | import java.io.FileInputStream;
|
| 9 | import java.io.FileOutputStream;
|
| 10 | import java.util.List;
|
| 11 | import java.util.Map;
|
| 12 | import net.minecraft.nbt.CompressedStreamTools;
|
| 13 | import net.minecraft.nbt.NBTBase;
|
| 14 | import net.minecraft.nbt.NBTTagCompound;
|
| 15 | import net.minecraft.nbt.NBTTagShort;
|
| 16 | import net.minecraft.world.WorldSavedData;
|
| 17 |
|
| 18 | public class MapStorage
|
| 19 | {
|
| 20 | private ISaveHandler saveHandler;
|
| 21 | protected Map<String, WorldSavedData> loadedDataMap = Maps.<String, WorldSavedData>newHashMap();
|
| 22 | private List<WorldSavedData> loadedDataList = Lists.<WorldSavedData>newArrayList();
|
| 23 | private Map<String, Short> idCounts = Maps.<String, Short>newHashMap();
|
| 24 |
|
| 25 | public MapStorage(ISaveHandler saveHandlerIn)
|
| 26 | {
|
| 27 | this.saveHandler = saveHandlerIn;
|
| 28 | this.loadIdCounts();
|
| 29 | }
|
| 30 |
|
| 31 | /**
|
| 32 | * Loads an existing MapDataBase corresponding to the given String id from disk, instantiating the given Class, or
|
| 33 | * returns null if none such file exists. args: Class to instantiate, String dataid
|
| 34 | */
|
| 35 | public WorldSavedData loadData(Class <? extends WorldSavedData > clazz, String dataIdentifier)
|
| 36 | {
|
| 37 | WorldSavedData worldsaveddata = (WorldSavedData)this.loadedDataMap.get(dataIdentifier);
|
| 38 |
|
| 39 | if (worldsaveddata != null)
|
| 40 | {
|
| 41 | return worldsaveddata;
|
| 42 | }
|
| 43 | else
|
| 44 | {
|
| 45 | if (this.saveHandler != null)
|
| 46 | {
|
| 47 | try
|
| 48 | {
|
| 49 | File file1 = this.saveHandler.getMapFileFromName(dataIdentifier);
|
| 50 |
|
| 51 | if (file1 != null && file1.exists())
|
| 52 | {
|
| 53 | try
|
| 54 | {
|
| 55 | worldsaveddata = (WorldSavedData)clazz.getConstructor(new Class[] {String.class}).newInstance(new Object[] {dataIdentifier});
|
| 56 | }
|
| 57 | catch (Exception exception)
|
| 58 | {
|
| 59 | throw new RuntimeException("Failed to instantiate " + clazz.toString(), exception);
|
| 60 | }
|
| 61 |
|
| 62 | FileInputStream fileinputstream = new FileInputStream(file1);
|
| 63 | NBTTagCompound nbttagcompound = CompressedStreamTools.readCompressed(fileinputstream);
|
| 64 | fileinputstream.close();
|
| 65 | worldsaveddata.readFromNBT(nbttagcompound.getCompoundTag("data"));
|
| 66 | }
|
| 67 | }
|
| 68 | catch (Exception exception1)
|
| 69 | {
|
| 70 | exception1.printStackTrace();
|
| 71 | }
|
| 72 | }
|
| 73 |
|
| 74 | if (worldsaveddata != null)
|
| 75 | {
|
| 76 | this.loadedDataMap.put(dataIdentifier, worldsaveddata);
|
| 77 | this.loadedDataList.add(worldsaveddata);
|
| 78 | }
|
| 79 |
|
| 80 | return worldsaveddata;
|
| 81 | }
|
| 82 | }
|
| 83 |
|
| 84 | /**
|
| 85 | * Assigns the given String id to the given MapDataBase, removing any existing ones of the same id.
|
| 86 | */
|
| 87 | public void setData(String dataIdentifier, WorldSavedData data)
|
| 88 | {
|
| 89 | if (this.loadedDataMap.containsKey(dataIdentifier))
|
| 90 | {
|
| 91 | this.loadedDataList.remove(this.loadedDataMap.remove(dataIdentifier));
|
| 92 | }
|
| 93 |
|
| 94 | this.loadedDataMap.put(dataIdentifier, data);
|
| 95 | this.loadedDataList.add(data);
|
| 96 | }
|
| 97 |
|
| 98 | /**
|
| 99 | * Saves all dirty loaded MapDataBases to disk.
|
| 100 | */
|
| 101 | public void saveAllData()
|
| 102 | {
|
| 103 | for (int i = 0; i < this.loadedDataList.size(); ++i)
|
| 104 | {
|
| 105 | WorldSavedData worldsaveddata = (WorldSavedData)this.loadedDataList.get(i);
|
| 106 |
|
| 107 | if (worldsaveddata.isDirty())
|
| 108 | {
|
| 109 | this.saveData(worldsaveddata);
|
| 110 | worldsaveddata.setDirty(false);
|
| 111 | }
|
| 112 | }
|
| 113 | }
|
| 114 |
|
| 115 | /**
|
| 116 | * Saves the given MapDataBase to disk.
|
| 117 | */
|
| 118 | private void saveData(WorldSavedData p_75747_1_)
|
| 119 | {
|
| 120 | if (this.saveHandler != null)
|
| 121 | {
|
| 122 | try
|
| 123 | {
|
| 124 | File file1 = this.saveHandler.getMapFileFromName(p_75747_1_.mapName);
|
| 125 |
|
| 126 | if (file1 != null)
|
| 127 | {
|
| 128 | NBTTagCompound nbttagcompound = new NBTTagCompound();
|
| 129 | p_75747_1_.writeToNBT(nbttagcompound);
|
| 130 | NBTTagCompound nbttagcompound1 = new NBTTagCompound();
|
| 131 | nbttagcompound1.setTag("data", nbttagcompound);
|
| 132 | FileOutputStream fileoutputstream = new FileOutputStream(file1);
|
| 133 | CompressedStreamTools.writeCompressed(nbttagcompound1, fileoutputstream);
|
| 134 | fileoutputstream.close();
|
| 135 | }
|
| 136 | }
|
| 137 | catch (Exception exception)
|
| 138 | {
|
| 139 | exception.printStackTrace();
|
| 140 | }
|
| 141 | }
|
| 142 | }
|
| 143 |
|
| 144 | /**
|
| 145 | * Loads the idCounts Map from the 'idcounts' file.
|
| 146 | */
|
| 147 | private void loadIdCounts()
|
| 148 | {
|
| 149 | try
|
| 150 | {
|
| 151 | this.idCounts.clear();
|
| 152 |
|
| 153 | if (this.saveHandler == null)
|
| 154 | {
|
| 155 | return;
|
| 156 | }
|
| 157 |
|
| 158 | File file1 = this.saveHandler.getMapFileFromName("idcounts");
|
| 159 |
|
| 160 | if (file1 != null && file1.exists())
|
| 161 | {
|
| 162 | DataInputStream datainputstream = new DataInputStream(new FileInputStream(file1));
|
| 163 | NBTTagCompound nbttagcompound = CompressedStreamTools.read(datainputstream);
|
| 164 | datainputstream.close();
|
| 165 |
|
| 166 | for (String s : nbttagcompound.getKeySet())
|
| 167 | {
|
| 168 | NBTBase nbtbase = nbttagcompound.getTag(s);
|
| 169 |
|
| 170 | if (nbtbase instanceof NBTTagShort)
|
| 171 | {
|
| 172 | NBTTagShort nbttagshort = (NBTTagShort)nbtbase;
|
| 173 | short short1 = nbttagshort.getShort();
|
| 174 | this.idCounts.put(s, Short.valueOf(short1));
|
| 175 | }
|
| 176 | }
|
| 177 | }
|
| 178 | }
|
| 179 | catch (Exception exception)
|
| 180 | {
|
| 181 | exception.printStackTrace();
|
| 182 | }
|
| 183 | }
|
| 184 |
|
| 185 | /**
|
| 186 | * Returns an unique new data id for the given prefix and saves the idCounts map to the 'idcounts' file.
|
| 187 | */
|
| 188 | public int getUniqueDataId(String key)
|
| 189 | {
|
| 190 | Short oshort = (Short)this.idCounts.get(key);
|
| 191 |
|
| 192 | if (oshort == null)
|
| 193 | {
|
| 194 | oshort = Short.valueOf((short)0);
|
| 195 | }
|
| 196 | else
|
| 197 | {
|
| 198 | oshort = Short.valueOf((short)(oshort.shortValue() + 1));
|
| 199 | }
|
| 200 |
|
| 201 | this.idCounts.put(key, oshort);
|
| 202 |
|
| 203 | if (this.saveHandler == null)
|
| 204 | {
|
| 205 | return oshort.shortValue();
|
| 206 | }
|
| 207 | else
|
| 208 | {
|
| 209 | try
|
| 210 | {
|
| 211 | File file1 = this.saveHandler.getMapFileFromName("idcounts");
|
| 212 |
|
| 213 | if (file1 != null)
|
| 214 | {
|
| 215 | NBTTagCompound nbttagcompound = new NBTTagCompound();
|
| 216 |
|
| 217 | for (String s : this.idCounts.keySet())
|
| 218 | {
|
| 219 | short short1 = ((Short)this.idCounts.get(s)).shortValue();
|
| 220 | nbttagcompound.setShort(s, short1);
|
| 221 | }
|
| 222 |
|
| 223 | DataOutputStream dataoutputstream = new DataOutputStream(new FileOutputStream(file1));
|
| 224 | CompressedStreamTools.write(nbttagcompound, dataoutputstream);
|
| 225 | dataoutputstream.close();
|
| 226 | }
|
| 227 | }
|
| 228 | catch (Exception exception)
|
| 229 | {
|
| 230 | exception.printStackTrace();
|
| 231 | }
|
| 232 |
|
| 233 | return oshort.shortValue();
|
| 234 | }
|
| 235 | }
|
| 236 | }
|
| 237 |
|