/Myst/SaveHandler.java

https://github.com/Jackswalloms/Myst-And-Beyond · Java · 212 lines · 190 code · 15 blank · 7 comment · 13 complexity · 42715a9660feb34bfacfdbcdf98c2abc 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
  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. // CompressedStreamTools, NBTTagCompound, WorldInfo, WorldProvider,
  11. // IChunkLoader
  12. public class SaveHandler
  13. implements ISaveHandler
  14. {
  15. public SaveHandler(File file, String s, boolean flag)
  16. {
  17. saveDirectory = new File(file, s);
  18. saveDirectory.mkdirs();
  19. playersDirectory = new File(saveDirectory, "players");
  20. field_28114_d = new File(saveDirectory, "data");
  21. field_28114_d.mkdirs();
  22. if(flag)
  23. {
  24. playersDirectory.mkdirs();
  25. }
  26. func_22154_d();
  27. }
  28. private void func_22154_d()
  29. {
  30. try
  31. {
  32. File file = new File(saveDirectory, "session.lock");
  33. DataOutputStream dataoutputstream = new DataOutputStream(new FileOutputStream(file));
  34. try
  35. {
  36. dataoutputstream.writeLong(now);
  37. }
  38. finally
  39. {
  40. dataoutputstream.close();
  41. }
  42. }
  43. catch(IOException ioexception)
  44. {
  45. ioexception.printStackTrace();
  46. throw new RuntimeException("Failed to check session lock, aborting");
  47. }
  48. }
  49. protected File getSaveDirectory()
  50. {
  51. return saveDirectory;
  52. }
  53. public void checkSessionLock()
  54. {
  55. try
  56. {
  57. File file = new File(saveDirectory, "session.lock");
  58. DataInputStream datainputstream = new DataInputStream(new FileInputStream(file));
  59. try
  60. {
  61. if(datainputstream.readLong() != now)
  62. {
  63. throw new MinecraftException("The save is being accessed from another location, aborting");
  64. }
  65. }
  66. finally
  67. {
  68. datainputstream.close();
  69. }
  70. }
  71. catch(IOException ioexception)
  72. {
  73. throw new MinecraftException("Failed to check session lock, aborting");
  74. }
  75. }
  76. public IChunkLoader getChunkLoader(WorldProvider worldprovider)
  77. {
  78. if(worldprovider instanceof WorldProviderHell)
  79. {
  80. File file = new File(saveDirectory, "DIM-1");
  81. file.mkdirs();
  82. return new ChunkLoader(file, true);
  83. }
  84. if(worldprovider instanceof WorldProviderMoon)
  85. {
  86. File file = new File(saveDirectory, "DIM-2");
  87. file.mkdirs();
  88. return new ChunkLoader(file, true);
  89. }else
  90. {
  91. return new ChunkLoader(saveDirectory, true);
  92. }
  93. }
  94. public WorldInfo loadWorldInfo()
  95. {
  96. File file = new File(saveDirectory, "level.dat");
  97. if(file.exists())
  98. {
  99. try
  100. {
  101. NBTTagCompound nbttagcompound = CompressedStreamTools.loadGzippedCompoundFromOutputStream(new FileInputStream(file));
  102. NBTTagCompound nbttagcompound2 = nbttagcompound.getCompoundTag("Data");
  103. return new WorldInfo(nbttagcompound2);
  104. }
  105. catch(Exception exception)
  106. {
  107. exception.printStackTrace();
  108. }
  109. }
  110. file = new File(saveDirectory, "level.dat_old");
  111. if(file.exists())
  112. {
  113. try
  114. {
  115. NBTTagCompound nbttagcompound1 = CompressedStreamTools.loadGzippedCompoundFromOutputStream(new FileInputStream(file));
  116. NBTTagCompound nbttagcompound3 = nbttagcompound1.getCompoundTag("Data");
  117. return new WorldInfo(nbttagcompound3);
  118. }
  119. catch(Exception exception1)
  120. {
  121. exception1.printStackTrace();
  122. }
  123. }
  124. return null;
  125. }
  126. public void saveWorldInfoAndPlayer(WorldInfo worldinfo, List list)
  127. {
  128. NBTTagCompound nbttagcompound = worldinfo.getNBTTagCompoundWithPlayer(list);
  129. NBTTagCompound nbttagcompound1 = new NBTTagCompound();
  130. nbttagcompound1.setTag("Data", nbttagcompound);
  131. try
  132. {
  133. File file = new File(saveDirectory, "level.dat_new");
  134. File file1 = new File(saveDirectory, "level.dat_old");
  135. File file2 = new File(saveDirectory, "level.dat");
  136. CompressedStreamTools.writeGzippedCompoundToOutputStream(nbttagcompound1, new FileOutputStream(file));
  137. if(file1.exists())
  138. {
  139. file1.delete();
  140. }
  141. file2.renameTo(file1);
  142. if(file2.exists())
  143. {
  144. file2.delete();
  145. }
  146. file.renameTo(file2);
  147. if(file.exists())
  148. {
  149. file.delete();
  150. }
  151. }
  152. catch(Exception exception)
  153. {
  154. exception.printStackTrace();
  155. }
  156. }
  157. public void saveWorldInfo(WorldInfo worldinfo)
  158. {
  159. NBTTagCompound nbttagcompound = worldinfo.getNBTTagCompound();
  160. NBTTagCompound nbttagcompound1 = new NBTTagCompound();
  161. nbttagcompound1.setTag("Data", nbttagcompound);
  162. try
  163. {
  164. File file = new File(saveDirectory, "level.dat_new");
  165. File file1 = new File(saveDirectory, "level.dat_old");
  166. File file2 = new File(saveDirectory, "level.dat");
  167. CompressedStreamTools.writeGzippedCompoundToOutputStream(nbttagcompound1, new FileOutputStream(file));
  168. if(file1.exists())
  169. {
  170. file1.delete();
  171. }
  172. file2.renameTo(file1);
  173. if(file2.exists())
  174. {
  175. file2.delete();
  176. }
  177. file.renameTo(file2);
  178. if(file.exists())
  179. {
  180. file.delete();
  181. }
  182. }
  183. catch(Exception exception)
  184. {
  185. exception.printStackTrace();
  186. }
  187. }
  188. public File func_28113_a(String s)
  189. {
  190. return new File(field_28114_d, (new StringBuilder()).append(s).append(".dat").toString());
  191. }
  192. private static final Logger logger = Logger.getLogger("Minecraft");
  193. private final File saveDirectory;
  194. private final File playersDirectory;
  195. private final File field_28114_d;
  196. private final long now = System.currentTimeMillis();
  197. }