/net/minecraft/server/PlayerNBTManager.java

https://github.com/fouram/mc-dev · Java · 222 lines · 182 code · 40 blank · 0 comment · 17 complexity · 5481f23a6294d9689d4d42f3dda3ff17 MD5 · raw file

  1. package net.minecraft.server;
  2. import java.io.DataInputStream;
  3. import java.io.DataOutputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.OutputStream;
  10. import java.util.List;
  11. import java.util.logging.Logger;
  12. public class PlayerNBTManager implements PlayerFileData, IDataManager {
  13. private static final Logger a = Logger.getLogger("Minecraft");
  14. private final File b;
  15. private final File c;
  16. private final File d;
  17. private final long e = System.currentTimeMillis();
  18. public PlayerNBTManager(File file1, String s, boolean flag) {
  19. this.b = new File(file1, s);
  20. this.b.mkdirs();
  21. this.c = new File(this.b, "players");
  22. this.d = new File(this.b, "data");
  23. this.d.mkdirs();
  24. if (flag) {
  25. this.c.mkdirs();
  26. }
  27. this.f();
  28. }
  29. private void f() {
  30. try {
  31. File file1 = new File(this.b, "session.lock");
  32. DataOutputStream dataoutputstream = new DataOutputStream(new FileOutputStream(file1));
  33. try {
  34. dataoutputstream.writeLong(this.e);
  35. } finally {
  36. dataoutputstream.close();
  37. }
  38. } catch (IOException ioexception) {
  39. ioexception.printStackTrace();
  40. throw new RuntimeException("Failed to check session lock, aborting");
  41. }
  42. }
  43. protected File a() {
  44. return this.b;
  45. }
  46. public void b() {
  47. try {
  48. File file1 = new File(this.b, "session.lock");
  49. DataInputStream datainputstream = new DataInputStream(new FileInputStream(file1));
  50. try {
  51. if (datainputstream.readLong() != this.e) {
  52. throw new MinecraftException("The save is being accessed from another location, aborting");
  53. }
  54. } finally {
  55. datainputstream.close();
  56. }
  57. } catch (IOException ioexception) {
  58. throw new MinecraftException("Failed to check session lock, aborting");
  59. }
  60. }
  61. public IChunkLoader a(WorldProvider worldprovider) {
  62. if (worldprovider instanceof WorldProviderHell) {
  63. File file1 = new File(this.b, "DIM-1");
  64. file1.mkdirs();
  65. return new ChunkLoader(file1, true);
  66. } else {
  67. return new ChunkLoader(this.b, true);
  68. }
  69. }
  70. public WorldData c() {
  71. File file1 = new File(this.b, "level.dat");
  72. NBTTagCompound nbttagcompound;
  73. NBTTagCompound nbttagcompound1;
  74. if (file1.exists()) {
  75. try {
  76. nbttagcompound = CompressedStreamTools.a((InputStream) (new FileInputStream(file1)));
  77. nbttagcompound1 = nbttagcompound.k("Data");
  78. return new WorldData(nbttagcompound1);
  79. } catch (Exception exception) {
  80. exception.printStackTrace();
  81. }
  82. }
  83. file1 = new File(this.b, "level.dat_old");
  84. if (file1.exists()) {
  85. try {
  86. nbttagcompound = CompressedStreamTools.a((InputStream) (new FileInputStream(file1)));
  87. nbttagcompound1 = nbttagcompound.k("Data");
  88. return new WorldData(nbttagcompound1);
  89. } catch (Exception exception1) {
  90. exception1.printStackTrace();
  91. }
  92. }
  93. return null;
  94. }
  95. public void a(WorldData worlddata, List list) {
  96. NBTTagCompound nbttagcompound = worlddata.a(list);
  97. NBTTagCompound nbttagcompound1 = new NBTTagCompound();
  98. nbttagcompound1.a("Data", (NBTBase) nbttagcompound);
  99. try {
  100. File file1 = new File(this.b, "level.dat_new");
  101. File file2 = new File(this.b, "level.dat_old");
  102. File file3 = new File(this.b, "level.dat");
  103. CompressedStreamTools.a(nbttagcompound1, (OutputStream) (new FileOutputStream(file1)));
  104. if (file2.exists()) {
  105. file2.delete();
  106. }
  107. file3.renameTo(file2);
  108. if (file3.exists()) {
  109. file3.delete();
  110. }
  111. file1.renameTo(file3);
  112. if (file1.exists()) {
  113. file1.delete();
  114. }
  115. } catch (Exception exception) {
  116. exception.printStackTrace();
  117. }
  118. }
  119. public void a(WorldData worlddata) {
  120. NBTTagCompound nbttagcompound = worlddata.a();
  121. NBTTagCompound nbttagcompound1 = new NBTTagCompound();
  122. nbttagcompound1.a("Data", (NBTBase) nbttagcompound);
  123. try {
  124. File file1 = new File(this.b, "level.dat_new");
  125. File file2 = new File(this.b, "level.dat_old");
  126. File file3 = new File(this.b, "level.dat");
  127. CompressedStreamTools.a(nbttagcompound1, (OutputStream) (new FileOutputStream(file1)));
  128. if (file2.exists()) {
  129. file2.delete();
  130. }
  131. file3.renameTo(file2);
  132. if (file3.exists()) {
  133. file3.delete();
  134. }
  135. file1.renameTo(file3);
  136. if (file1.exists()) {
  137. file1.delete();
  138. }
  139. } catch (Exception exception) {
  140. exception.printStackTrace();
  141. }
  142. }
  143. public void a(EntityHuman entityhuman) {
  144. try {
  145. NBTTagCompound nbttagcompound = new NBTTagCompound();
  146. entityhuman.d(nbttagcompound);
  147. File file1 = new File(this.c, "_tmp_.dat");
  148. File file2 = new File(this.c, entityhuman.name + ".dat");
  149. CompressedStreamTools.a(nbttagcompound, (OutputStream) (new FileOutputStream(file1)));
  150. if (file2.exists()) {
  151. file2.delete();
  152. }
  153. file1.renameTo(file2);
  154. } catch (Exception exception) {
  155. a.warning("Failed to save player data for " + entityhuman.name);
  156. }
  157. }
  158. public void b(EntityHuman entityhuman) {
  159. NBTTagCompound nbttagcompound = this.a(entityhuman.name);
  160. if (nbttagcompound != null) {
  161. entityhuman.e(nbttagcompound);
  162. }
  163. }
  164. public NBTTagCompound a(String s) {
  165. try {
  166. File file1 = new File(this.c, s + ".dat");
  167. if (file1.exists()) {
  168. return CompressedStreamTools.a((InputStream) (new FileInputStream(file1)));
  169. }
  170. } catch (Exception exception) {
  171. a.warning("Failed to load player data for " + s);
  172. }
  173. return null;
  174. }
  175. public PlayerFileData d() {
  176. return this;
  177. }
  178. public void e() {}
  179. public File b(String s) {
  180. return new File(this.d, s + ".dat");
  181. }
  182. }