From 123f22f20ee76e90249317d2696b5c9328d813de Mon Sep 17 00:00:00 2001 From: Pelotrio <45769595+Pelotrio@users.noreply.github.com> Date: Thu, 16 Nov 2023 13:42:32 +0100 Subject: [PATCH] Add unload event handling to ClientNetworkManager (#137) Added a new method `onWorldUnload` to `ClientNetworkManager` to destroy and clear any network instances when the world event `Unload` is triggered. The addition of this event handler was necessary to prevent the possibility of any network instances persisting on the client side beyond the World lifetime, thus potentially causing memory leaks. --- .../capbank/network/ClientNetworkManager.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main/java/crazypants/enderio/machine/capbank/network/ClientNetworkManager.java b/src/main/java/crazypants/enderio/machine/capbank/network/ClientNetworkManager.java index 7a578a245a..3d74ca9efc 100644 --- a/src/main/java/crazypants/enderio/machine/capbank/network/ClientNetworkManager.java +++ b/src/main/java/crazypants/enderio/machine/capbank/network/ClientNetworkManager.java @@ -4,13 +4,20 @@ import java.util.Map; import net.minecraft.world.World; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.world.WorldEvent; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; import crazypants.enderio.machine.capbank.TileCapBank; public class ClientNetworkManager { private static final ClientNetworkManager instance = new ClientNetworkManager(); + static { + MinecraftForge.EVENT_BUS.register(instance); + } + public static ClientNetworkManager getInstance() { return instance; } @@ -53,4 +60,12 @@ public void addToNetwork(int id, TileCapBank tileCapBank) { CapBankClientNetwork network = getOrCreateNetwork(id); network.addMember(tileCapBank); } + + @SubscribeEvent + public void onWorldUnload(WorldEvent.Unload event) { + if (event.world.isRemote) { + networks.forEach((id, network) -> network.destroyNetwork()); + networks.clear(); + } + } }