]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/commitdiff
Tidy up memory leak
authorIrtimaled <irtimaled@gmail.com>
Thu, 8 Aug 2019 04:00:56 +0000 (21:00 -0700)
committerIrtimaled <irtimaled@gmail.com>
Fri, 9 Aug 2019 07:20:28 +0000 (00:20 -0700)
src/main/java/com/irtimaled/bbor/common/CommonProxy.java
src/main/java/com/irtimaled/bbor/common/events/PlayerLoggedOut.java
src/main/java/com/irtimaled/bbor/common/events/PlayerSubscribed.java
src/main/java/com/irtimaled/bbor/common/interop/CommonInterop.java
src/main/java/com/irtimaled/bbor/common/models/ServerPlayer.java
src/main/java/com/irtimaled/bbor/mixin/network/play/client/MixinCPacketCustomPayload.java

index 1543710f477b61f8c0cac5985472e8ce1b1d4e40..ffa20b2bfc83097e84ff85aba7bb9fd01fd133b8 100644 (file)
@@ -21,8 +21,8 @@ import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 
 public class CommonProxy {
-    private Set<ServerPlayer> players = ConcurrentHashMap.newKeySet();
-    private Map<ServerPlayer, Set<AbstractBoundingBox>> playerBoundingBoxesCache = new HashMap<>();
+    private Map<Integer, ServerPlayer> players = new ConcurrentHashMap<>();
+    private Map<Integer, Set<AbstractBoundingBox>> playerBoundingBoxesCache = new HashMap<>();
     private Map<Integer, VillageProcessor> villageProcessors = new HashMap<>();
     private Map<Integer, AbstractChunkProcessor> chunkProcessors = new HashMap<>();
     private final Map<Integer, BoundingBoxCache> dimensionCache = new ConcurrentHashMap<>();
@@ -90,9 +90,9 @@ public class CommonProxy {
     }
 
     private void playerLoggedOut(PlayerLoggedOut event) {
-        ServerPlayer player = event.getPlayer();
-        players.remove(player);
-        playerBoundingBoxesCache.remove(player);
+        int playerId = event.getPlayerId();
+        players.remove(playerId);
+        playerBoundingBoxesCache.remove(playerId);
     }
 
     private void onVillageRemoved(VillageRemoved event) {
@@ -103,51 +103,47 @@ public class CommonProxy {
         PayloadBuilder payload = RemoveBoundingBox.getPayload(dimensionId, boundingBox);
         if (payload == null) return;
 
-        for (ServerPlayer player : players) {
+        for (Map.Entry<Integer, ServerPlayer> playerEntry : players.entrySet()) {
+            int playerId = playerEntry.getKey();
+            ServerPlayer player = playerEntry.getValue();
             if (player.getDimensionId() == dimensionId) {
                 player.sendPacket(payload);
 
-                if (playerBoundingBoxesCache.containsKey(player)) {
-                    playerBoundingBoxesCache.get(player).remove(boundingBox);
+                if (playerBoundingBoxesCache.containsKey(playerId)) {
+                    playerBoundingBoxesCache.get(playerId).remove(boundingBox);
                 }
             }
         }
     }
 
     private void onPlayerSubscribed(PlayerSubscribed event) {
+        int playerId = event.getPlayerId();
         ServerPlayer player = event.getPlayer();
-        players.add(player);
-        sendToPlayer(player, getCache(player.getDimensionId()));
+        players.put(playerId, player);
+        sendToPlayer(playerId, player);
     }
 
-    private void sendToPlayer(ServerPlayer player, BoundingBoxCache boundingBoxCache) {
+    private void sendToPlayer(int playerId, ServerPlayer player) {
+        BoundingBoxCache boundingBoxCache = getCache(player.getDimensionId());
         if (boundingBoxCache == null) return;
 
-        Map<AbstractBoundingBox, Set<AbstractBoundingBox>> cacheSubset = getBoundingBoxMap(player, boundingBoxCache.getBoundingBoxes());
+        Set<AbstractBoundingBox> playerBoundingBoxes = playerBoundingBoxesCache.computeIfAbsent(playerId, k -> new HashSet<>());
+
+        Map<AbstractBoundingBox, Set<AbstractBoundingBox>> boundingBoxMap = boundingBoxCache.getBoundingBoxes();
+        for (AbstractBoundingBox key : boundingBoxMap.keySet()) {
+            if (playerBoundingBoxes.contains(key)) {
+                continue;
+            }
 
-        for (AbstractBoundingBox key : cacheSubset.keySet()) {
-            Set<AbstractBoundingBox> boundingBoxes = cacheSubset.get(key);
+            Set<AbstractBoundingBox> boundingBoxes = boundingBoxMap.get(key);
             PayloadBuilder payload = AddBoundingBox.getPayload(player.getDimensionId(), key, boundingBoxes);
             if (payload != null)
                 player.sendPacket(payload);
 
-            if (!playerBoundingBoxesCache.containsKey(player)) {
-                playerBoundingBoxesCache.put(player, new HashSet<>());
-            }
-            playerBoundingBoxesCache.get(player).add(key);
+            playerBoundingBoxes.add(key);
         }
     }
 
-    private Map<AbstractBoundingBox, Set<AbstractBoundingBox>> getBoundingBoxMap(ServerPlayer player, Map<AbstractBoundingBox, Set<AbstractBoundingBox>> boundingBoxMap) {
-        Map<AbstractBoundingBox, Set<AbstractBoundingBox>> cacheSubset = new HashMap<>();
-        for (AbstractBoundingBox key : boundingBoxMap.keySet()) {
-            if (!playerBoundingBoxesCache.containsKey(player) || !playerBoundingBoxesCache.get(player).contains(key)) {
-                cacheSubset.put(key, boundingBoxMap.get(key));
-            }
-        }
-        return cacheSubset;
-    }
-
     protected void removeBoundingBox(int dimensionId, AbstractBoundingBox key) {
         BoundingBoxCache cache = getCache(dimensionId);
         if (cache == null) return;
@@ -163,8 +159,11 @@ public class CommonProxy {
     }
 
     private void serverTick() {
-        for (ServerPlayer player : players) {
-            sendToPlayer(player, getCache(player.getDimensionId()));
+        for (Map.Entry<Integer, ServerPlayer> playerEntry : players.entrySet()) {
+            int playerId = playerEntry.getKey();
+            ServerPlayer player = playerEntry.getValue();
+
+            sendToPlayer(playerId, player);
         }
     }
 
index ec6ce1027008b722c55d673b85235790110b8b1a..e21caaa716df5d69bc9ac00b49d376cd8789279c 100644 (file)
@@ -1,15 +1,13 @@
 package com.irtimaled.bbor.common.events;
 
-import com.irtimaled.bbor.common.models.ServerPlayer;
-
 public class PlayerLoggedOut {
-    private final ServerPlayer player;
+    private int playerId;
 
-    public PlayerLoggedOut(ServerPlayer player) {
-        this.player = player;
+    public PlayerLoggedOut(int playerId) {
+        this.playerId = playerId;
     }
 
-    public ServerPlayer getPlayer() {
-        return player;
+    public int getPlayerId() {
+        return playerId;
     }
 }
index d7943d1e4f17ec5336d3084ed068e0b493e08817..fccbd075f435644347580c576b7ea81fd3e01feb 100644 (file)
@@ -3,12 +3,18 @@ package com.irtimaled.bbor.common.events;
 import com.irtimaled.bbor.common.models.ServerPlayer;
 
 public class PlayerSubscribed {
+    private final int playerId;
     private final ServerPlayer player;
 
-    public PlayerSubscribed(ServerPlayer player) {
+    public PlayerSubscribed(int playerId, ServerPlayer player) {
+        this.playerId = playerId;
         this.player = player;
     }
 
+    public int getPlayerId() {
+        return playerId;
+    }
+
     public ServerPlayer getPlayer() {
         return player;
     }
index f079b9a636bbf57bb710f6f408cce952acd2f447..72dc2c8c74da9ae454e0155851c3d289a05317ac 100644 (file)
@@ -55,7 +55,7 @@ public class CommonInterop {
     }
 
     public static void playerLoggedOut(EntityPlayerMP player) {
-        EventBus.publish(new PlayerLoggedOut(new ServerPlayer(player)));
+        EventBus.publish(new PlayerLoggedOut(player.getEntityId()));
     }
 
     public static void tryHarvestBlock(Block block, BlockPos pos, World world) {
index e55fac62de97b25eaa72a8de214d40b28dda6bd1..d4c5eba845dc1faa9ccb56b8304fab7ae8fa3726 100644 (file)
@@ -9,12 +9,10 @@ import java.util.function.Consumer;
 public class ServerPlayer {
     private final int dimensionId;
     private final Consumer<Packet<?>> packetConsumer;
-    private final int playerId;
 
     public ServerPlayer(EntityPlayerMP player) {
         this.dimensionId = player.dimension;
         this.packetConsumer = player.connection::sendPacket;
-        this.playerId = player.getEntityId();
     }
 
     public int getDimensionId() {
@@ -24,9 +22,4 @@ public class ServerPlayer {
     public void sendPacket(PayloadBuilder payloadBuilder) {
         packetConsumer.accept(payloadBuilder.build());
     }
-
-    @Override
-    public int hashCode() {
-        return playerId;
-    }
 }
index 93249604270f5b833f34c2918166392aea8633af..7c29961c707a34bd48c54f878736431316edc432 100644 (file)
@@ -4,6 +4,7 @@ import com.irtimaled.bbor.common.EventBus;
 import com.irtimaled.bbor.common.events.PlayerSubscribed;
 import com.irtimaled.bbor.common.messages.SubscribeToServer;
 import com.irtimaled.bbor.common.models.ServerPlayer;
+import net.minecraft.entity.player.EntityPlayerMP;
 import net.minecraft.network.NetHandlerPlayServer;
 import net.minecraft.network.play.INetHandlerPlayServer;
 import net.minecraft.network.play.client.CPacketCustomPayload;
@@ -21,8 +22,8 @@ public class MixinCPacketCustomPayload {
     @Redirect(method = "processPacket", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/play/INetHandlerPlayServer;processCustomPayload(Lnet/minecraft/network/play/client/CPacketCustomPayload;)V"))
     private void processPacket(INetHandlerPlayServer netHandlerPlayServer, CPacketCustomPayload packet) {
         if (this.channel.toString().equals(SubscribeToServer.NAME)) {
-            ServerPlayer player = new ServerPlayer(((NetHandlerPlayServer) netHandlerPlayServer).player);
-            EventBus.publish(new PlayerSubscribed(player));
+            EntityPlayerMP player = ((NetHandlerPlayServer) netHandlerPlayServer).player;
+            EventBus.publish(new PlayerSubscribed(player.getEntityId(), new ServerPlayer(player)));
         } else {
             netHandlerPlayServer.processCustomPayload(packet);
         }