From 02d756137e437f0f2a6fd228eb030505469ce341 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Thu, 23 Feb 2023 17:56:25 +0100 Subject: [PATCH] Fix Memory Leak --- .../src/de/steamwar/entity/REntityServer.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/entity/REntityServer.java b/SpigotCore_Main/src/de/steamwar/entity/REntityServer.java index d01a64f..b2bec15 100644 --- a/SpigotCore_Main/src/de/steamwar/entity/REntityServer.java +++ b/SpigotCore_Main/src/de/steamwar/entity/REntityServer.java @@ -197,11 +197,18 @@ public class REntityServer implements Listener { @EventHandler public void onQuit(PlayerQuitEvent e) { Player player = e.getPlayer(); + viewDistance.remove(player); Location location = lastLocation.remove(player); if(location == null) return; - forChunkInView(player, location, (x, z) -> players.get(chunkToId(x, z)).remove(player)); + forChunkInView(player, location, (x, z) -> { + long id = chunkToId(x, z); + Set playersInChunk = players.get(id); + playersInChunk.remove(player); + if(playersInChunk.isEmpty()) + players.remove(id); + }); } private void onMissing(Set of, Set in, Consumer> packetProvider) { @@ -237,7 +244,12 @@ public class REntityServer implements Listener { private void removePlayerFromChunk(Player player, int x, int z) { long id = chunkToId(x, z); - players.get(id).remove(player); + + Set playersInChunk = players.get(id); + playersInChunk.remove(player); + if(playersInChunk.isEmpty()) + players.remove(id); + for(REntity entity : entities.getOrDefault(id, emptyEntities)) { entity.despawn(packet -> TinyProtocol.instance.sendPacket(player, packet)); }