-
Notifications
You must be signed in to change notification settings - Fork 497
/
MapChunk.java
81 lines (66 loc) · 1.69 KB
/
MapChunk.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
package buildcraft.robotics.map;
import net.minecraft.block.material.MapColor;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.chunk.Chunk;
import buildcraft.api.core.BCLog;
public class MapChunk {
private static final int VERSION = 1;
private int x, z;
private byte[] data;
public MapChunk(int x, int z) {
this.x = x;
this.z = z;
data = new byte[256];
}
public MapChunk(NBTTagCompound compound) {
readFromNBT(compound);
}
public int getX() {
return x;
}
public int getZ() {
return z;
}
public int getColor(int x, int z) {
return (int) data[((z & 15) << 4) | (x & 15)];
}
public void update(Chunk chunk) {
for (int bz = 0; bz < 16; bz++) {
for (int bx = 0; bx < 16; bx++) {
int y = chunk.getHeightValue(bx, bz);
int color;
while ((color = chunk.getBlock(bx, y, bz).getMapColor(0).colorIndex) == MapColor.airColor.colorIndex) {
y--;
if (y < 0) {
break;
}
}
data[(bz << 4) | bx] = (byte) color;
}
}
}
public void readFromNBT(NBTTagCompound compound) {
int version = compound.getShort("version");
if (version > MapChunk.VERSION) {
BCLog.logger.error("Unsupported MapChunk version: " + version);
return;
}
x = compound.getInteger("x");
z = compound.getInteger("z");
data = compound.getByteArray("data");
if (data.length != 256) {
BCLog.logger.error("Invalid MapChunk data length: " + data.length);
data = new byte[256];
}
}
public void writeToNBT(NBTTagCompound compound) {
compound.setShort("version", (short) VERSION);
compound.setInteger("x", x);
compound.setInteger("z", z);
compound.setByteArray("data", data);
}
@Override
public int hashCode() {
return 31 * x + z;
}
}