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) {
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);
}
}
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);
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;
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());
}
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);
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);
+++ /dev/null
-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> { }
--- /dev/null
+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> { }
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);
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));
}
public void removeBoundingBox(BoundingBox key) {
- if (cache.containsKey(key)) {
- cache.remove(key);
- }
+ cache.remove(key);
}
}
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;
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()));
}
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());
}
private void playerLoggedIn(EntityPlayerMP player) {
- player.connection.sendPacket(InitializeClient.getPayload(dimensionCache.getWorldData()));
+ player.connection.sendPacket(InitializeClient.getPayload(worldData));
}
private void playerLoggedOut(EntityPlayerMP player) {
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);
}
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) {
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();
}
}
+ 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();
}
}
+++ /dev/null
-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;
- }
-}