Skip to content

Commit 9706d67

Browse files
committed
fix possible loss of referent (?)
move comment to more appropriate place mark certain methods as not null
1 parent a5b266d commit 9706d67

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,26 +68,29 @@ public void onChunkUnload() {
6868
@Override
6969
public @Nullable TileEntity getNeighbor(@NotNull EnumFacing facing) {
7070
if (world == null || pos == null) return null;
71+
// if the ref is INVALID, compute neighbor, otherwise, return TE or null
7172
WeakReference<TileEntity> ref = invalidRef(facing) ? computeNeighbor(facing) : getRef(facing);
7273
return ref.get();
7374
}
7475

75-
// if true, compute neighbor, if false, return TE or null
7676
private boolean invalidRef(EnumFacing facing) {
7777
WeakReference<TileEntity> ref = getRef(facing);
7878
if (ref == INVALID) return true;
7979
TileEntity te = ref.get();
8080
return te != null && te.isInvalid();
8181
}
8282

83+
@NotNull
8384
private WeakReference<TileEntity> computeNeighbor(EnumFacing facing) {
8485
TileEntity te = super.getNeighbor(facing);
8586
// avoid making new references to null TEs
86-
this.neighbors.set(facing.getIndex(), te == null ? NULL : new WeakReference<>(te));
87+
WeakReference<TileEntity> ref = te == null ? NULL : new WeakReference<>(te);
88+
this.neighbors.set(facing.getIndex(), ref);
8789
this.neighborsInvalidated = false;
88-
return getRef(facing);
90+
return ref;
8991
}
9092

93+
@NotNull
9194
private WeakReference<TileEntity> getRef(EnumFacing facing) {
9295
return this.neighbors.get(facing.getIndex());
9396
}

0 commit comments

Comments
 (0)