Skip to content

Commit

Permalink
Add unload event handling to ClientNetworkManager (#137)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Pelotrio authored Nov 16, 2023
1 parent 0b0f7a4 commit 123f22f
Showing 1 changed file with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
}
}
}

0 comments on commit 123f22f

Please sign in to comment.