-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChunkLoader.java
More file actions
245 lines (229 loc) · 9.15 KB
/
ChunkLoader.java
File metadata and controls
245 lines (229 loc) · 9.15 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package net.minecraft.src;
// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) braces deadcode
import java.io.*;
import java.util.*;
public class ChunkLoader
implements IChunkLoader
{
public ChunkLoader(File file, boolean flag)
{
saveDir = file;
createIfNecessary = flag;
}
private File chunkFileForXZ(int i, int j)
{
String s = (new StringBuilder()).append("c.").append(Integer.toString(i, 36)).append(".").append(Integer.toString(j, 36)).append(".dat").toString();
String s1 = Integer.toString(i & 0x3f, 36);
String s2 = Integer.toString(j & 0x3f, 36);
File file = new File(saveDir, s1);
if(!file.exists())
{
if(createIfNecessary)
{
file.mkdir();
} else
{
return null;
}
}
file = new File(file, s2);
if(!file.exists())
{
if(createIfNecessary)
{
file.mkdir();
} else
{
return null;
}
}
file = new File(file, s);
if(!file.exists() && !createIfNecessary)
{
return null;
} else
{
return file;
}
}
public Chunk loadChunk(World world, int i, int j)
{
File file = chunkFileForXZ(i, j);
if(file != null && file.exists())
{
try
{
FileInputStream fileinputstream = new FileInputStream(file);
NBTTagCompound nbttagcompound = CompressedStreamTools.func_1138_a(fileinputstream);
if(!nbttagcompound.hasKey("Level"))
{
System.out.println((new StringBuilder()).append("Chunk file at ").append(i).append(",").append(j).append(" is missing level data, skipping").toString());
return null;
}
if(!nbttagcompound.getCompoundTag("Level").hasKey("Blocks"))
{
System.out.println((new StringBuilder()).append("Chunk file at ").append(i).append(",").append(j).append(" is missing block data, skipping").toString());
return null;
}
Chunk chunk = loadChunkIntoWorldFromCompound(world, nbttagcompound.getCompoundTag("Level"));
if(!chunk.isAtLocation(i, j))
{
System.out.println((new StringBuilder()).append("Chunk file at ").append(i).append(",").append(j).append(" is in the wrong location; relocating. (Expected ").append(i).append(", ").append(j).append(", got ").append(chunk.xPosition).append(", ").append(chunk.zPosition).append(")").toString());
nbttagcompound.setInteger("xPos", i);
nbttagcompound.setInteger("zPos", j);
chunk = loadChunkIntoWorldFromCompound(world, nbttagcompound.getCompoundTag("Level"));
}
return chunk;
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
return null;
}
public void saveChunk(World world, Chunk chunk)
{
world.checkSessionLock();
File file = chunkFileForXZ(chunk.xPosition, chunk.zPosition);
if(file.exists())
{
WorldInfo worldinfo = world.func_22144_v();
worldinfo.func_22297_b(worldinfo.func_22306_g() - file.length());
}
try
{
File file1 = new File(saveDir, "tmp_chunk.dat");
FileOutputStream fileoutputstream = new FileOutputStream(file1);
NBTTagCompound nbttagcompound = new NBTTagCompound();
NBTTagCompound nbttagcompound1 = new NBTTagCompound();
nbttagcompound.setTag("Level", nbttagcompound1);
storeChunkInCompound(chunk, world, nbttagcompound1);
CompressedStreamTools.writeGzippedCompoundToOutputStream(nbttagcompound, fileoutputstream);
fileoutputstream.close();
if(file.exists())
{
file.delete();
}
file1.renameTo(file);
WorldInfo worldinfo1 = world.func_22144_v();
worldinfo1.func_22297_b(worldinfo1.func_22306_g() + file.length());
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
public static void storeChunkInCompound(Chunk chunk, World world, NBTTagCompound nbttagcompound)
{
world.checkSessionLock();
nbttagcompound.setInteger("xPos", chunk.xPosition);
nbttagcompound.setInteger("zPos", chunk.zPosition);
nbttagcompound.setLong("LastUpdate", world.func_22139_r());
nbttagcompound.setByteArray("Blocks", chunk.blocks);
nbttagcompound.setByteArray("Data", chunk.data.data);
nbttagcompound.setByteArray("SkyLight", chunk.skylightMap.data);
nbttagcompound.setByteArray("BlockLight", chunk.blocklightMap.data);
nbttagcompound.setByteArray("HeightMap", chunk.heightMap);
nbttagcompound.setBoolean("TerrainPopulated", chunk.isTerrainPopulated);
chunk.hasEntities = false;
NBTTagList nbttaglist = new NBTTagList();
label0:
for(int i = 0; i < chunk.entities.length; i++)
{
Iterator iterator = chunk.entities[i].iterator();
do
{
if(!iterator.hasNext())
{
continue label0;
}
Entity entity = (Entity)iterator.next();
chunk.hasEntities = true;
NBTTagCompound nbttagcompound1 = new NBTTagCompound();
if(entity.addEntityID(nbttagcompound1))
{
nbttaglist.setTag(nbttagcompound1);
}
} while(true);
}
nbttagcompound.setTag("Entities", nbttaglist);
NBTTagList nbttaglist1 = new NBTTagList();
NBTTagCompound nbttagcompound2;
for(Iterator iterator1 = chunk.chunkTileEntityMap.values().iterator(); iterator1.hasNext(); nbttaglist1.setTag(nbttagcompound2))
{
TileEntity tileentity = (TileEntity)iterator1.next();
nbttagcompound2 = new NBTTagCompound();
tileentity.writeToNBT(nbttagcompound2);
}
nbttagcompound.setTag("TileEntities", nbttaglist1);
}
public static Chunk loadChunkIntoWorldFromCompound(World world, NBTTagCompound nbttagcompound)
{
int i = nbttagcompound.getInteger("xPos");
int j = nbttagcompound.getInteger("zPos");
Chunk chunk = new Chunk(world, i, j);
chunk.blocks = nbttagcompound.getByteArray("Blocks");
chunk.data = new NibbleArray(nbttagcompound.getByteArray("Data"));
chunk.skylightMap = new NibbleArray(nbttagcompound.getByteArray("SkyLight"));
chunk.blocklightMap = new NibbleArray(nbttagcompound.getByteArray("BlockLight"));
chunk.heightMap = nbttagcompound.getByteArray("HeightMap");
chunk.isTerrainPopulated = nbttagcompound.getBoolean("TerrainPopulated");
if(!chunk.data.isValid())
{
chunk.data = new NibbleArray(chunk.blocks.length);
}
if(chunk.heightMap == null || !chunk.skylightMap.isValid())
{
chunk.heightMap = new byte[256];
chunk.skylightMap = new NibbleArray(chunk.blocks.length);
chunk.func_1024_c();
}
if(!chunk.blocklightMap.isValid())
{
chunk.blocklightMap = new NibbleArray(chunk.blocks.length);
chunk.func_1014_a();
}
NBTTagList nbttaglist = nbttagcompound.getTagList("Entities");
if(nbttaglist != null)
{
for(int k = 0; k < nbttaglist.tagCount(); k++)
{
NBTTagCompound nbttagcompound1 = (NBTTagCompound)nbttaglist.tagAt(k);
Entity entity = EntityList.createEntityFromNBT(nbttagcompound1, world);
chunk.hasEntities = true;
if(entity != null)
{
chunk.addEntity(entity);
}
}
}
NBTTagList nbttaglist1 = nbttagcompound.getTagList("TileEntities");
if(nbttaglist1 != null)
{
for(int l = 0; l < nbttaglist1.tagCount(); l++)
{
NBTTagCompound nbttagcompound2 = (NBTTagCompound)nbttaglist1.tagAt(l);
TileEntity tileentity = TileEntity.createAndLoadEntity(nbttagcompound2);
if(tileentity != null)
{
chunk.func_1001_a(tileentity);
}
}
}
return chunk;
}
public void func_814_a()
{
}
public void saveExtraData()
{
}
public void saveExtraChunkData(World world, Chunk chunk)
{
}
private File saveDir;
private boolean createIfNecessary;
}