From 1a539ffebcbc3d9eba46cd489066c228a4e6fc2a Mon Sep 17 00:00:00 2001 From: Richard Freimer Date: Sat, 5 Dec 2020 15:05:32 -0500 Subject: [PATCH] Make TEs invalidate capabilities when the chunk they are in unloads (#7529) Fixed LazyOptional potentially notifying invalidation listeners multiple times. --- .../net/minecraft/tileentity/TileEntity.java.patch | 11 +++++++++-- .../minecraftforge/common/util/LazyOptional.java | 14 +++++++++----- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/patches/minecraft/net/minecraft/tileentity/TileEntity.java.patch b/patches/minecraft/net/minecraft/tileentity/TileEntity.java.patch index 2fb20af62..4f8c9e3de 100644 --- a/patches/minecraft/net/minecraft/tileentity/TileEntity.java.patch +++ b/patches/minecraft/net/minecraft/tileentity/TileEntity.java.patch @@ -49,7 +49,7 @@ this.field_145850_b.func_175666_e(this.field_174879_c, this.field_195045_e.func_177230_c()); } } -@@ -133,6 +140,8 @@ +@@ -133,8 +140,15 @@ public void func_145843_s() { this.field_145846_f = true; @@ -57,8 +57,15 @@ + requestModelDataUpdate(); } ++ @Override ++ public void onChunkUnloaded() { ++ this.invalidateCaps(); ++ } ++ public void func_145829_t() { -@@ -175,6 +184,13 @@ + this.field_145846_f = false; + } +@@ -175,6 +189,13 @@ return this.field_200663_e; } diff --git a/src/main/java/net/minecraftforge/common/util/LazyOptional.java b/src/main/java/net/minecraftforge/common/util/LazyOptional.java index 293ae2236..bd7f41aa9 100644 --- a/src/main/java/net/minecraftforge/common/util/LazyOptional.java +++ b/src/main/java/net/minecraftforge/common/util/LazyOptional.java @@ -318,13 +318,17 @@ public class LazyOptional * call this, if they are covered with a microblock panel, thus cutting off pipe * connectivity to this side. *

- * Also should be called for all when a TE is invalidated, or a world/chunk - * unloads, or a entity dies, etc... This allows modders to keep a cache of - * capability objects instead of re-checking them every tick. + * Also should be called for all when a TE is invalidated (for example, when + * the TE is removed or unloaded), or a world/chunk unloads, or a entity dies, + * etc... This allows modders to keep a cache of capability objects instead of + * re-checking them every tick. */ public void invalidate() { - this.isValid = false; - this.listeners.forEach(e -> e.accept(this)); + if (this.isValid) + { + this.isValid = false; + this.listeners.forEach(e -> e.accept(this)); + } } }