Skip to content

Commit e88b75d

Browse files
committed
on chunk unload, notify neighbors if crossing a chunk to invalidate us
1 parent 2a86351 commit e88b75d

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

src/main/java/gregtech/api/metatileentity/NeighborCacheTileEntityBase.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@ public abstract class NeighborCacheTileEntityBase extends SyncedTileEntityBase i
2525
private boolean neighborsInvalidated = false;
2626

2727
public NeighborCacheTileEntityBase() {
28-
invalidateNeighbors();
28+
invalidateNeighbors(false);
2929
}
3030

31-
protected void invalidateNeighbors() {
31+
protected void invalidateNeighbors(boolean notify) {
3232
if (!this.neighborsInvalidated) {
3333
for (EnumFacing value : EnumFacing.VALUES) {
34+
if (notify && crossesChunk(value) && getNeighbor(value) instanceof INeighborCache neighborCache) {
35+
// notify neighbor on a different chunk to invalidate us
36+
neighborCache.onNeighborChanged(value.getOpposite());
37+
}
3438
this.neighbors.set(value.getIndex(), INVALID);
3539
}
3640
this.neighborsInvalidated = true;
@@ -41,28 +45,28 @@ protected void invalidateNeighbors() {
4145
@Override
4246
public void setWorld(@NotNull World worldIn) {
4347
super.setWorld(worldIn);
44-
invalidateNeighbors();
48+
invalidateNeighbors(false);
4549
}
4650

4751
@MustBeInvokedByOverriders
4852
@Override
4953
public void setPos(@NotNull BlockPos posIn) {
5054
super.setPos(posIn);
51-
invalidateNeighbors();
55+
invalidateNeighbors(false);
5256
}
5357

5458
@MustBeInvokedByOverriders
5559
@Override
5660
public void invalidate() {
5761
super.invalidate();
58-
invalidateNeighbors();
62+
invalidateNeighbors(false);
5963
}
6064

6165
@MustBeInvokedByOverriders
6266
@Override
6367
public void onChunkUnload() {
6468
super.onChunkUnload();
65-
invalidateNeighbors();
69+
invalidateNeighbors(true);
6670
}
6771

6872
@Override
@@ -81,16 +85,21 @@ private boolean invalidRef(EnumFacing facing) {
8185
}
8286

8387
private boolean crossesUnloadedChunk(EnumFacing facing) {
84-
int cx = getPos().getX() >> 4, cz = getPos().getZ() >> 4;
85-
BlockPos offset = getPos().offset(facing);
86-
int ncx = offset.getX() >> 4, ncz = offset.getZ() >> 4;
87-
88-
if (cx != ncx || cz != ncz) {
88+
if (crossesChunk(facing)) {
89+
int ncx = getPos().offset(facing).getX() >> 4;
90+
int ncz = getPos().offset(facing).getZ() >> 4;
8991
return getWorld().getChunkProvider().getLoadedChunk(ncx, ncz) == null;
9092
}
9193
return false;
9294
}
9395

96+
private boolean crossesChunk(EnumFacing facing) {
97+
int cx = getPos().getX() >> 4, cz = getPos().getZ() >> 4;
98+
BlockPos offset = getPos().offset(facing);
99+
int ncx = offset.getX() >> 4, ncz = offset.getZ() >> 4;
100+
return cx != ncx || cz != ncz;
101+
}
102+
94103
@NotNull
95104
private WeakReference<TileEntity> computeNeighbor(EnumFacing facing) {
96105
TileEntity te = super.getNeighbor(facing);

0 commit comments

Comments
 (0)