]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/commitdiff
Remove unnecessary cache wrapper
authorIrtimaled <irtimaled@gmail.com>
Sat, 2 Mar 2019 00:32:45 +0000 (16:32 -0800)
committerIrtimaled <irtimaled@gmail.com>
Sun, 10 Mar 2019 01:43:25 +0000 (17:43 -0800)
src/main/java/com/irtimaled/bbor/client/ClientProxy.java
src/main/java/com/irtimaled/bbor/client/ClientRenderer.java
src/main/java/com/irtimaled/bbor/client/CreateCache.java [deleted file]
src/main/java/com/irtimaled/bbor/client/GetCache.java [new file with mode: 0644]
src/main/java/com/irtimaled/bbor/client/NBTFileParser.java
src/main/java/com/irtimaled/bbor/common/BoundingBoxCache.java
src/main/java/com/irtimaled/bbor/common/CommonProxy.java
src/main/java/com/irtimaled/bbor/common/DimensionCache.java [deleted file]

index 2ea9cf03569e11e3a7f71d7d76b65f500b737d12..84e37747d7ede0bae8ca57f7f881920aca370d4c 100644 (file)
@@ -37,10 +37,10 @@ public class ClientProxy extends CommonProxy {
         EventBus.subscribe(ConnectedToRemoteServer.class, e -> connectedToServer(e.getNetworkManager()));
         EventBus.subscribe(DisconnectedFromRemoteServer.class, e -> disconnectedFromServer());
         EventBus.subscribe(InitializeClientReceived.class, e -> setWorldData(e.getSeed(), e.getSpawnX(), e.getSpawnZ()));
-        EventBus.subscribe(AddBoundingBoxReceived.class, e -> dimensionCache.delegate(e.getDimensionType(), cache -> cache.addBoundingBoxes(e.getKey(), e.getBoundingBoxes())));
+        EventBus.subscribe(AddBoundingBoxReceived.class, e -> runOnCache(e.getDimensionType(), cache -> cache.addBoundingBoxes(e.getKey(), e.getBoundingBoxes())));
         EventBus.subscribe(RemoveBoundingBoxReceived.class, e -> removeBoundingBox(e.getDimensionType(), e.getKey()));
 
-        renderer = new ClientRenderer(dimensionCache);
+        renderer = new ClientRenderer(this::getCache);
     }
 
     private void render(float partialTicks) {
@@ -66,7 +66,7 @@ public class ClientProxy extends CommonProxy {
         SocketAddress remoteAddress = networkManager.getRemoteAddress();
         if (remoteAddress instanceof InetSocketAddress) {
             InetSocketAddress socketAddress = (InetSocketAddress) remoteAddress;
-            NBTFileParser.loadLocalDatFiles(socketAddress.getHostName(), socketAddress.getPort(), this::setWorldData, dimensionCache::getOrCreateCache);
+            NBTFileParser.loadLocalDatFiles(socketAddress.getHostName(), socketAddress.getPort(), this::setWorldData, this::getOrCreateCache);
         }
     }
 
@@ -88,7 +88,7 @@ public class ClientProxy extends CommonProxy {
         BoundingBox spawnChunksBoundingBox = buildSpawnChunksBoundingBox(spawnX, spawnZ, 12, BoundingBoxType.SpawnChunks);
         BoundingBox lazySpawnChunksBoundingBox = buildSpawnChunksBoundingBox(spawnX, spawnZ, 16, BoundingBoxType.LazySpawnChunks);
 
-        dimensionCache.delegate(DimensionType.OVERWORLD, cache -> {
+        runOnCache(DimensionType.OVERWORLD, cache -> {
             cache.addBoundingBox(worldSpawnBoundingBox);
             cache.addBoundingBox(spawnChunksBoundingBox);
             cache.addBoundingBox(lazySpawnChunksBoundingBox);
index b1773afb520f73a11152dbec9b53bba018a0bbdb..dc56e753fc1b4da7b425f9cd344b2d7d8415619e 100644 (file)
@@ -2,7 +2,6 @@ package com.irtimaled.bbor.client;
 
 import com.irtimaled.bbor.client.renderers.*;
 import com.irtimaled.bbor.common.BoundingBoxCache;
-import com.irtimaled.bbor.common.DimensionCache;
 import com.irtimaled.bbor.common.models.*;
 import com.irtimaled.bbor.config.ConfigManager;
 import net.minecraft.client.Minecraft;
@@ -18,11 +17,11 @@ import java.util.Set;
 import static com.irtimaled.bbor.client.Constants.CHUNK_SIZE;
 
 public class ClientRenderer {
-    private final DimensionCache dimensionCache;
+    private final GetCache getCache;
     private static final Map<Class<? extends BoundingBox>, Renderer> boundingBoxRendererMap = new HashMap<>();
 
-    ClientRenderer(DimensionCache dimensionCache) {
-        this.dimensionCache = dimensionCache;
+    ClientRenderer(GetCache getCache) {
+        this.getCache = getCache;
         boundingBoxRendererMap.put(BoundingBoxVillage.class, new VillageRenderer());
         boundingBoxRendererMap.put(BoundingBoxSlimeChunk.class, new SlimeChunkRenderer());
         boundingBoxRendererMap.put(BoundingBoxWorldSpawn.class, new WorldSpawnRenderer());
@@ -44,7 +43,7 @@ public class ClientRenderer {
     }
 
     public void render(DimensionType dimensionType, Boolean outerBoxesOnly) {
-        BoundingBoxCache cache = dimensionCache.getCache(dimensionType);
+        BoundingBoxCache cache = getCache.apply(dimensionType);
         if (cache == null) return;
 
         GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
@@ -62,10 +61,14 @@ public class ClientRenderer {
             Renderer renderer = boundingBoxRendererMap.get(key.getClass());
             if (renderer == null) continue;
 
-            if (outerBoxesOnly)
-                renderer.render(key);
-            else
-                entry.getValue().forEach(renderer::render);
+            if (!outerBoxesOnly) {
+                Set<BoundingBox> boundingBoxes = entry.getValue();
+                if (boundingBoxes != null) {
+                    boundingBoxes.forEach(renderer::render);
+                    continue;
+                }
+            }
+            renderer.render(key);
         }
 
         GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_FILL);
diff --git a/src/main/java/com/irtimaled/bbor/client/CreateCache.java b/src/main/java/com/irtimaled/bbor/client/CreateCache.java
deleted file mode 100644 (file)
index 2d68936..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-package com.irtimaled.bbor.client;
-
-import com.irtimaled.bbor.common.BoundingBoxCache;
-import net.minecraft.world.dimension.DimensionType;
-
-import java.util.function.Function;
-
-public interface CreateCache extends Function<DimensionType, BoundingBoxCache> { }
diff --git a/src/main/java/com/irtimaled/bbor/client/GetCache.java b/src/main/java/com/irtimaled/bbor/client/GetCache.java
new file mode 100644 (file)
index 0000000..b4b29a4
--- /dev/null
@@ -0,0 +1,8 @@
+package com.irtimaled.bbor.client;
+
+import com.irtimaled.bbor.common.BoundingBoxCache;
+import net.minecraft.world.dimension.DimensionType;
+
+import java.util.function.Function;
+
+public interface GetCache extends Function<DimensionType, BoundingBoxCache> { }
index b3aa300d50d252d21ca4a6876b1f9f5b6773eb42..7f19134fb81422d03ceeb80850cd5d9620ad7dff 100644 (file)
@@ -20,7 +20,7 @@ import java.util.HashSet;
 import java.util.Set;
 
 class NBTFileParser {
-    static void loadLocalDatFiles(String host, int port, SetWorldData setWorldData, CreateCache createCache) {
+    static void loadLocalDatFiles(String host, int port, SetWorldData setWorldData, GetCache createCache) {
         Logger.info("Looking for local structures (host:port=%s:%d)", host, port);
         String path = String.format("BBOutlineReloaded%s%s%s%d", File.separator, host, File.separator, port);
         File localStructuresFolder = new File(ConfigManager.configDir, path);
@@ -57,7 +57,7 @@ class NBTFileParser {
         setWorldData.accept(seed, spawnX, spawnZ);
     }
 
-    private static void populateBoundingBoxCache(File localStructuresFolder, CreateCache createCache) {
+    private static void populateBoundingBoxCache(File localStructuresFolder, GetCache createCache) {
         loadOverworldStructures(localStructuresFolder, createCache.apply(DimensionType.OVERWORLD));
         loadNetherStructures(localStructuresFolder, createCache.apply(DimensionType.NETHER));
         loadEndStructures(localStructuresFolder, createCache.apply(DimensionType.THE_END));
index e92407a6cffa7c7fa251d5ee04c1cdb6ba7f1a1b..6b76b9c19e4ddf6275c8c0f2fef1765383c0e5e5 100644 (file)
@@ -33,8 +33,6 @@ public class BoundingBoxCache {
     }
 
     public void removeBoundingBox(BoundingBox key) {
-        if (cache.containsKey(key)) {
-            cache.remove(key);
-        }
+        cache.remove(key);
     }
 }
index e4b76d96548f7761b521206df3112b9d7e315d29..995a5ffd5033bac19cd779f10a5f030f2dd5721f 100644 (file)
@@ -12,6 +12,7 @@ import com.irtimaled.bbor.common.messages.RemoveBoundingBox;
 import com.irtimaled.bbor.common.models.BoundingBox;
 import com.irtimaled.bbor.common.models.BoundingBoxMobSpawner;
 import com.irtimaled.bbor.common.models.BoundingBoxVillage;
+import com.irtimaled.bbor.common.models.WorldData;
 import com.irtimaled.bbor.config.ConfigManager;
 import net.minecraft.entity.player.EntityPlayerMP;
 import net.minecraft.network.play.server.SPacketCustomPayload;
@@ -28,14 +29,15 @@ import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.Consumer;
 
 public class CommonProxy {
     private Map<EntityPlayerMP, DimensionType> playerDimensions = new ConcurrentHashMap<>();
     private Map<EntityPlayerMP, Set<BoundingBox>> playerBoundingBoxesCache = new HashMap<>();
     private Map<Integer, BoundingBoxVillage> villageCache = new HashMap<>();
     private Map<DimensionType, ChunkProcessor> chunkProcessors = new HashMap<>();
-
-    protected DimensionCache dimensionCache = new DimensionCache();
+    private WorldData worldData = null;
+    private final Map<DimensionType, BoundingBoxCache> dimensionCache = new ConcurrentHashMap<>();
 
     public void init() {
         EventBus.subscribe(WorldLoaded.class, e -> worldLoaded(e.getWorld()));
@@ -52,14 +54,14 @@ public class CommonProxy {
     }
 
     protected void setWorldData(long seed, int spawnX, int spawnZ) {
-        dimensionCache.setWorldData(seed, spawnX, spawnZ);
+        worldData = new WorldData(seed, spawnX, spawnZ);
     }
 
     private void worldLoaded(World world) {
         IChunkProvider chunkProvider = world.getChunkProvider();
         if (chunkProvider instanceof ChunkProviderServer) {
             DimensionType dimensionType = world.dimension.getType();
-            BoundingBoxCache boundingBoxCache = dimensionCache.getOrCreateCache(dimensionType);
+            BoundingBoxCache boundingBoxCache = getOrCreateCache(dimensionType);
             ChunkProcessor chunkProcessor = null;
             if (dimensionType == DimensionType.OVERWORLD) {
                 setWorldData(world.getSeed(), world.getWorldInfo().getSpawnX(), world.getWorldInfo().getSpawnZ());
@@ -91,7 +93,7 @@ public class CommonProxy {
     }
 
     private void playerLoggedIn(EntityPlayerMP player) {
-        player.connection.sendPacket(InitializeClient.getPayload(dimensionCache.getWorldData()));
+        player.connection.sendPacket(InitializeClient.getPayload(worldData));
     }
 
     private void playerLoggedOut(EntityPlayerMP player) {
@@ -116,12 +118,12 @@ public class CommonProxy {
     private void sendBoundingBoxes(EntityPlayerMP player) {
         DimensionType dimensionType = DimensionType.getById(player.dimension);
         playerDimensions.put(player, dimensionType);
-        sendToPlayer(player, dimensionCache.getCache(dimensionType));
+        sendToPlayer(player, getCache(dimensionType));
     }
 
     private void sendToPlayer(EntityPlayerMP player, BoundingBoxCache boundingBoxCache) {
-        if (boundingBoxCache == null)
-            return;
+        if (boundingBoxCache == null) return;
+
         Map<BoundingBox, Set<BoundingBox>> cacheSubset = getBoundingBoxMap(player, boundingBoxCache.getBoundingBoxes());
 
         DimensionType dimensionType = DimensionType.getById(player.dimension);
@@ -151,10 +153,10 @@ public class CommonProxy {
     }
 
     protected void removeBoundingBox(DimensionType dimensionType, BoundingBox key) {
-        BoundingBoxCache cache = dimensionCache.getCache(dimensionType);
-        if (cache != null) {
-            cache.removeBoundingBox(key);
-        }
+        BoundingBoxCache cache = getCache(dimensionType);
+        if (cache == null) return;
+
+        cache.removeBoundingBox(key);
     }
 
     private void mobSpawnerBroken(DimensionType dimensionType, BlockPos pos) {
@@ -166,12 +168,12 @@ public class CommonProxy {
     private void tick() {
         for (EntityPlayerMP player : playerDimensions.keySet()) {
             DimensionType dimensionType = playerDimensions.get(player);
-            sendToPlayer(player, dimensionCache.getCache(dimensionType));
+            sendToPlayer(player, getCache(dimensionType));
         }
     }
 
     private void villageUpdated(DimensionType dimensionType, Village village) {
-        BoundingBoxCache cache = dimensionCache.getCache(dimensionType);
+        BoundingBoxCache cache = getCache(dimensionType);
         if (cache == null) return;
 
         int villageId = village.hashCode();
@@ -190,8 +192,24 @@ public class CommonProxy {
         }
     }
 
+    protected BoundingBoxCache getCache(DimensionType dimensionType) {
+        return dimensionCache.get(dimensionType);
+    }
+
+    protected BoundingBoxCache getOrCreateCache(DimensionType dimensionType)
+    {
+        return dimensionCache.computeIfAbsent(dimensionType, dt -> new BoundingBoxCache());
+    }
+
+    protected void runOnCache(DimensionType dimensionType, Consumer<BoundingBoxCache> action) {
+        action.accept(getOrCreateCache(dimensionType));
+    }
+
     protected void clearCaches() {
         villageCache.clear();
+        for (BoundingBoxCache cache : dimensionCache.values()) {
+            cache.close();
+        }
         dimensionCache.clear();
     }
 }
diff --git a/src/main/java/com/irtimaled/bbor/common/DimensionCache.java b/src/main/java/com/irtimaled/bbor/common/DimensionCache.java
deleted file mode 100644 (file)
index 9c3bdcb..0000000
+++ /dev/null
@@ -1,41 +0,0 @@
-package com.irtimaled.bbor.common;
-
-import com.irtimaled.bbor.common.models.WorldData;
-import net.minecraft.world.dimension.DimensionType;
-
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.function.Consumer;
-
-public class DimensionCache {
-    private final Map<DimensionType, BoundingBoxCache> map = new ConcurrentHashMap<>();
-    private WorldData worldData;
-
-    public BoundingBoxCache getCache(DimensionType dimensionType) {
-        return map.get(dimensionType);
-    }
-
-    public void delegate(DimensionType dimensionType, Consumer<BoundingBoxCache> action) {
-        action.accept(getOrCreateCache(dimensionType));
-    }
-
-    public BoundingBoxCache getOrCreateCache(DimensionType dimensionType) {
-        return map.computeIfAbsent(dimensionType, dt -> new BoundingBoxCache());
-    }
-
-    public void clear() {
-        worldData = null;
-        for (BoundingBoxCache cache : map.values()) {
-            cache.close();
-        }
-        map.clear();
-    }
-
-    void setWorldData(long seed, int spawnX, int spawnZ) {
-        this.worldData = new WorldData(seed, spawnX, spawnZ);
-    }
-
-    public WorldData getWorldData() {
-        return worldData;
-    }
-}