Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
1package net.minecraft.server;
2
3import com.legacyminecraft.poseidon.PoseidonConfig;
4import com.projectposeidon.johnymuffin.UUIDManager;
5
6import java.io.*;
7import java.util.List;
8import java.util.UUID;
9import java.util.logging.Logger;
10
11public class PlayerNBTManager implements PlayerFileData, IDataManager {
12
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 private UUID uuid = null; // CraftBukkit
19
20 public PlayerNBTManager(File file1, String s, boolean flag) {
21 this.b = new File(file1, s);
22 this.b.mkdirs();
23 this.c = new File(this.b, "players");
24 this.d = new File(this.b, "data");
25 this.d.mkdirs();
26 if (flag) {
27 this.c.mkdirs();
28 }
29
30 this.f();
31 }
32
33 private void f() {
34 try {
35 File file1 = new File(this.b, "session.lock");
36 DataOutputStream dataoutputstream = new DataOutputStream(new FileOutputStream(file1));
37
38 try {
39 dataoutputstream.writeLong(this.e);
40 } finally {
41 dataoutputstream.close();
42 }
43 } catch (IOException ioexception) {
44 ioexception.printStackTrace();
45 throw new RuntimeException("Failed to check session lock, aborting");
46 }
47 }
48
49 protected File a() {
50 return this.b;
51 }
52
53 public void b() {
54 try {
55 File file1 = new File(this.b, "session.lock");
56 DataInputStream datainputstream = new DataInputStream(new FileInputStream(file1));
57
58 try {
59 if (datainputstream.readLong() != this.e) {
60 throw new MinecraftException("The save is being accessed from another location, aborting");
61 }
62 } finally {
63 datainputstream.close();
64 }
65 } catch (IOException ioexception) {
66 throw new MinecraftException("Failed to check session lock, aborting");
67 }
68 }
69
70 public IChunkLoader a(WorldProvider worldprovider) {
71 if (worldprovider instanceof WorldProviderHell) {
72 File file1 = new File(this.b, "DIM-1");
73
74 file1.mkdirs();
75 return new ChunkLoader(file1, true);
76 } else {
77 return new ChunkLoader(this.b, true);
78 }
79 }
80
81 public WorldData c() {
82 File file1 = new File(this.b, "level.dat");
83 NBTTagCompound nbttagcompound;
84 NBTTagCompound nbttagcompound1;
85
86 if (file1.exists()) {
87 try {
88 nbttagcompound = CompressedStreamTools.a((InputStream) (new FileInputStream(file1)));
89 nbttagcompound1 = nbttagcompound.k("Data");
90 return new WorldData(nbttagcompound1);
91 } catch (Exception exception) {
92 exception.printStackTrace();
93 }
94 }
95
96 file1 = new File(this.b, "level.dat_old");
97 if (file1.exists()) {
98 try {
99 nbttagcompound = CompressedStreamTools.a((InputStream) (new FileInputStream(file1)));
100 nbttagcompound1 = nbttagcompound.k("Data");
101 return new WorldData(nbttagcompound1);
102 } catch (Exception exception1) {
103 exception1.printStackTrace();
104 }
105 }
106
107 return null;
108 }
109
110 public void a(WorldData worlddata, List list) {
111 NBTTagCompound nbttagcompound = worlddata.a(list);
112 NBTTagCompound nbttagcompound1 = new NBTTagCompound();
113
114 nbttagcompound1.a("Data", (NBTBase) nbttagcompound);
115
116 try {
117 File file1 = new File(this.b, "level.dat_new");
118 File file2 = new File(this.b, "level.dat_old");
119 File file3 = new File(this.b, "level.dat");
120
121 CompressedStreamTools.a(nbttagcompound1, (OutputStream) (new FileOutputStream(file1)));
122 if (file2.exists()) {
123 file2.delete();
124 }
125
126 file3.renameTo(file2);
127 if (file3.exists()) {
128 file3.delete();
129 }
130
131 file1.renameTo(file3);
132 if (file1.exists()) {
133 file1.delete();
134 }
135 } catch (Exception exception) {
136 exception.printStackTrace();
137 }
138 }
139
140 public void a(WorldData worlddata) {
141 NBTTagCompound nbttagcompound = worlddata.a();
142 NBTTagCompound nbttagcompound1 = new NBTTagCompound();
143
144 nbttagcompound1.a("Data", (NBTBase) nbttagcompound);
145
146 try {
147 File file1 = new File(this.b, "level.dat_new");
148 File file2 = new File(this.b, "level.dat_old");
149 File file3 = new File(this.b, "level.dat");
150
151 CompressedStreamTools.a(nbttagcompound1, (OutputStream) (new FileOutputStream(file1)));
152 if (file2.exists()) {
153 file2.delete();
154 }
155
156 file3.renameTo(file2);
157 if (file3.exists()) {
158 file3.delete();
159 }
160
161 file1.renameTo(file3);
162 if (file1.exists()) {
163 file1.delete();
164 }
165 } catch (Exception exception) {
166 exception.printStackTrace();
167 }
168 }
169
170 public void a(EntityHuman entityhuman) {
171 if ((boolean) PoseidonConfig.getInstance().getConfigOption("settings.save-playerdata-by-uuid")) {
172 try {
173 NBTTagCompound nbttagcompound = new NBTTagCompound();
174
175 entityhuman.d(nbttagcompound);
176 File file1 = new File(this.c, "_tmp_.dat");
177 //File file2 = new File(this.c, entityhuman.name + ".dat");
178 //UUIDPlayerStorage.getInstance().getUUIDGraceful(entityhuman.name)
179 File file2 = new File(this.c, UUIDManager.getInstance().getUUIDGraceful(entityhuman.name) + ".dat");
180 CompressedStreamTools.a(nbttagcompound, (OutputStream) (new FileOutputStream(file1)));
181 if (file2.exists()) {
182 file2.delete();
183 }
184
185 file1.renameTo(file2);
186 } catch (Exception exception) {
187 a.warning("Failed to save player data for " + entityhuman.name);
188 }
189 } else {
190 try {
191 NBTTagCompound nbttagcompound = new NBTTagCompound();
192
193 entityhuman.d(nbttagcompound);
194 File file1 = new File(this.c, "_tmp_.dat");
195 File file2 = new File(this.c, entityhuman.name + ".dat");
196
197 CompressedStreamTools.a(nbttagcompound, (OutputStream) (new FileOutputStream(file1)));
198 if (file2.exists()) {
199 file2.delete();
200 }
201
202 file1.renameTo(file2);
203 } catch (Exception exception) {
204 a.warning("Failed to save player data for " + entityhuman.name);
205 }
206 }
207 }
208
209 public void b(EntityHuman entityhuman) {
210 NBTTagCompound nbttagcompound = this.a(entityhuman.name);
211
212 if (nbttagcompound != null) {
213 entityhuman.e(nbttagcompound);
214 }
215 }
216
217 //Credit https://www.journaldev.com/861/java-copy-file
218 private static void copyFileUsingStream(File source, File dest) throws IOException {
219 InputStream is = null;
220 OutputStream os = null;
221 try {
222 is = new FileInputStream(source);
223 os = new FileOutputStream(dest);
224 byte[] buffer = new byte[1024];
225 int length;
226 while ((length = is.read(buffer)) > 0) {
227 os.write(buffer, 0, length);
228 }
229 } finally {
230 is.close();
231 os.close();
232 }
233 }
234
235 public NBTTagCompound a(String s) {
236 if ((boolean) PoseidonConfig.getInstance().getConfigOption("settings.save-playerdata-by-uuid")) {
237 try {
238 File file1 = new File(this.c, UUIDManager.getInstance().getUUIDGraceful(s) + ".dat");
239 File file2 = new File(this.c, s + ".dat");
240 if (!file1.exists()) {
241 if (file2.exists()) {
242 //Convert player data
243 copyFileUsingStream(file2, file1);
244 File file3 = new File(this.c, s + ".datbackup");
245 file2.renameTo(file3);
246 System.out.println("Converting playerdata for " + s + " to a UUID");
247 }
248 }
249
250
251 if (file1.exists()) {
252 return CompressedStreamTools.a((InputStream) (new FileInputStream(file1)));
253 }
254 } catch (Exception exception) {
255 a.warning("Failed to load player data for " + s);
256 }
257
258 return null;
259 } else {
260 try {
261 File file1 = new File(this.c, s + ".dat");
262
263 if (file1.exists()) {
264 return CompressedStreamTools.a((InputStream) (new FileInputStream(file1)));
265 }
266 } catch (Exception exception) {
267 a.warning("Failed to load player data for " + s);
268 }
269
270 return null;
271 }
272
273 }
274
275 public PlayerFileData d() {
276 return this;
277 }
278
279 public void e() {
280 }
281
282 public File b(String s) {
283 return new File(this.d, s + ".dat");
284 }
285
286 // CraftBukkit start
287 public UUID getUUID() {
288 if (uuid != null) return uuid;
289 try {
290 File file1 = new File(this.b, "uid.dat");
291 if (!file1.exists()) {
292 DataOutputStream dos = new DataOutputStream(new FileOutputStream(file1));
293 uuid = UUID.randomUUID();
294 dos.writeLong(uuid.getMostSignificantBits());
295 dos.writeLong(uuid.getLeastSignificantBits());
296 dos.close();
297 } else {
298 DataInputStream dis = new DataInputStream(new FileInputStream(file1));
299 uuid = new UUID(dis.readLong(), dis.readLong());
300 dis.close();
301 }
302 return uuid;
303 } catch (IOException ex) {
304 return null;
305 }
306 }
307 // CraftBukkit end
308}