Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lDEVinux
GitHub Repository: lDEVinux/eaglercraft
Path: blob/main/sp-server/src_aux/SaveHandler.java
8641 views
1
package net.minecraft.src;
2
3
import java.io.DataInputStream;
4
import java.io.DataOutputStream;
5
import java.io.File;
6
import java.io.FileInputStream;
7
import java.io.FileOutputStream;
8
import java.io.IOException;
9
import net.minecraft.server.MinecraftServer;
10
11
public class SaveHandler implements ISaveHandler, IPlayerFileData {
12
/** The directory in which to save world data. */
13
private final File worldDirectory;
14
15
/** The directory in which to save player data. */
16
private final File playersDirectory;
17
private final File mapDataDir;
18
19
/**
20
* The time in milliseconds when this field was initialized. Stored in the
21
* session lock file.
22
*/
23
private final long initializationTime = System.currentTimeMillis();
24
25
/** The directory name of the world */
26
private final String saveDirectoryName;
27
28
public SaveHandler(File par1File, String par2Str, boolean par3) {
29
this.worldDirectory = new File(par1File, par2Str);
30
this.worldDirectory.mkdirs();
31
this.playersDirectory = new File(this.worldDirectory, "players");
32
this.mapDataDir = new File(this.worldDirectory, "data");
33
this.mapDataDir.mkdirs();
34
this.saveDirectoryName = par2Str;
35
36
if (par3) {
37
this.playersDirectory.mkdirs();
38
}
39
40
this.setSessionLock();
41
}
42
43
/**
44
* Creates a session lock file for this process
45
*/
46
private void setSessionLock() {
47
try {
48
File var1 = new File(this.worldDirectory, "session.lock");
49
DataOutputStream var2 = new DataOutputStream(new FileOutputStream(var1));
50
51
try {
52
var2.writeLong(this.initializationTime);
53
} finally {
54
var2.close();
55
}
56
} catch (IOException var7) {
57
var7.printStackTrace();
58
throw new RuntimeException("Failed to check session lock, aborting");
59
}
60
}
61
62
/**
63
* Gets the File object corresponding to the base directory of this world.
64
*/
65
protected File getWorldDirectory() {
66
return this.worldDirectory;
67
}
68
69
/**
70
* Checks the session lock to prevent save collisions
71
*/
72
public void checkSessionLock() throws MinecraftException {
73
try {
74
File var1 = new File(this.worldDirectory, "session.lock");
75
DataInputStream var2 = new DataInputStream(new FileInputStream(var1));
76
77
try {
78
if (var2.readLong() != this.initializationTime) {
79
throw new MinecraftException("The save is being accessed from another location, aborting");
80
}
81
} finally {
82
var2.close();
83
}
84
} catch (IOException var7) {
85
throw new MinecraftException("Failed to check session lock, aborting");
86
}
87
}
88
89
/**
90
* initializes and returns the chunk loader for the specified world provider
91
*/
92
public IChunkLoader getChunkLoader(WorldProvider par1WorldProvider) {
93
throw new RuntimeException("Old Chunk Storage is no longer supported.");
94
}
95
96
/**
97
* Loads and returns the world info
98
*/
99
public WorldInfo loadWorldInfo() {
100
File var1 = new File(this.worldDirectory, "level.dat");
101
NBTTagCompound var2;
102
NBTTagCompound var3;
103
104
if (var1.exists()) {
105
try {
106
var2 = CompressedStreamTools.readCompressed(new FileInputStream(var1));
107
var3 = var2.getCompoundTag("Data");
108
return new WorldInfo(var3);
109
} catch (Exception var5) {
110
var5.printStackTrace();
111
}
112
}
113
114
var1 = new File(this.worldDirectory, "level.dat_old");
115
116
if (var1.exists()) {
117
try {
118
var2 = CompressedStreamTools.readCompressed(new FileInputStream(var1));
119
var3 = var2.getCompoundTag("Data");
120
return new WorldInfo(var3);
121
} catch (Exception var4) {
122
var4.printStackTrace();
123
}
124
}
125
126
return null;
127
}
128
129
/**
130
* Saves the given World Info with the given NBTTagCompound as the Player.
131
*/
132
public void saveWorldInfoWithPlayer(WorldInfo par1WorldInfo, NBTTagCompound par2NBTTagCompound) {
133
NBTTagCompound var3 = par1WorldInfo.cloneNBTCompound(par2NBTTagCompound);
134
NBTTagCompound var4 = new NBTTagCompound();
135
var4.setTag("Data", var3);
136
137
try {
138
File var5 = new File(this.worldDirectory, "level.dat_new");
139
File var6 = new File(this.worldDirectory, "level.dat_old");
140
File var7 = new File(this.worldDirectory, "level.dat");
141
CompressedStreamTools.writeCompressed(var4, new FileOutputStream(var5));
142
143
if (var6.exists()) {
144
var6.delete();
145
}
146
147
var7.renameTo(var6);
148
149
if (var7.exists()) {
150
var7.delete();
151
}
152
153
var5.renameTo(var7);
154
155
if (var5.exists()) {
156
var5.delete();
157
}
158
} catch (Exception var8) {
159
var8.printStackTrace();
160
}
161
}
162
163
/**
164
* used to update level.dat from old format to MCRegion format
165
*/
166
public void saveWorldInfo(WorldInfo par1WorldInfo) {
167
NBTTagCompound var2 = par1WorldInfo.getNBTTagCompound();
168
NBTTagCompound var3 = new NBTTagCompound();
169
var3.setTag("Data", var2);
170
171
try {
172
File var4 = new File(this.worldDirectory, "level.dat_new");
173
File var5 = new File(this.worldDirectory, "level.dat_old");
174
File var6 = new File(this.worldDirectory, "level.dat");
175
CompressedStreamTools.writeCompressed(var3, new FileOutputStream(var4));
176
177
if (var5.exists()) {
178
var5.delete();
179
}
180
181
var6.renameTo(var5);
182
183
if (var6.exists()) {
184
var6.delete();
185
}
186
187
var4.renameTo(var6);
188
189
if (var4.exists()) {
190
var4.delete();
191
}
192
} catch (Exception var7) {
193
var7.printStackTrace();
194
}
195
}
196
197
/**
198
* Writes the player data to disk from the specified PlayerEntityMP.
199
*/
200
public void writePlayerData(EntityPlayer par1EntityPlayer) {
201
try {
202
NBTTagCompound var2 = new NBTTagCompound();
203
par1EntityPlayer.writeToNBT(var2);
204
File var3 = new File(this.playersDirectory, par1EntityPlayer.username + ".dat.tmp");
205
File var4 = new File(this.playersDirectory, par1EntityPlayer.username + ".dat");
206
CompressedStreamTools.writeCompressed(var2, new FileOutputStream(var3));
207
208
if (var4.exists()) {
209
var4.delete();
210
}
211
212
var3.renameTo(var4);
213
} catch (Exception var5) {
214
MinecraftServer.getServer().getLogAgent()
215
.func_98236_b("Failed to save player data for " + par1EntityPlayer.username);
216
}
217
}
218
219
/**
220
* Reads the player data from disk into the specified PlayerEntityMP.
221
*/
222
public NBTTagCompound readPlayerData(EntityPlayer par1EntityPlayer) {
223
NBTTagCompound var2 = this.getPlayerData(par1EntityPlayer.username);
224
225
if (var2 != null) {
226
par1EntityPlayer.readFromNBT(var2);
227
}
228
229
return var2;
230
}
231
232
/**
233
* Gets the player data for the given playername as a NBTTagCompound.
234
*/
235
public NBTTagCompound getPlayerData(String par1Str) {
236
try {
237
File var2 = new File(this.playersDirectory, par1Str + ".dat");
238
239
if (var2.exists()) {
240
return CompressedStreamTools.readCompressed(new FileInputStream(var2));
241
}
242
} catch (Exception var3) {
243
MinecraftServer.getServer().getLogAgent().func_98236_b("Failed to load player data for " + par1Str);
244
}
245
246
return null;
247
}
248
249
public IPlayerFileData getPlayerNBTManager() {
250
return this;
251
}
252
253
/**
254
* Returns an array of usernames for which player.dat exists for.
255
*/
256
public String[] getAvailablePlayerDat() {
257
String[] var1 = this.playersDirectory.list();
258
259
for (int var2 = 0; var2 < var1.length; ++var2) {
260
if (var1[var2].endsWith(".dat")) {
261
var1[var2] = var1[var2].substring(0, var1[var2].length() - 4);
262
}
263
}
264
265
return var1;
266
}
267
268
/**
269
* Called to flush all changes to disk, waiting for them to complete.
270
*/
271
public void flush() {
272
}
273
274
/**
275
* Gets the file location of the given map
276
*/
277
public File getMapFileFromName(String par1Str) {
278
return new File(this.mapDataDir, par1Str + ".dat");
279
}
280
281
/**
282
* Returns the name of the directory where world information is saved.
283
*/
284
public String getWorldDirectoryName() {
285
return this.saveDirectoryName;
286
}
287
}
288
289