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
|
package net.minecraft.server;
public class ChunkSection {
private int yPos;
private int nonEmptyBlockCount;
private int tickingBlockCount;
private byte[] blockIds;
private NibbleArray extBlockIds;
private NibbleArray blockData;
private NibbleArray emittedLight;
private NibbleArray skyLight;
public ChunkSection(int i, boolean flag) {
this.yPos = i;
this.blockIds = new byte[4096];
this.blockData = new NibbleArray(this.blockIds.length, 4);
this.emittedLight = new NibbleArray(this.blockIds.length, 4);
if (flag) {
this.skyLight = new NibbleArray(this.blockIds.length, 4);
}
}
// CraftBukkit start
public ChunkSection(int y, boolean flag, byte[] blkIds, byte[] extBlkIds) {
this.yPos = y;
this.blockIds = blkIds;
if (extBlkIds != null) {
this.extBlockIds = new NibbleArray(extBlkIds, 4);
}
this.blockData = new NibbleArray(this.blockIds.length, 4);
this.emittedLight = new NibbleArray(this.blockIds.length, 4);
if (flag) {
this.skyLight = new NibbleArray(this.blockIds.length, 4);
}
this.recalcBlockCounts();
}
// CraftBukkit end
public Block getTypeId(int i, int j, int k) {
int l = this.blockIds[j << 8 | k << 4 | i] & 255;
if (this.extBlockIds != null) {
l |= this.extBlockIds.a(i, j, k) << 8;
}
return Block.e(l);
}
public void setTypeId(int i, int j, int k, Block block) {
int l = this.blockIds[j << 8 | k << 4 | i] & 255;
if (this.extBlockIds != null) {
l |= this.extBlockIds.a(i, j, k) << 8;
}
Block block1 = Block.e(l);
if (block1 != Blocks.AIR) {
--this.nonEmptyBlockCount;
if (block1.isTicking()) {
--this.tickingBlockCount;
}
}
if (block != Blocks.AIR) {
++this.nonEmptyBlockCount;
if (block.isTicking()) {
++this.tickingBlockCount;
}
}
int i1 = Block.b(block);
this.blockIds[j << 8 | k << 4 | i] = (byte) (i1 & 255);
if (i1 > 255) {
if (this.extBlockIds == null) {
this.extBlockIds = new NibbleArray(this.blockIds.length, 4);
}
this.extBlockIds.a(i, j, k, (i1 & 3840) >> 8);
} else if (this.extBlockIds != null) {
this.extBlockIds.a(i, j, k, 0);
}
}
public int getData(int i, int j, int k) {
return this.blockData.a(i, j, k);
}
public void setData(int i, int j, int k, int l) {
this.blockData.a(i, j, k, l);
}
public boolean isEmpty() {
return this.nonEmptyBlockCount == 0;
}
public boolean shouldTick() {
return this.tickingBlockCount > 0;
}
public int getYPosition() {
return this.yPos;
}
public void setSkyLight(int i, int j, int k, int l) {
this.skyLight.a(i, j, k, l);
}
public int getSkyLight(int i, int j, int k) {
return this.skyLight.a(i, j, k);
}
public void setEmittedLight(int i, int j, int k, int l) {
this.emittedLight.a(i, j, k, l);
}
public int getEmittedLight(int i, int j, int k) {
return this.emittedLight.a(i, j, k);
}
public void recalcBlockCounts() {
// CraftBukkit start - Optimize for speed
byte[] blkIds = this.blockIds;
int cntNonEmpty = 0;
int cntTicking = 0;
if (this.extBlockIds == null) { // No extended block IDs? Don't waste time messing with them
for (int off = 0; off < blkIds.length; off++) {
int l = blkIds[off] & 0xFF;
if (l > 0) {
if (Block.e(l) == null) {
blkIds[off] = 0;
} else {
++cntNonEmpty;
if (Block.e(l).isTicking()) {
++cntTicking;
}
}
}
}
} else {
byte[] ext = this.extBlockIds.a;
for (int off = 0, off2 = 0; off < blkIds.length;) {
byte extid = ext[off2];
int l = (blkIds[off] & 0xFF) | ((extid & 0xF) << 8); // Even data
if (l > 0) {
if (Block.e(l) == null) {
blkIds[off] = 0;
ext[off2] &= 0xF0;
} else {
++cntNonEmpty;
if (Block.e(l).isTicking()) {
++cntTicking;
}
}
}
off++;
l = (blkIds[off] & 0xFF) | ((extid & 0xF0) << 4); // Odd data
if (l > 0) {
if (Block.e(l) == null) {
blkIds[off] = 0;
ext[off2] &= 0x0F;
} else {
++cntNonEmpty;
if (Block.e(l).isTicking()) {
++cntTicking;
}
}
}
off++;
off2++;
}
}
this.nonEmptyBlockCount = cntNonEmpty;
this.tickingBlockCount = cntTicking;
}
public void old_recalcBlockCounts() {
// CraftBukkit end
this.nonEmptyBlockCount = 0;
this.tickingBlockCount = 0;
for (int i = 0; i < 16; ++i) {
for (int j = 0; j < 16; ++j) {
for (int k = 0; k < 16; ++k) {
Block block = this.getTypeId(i, j, k);
if (block != Blocks.AIR) {
++this.nonEmptyBlockCount;
if (block.isTicking()) {
++this.tickingBlockCount;
}
}
}
}
}
}
public byte[] getIdArray() {
return this.blockIds;
}
public NibbleArray getExtendedIdArray() {
return this.extBlockIds;
}
public NibbleArray getDataArray() {
return this.blockData;
}
public NibbleArray getEmittedLightArray() {
return this.emittedLight;
}
public NibbleArray getSkyLightArray() {
return this.skyLight;
}
public void setIdArray(byte[] abyte) {
this.blockIds = this.validateByteArray(abyte); // CraftBukkit - Validate data
}
public void setExtendedIdArray(NibbleArray nibblearray) {
// CraftBukkit start - Don't hang on to an empty nibble array
boolean empty = true;
for (int i = 0; i < nibblearray.a.length; i++) {
if (nibblearray.a[i] != 0) {
empty = false;
break;
}
}
if (empty) {
return;
}
// CraftBukkit end
this.extBlockIds = this.validateNibbleArray(nibblearray); // CraftBukkit - Validate data
}
public void setDataArray(NibbleArray nibblearray) {
this.blockData = this.validateNibbleArray(nibblearray); // CraftBukkit - Validate data
}
public void setEmittedLightArray(NibbleArray nibblearray) {
this.emittedLight = this.validateNibbleArray(nibblearray); // CraftBukkit - Validate data
}
public void setSkyLightArray(NibbleArray nibblearray) {
this.skyLight = this.validateNibbleArray(nibblearray); // CraftBukkit - Validate data
}
// CraftBukkit start - Validate array lengths
private NibbleArray validateNibbleArray(NibbleArray nibbleArray) {
if (nibbleArray != null && nibbleArray.a.length < 2048) {
byte[] newArray = new byte[2048];
System.arraycopy(nibbleArray.a, 0, newArray, 0, ((nibbleArray.a.length > 2048) ? 2048 : nibbleArray.a.length));
nibbleArray = new NibbleArray(newArray, 4);
}
return nibbleArray;
}
private byte[] validateByteArray(byte[] byteArray) {
if (byteArray != null && byteArray.length < 4096) {
byte[] newArray = new byte[4096];
System.arraycopy(byteArray, 0, newArray, 0, byteArray.length);
byteArray = newArray;
}
return byteArray;
}
// CraftBukkit end
}
|