| 1 | package net.minecraft.world.storage;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import java.io.File;
|
| 5 | import java.io.FileInputStream;
|
| 6 | import java.io.FileOutputStream;
|
| 7 | import java.util.List;
|
| 8 | import net.minecraft.client.AnvilConverterException;
|
| 9 | import net.minecraft.nbt.CompressedStreamTools;
|
| 10 | import net.minecraft.nbt.NBTTagCompound;
|
| 11 | import net.minecraft.util.IProgressUpdate;
|
| 12 | import org.apache.logging.log4j.LogManager;
|
| 13 | import org.apache.logging.log4j.Logger;
|
| 14 |
|
| 15 | public class SaveFormatOld implements ISaveFormat
|
| 16 | {
|
| 17 | private static final Logger logger = LogManager.getLogger();
|
| 18 |
|
| 19 | /**
|
| 20 | * Reference to the File object representing the directory for the world saves
|
| 21 | */
|
| 22 | protected final File savesDirectory;
|
| 23 |
|
| 24 | public SaveFormatOld(File savesDirectoryIn)
|
| 25 | {
|
| 26 | if (!savesDirectoryIn.exists())
|
| 27 | {
|
| 28 | savesDirectoryIn.mkdirs();
|
| 29 | }
|
| 30 |
|
| 31 | this.savesDirectory = savesDirectoryIn;
|
| 32 | }
|
| 33 |
|
| 34 | /**
|
| 35 | * Returns the name of the save format.
|
| 36 | */
|
| 37 | public String getName()
|
| 38 | {
|
| 39 | return "Old Format";
|
| 40 | }
|
| 41 |
|
| 42 | public List<SaveFormatComparator> getSaveList() throws AnvilConverterException
|
| 43 | {
|
| 44 | List<SaveFormatComparator> list = Lists.<SaveFormatComparator>newArrayList();
|
| 45 |
|
| 46 | for (int i = 0; i < 5; ++i)
|
| 47 | {
|
| 48 | String s = "World" + (i + 1);
|
| 49 | WorldInfo worldinfo = this.getWorldInfo(s);
|
| 50 |
|
| 51 | if (worldinfo != null)
|
| 52 | {
|
| 53 | list.add(new SaveFormatComparator(s, "", worldinfo.getLastTimePlayed(), worldinfo.getSizeOnDisk(), worldinfo.getGameType(), false, worldinfo.isHardcoreModeEnabled(), worldinfo.areCommandsAllowed()));
|
| 54 | }
|
| 55 | }
|
| 56 |
|
| 57 | return list;
|
| 58 | }
|
| 59 |
|
| 60 | public void flushCache()
|
| 61 | {
|
| 62 | }
|
| 63 |
|
| 64 | /**
|
| 65 | * Returns the world's WorldInfo object
|
| 66 | */
|
| 67 | public WorldInfo getWorldInfo(String saveName)
|
| 68 | {
|
| 69 | File file1 = new File(this.savesDirectory, saveName);
|
| 70 |
|
| 71 | if (!file1.exists())
|
| 72 | {
|
| 73 | return null;
|
| 74 | }
|
| 75 | else
|
| 76 | {
|
| 77 | File file2 = new File(file1, "level.dat");
|
| 78 |
|
| 79 | if (file2.exists())
|
| 80 | {
|
| 81 | try
|
| 82 | {
|
| 83 | NBTTagCompound nbttagcompound2 = CompressedStreamTools.readCompressed(new FileInputStream(file2));
|
| 84 | NBTTagCompound nbttagcompound3 = nbttagcompound2.getCompoundTag("Data");
|
| 85 | return new WorldInfo(nbttagcompound3);
|
| 86 | }
|
| 87 | catch (Exception exception1)
|
| 88 | {
|
| 89 | logger.error((String)("Exception reading " + file2), (Throwable)exception1);
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | file2 = new File(file1, "level.dat_old");
|
| 94 |
|
| 95 | if (file2.exists())
|
| 96 | {
|
| 97 | try
|
| 98 | {
|
| 99 | NBTTagCompound nbttagcompound = CompressedStreamTools.readCompressed(new FileInputStream(file2));
|
| 100 | NBTTagCompound nbttagcompound1 = nbttagcompound.getCompoundTag("Data");
|
| 101 | return new WorldInfo(nbttagcompound1);
|
| 102 | }
|
| 103 | catch (Exception exception)
|
| 104 | {
|
| 105 | logger.error((String)("Exception reading " + file2), (Throwable)exception);
|
| 106 | }
|
| 107 | }
|
| 108 |
|
| 109 | return null;
|
| 110 | }
|
| 111 | }
|
| 112 |
|
| 113 | /**
|
| 114 | * Renames the world by storing the new name in level.dat. It does *not* rename the directory containing the world
|
| 115 | * data.
|
| 116 | */
|
| 117 | public void renameWorld(String dirName, String newName)
|
| 118 | {
|
| 119 | File file1 = new File(this.savesDirectory, dirName);
|
| 120 |
|
| 121 | if (file1.exists())
|
| 122 | {
|
| 123 | File file2 = new File(file1, "level.dat");
|
| 124 |
|
| 125 | if (file2.exists())
|
| 126 | {
|
| 127 | try
|
| 128 | {
|
| 129 | NBTTagCompound nbttagcompound = CompressedStreamTools.readCompressed(new FileInputStream(file2));
|
| 130 | NBTTagCompound nbttagcompound1 = nbttagcompound.getCompoundTag("Data");
|
| 131 | nbttagcompound1.setString("LevelName", newName);
|
| 132 | CompressedStreamTools.writeCompressed(nbttagcompound, new FileOutputStream(file2));
|
| 133 | }
|
| 134 | catch (Exception exception)
|
| 135 | {
|
| 136 | exception.printStackTrace();
|
| 137 | }
|
| 138 | }
|
| 139 | }
|
| 140 | }
|
| 141 |
|
| 142 | public boolean isNewLevelIdAcceptable(String saveName)
|
| 143 | {
|
| 144 | File file1 = new File(this.savesDirectory, saveName);
|
| 145 |
|
| 146 | if (file1.exists())
|
| 147 | {
|
| 148 | return false;
|
| 149 | }
|
| 150 | else
|
| 151 | {
|
| 152 | try
|
| 153 | {
|
| 154 | file1.mkdir();
|
| 155 | file1.delete();
|
| 156 | return true;
|
| 157 | }
|
| 158 | catch (Throwable throwable)
|
| 159 | {
|
| 160 | logger.warn("Couldn\'t make new level", throwable);
|
| 161 | return false;
|
| 162 | }
|
| 163 | }
|
| 164 | }
|
| 165 |
|
| 166 | /**
|
| 167 | * @args: Takes one argument - the name of the directory of the world to delete. @desc: Delete the world by deleting
|
| 168 | * the associated directory recursively.
|
| 169 | *
|
| 170 | * @param saveName The current save's name
|
| 171 | */
|
| 172 | public boolean deleteWorldDirectory(String saveName)
|
| 173 | {
|
| 174 | File file1 = new File(this.savesDirectory, saveName);
|
| 175 |
|
| 176 | if (!file1.exists())
|
| 177 | {
|
| 178 | return true;
|
| 179 | }
|
| 180 | else
|
| 181 | {
|
| 182 | logger.info("Deleting level " + saveName);
|
| 183 |
|
| 184 | for (int i = 1; i <= 5; ++i)
|
| 185 | {
|
| 186 | logger.info("Attempt " + i + "...");
|
| 187 |
|
| 188 | if (deleteFiles(file1.listFiles()))
|
| 189 | {
|
| 190 | break;
|
| 191 | }
|
| 192 |
|
| 193 | logger.warn("Unsuccessful in deleting contents.");
|
| 194 |
|
| 195 | if (i < 5)
|
| 196 | {
|
| 197 | try
|
| 198 | {
|
| 199 | Thread.sleep(500L);
|
| 200 | }
|
| 201 | catch (InterruptedException var5)
|
| 202 | {
|
| 203 | ;
|
| 204 | }
|
| 205 | }
|
| 206 | }
|
| 207 |
|
| 208 | return file1.delete();
|
| 209 | }
|
| 210 | }
|
| 211 |
|
| 212 | /**
|
| 213 | * @args: Takes one argument - the list of files and directories to delete. @desc: Deletes the files and directory
|
| 214 | * listed in the list recursively.
|
| 215 | */
|
| 216 | protected static boolean deleteFiles(File[] files)
|
| 217 | {
|
| 218 | for (int i = 0; i < files.length; ++i)
|
| 219 | {
|
| 220 | File file1 = files[i];
|
| 221 | logger.debug("Deleting " + file1);
|
| 222 |
|
| 223 | if (file1.isDirectory() && !deleteFiles(file1.listFiles()))
|
| 224 | {
|
| 225 | logger.warn("Couldn\'t delete directory " + file1);
|
| 226 | return false;
|
| 227 | }
|
| 228 |
|
| 229 | if (!file1.delete())
|
| 230 | {
|
| 231 | logger.warn("Couldn\'t delete file " + file1);
|
| 232 | return false;
|
| 233 | }
|
| 234 | }
|
| 235 |
|
| 236 | return true;
|
| 237 | }
|
| 238 |
|
| 239 | /**
|
| 240 | * Returns back a loader for the specified save directory
|
| 241 | */
|
| 242 | public ISaveHandler getSaveLoader(String saveName, boolean storePlayerdata)
|
| 243 | {
|
| 244 | return new SaveHandler(this.savesDirectory, saveName, storePlayerdata);
|
| 245 | }
|
| 246 |
|
| 247 | public boolean isConvertible(String saveName)
|
| 248 | {
|
| 249 | return false;
|
| 250 | }
|
| 251 |
|
| 252 | /**
|
| 253 | * gets if the map is old chunk saving (true) or McRegion (false)
|
| 254 | */
|
| 255 | public boolean isOldMapFormat(String saveName)
|
| 256 | {
|
| 257 | return false;
|
| 258 | }
|
| 259 |
|
| 260 | /**
|
| 261 | * converts the map to mcRegion
|
| 262 | */
|
| 263 | public boolean convertMapFormat(String filename, IProgressUpdate progressCallback)
|
| 264 | {
|
| 265 | return false;
|
| 266 | }
|
| 267 |
|
| 268 | /**
|
| 269 | * Return whether the given world can be loaded.
|
| 270 | *
|
| 271 | * @param saveName The current save's name
|
| 272 | */
|
| 273 | public boolean canLoadWorld(String saveName)
|
| 274 | {
|
| 275 | File file1 = new File(this.savesDirectory, saveName);
|
| 276 | return file1.isDirectory();
|
| 277 | }
|
| 278 | }
|
| 279 |
|