Skip to content
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ public abstract class NeighborCacheTileEntityBase extends SyncedTileEntityBase i
private boolean neighborsInvalidated = false;

public NeighborCacheTileEntityBase() {
invalidateNeighbors();
invalidateNeighbors(false);
}

protected void invalidateNeighbors() {
protected void invalidateNeighbors(boolean notify) {
if (!this.neighborsInvalidated) {
for (EnumFacing value : EnumFacing.VALUES) {
if (notify && crossesChunk(value) && getNeighbor(value) instanceof INeighborCache neighborCache) {
// notify neighbor on a different chunk to invalidate us
neighborCache.onNeighborChanged(value.getOpposite());
}
this.neighbors.set(value.getIndex(), INVALID);
}
this.neighborsInvalidated = true;
Expand All @@ -41,28 +45,28 @@ protected void invalidateNeighbors() {
@Override
public void setWorld(@NotNull World worldIn) {
super.setWorld(worldIn);
invalidateNeighbors();
invalidateNeighbors(false);
}

@MustBeInvokedByOverriders
@Override
public void setPos(@NotNull BlockPos posIn) {
super.setPos(posIn);
invalidateNeighbors();
invalidateNeighbors(false);
}

@MustBeInvokedByOverriders
@Override
public void invalidate() {
super.invalidate();
invalidateNeighbors();
invalidateNeighbors(false);
}

@MustBeInvokedByOverriders
@Override
public void onChunkUnload() {
super.onChunkUnload();
invalidateNeighbors();
invalidateNeighbors(true);
}

@Override
Expand All @@ -75,11 +79,27 @@ public void onChunkUnload() {

private boolean invalidRef(EnumFacing facing) {
WeakReference<TileEntity> ref = getRef(facing);
if (ref == INVALID) return true;
if (ref == INVALID || crossesUnloadedChunk(facing)) return true;
Comment thread
ghzdude marked this conversation as resolved.
TileEntity te = ref.get();
return te != null && te.isInvalid();
}

private boolean crossesUnloadedChunk(EnumFacing facing) {
if (crossesChunk(facing)) {
int ncx = getPos().offset(facing).getX() >> 4;
int ncz = getPos().offset(facing).getZ() >> 4;
return getWorld().getChunkProvider().getLoadedChunk(ncx, ncz) == null;
}
return false;
}

private boolean crossesChunk(EnumFacing facing) {
int cx = getPos().getX() >> 4, cz = getPos().getZ() >> 4;
BlockPos offset = getPos().offset(facing);
int ncx = offset.getX() >> 4, ncz = offset.getZ() >> 4;
return cx != ncx || cz != ncz;
}

@NotNull
private WeakReference<TileEntity> computeNeighbor(EnumFacing facing) {
TileEntity te = super.getNeighbor(facing);
Expand Down
Loading