]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blobdiff - src/main/java/com/irtimaled/bbor/common/CommonProxy.java
Move Mob Spawner processing to client side
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / common / CommonProxy.java
index f637bf55110ffd7629afca19852cd86f2604c907..9606d02f0efc6017cea3fd3c132d2f7fc5b99fdf 100644 (file)
@@ -1,27 +1,25 @@
 package com.irtimaled.bbor.common;
 
 import com.irtimaled.bbor.Logger;
-import com.irtimaled.bbor.common.chunkProcessors.AbstractChunkProcessor;
-import com.irtimaled.bbor.common.chunkProcessors.EndChunkProcessor;
-import com.irtimaled.bbor.common.chunkProcessors.NetherChunkProcessor;
-import com.irtimaled.bbor.common.chunkProcessors.OverworldChunkProcessor;
 import com.irtimaled.bbor.common.events.*;
 import com.irtimaled.bbor.common.messages.AddBoundingBox;
 import com.irtimaled.bbor.common.messages.InitializeClient;
 import com.irtimaled.bbor.common.messages.PayloadBuilder;
 import com.irtimaled.bbor.common.messages.RemoveBoundingBox;
 import com.irtimaled.bbor.common.models.AbstractBoundingBox;
-import com.irtimaled.bbor.common.models.BoundingBoxMobSpawner;
 import com.irtimaled.bbor.common.models.ServerPlayer;
 
-import java.util.*;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+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, VillageProcessor> villageProcessors = new HashMap<>();
-    private Map<Integer, AbstractChunkProcessor> chunkProcessors = new HashMap<>();
+    private final Map<Integer, ServerPlayer> players = new ConcurrentHashMap<>();
+    private final Map<Integer, Set<AbstractBoundingBox>> playerBoundingBoxesCache = new HashMap<>();
+    private final Map<Integer, VillageProcessor> villageProcessors = new HashMap<>();
+    private final Map<Integer, ChunkProcessor> chunkProcessors = new HashMap<>();
     private final Map<Integer, BoundingBoxCache> dimensionCache = new ConcurrentHashMap<>();
     private Long seed = null;
     private Integer spawnX = null;
@@ -30,7 +28,6 @@ public class CommonProxy {
     public void init() {
         EventBus.subscribe(WorldLoaded.class, this::worldLoaded);
         EventBus.subscribe(ChunkLoaded.class, this::chunkLoaded);
-        EventBus.subscribe(MobSpawnerBroken.class, this::mobSpawnerBroken);
         EventBus.subscribe(PlayerLoggedIn.class, this::playerLoggedIn);
         EventBus.subscribe(PlayerLoggedOut.class, this::playerLoggedOut);
         EventBus.subscribe(PlayerSubscribed.class, this::onPlayerSubscribed);
@@ -51,28 +48,18 @@ public class CommonProxy {
     private void worldLoaded(WorldLoaded event) {
         int dimensionId = event.getDimensionId();
         long seed = event.getSeed();
-        BoundingBoxCache boundingBoxCache = getOrCreateCache(dimensionId);
-        AbstractChunkProcessor chunkProcessor = null;
-        switch (dimensionId) {
-            case Dimensions.OVERWORLD:
-                setSeed(seed);
-                setWorldSpawn(event.getSpawnX(), event.getSpawnZ());
-                chunkProcessor = new OverworldChunkProcessor(boundingBoxCache);
-                break;
-            case Dimensions.NETHER:
-                chunkProcessor = new NetherChunkProcessor(boundingBoxCache);
-                break;
-            case Dimensions.THE_END:
-                chunkProcessor = new EndChunkProcessor(boundingBoxCache);
-                break;
+        if (dimensionId == Dimensions.OVERWORLD) {
+            setSeed(seed);
+            setWorldSpawn(event.getSpawnX(), event.getSpawnZ());
         }
         Logger.info("create world dimension: %s (seed: %d)", dimensionId, seed);
-        chunkProcessors.put(dimensionId, chunkProcessor);
+        BoundingBoxCache boundingBoxCache = getOrCreateCache(dimensionId);
+        chunkProcessors.put(dimensionId, new ChunkProcessor(boundingBoxCache));
         villageProcessors.put(dimensionId, new VillageProcessor(dimensionId, boundingBoxCache));
     }
 
     private void chunkLoaded(ChunkLoaded event) {
-        AbstractChunkProcessor chunkProcessor = chunkProcessors.get(event.getDimensionId());
+        ChunkProcessor chunkProcessor = chunkProcessors.get(event.getDimensionId());
         if (chunkProcessor == null) return;
 
         chunkProcessor.process(event.getChunk());
@@ -87,9 +74,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) {
@@ -100,49 +87,48 @@ 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) {
-        if (boundingBoxCache == null) return;
+    private void sendToPlayer(int playerId, ServerPlayer player) {
+        for (Map.Entry<Integer, BoundingBoxCache> entry : dimensionCache.entrySet()) {
+            int dimensionId = entry.getKey();
+            BoundingBoxCache boundingBoxCache = entry.getValue();
+            if (boundingBoxCache == null) return;
 
-        Map<AbstractBoundingBox, Set<AbstractBoundingBox>> cacheSubset = getBoundingBoxMap(player, boundingBoxCache.getBoundingBoxes());
+            Set<AbstractBoundingBox> playerBoundingBoxes = playerBoundingBoxesCache.computeIfAbsent(playerId, k -> new HashSet<>());
 
-        for (AbstractBoundingBox key : cacheSubset.keySet()) {
-            Set<AbstractBoundingBox> boundingBoxes = cacheSubset.get(key);
-            PayloadBuilder payload = AddBoundingBox.getPayload(player.getDimensionId(), key, boundingBoxes);
-            if (payload != null)
-                player.sendPacket(payload);
+            Map<AbstractBoundingBox, Set<AbstractBoundingBox>> boundingBoxMap = boundingBoxCache.getBoundingBoxes();
+            for (AbstractBoundingBox key : boundingBoxMap.keySet()) {
+                if (playerBoundingBoxes.contains(key)) {
+                    continue;
+                }
 
-            if (!playerBoundingBoxesCache.containsKey(player)) {
-                playerBoundingBoxesCache.put(player, new HashSet<>());
-            }
-            playerBoundingBoxesCache.get(player).add(key);
-        }
-    }
+                Set<AbstractBoundingBox> boundingBoxes = boundingBoxMap.get(key);
+                PayloadBuilder payload = AddBoundingBox.getPayload(dimensionId, key, boundingBoxes);
+                if (payload != null)
+                    player.sendPacket(payload);
 
-    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));
+                playerBoundingBoxes.add(key);
             }
         }
-        return cacheSubset;
     }
 
     protected void removeBoundingBox(int dimensionId, AbstractBoundingBox key) {
@@ -152,16 +138,12 @@ public class CommonProxy {
         cache.removeBoundingBox(key);
     }
 
-    private void mobSpawnerBroken(MobSpawnerBroken event) {
-        int dimensionId = event.getDimensionId();
-        AbstractBoundingBox boundingBox = BoundingBoxMobSpawner.from(event.getPos());
-        removeBoundingBox(dimensionId, boundingBox);
-        sendRemoveBoundingBox(dimensionId, boundingBox);
-    }
-
     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);
         }
     }