/net/minecraft/src/SaveHandler.java

https://bitbucket.org/kingbowser/stormclient · Java · 219 lines · 196 code · 16 blank · 7 comment · 13 complexity · f0616638ad1604908ca90980eef35c75 MD5 · raw file

  1. // Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
  2. // Jad home page: http://www.kpdus.com/jad.html
  3. // Decompiler options: packimports(3) braces deadcode fieldsfirst
  4. package net.minecraft.src;
  5. import java.io.*;
  6. import java.util.List;
  7. import java.util.logging.Logger;
  8. // Referenced classes of package net.minecraft.src:
  9. // ISaveHandler, MinecraftException, WorldProviderHell, ChunkLoader,
  10. // WorldProviderEnd, CompressedStreamTools, NBTTagCompound, WorldInfo,
  11. // WorldProvider, IChunkLoader
  12. public class SaveHandler
  13. implements ISaveHandler
  14. {
  15. private static final Logger logger = Logger.getLogger("Minecraft");
  16. private final File saveDirectory;
  17. private final File playersDirectory;
  18. private final File mapDataDir;
  19. private final long now = System.currentTimeMillis();
  20. private final String field_40531_f;
  21. public SaveHandler(File file, String s, boolean flag)
  22. {
  23. saveDirectory = new File(file, s);
  24. saveDirectory.mkdirs();
  25. playersDirectory = new File(saveDirectory, "players");
  26. mapDataDir = new File(saveDirectory, "data");
  27. mapDataDir.mkdirs();
  28. field_40531_f = s;
  29. if(flag)
  30. {
  31. playersDirectory.mkdirs();
  32. }
  33. func_22154_d();
  34. }
  35. private void func_22154_d()
  36. {
  37. try
  38. {
  39. File file = new File(saveDirectory, "session.lock");
  40. DataOutputStream dataoutputstream = new DataOutputStream(new FileOutputStream(file));
  41. try
  42. {
  43. dataoutputstream.writeLong(now);
  44. }
  45. finally
  46. {
  47. dataoutputstream.close();
  48. }
  49. }
  50. catch(IOException ioexception)
  51. {
  52. ioexception.printStackTrace();
  53. throw new RuntimeException("Failed to check session lock, aborting");
  54. }
  55. }
  56. protected File getSaveDirectory()
  57. {
  58. return saveDirectory;
  59. }
  60. public void checkSessionLock()
  61. {
  62. try
  63. {
  64. File file = new File(saveDirectory, "session.lock");
  65. DataInputStream datainputstream = new DataInputStream(new FileInputStream(file));
  66. try
  67. {
  68. if(datainputstream.readLong() != now)
  69. {
  70. throw new MinecraftException("The save is being accessed from another location, aborting");
  71. }
  72. }
  73. finally
  74. {
  75. datainputstream.close();
  76. }
  77. }
  78. catch(IOException ioexception)
  79. {
  80. throw new MinecraftException("Failed to check session lock, aborting");
  81. }
  82. }
  83. public IChunkLoader getChunkLoader(WorldProvider worldprovider)
  84. {
  85. if(worldprovider instanceof WorldProviderHell)
  86. {
  87. File file = new File(saveDirectory, "DIM-1");
  88. file.mkdirs();
  89. return new ChunkLoader(file, true);
  90. }
  91. if(worldprovider instanceof WorldProviderEnd)
  92. {
  93. File file1 = new File(saveDirectory, "DIM1");
  94. file1.mkdirs();
  95. return new ChunkLoader(file1, true);
  96. } else
  97. {
  98. return new ChunkLoader(saveDirectory, true);
  99. }
  100. }
  101. public WorldInfo loadWorldInfo()
  102. {
  103. File file = new File(saveDirectory, "level.dat");
  104. if(file.exists())
  105. {
  106. try
  107. {
  108. NBTTagCompound nbttagcompound = CompressedStreamTools.loadGzippedCompoundFromOutputStream(new FileInputStream(file));
  109. NBTTagCompound nbttagcompound2 = nbttagcompound.getCompoundTag("Data");
  110. return new WorldInfo(nbttagcompound2);
  111. }
  112. catch(Exception exception)
  113. {
  114. exception.printStackTrace();
  115. }
  116. }
  117. file = new File(saveDirectory, "level.dat_old");
  118. if(file.exists())
  119. {
  120. try
  121. {
  122. NBTTagCompound nbttagcompound1 = CompressedStreamTools.loadGzippedCompoundFromOutputStream(new FileInputStream(file));
  123. NBTTagCompound nbttagcompound3 = nbttagcompound1.getCompoundTag("Data");
  124. return new WorldInfo(nbttagcompound3);
  125. }
  126. catch(Exception exception1)
  127. {
  128. exception1.printStackTrace();
  129. }
  130. }
  131. return null;
  132. }
  133. public void saveWorldInfoAndPlayer(WorldInfo worldinfo, List list)
  134. {
  135. NBTTagCompound nbttagcompound = worldinfo.getNBTTagCompoundWithPlayer(list);
  136. NBTTagCompound nbttagcompound1 = new NBTTagCompound();
  137. nbttagcompound1.setTag("Data", nbttagcompound);
  138. try
  139. {
  140. File file = new File(saveDirectory, "level.dat_new");
  141. File file1 = new File(saveDirectory, "level.dat_old");
  142. File file2 = new File(saveDirectory, "level.dat");
  143. CompressedStreamTools.writeGzippedCompoundToOutputStream(nbttagcompound1, new FileOutputStream(file));
  144. if(file1.exists())
  145. {
  146. file1.delete();
  147. }
  148. file2.renameTo(file1);
  149. if(file2.exists())
  150. {
  151. file2.delete();
  152. }
  153. file.renameTo(file2);
  154. if(file.exists())
  155. {
  156. file.delete();
  157. }
  158. }
  159. catch(Exception exception)
  160. {
  161. exception.printStackTrace();
  162. }
  163. }
  164. public void saveWorldInfo(WorldInfo worldinfo)
  165. {
  166. NBTTagCompound nbttagcompound = worldinfo.getNBTTagCompound();
  167. NBTTagCompound nbttagcompound1 = new NBTTagCompound();
  168. nbttagcompound1.setTag("Data", nbttagcompound);
  169. try
  170. {
  171. File file = new File(saveDirectory, "level.dat_new");
  172. File file1 = new File(saveDirectory, "level.dat_old");
  173. File file2 = new File(saveDirectory, "level.dat");
  174. CompressedStreamTools.writeGzippedCompoundToOutputStream(nbttagcompound1, new FileOutputStream(file));
  175. if(file1.exists())
  176. {
  177. file1.delete();
  178. }
  179. file2.renameTo(file1);
  180. if(file2.exists())
  181. {
  182. file2.delete();
  183. }
  184. file.renameTo(file2);
  185. if(file.exists())
  186. {
  187. file.delete();
  188. }
  189. }
  190. catch(Exception exception)
  191. {
  192. exception.printStackTrace();
  193. }
  194. }
  195. public File getMapFile(String s)
  196. {
  197. return new File(mapDataDir, (new StringBuilder()).append(s).append(".dat").toString());
  198. }
  199. public String func_40530_d()
  200. {
  201. return field_40531_f;
  202. }
  203. }