/
PatchChunk.java
581 lines (490 loc) · 17.2 KB
/
PatchChunk.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
package nallar.patched.storage;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import cpw.mods.fml.common.FMLLog;
import nallar.collections.SynchronizedList;
import nallar.tickthreading.Log;
import nallar.tickthreading.patcher.Declare;
import nallar.tickthreading.util.BlockInfo;
import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.command.IEntitySelector;
import net.minecraft.entity.Entity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.MathHelper;
import net.minecraft.world.ChunkPosition;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.storage.ExtendedBlockStorage;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.EntityEvent;
import net.minecraftforge.event.world.ChunkEvent;
@SuppressWarnings ("unchecked")
public abstract class PatchChunk extends Chunk {
@Declare
public List pendingBlockUpdates_;
@Declare
public boolean queuedUnload_;
@Declare
public boolean partiallyUnloaded_;
@Declare
public boolean alreadySavedAfterUnload_;
public PatchChunk(World par1World, int par2, int par3) {
super(par1World, par2, par3);
}
private List<TileEntity> toInvalidate;
public void construct() {
for (int i = 0; i < entityLists.length; i++) {
entityLists[i] = new SynchronizedList();
}
toInvalidate = new ArrayList<TileEntity>();
}
@Override
@Declare
public boolean isNormallyLoaded() {
return !this.alreadySavedAfterUnload && !this.partiallyUnloaded && this.isChunkLoaded;
}
@Override
@Declare
public boolean needsSaving(boolean force, long worldTime) {
if (lastSaveTime == worldTime) {
return false;
}
if (force && (isModified || hasEntities || !chunkTileEntityMap.isEmpty() || lastSaveTime + 1000 < worldTime)) {
return true;
}
long nextSaveTime;
if (isModified) {
nextSaveTime = 4000;
} else if (hasEntities) {
nextSaveTime = 6000;
} else if (!chunkTileEntityMap.isEmpty()) {
nextSaveTime = 9000;
} else {
return false;
}
return (nextSaveTime + lastSaveTime) < worldTime;
}
@Override
public String toString() {
return "chunk at " + xPosition + ',' + zPosition + " which is " + (partiallyUnloaded ? (alreadySavedAfterUnload ? "unloaded and saved" : "unloading") : "loaded") + " and " + (isTerrainPopulated ? "" : "un") + "populated";
}
@Override
@Declare
public <T> ArrayList<T> getEntitiesOfType(Class<T> aClass) {
ArrayList<T> list = new ArrayList<T>();
int min = 0;
int max = entityLists.length;
for (int i = min; i < max; ++i) {
SynchronizedList<Entity> entityList = (SynchronizedList<Entity>) this.entityLists[i];
Object[] entities = entityList.elementData();
int length = entityList.size() - 1;
for (; length >= 0; length--) {
Entity entity = (Entity) entities[length];
if (entity == null) {
continue;
}
if (entity.getClass().equals(aClass)) {
list.add((T) entity);
}
}
}
return list;
}
@Override
public void getEntitiesOfTypeWithinAAAB(Class aClass, AxisAlignedBB aabb, List list, IEntitySelector selector) {
int var5 = MathHelper.floor_double((aabb.minY - 2D) / 16.0D);
int var6 = MathHelper.floor_double((aabb.maxY + 2D) / 16.0D);
if (var5 < 0) {
var5 = 0;
} else if (var5 >= this.entityLists.length) {
var5 = this.entityLists.length - 1;
}
if (var6 >= this.entityLists.length) {
var6 = this.entityLists.length - 1;
} else if (var6 < 0) {
var6 = 0;
}
for (int var7 = var5; var7 <= var6; ++var7) {
SynchronizedList<Entity> entityList = (SynchronizedList<Entity>) this.entityLists[var7];
Object[] entities = entityList.elementData();
int length = entityList.size() - 1;
for (; length >= 0; length--) {
Entity entity = (Entity) entities[length];
if (entity == null) {
continue;
}
if (entity.boundingBox.intersectsWith(aabb) && aClass.isAssignableFrom(entity.getClass()) && (selector == null || selector.isEntityApplicable(entity))) {
list.add(entity);
}
}
}
}
@Override
public void getEntitiesWithinAABBForEntity(Entity excludedEntity, AxisAlignedBB collisionArea, List collidingAABBs) {
getEntitiesWithinAABBForEntity(excludedEntity, collisionArea, collidingAABBs, 2000);
}
@Override
@Declare
public int getEntitiesWithinAABBForEntity(Entity excludedEntity, AxisAlignedBB collisionArea, List collidingAABBs, int limit) {
int var4 = MathHelper.floor_double((collisionArea.minY - 2D) / 16.0D);
int var5 = MathHelper.floor_double((collisionArea.maxY + 2D) / 16.0D);
if (var4 < 0) {
var4 = 0;
}
if (var5 >= entityLists.length) {
var5 = entityLists.length - 1;
}
for (int var6 = var4; var6 <= var5; ++var6) {
SynchronizedList<Entity> entityList = (SynchronizedList<Entity>) entityLists[var6];
Object[] entities = entityList.elementData();
int length = entityList.size() - 1;
for (; length >= 0; length--) {
Entity entity = (Entity) entities[length];
if (entity == null) {
continue;
}
if (entity != excludedEntity && entity.boundingBox.intersectsWith(collisionArea)) {
collidingAABBs.add(entity);
if (--limit == 0) {
return limit;
}
Entity[] var10 = entity.getParts();
if (var10 != null) {
for (Entity part : var10) {
if (part != excludedEntity && part.boundingBox.intersectsWith(collisionArea)) {
collidingAABBs.add(part);
if (--limit == 0) {
return limit;
}
}
}
}
}
}
}
return limit;
}
@Override
public void addTileEntity(TileEntity tileEntity) {
int x = tileEntity.xCoord - xPosition * 16;
int y = tileEntity.yCoord;
int z = tileEntity.zCoord - zPosition * 16;
if (isChunkLoaded) {
setChunkBlockTileEntity(x, y, z, tileEntity);
worldObj.addTileEntity(tileEntity);
} else {
ChunkPosition chunkPosition = new ChunkPosition(x, y, z);
tileEntity.setWorldObj(worldObj);
Block block = Block.blocksList[getBlockID(x, y, z)];
if (block != null && block.hasTileEntity(getBlockMetadata(x, y, z))) {
TileEntity old = (TileEntity) chunkTileEntityMap.put(chunkPosition, tileEntity);
if (old != null) {
toInvalidate.add(old);
}
}
}
}
@Override
public void onChunkUnload() {
throw new Error("Not supported with TT");
}
@SuppressWarnings ("FieldRepeatedlyAccessedInMethod") // Patcher makes worldObj final
@Override
@Declare
public void onChunkUnloadTT() {
isChunkLoaded = false;
Set<TileEntity> removalSet = worldObj.tileEntityRemovalSet;
for (TileEntity var2 : (Iterable<TileEntity>) chunkTileEntityMap.values()) {
removalSet.add(var2);
}
for (List entityList : entityLists) {
worldObj.unloadEntities(entityList);
}
synchronized (ChunkEvent.class) {
MinecraftForge.EVENT_BUS.post(new ChunkEvent.Unload(this));
}
}
@Override
public void removeEntityAtIndex(Entity e, int index) {
if (index < 0) {
index = 0;
}
if (index >= this.entityLists.length) {
index = this.entityLists.length - 1;
}
this.entityLists[index].remove(e);
e.chunk = null;
}
@Override
public void onChunkLoad() {
for (TileEntity tileEntity : toInvalidate) {
tileEntity.invalidate();
}
toInvalidate.clear();
for (Map.Entry<ChunkPosition, TileEntity> entry : ((Map<ChunkPosition, TileEntity>) chunkTileEntityMap).entrySet()) {
TileEntity tileEntity = entry.getValue();
if ((!tileEntity.canUpdate() && tileEntity.isInvalid()) || tileEntity.getClass() == TileEntity.class) {
tileEntity.invalidate();
ChunkPosition position = entry.getKey();
chunkTileEntityMap.remove(position);
worldObj.loadedTileEntityList.remove(tileEntity);
int x = position.x, y = position.y, z = position.z;
int id = getBlockID(x, y, z);
int meta = getBlockMetadata(x, y, z);
Log.info("Resetting invalid TileEntity " + Log.toString(tileEntity) + " for block: " + new BlockInfo(id, meta) + " at within chunk coords " + x + ',' + y + ',' + z + " in chunk " + this);
setBlockID(x, y, z, 0);
setBlockIDWithMetadata(x, y, z, id, meta);
}
}
}
@SuppressWarnings ("FieldRepeatedlyAccessedInMethod") // Patcher makes worldObj final
@Override
@Declare
public void threadUnsafeChunkLoad() {
isChunkLoaded = true;
worldObj.addTileEntity(chunkTileEntityMap.values());
for (List entityList : entityLists) {
synchronized (entityList) {
worldObj.addLoadedEntities(entityList);
}
}
synchronized (ChunkEvent.class) {
MinecraftForge.EVENT_BUS.post(new ChunkEvent.Load(this));
}
}
@SuppressWarnings ("FieldRepeatedlyAccessedInMethod") // Patcher makes x/zPosition and worldObj final
@Override
public void addEntity(Entity par1Entity) {
int entityChunkX = MathHelper.floor_double(par1Entity.posX / 16.0D);
int entityChunkZ = MathHelper.floor_double(par1Entity.posZ / 16.0D);
if (entityChunkX != xPosition || entityChunkZ != zPosition) {
if (Log.debug) {
FMLLog.log(Log.DEBUG, new Throwable(), "Entity %s added to the wrong chunk - expected x%d z%d, got x%d z%d", par1Entity.toString(), xPosition, zPosition, entityChunkX, entityChunkZ);
}
if (worldObj instanceof WorldServer) {
Chunk correctChunk = ((WorldServer) worldObj).theChunkProviderServer.getChunkIfExists(entityChunkX, entityChunkZ);
if (correctChunk == this) {
Log.severe("What?! This chunk isn't at the position it says it's at...: " + this + " was added at " + entityChunkX + ", " + entityChunkZ);
} else if (correctChunk != null) {
correctChunk.addEntity(par1Entity);
return;
}
}
}
hasEntities = true;
int var4 = MathHelper.floor_double(par1Entity.posY / 16.0D);
if (var4 < 0) {
var4 = 0;
}
if (var4 >= entityLists.length) {
var4 = entityLists.length - 1;
}
MinecraftForge.EVENT_BUS.post(new EntityEvent.EnteringChunk(par1Entity, xPosition, zPosition, par1Entity.chunkCoordX, par1Entity.chunkCoordZ));
par1Entity.addedToChunk = true;
par1Entity.chunkCoordX = xPosition;
par1Entity.chunkCoordY = var4;
par1Entity.chunkCoordZ = zPosition;
entityLists[var4].add(par1Entity);
par1Entity.chunk = this;
}
@Override
@Declare
public void setChunkBlockTileEntityWithoutValidate(int x, int y, int z, TileEntity tileEntity) {
ChunkPosition var5 = new ChunkPosition(x, y, z);
tileEntity.worldObj = worldObj;
tileEntity.xCoord = xPosition * 16 + x;
tileEntity.yCoord = y;
tileEntity.zCoord = zPosition * 16 + z;
Block block = Block.blocksList[getBlockID(x, y, z)];
if (block != null && block.hasTileEntity(getBlockMetadata(x, y, z))) {
chunkTileEntityMap.put(var5, tileEntity);
}
isModified = true;
}
@Override
@Declare
public boolean setBlockIDWithMetadataWithoutValidate(int x, int y, int z, int id, int meta) {
int xzIndex = z << 4 | x;
if (y >= precipitationHeightMap[xzIndex] - 1) {
precipitationHeightMap[xzIndex] = -999;
}
int var7 = heightMap[xzIndex];
int var8 = getBlockID(x, y, z);
int var9 = getBlockMetadata(x, y, z);
if (var8 == id && var9 == meta) {
return false;
} else {
if (y >> 4 >= storageArrays.length || y >> 4 < 0) {
return false;
}
ExtendedBlockStorage var10 = storageArrays[y >> 4];
boolean var11 = false;
if (var10 == null) {
if (id == 0) {
return false;
}
var10 = storageArrays[y >> 4] = new ExtendedBlockStorage(y >> 4 << 4, !worldObj.provider.hasNoSky);
var11 = y >= var7;
}
int var12 = xPosition * 16 + x;
int var13 = zPosition * 16 + z;
var10.setExtBlockID(x, y & 15, z, id);
if (var8 != 0) {
if (Block.blocksList[var8] != null && Block.blocksList[var8].hasTileEntity(var9)) {
TileEntity te = worldObj.getBlockTileEntity(var12, y, var13);
if (te != null && te.shouldRefresh(var8, id, var9, meta, worldObj, var12, y, var13)) {
worldObj.removeBlockTileEntity(var12, y, var13);
}
}
}
if (var10.getExtBlockID(x, y & 15, z) != id) {
return false;
} else {
var10.setExtBlockMetadata(x, y & 15, z, meta);
if (var11) {
generateSkylightMap();
} else {
if (getBlockLightOpacity(x, y, z) > 0) {
if (y >= var7) {
relightBlock(x, y + 1, z);
}
} else if (y == var7 - 1) {
relightBlock(x, y, z);
}
propagateSkylightOcclusion(x, z);
}
TileEntity var14;
if (id != 0) {
if (Block.blocksList[id] != null && Block.blocksList[id].hasTileEntity(meta)) {
var14 = getChunkBlockTileEntity(x, y, z);
if (var14 == null) {
var14 = Block.blocksList[id].createTileEntity(worldObj, meta);
worldObj.setBlockTileEntity(var12, y, var13, var14);
}
if (var14 != null) {
var14.updateContainingBlockInfo();
var14.blockMetadata = meta;
}
}
}
isModified = true;
return true;
}
}
}
@Override
public boolean setBlockIDWithMetadata(int x, int y, int z, int id, int meta) {
int horizontalIndex = z << 4 | x;
if (y >= precipitationHeightMap[horizontalIndex] - 1) {
precipitationHeightMap[horizontalIndex] = -999;
}
int height = heightMap[horizontalIndex];
int oldId = getBlockID(x, y, z);
int oldMeta = getBlockMetadata(x, y, z);
if (oldId == id && oldMeta == meta) {
return false;
} else if (id < 0 || (id != 0 && Block.blocksList[id] == null)) {
Log.warning("Tried to set invalid block ID " + id + ':' + meta, new Throwable());
return false;
} else {
if (y >> 4 >= storageArrays.length || y >> 4 < 0) {
return false;
}
ExtendedBlockStorage ebs = storageArrays[y >> 4];
boolean changedHeightMap = false;
if (ebs == null) {
if (id == 0) {
return false;
}
ebs = storageArrays[y >> 4] = new ExtendedBlockStorage(y >> 4 << 4, !worldObj.provider.hasNoSky);
changedHeightMap = y >= height;
}
int wX = xPosition * 16 + x;
int wZ = zPosition * 16 + z;
Block oldBlock = oldId > 0 ? Block.blocksList[oldId] : null;
if (oldBlock != null && !worldObj.isRemote) {
oldBlock.onSetBlockIDWithMetaData(worldObj, wX, y, wZ, oldMeta);
}
ebs.setExtBlockID(x, y & 15, z, id);
if (oldBlock != null) {
if (!worldObj.isRemote) {
oldBlock.breakBlock(worldObj, wX, y, wZ, oldId, oldMeta);
} else if (oldBlock.hasTileEntity(oldMeta)) {
TileEntity te = worldObj.getBlockTileEntity(wX, y, wZ);
if (te != null && te.shouldRefresh(oldId, id, oldMeta, meta, worldObj, wX, y, wZ)) {
worldObj.removeBlockTileEntity(wX, y, wZ);
}
}
}
if (ebs.getExtBlockID(x, y & 15, z) != id) {
return false;
} else {
ebs.setExtBlockMetadata(x, y & 15, z, meta);
if (changedHeightMap) {
generateSkylightMap();
} else {
if (getBlockLightOpacity(x, y, z) > 0) {
if (y >= height) {
relightBlock(x, y + 1, z);
}
} else if (y == height - 1) {
relightBlock(x, y, z);
}
propagateSkylightOcclusion(x, z);
}
Block block = id > 0 ? Block.blocksList[id] : null;
if (block != null) {
// CraftBukkit - Don't place while processing the BlockPlaceEvent, unless it's a BlockContainer
if (!worldObj.isRemote && (block instanceof BlockContainer || (worldObj.inPlaceEvent == null || worldObj.inPlaceEvent.get() == Boolean.FALSE))) {
block.onBlockAdded(worldObj, wX, y, wZ);
}
if (block.hasTileEntity(meta)) {
// CraftBukkit start - don't create tile entity if placement failed
if (getBlockID(x, y, z) != id) {
return false;
}
//CraftBukkit end
TileEntity tileEntity = getChunkBlockTileEntity(x, y, z);
if (tileEntity == null) {
tileEntity = block.createTileEntity(worldObj, meta);
worldObj.setBlockTileEntity(wX, y, wZ, tileEntity);
}
if (tileEntity != null) {
tileEntity.updateContainingBlockInfo();
tileEntity.blockMetadata = meta;
}
}
}
isModified = true;
return true;
}
}
}
@Override
public TileEntity getChunkBlockTileEntity(int par1, int par2, int par3) {
ChunkPosition position = new ChunkPosition(par1, par2, par3);
TileEntity tileEntity = (TileEntity) this.chunkTileEntityMap.get(position);
if (tileEntity != null && tileEntity.isInvalid()) {
chunkTileEntityMap.remove(position);
worldObj.loadedTileEntityList.remove(tileEntity);
tileEntity = null;
}
if (tileEntity == null) {
int id = this.getBlockID(par1, par2, par3);
int meta = this.getBlockMetadata(par1, par2, par3);
Block block;
if (id <= 0 || (block = Block.blocksList[id]) == null || !block.hasTileEntity(meta)) {
return null;
}
tileEntity = block.createTileEntity(this.worldObj, meta);
this.worldObj.setBlockTileEntity(this.xPosition * 16 + par1, par2, this.zPosition * 16 + par3, tileEntity);
tileEntity = (TileEntity) this.chunkTileEntityMap.get(position);
}
return tileEntity;
}
}