forked from cybertiger/Bukkit-LibNBT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InstanceDataManager.java
126 lines (118 loc) · 4.82 KB
/
InstanceDataManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.cyberiantiger.minecraft.unsafe.v1_10_R1;
import com.google.common.io.Files;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.UUID;
import java.util.logging.Level;
import net.minecraft.server.v1_10_R1.ChunkRegionLoader;
import net.minecraft.server.v1_10_R1.DataConverterManager;
import net.minecraft.server.v1_10_R1.IChunkLoader;
import net.minecraft.server.v1_10_R1.NBTCompressedStreamTools;
import net.minecraft.server.v1_10_R1.NBTTagCompound;
import net.minecraft.server.v1_10_R1.ServerNBTManager;
import net.minecraft.server.v1_10_R1.WorldData;
import net.minecraft.server.v1_10_R1.WorldProvider;
import net.minecraft.server.v1_10_R1.WorldProviderHell;
import net.minecraft.server.v1_10_R1.WorldProviderTheEnd;
import org.bukkit.plugin.Plugin;
// Have to extend WorldNBTStorage - CB casts IDataManager to it in World.getWorldFolder()
// cannot just implement IDataManager, PlayerFileData
class InstanceDataManager extends ServerNBTManager {
private static final String WORLD_DATA = "level.dat";
private static final String WORLD_DATA_OLD = "level.dat_old";
private final Plugin instances;
private final File loadDataFolder;
private final String world;
public InstanceDataManager(Plugin instances, String instanceName, File loadDataFolder, File saveDataFolder, DataConverterManager converter) {
// false flag - do not create players directory.
super(saveDataFolder.getParentFile(), saveDataFolder.getName(), false, converter);
this.instances = instances;
this.loadDataFolder = loadDataFolder;
this.world = instanceName;
}
@Override
public WorldData getWorldData() {
File levelData = new File(getDirectory(), WORLD_DATA);
if (levelData.isFile()) {
return super.getWorldData();
}
levelData = new File(getDirectory(), WORLD_DATA_OLD);
if (levelData.isFile()) {
return super.getWorldData();
}
File file1 = new File(loadDataFolder, WORLD_DATA);
NBTTagCompound nbttagcompound;
NBTTagCompound nbttagcompound1;
WorldData result = null;
if (file1.exists()) {
try {
nbttagcompound = NBTCompressedStreamTools.a((InputStream) (new FileInputStream(file1)));
nbttagcompound1 = nbttagcompound.getCompound("Data");
result = new WorldData(nbttagcompound1);
} catch (Exception exception) {
exception.printStackTrace();
}
} else {
file1 = new File(loadDataFolder, WORLD_DATA_OLD);
if (file1.exists()) {
try {
nbttagcompound = NBTCompressedStreamTools.a((InputStream) (new FileInputStream(file1)));
nbttagcompound1 = nbttagcompound.getCompound("Data");
result = new WorldData(nbttagcompound1);
} catch (Exception exception1) {
exception1.printStackTrace();
}
}
}
if (result != null) {
result.a(world);
}
return result;
}
@Override
public IChunkLoader createChunkLoader(WorldProvider wp) {
File loadChunkDir;
File saveChunkDir;
if (wp instanceof WorldProviderHell) {
loadChunkDir = new File(loadDataFolder, "DIM-1");
saveChunkDir = new File(getDirectory(), "DIM-1");
} else if (wp instanceof WorldProviderTheEnd) {
loadChunkDir = new File(loadDataFolder, "DIM1");
saveChunkDir = new File(getDirectory(), "DIM1");
} else {
loadChunkDir = loadDataFolder;
saveChunkDir = getDirectory();
}
ChunkRegionLoader loadLoader = new ChunkRegionLoader(loadChunkDir, this.a);
ChunkRegionLoader saveLoader = new ChunkRegionLoader(saveChunkDir, this.a);
return new InstanceChunkLoader(loadLoader, saveLoader);
}
@Override
public File getDataFile(String string) {
File result = new File(this.loadDataFolder, string + ".dat");
if (result.isFile()) {
return result;
}
File source = new File(getDirectory(), string + ".dat");
if (!source.isFile()) {
return result;
}
try {
Files.copy(source, result);
} catch (IOException ex) {
instances.getLogger().log(Level.SEVERE, "Error copying " + source.getPath() + " to " + result.getPath() + " for Instance world: " + world, ex);
}
return result;
}
@Override
public UUID getUUID() {
return UUID.nameUUIDFromBytes( ("libnbt:" + world).getBytes(StandardCharsets.UTF_8) );
}
}