-
Notifications
You must be signed in to change notification settings - Fork 46
/
Chunk.java
340 lines (285 loc) · 10 KB
/
Chunk.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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package net.querz.nbt.mca;
import net.querz.nbt.CompoundTag;
import net.querz.nbt.ListTag;
import net.querz.nbt.Tag;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
public class Chunk {
public static final int DEFAULT_DATA_VERSION = 1628;
private int lastMCAUpdate;
private CompoundTag data;
private int dataVersion;
private long lastUpdate;
private long inhabitedTime;
private int[] biomes;
private CompoundTag heightMaps;
private CompoundTag carvingMasks;
private Section[] sections = new Section[16]; //always initialized with size = 16 for fast access
private ListTag<CompoundTag> entities;
private ListTag<CompoundTag> tileEntities;
private ListTag<CompoundTag> tileTicks;
private ListTag<CompoundTag> liquidTicks;
private ListTag<ListTag<?>> lights;
private ListTag<ListTag<?>> liquidsToBeTicked;
private ListTag<ListTag<?>> toBeTicked;
private ListTag<ListTag<?>> postProcessing;
private String status;
private CompoundTag structures;
Chunk(int lastMCAUpdate) {
this.lastMCAUpdate = lastMCAUpdate;
}
public Chunk(CompoundTag data) {
this.data = data;
initReferences();
}
private void initReferences() {
if (data == null) {
throw new NullPointerException("data cannot be null");
}
CompoundTag level;
if ((level = data.getCompoundTag("Level")) == null) {
throw new IllegalArgumentException("data does not contain \"Level\" tag");
}
this.dataVersion = data.getInt("DataVersion");
this.inhabitedTime = level.getLong("InhabitedTime");
this.lastUpdate = level.getLong("LastUpdate");
this.biomes = level.getIntArray("Biomes");
this.heightMaps = level.getCompoundTag("HeightMaps");
this.carvingMasks = level.getCompoundTag("CarvingMasks");
this.entities = level.containsKey("Entities") ? level.getListTag("Entities").asCompoundTagList() : null;
this.tileEntities = level.containsKey("TileEntities") ? level.getListTag("TileEntities").asCompoundTagList() : null;
this.tileTicks = level.containsKey("TileTicks") ? level.getListTag("TileTicks").asCompoundTagList() : null;
this.liquidTicks = level.containsKey("LiquidTicks") ? level.getListTag("LiquidTicks").asCompoundTagList() : null;
this.lights = level.containsKey("Lights") ? level.getListTag("Lights").asListTagList() : null;
this.liquidsToBeTicked = level.containsKey("LiquidsToBeTicked") ? level.getListTag("LiquidsToBeTicked").asListTagList() : null;
this.toBeTicked = level.containsKey("ToBeTicked") ? level.getListTag("ToBeTicked").asListTagList() : null;
this.postProcessing = level.containsKey("PostProcessing") ? level.getListTag("PostProcessing").asListTagList() : null;
this.status = level.getString("Status");
this.structures = level.getCompoundTag("Structures");
if (level.containsKey("Sections")) {
for (CompoundTag section : level.getListTag("Sections").asCompoundTagList()) {
this.sections[section.getByte("Y")] = new Section(section);
}
}
}
public int serialize(RandomAccessFile raf, int xPos, int zPos) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
try (DataOutputStream nbtOut = new DataOutputStream(new BufferedOutputStream(CompressionType.ZLIB.compress(baos)))) {
updateHandle(xPos, zPos).serialize(nbtOut, Tag.DEFAULT_MAX_DEPTH);
}
byte[] rawData = baos.toByteArray();
raf.writeInt(rawData.length);
raf.writeByte(CompressionType.ZLIB.getID());
raf.write(rawData);
return rawData.length + 5;
}
public void deserialize(RandomAccessFile raf) throws IOException {
byte compressionTypeByte = raf.readByte();
CompressionType compressionType = CompressionType.getFromID(compressionTypeByte);
if (compressionType == null) {
throw new IOException("invalid compression type " + compressionTypeByte);
}
DataInputStream dis = new DataInputStream(new BufferedInputStream(compressionType.decompress(new FileInputStream(raf.getFD()))));
Tag<?> tag = Tag.deserialize(dis, Tag.DEFAULT_MAX_DEPTH);
if (tag instanceof CompoundTag) {
data = (CompoundTag) tag;
initReferences();
} else {
throw new IOException("invalid data tag: " + (tag == null ? "null" : tag.getClass().getName()));
}
}
public int getBiomeAt(int blockX, int blockZ) {
if (biomes == null || biomes.length != 256) {
return -1;
}
return biomes[getBlockIndex(blockX, blockZ)];
}
public void setBiomeAt(int blockX, int blockZ, int biomeID) {
if (biomes == null || biomes.length != 256) {
biomes = new int[256];
for (int i = 0; i < biomes.length; i++) {
biomes[i] = -1;
}
}
biomes[getBlockIndex(blockX, blockZ)] = biomeID;
}
public CompoundTag getBlockStateAt(int blockX, int blockY, int blockZ) {
Section section = sections[MCAUtil.blockToChunk(blockY)];
if (section == null) {
return null;
}
return section.getBlockStateAt(blockX, blockY, blockZ);
}
public void setBlockStateAt(int blockX, int blockY, int blockZ, CompoundTag state, boolean cleanup) {
int sectionIndex = MCAUtil.blockToChunk(blockY);
Section section = sections[sectionIndex];
if (section == null) {
section = sections[sectionIndex] = Section.newSection();
}
section.setBlockStateAt(blockX, blockY, blockZ, state, cleanup);
}
public int getDataVersion() {
return dataVersion;
}
public void setDataVersion(int dataVersion) {
this.dataVersion = dataVersion;
}
public int getLastMCAUpdate() {
return lastMCAUpdate;
}
public void setLastMCAUpdate(int lastMCAUpdate) {
this.lastMCAUpdate = lastMCAUpdate;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Section getSection(int sectionY) {
return sections[sectionY];
}
public void setSection(int sectionY, Section section) {
sections[sectionY] = section;
}
public long getLastUpdate() {
return lastUpdate;
}
public void setLastUpdate(long lastUpdate) {
this.lastUpdate = lastUpdate;
}
public long getInhabitedTime() {
return inhabitedTime;
}
public void setInhabitedTime(long inhabitedTime) {
this.inhabitedTime = inhabitedTime;
}
public int[] getBiomes() {
return biomes;
}
public void setBiomes(int[] biomes) {
if (biomes != null && biomes.length != 256) {
throw new IllegalArgumentException("biomes array must have a length of 256");
}
this.biomes = biomes;
}
public CompoundTag getHeightMaps() {
return heightMaps;
}
public void setHeightMaps(CompoundTag heightMaps) {
this.heightMaps = heightMaps;
}
public CompoundTag getCarvingMasks() {
return carvingMasks;
}
public void setCarvingMasks(CompoundTag carvingMasks) {
this.carvingMasks = carvingMasks;
}
public ListTag<CompoundTag> getEntities() {
return entities;
}
public void setEntities(ListTag<CompoundTag> entities) {
this.entities = entities;
}
public ListTag<CompoundTag> getTileEntities() {
return tileEntities;
}
public void setTileEntities(ListTag<CompoundTag> tileEntities) {
this.tileEntities = tileEntities;
}
public ListTag<CompoundTag> getTileTicks() {
return tileTicks;
}
public void setTileTicks(ListTag<CompoundTag> tileTicks) {
this.tileTicks = tileTicks;
}
public ListTag<CompoundTag> getLiquidTicks() {
return liquidTicks;
}
public void setLiquidTicks(ListTag<CompoundTag> liquidTicks) {
this.liquidTicks = liquidTicks;
}
public ListTag<ListTag<?>> getLights() {
return lights;
}
public void setLights(ListTag<ListTag<?>> lights) {
this.lights = lights;
}
public ListTag<ListTag<?>> getLiquidsToBeTicked() {
return liquidsToBeTicked;
}
public void setLiquidsToBeTicked(ListTag<ListTag<?>> liquidsToBeTicked) {
this.liquidsToBeTicked = liquidsToBeTicked;
}
public ListTag<ListTag<?>> getToBeTicked() {
return toBeTicked;
}
public void setToBeTicked(ListTag<ListTag<?>> toBeTicked) {
this.toBeTicked = toBeTicked;
}
public ListTag<ListTag<?>> getPostProcessing() {
return postProcessing;
}
public void setPostProcessing(ListTag<ListTag<?>> postProcessing) {
this.postProcessing = postProcessing;
}
public CompoundTag getStructures() {
return structures;
}
public void setStructures(CompoundTag structures) {
this.structures = structures;
}
int getBlockIndex(int blockX, int blockZ) {
return (blockZ & 0xF) * 16 + (blockX & 0xF);
}
public void cleanupPalettesAndBlockStates() {
for (Section section : sections) {
if (section != null) {
section.cleanupPaletteAndBlockStates();
}
}
}
public static Chunk newChunk() {
Chunk c = new Chunk(0);
c.dataVersion = DEFAULT_DATA_VERSION;
c.data = new CompoundTag();
c.data.put("Level", new CompoundTag());
c.status = "mobs_spawned";
return c;
}
public CompoundTag updateHandle(int xPos, int zPos) {
data.putInt("DataVersion", dataVersion);
CompoundTag level = data.getCompoundTag("Level");
level.putInt("xPos", xPos);
level.putInt("zPos", zPos);
level.putLong("LastUpdate", lastUpdate);
level.putLong("InhabitedTime", inhabitedTime);
if (biomes != null && biomes.length == 256) level.putIntArray("Biomes", biomes);
if (heightMaps != null) level.put("HeightMaps", heightMaps);
if (carvingMasks != null) level.put("CarvingMasks", carvingMasks);
if (entities != null) level.put("Entities", entities);
if (tileEntities != null) level.put("TileEntities", tileEntities);
if (tileTicks != null) level.put("TileTicks", tileTicks);
if (liquidTicks != null) level.put("LiquidTicks", liquidTicks);
if (lights != null) level.put("Lights", lights);
if (liquidsToBeTicked != null) level.put("LiquidsToBeTicked", liquidsToBeTicked);
if (toBeTicked != null) level.put("ToBeTicked", toBeTicked);
if (postProcessing != null) level.put("PostProcessing", postProcessing);
level.putString("Status", status);
if (structures != null) level.put("Structures", structures);
ListTag<CompoundTag> sections = new ListTag<>(CompoundTag.class);
for (int i = 0; i < this.sections.length; i++) {
if (this.sections[i] != null) {
sections.add(this.sections[i].updateHandle(i));
}
}
level.put("Sections", sections);
return data;
}
}