SaveHandler .java

net.minecraft.world.storage.SaveHandler (class): members: initializationTime, saveDirectoryName, playersDirectory, worldDirectory, mapDataDir, flush.

net.minecraft.world.storageclass353 linescommon9.9 KB

Implements

API Surface (13)

File getWorldDirectory()
void checkSessionLock()
IChunkLoader getChunkLoader(WorldProvider provider)
WorldInfo loadWorldInfo()
void saveWorldInfoWithPlayer(WorldInfo worldInformation, NBTTagCompound tagCompound)
void saveWorldInfo(WorldInfo worldInformation)
void writePlayerData(EntityPlayer player)
NBTTagCompound readPlayerData(EntityPlayer player)
IPlayerFileData getPlayerNBTManager()
String[] getAvailablePlayerDat()
void flush()
File getMapFileFromName(String mapName)
String getWorldDirectoryName()

Fields & Constants (6)

logger
worldDirectory
playersDirectory
mapDataDir
initializationTime
saveDirectoryName

String literals (11)

"playerdata"
"session.lock"
"Failed to check session lock, aborting"
"Old Chunk Storage is no longer supported."
"level.dat"
"level.dat_old"
"level.dat_new"
".dat.tmp"
".dat"
"Failed to save player data for "
"Failed to load player data for "

Full Source

SaveHandler.java353 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.world.storage;
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.entity.player.EntityPlayer;
10
import net.minecraft.nbt.CompressedStreamTools;
11
import net.minecraft.nbt.NBTTagCompound;
12
import net.minecraft.server.MinecraftServer;
13
import net.minecraft.world.MinecraftException;
14
import net.minecraft.world.WorldProvider;
15
import net.minecraft.world.chunk.storage.IChunkLoader;
16
import org.apache.logging.log4j.LogManager;
17
import org.apache.logging.log4j.Logger;
18
19
public class SaveHandler implements ISaveHandler, IPlayerFileData
20
{
21
    private static final Logger logger = LogManager.getLogger();
22
23
    /** The directory in which to save world data. */
24
    private final File worldDirectory;
25
26
    /** The directory in which to save player data. */
27
    private final File playersDirectory;
28
    private final File mapDataDir;
29
30
    /**
31
     * The time in milliseconds when this field was initialized. Stored in the session lock file.
32
     */
33
    private final long initializationTime = MinecraftServer.getCurrentTimeMillis();
34
35
    /** The directory name of the world */
36
    private final String saveDirectoryName;
37
38
    public SaveHandler(File savesDirectory, String directoryName, boolean playersDirectoryIn)
39
    {
40
        this.worldDirectory = new File(savesDirectory, directoryName);
41
        this.worldDirectory.mkdirs();
42
        this.playersDirectory = new File(this.worldDirectory, "playerdata");
43
        this.mapDataDir = new File(this.worldDirectory, "data");
44
        this.mapDataDir.mkdirs();
45
        this.saveDirectoryName = directoryName;
46
47
        if (playersDirectoryIn)
48
        {
49
            this.playersDirectory.mkdirs();
50
        }
51
52
        this.setSessionLock();
53
    }
54
55
    /**
56
     * Creates a session lock file for this process
57
     */
58
    private void setSessionLock()
59
    {
60
        try
61
        {
62
            File file1 = new File(this.worldDirectory, "session.lock");
63
            DataOutputStream dataoutputstream = new DataOutputStream(new FileOutputStream(file1));
64
65
            try
66
            {
67
                dataoutputstream.writeLong(this.initializationTime);
68
            }
69
            finally
70
            {
71
                dataoutputstream.close();
72
            }
73
        }
74
        catch (IOException ioexception)
75
        {
76
            ioexception.printStackTrace();
77
            throw new RuntimeException("Failed to check session lock, aborting");
78
        }
79
    }
80
81
    /**
82
     * Gets the File object corresponding to the base directory of this world.
83
     */
84
    public File getWorldDirectory()
85
    {
86
        return this.worldDirectory;
87
    }
88
89
    /**
90
     * Checks the session lock to prevent save collisions
91
     */
92
    public void checkSessionLock() throws MinecraftException
93
    {
94
        try
95
        {
96
            File file1 = new File(this.worldDirectory, "session.lock");
97
            DataInputStream datainputstream = new DataInputStream(new FileInputStream(file1));
98
99
            try
100
            {
101
                if (datainputstream.readLong() != this.initializationTime)
102
                {
103
                    throw new MinecraftException("The save is being accessed from another location, aborting");
104
                }
105
            }
106
            finally
107
            {
108
                datainputstream.close();
109
            }
110
        }
111
        catch (IOException var7)
112
        {
113
            throw new MinecraftException("Failed to check session lock, aborting");
114
        }
115
    }
116
117
    /**
118
     * initializes and returns the chunk loader for the specified world provider
119
     */
120
    public IChunkLoader getChunkLoader(WorldProvider provider)
121
    {
122
        throw new RuntimeException("Old Chunk Storage is no longer supported.");
123
    }
124
125
    /**
126
     * Loads and returns the world info
127
     */
128
    public WorldInfo loadWorldInfo()
129
    {
130
        File file1 = new File(this.worldDirectory, "level.dat");
131
132
        if (file1.exists())
133
        {
134
            try
135
            {
136
                NBTTagCompound nbttagcompound2 = CompressedStreamTools.readCompressed(new FileInputStream(file1));
137
                NBTTagCompound nbttagcompound3 = nbttagcompound2.getCompoundTag("Data");
138
                return new WorldInfo(nbttagcompound3);
139
            }
140
            catch (Exception exception1)
141
            {
142
                exception1.printStackTrace();
143
            }
144
        }
145
146
        file1 = new File(this.worldDirectory, "level.dat_old");
147
148
        if (file1.exists())
149
        {
150
            try
151
            {
152
                NBTTagCompound nbttagcompound = CompressedStreamTools.readCompressed(new FileInputStream(file1));
153
                NBTTagCompound nbttagcompound1 = nbttagcompound.getCompoundTag("Data");
154
                return new WorldInfo(nbttagcompound1);
155
            }
156
            catch (Exception exception)
157
            {
158
                exception.printStackTrace();
159
            }
160
        }
161
162
        return null;
163
    }
164
165
    /**
166
     * Saves the given World Info with the given NBTTagCompound as the Player.
167
     */
168
    public void saveWorldInfoWithPlayer(WorldInfo worldInformation, NBTTagCompound tagCompound)
169
    {
170
        NBTTagCompound nbttagcompound = worldInformation.cloneNBTCompound(tagCompound);
171
        NBTTagCompound nbttagcompound1 = new NBTTagCompound();
172
        nbttagcompound1.setTag("Data", nbttagcompound);
173
174
        try
175
        {
176
            File file1 = new File(this.worldDirectory, "level.dat_new");
177
            File file2 = new File(this.worldDirectory, "level.dat_old");
178
            File file3 = new File(this.worldDirectory, "level.dat");
179
            CompressedStreamTools.writeCompressed(nbttagcompound1, new FileOutputStream(file1));
180
181
            if (file2.exists())
182
            {
183
                file2.delete();
184
            }
185
186
            file3.renameTo(file2);
187
188
            if (file3.exists())
189
            {
190
                file3.delete();
191
            }
192
193
            file1.renameTo(file3);
194
195
            if (file1.exists())
196
            {
197
                file1.delete();
198
            }
199
        }
200
        catch (Exception exception)
201
        {
202
            exception.printStackTrace();
203
        }
204
    }
205
206
    /**
207
     * used to update level.dat from old format to MCRegion format
208
     */
209
    public void saveWorldInfo(WorldInfo worldInformation)
210
    {
211
        NBTTagCompound nbttagcompound = worldInformation.getNBTTagCompound();
212
        NBTTagCompound nbttagcompound1 = new NBTTagCompound();
213
        nbttagcompound1.setTag("Data", nbttagcompound);
214
215
        try
216
        {
217
            File file1 = new File(this.worldDirectory, "level.dat_new");
218
            File file2 = new File(this.worldDirectory, "level.dat_old");
219
            File file3 = new File(this.worldDirectory, "level.dat");
220
            CompressedStreamTools.writeCompressed(nbttagcompound1, new FileOutputStream(file1));
221
222
            if (file2.exists())
223
            {
224
                file2.delete();
225
            }
226
227
            file3.renameTo(file2);
228
229
            if (file3.exists())
230
            {
231
                file3.delete();
232
            }
233
234
            file1.renameTo(file3);
235
236
            if (file1.exists())
237
            {
238
                file1.delete();
239
            }
240
        }
241
        catch (Exception exception)
242
        {
243
            exception.printStackTrace();
244
        }
245
    }
246
247
    /**
248
     * Writes the player data to disk from the specified PlayerEntityMP.
249
     */
250
    public void writePlayerData(EntityPlayer player)
251
    {
252
        try
253
        {
254
            NBTTagCompound nbttagcompound = new NBTTagCompound();
255
            player.writeToNBT(nbttagcompound);
256
            File file1 = new File(this.playersDirectory, player.getUniqueID().toString() + ".dat.tmp");
257
            File file2 = new File(this.playersDirectory, player.getUniqueID().toString() + ".dat");
258
            CompressedStreamTools.writeCompressed(nbttagcompound, new FileOutputStream(file1));
259
260
            if (file2.exists())
261
            {
262
                file2.delete();
263
            }
264
265
            file1.renameTo(file2);
266
        }
267
        catch (Exception var5)
268
        {
269
            logger.warn("Failed to save player data for " + player.getName());
270
        }
271
    }
272
273
    /**
274
     * Reads the player data from disk into the specified PlayerEntityMP.
275
     */
276
    public NBTTagCompound readPlayerData(EntityPlayer player)
277
    {
278
        NBTTagCompound nbttagcompound = null;
279
280
        try
281
        {
282
            File file1 = new File(this.playersDirectory, player.getUniqueID().toString() + ".dat");
283
284
            if (file1.exists() && file1.isFile())
285
            {
286
                nbttagcompound = CompressedStreamTools.readCompressed(new FileInputStream(file1));
287
            }
288
        }
289
        catch (Exception var4)
290
        {
291
            logger.warn("Failed to load player data for " + player.getName());
292
        }
293
294
        if (nbttagcompound != null)
295
        {
296
            player.readFromNBT(nbttagcompound);
297
        }
298
299
        return nbttagcompound;
300
    }
301
302
    public IPlayerFileData getPlayerNBTManager()
303
    {
304
        return this;
305
    }
306
307
    /**
308
     * Returns an array of usernames for which player.dat exists for.
309
     */
310
    public String[] getAvailablePlayerDat()
311
    {
312
        String[] astring = this.playersDirectory.list();
313
314
        if (astring == null)
315
        {
316
            astring = new String[0];
317
        }
318
319
        for (int i = 0; i < astring.length; ++i)
320
        {
321
            if (astring[i].endsWith(".dat"))
322
            {
323
                astring[i] = astring[i].substring(0, astring[i].length() - 4);
324
            }
325
        }
326
327
        return astring;
328
    }
329
330
    /**
331
     * Called to flush all changes to disk, waiting for them to complete.
332
     */
333
    public void flush()
334
    {
335
    }
336
337
    /**
338
     * Gets the file location of the given map
339
     */
340
    public File getMapFileFromName(String mapName)
341
    {
342
        return new File(this.mapDataDir, mapName + ".dat");
343
    }
344
345
    /**
346
     * Returns the name of the directory where world information is saved.
347
     */
348
    public String getWorldDirectoryName()
349
    {
350
        return this.saveDirectoryName;
351
    }
352
}
353

Deobfuscated MCP 1.8.9 source of SaveHandler from Marcelektro/MavenMCP-1.8.9 (net/minecraft/world/storage/SaveHandler.java), stored verbatim.