X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fcom%2Firtimaled%2Fbbor%2Fcommon%2FCommonProxy.java;h=278d28c8216d4cfeeabd9ffbf7579f5636ab5193;hb=e2b4055fd2ccd326f55e08225e0b5792a39aa29e;hp=ffa20b2bfc83097e84ff85aba7bb9fd01fd133b8;hpb=cf74d65b53e1aa3f39d5f6e25da32077269372cc;p=BoundingBoxOutlineReloaded.git diff --git a/src/main/java/com/irtimaled/bbor/common/CommonProxy.java b/src/main/java/com/irtimaled/bbor/common/CommonProxy.java index ffa20b2..278d28c 100644 --- a/src/main/java/com/irtimaled/bbor/common/CommonProxy.java +++ b/src/main/java/com/irtimaled/bbor/common/CommonProxy.java @@ -1,17 +1,18 @@ 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.client.config.ConfigManager; +import com.irtimaled.bbor.common.events.PlayerLoggedIn; +import com.irtimaled.bbor.common.events.PlayerLoggedOut; +import com.irtimaled.bbor.common.events.PlayerSubscribed; +import com.irtimaled.bbor.common.events.ServerTick; +import com.irtimaled.bbor.common.events.StructuresLoaded; +import com.irtimaled.bbor.common.events.WorldLoaded; 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.DimensionId; import com.irtimaled.bbor.common.models.ServerPlayer; import java.util.HashMap; @@ -21,25 +22,26 @@ import java.util.Set; import java.util.concurrent.ConcurrentHashMap; public class CommonProxy { - private Map players = new ConcurrentHashMap<>(); - private Map> playerBoundingBoxesCache = new HashMap<>(); - private Map villageProcessors = new HashMap<>(); - private Map chunkProcessors = new HashMap<>(); - private final Map dimensionCache = new ConcurrentHashMap<>(); + private final Map players = new ConcurrentHashMap<>(); + private final Map> playerBoundingBoxesCache = new HashMap<>(); + private final Map structureProcessors = new HashMap<>(); + private final Map dimensionCache = new ConcurrentHashMap<>(); private Long seed = null; private Integer spawnX = null; private Integer spawnZ = null; + public CommonProxy(){ + ConfigManager.loadConfig(); + } + public void init() { + BoundingBoxType.registerTypes(); EventBus.subscribe(WorldLoaded.class, this::worldLoaded); - EventBus.subscribe(ChunkLoaded.class, this::chunkLoaded); - EventBus.subscribe(MobSpawnerBroken.class, this::mobSpawnerBroken); + EventBus.subscribe(StructuresLoaded.class, this::structuresLoaded); EventBus.subscribe(PlayerLoggedIn.class, this::playerLoggedIn); EventBus.subscribe(PlayerLoggedOut.class, this::playerLoggedOut); EventBus.subscribe(PlayerSubscribed.class, this::onPlayerSubscribed); - EventBus.subscribe(ServerWorldTick.class, this::serverWorldTick); EventBus.subscribe(ServerTick.class, e -> serverTick()); - EventBus.subscribe(VillageRemoved.class, this::onVillageRemoved); } protected void setSeed(long seed) { @@ -52,33 +54,28 @@ public class CommonProxy { } private void worldLoaded(WorldLoaded event) { - int dimensionId = event.getDimensionId(); + DimensionId 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 == DimensionId.OVERWORLD) { + setSeed(seed); + setWorldSpawn(event.getSpawnX(), event.getSpawnZ()); } Logger.info("create world dimension: %s (seed: %d)", dimensionId, seed); - chunkProcessors.put(dimensionId, chunkProcessor); - villageProcessors.put(dimensionId, new VillageProcessor(dimensionId, boundingBoxCache)); } - private void chunkLoaded(ChunkLoaded event) { - AbstractChunkProcessor chunkProcessor = chunkProcessors.get(event.getDimensionId()); - if (chunkProcessor == null) return; + private void structuresLoaded(StructuresLoaded event) { + DimensionId dimensionId = event.getDimensionId(); + StructureProcessor structureProcessor = getStructureProcessor(dimensionId); + structureProcessor.process(event.getStructures()); + } - chunkProcessor.process(event.getChunk()); + private StructureProcessor getStructureProcessor(DimensionId dimensionId) { + StructureProcessor structureProcessor = structureProcessors.get(dimensionId); + if (structureProcessor == null) { + structureProcessor = new StructureProcessor(getOrCreateCache(dimensionId)); + structureProcessors.put(dimensionId, structureProcessor); + } + return structureProcessor; } private void playerLoggedIn(PlayerLoggedIn event) { @@ -95,27 +92,6 @@ public class CommonProxy { playerBoundingBoxesCache.remove(playerId); } - private void onVillageRemoved(VillageRemoved event) { - sendRemoveBoundingBox(event.getDimensionId(), event.getVillage()); - } - - private void sendRemoveBoundingBox(int dimensionId, AbstractBoundingBox boundingBox) { - PayloadBuilder payload = RemoveBoundingBox.getPayload(dimensionId, boundingBox); - if (payload == null) return; - - for (Map.Entry playerEntry : players.entrySet()) { - int playerId = playerEntry.getKey(); - ServerPlayer player = playerEntry.getValue(); - if (player.getDimensionId() == dimensionId) { - player.sendPacket(payload); - - if (playerBoundingBoxesCache.containsKey(playerId)) { - playerBoundingBoxesCache.get(playerId).remove(boundingBox); - } - } - } - } - private void onPlayerSubscribed(PlayerSubscribed event) { int playerId = event.getPlayerId(); ServerPlayer player = event.getPlayer(); @@ -124,40 +100,29 @@ public class CommonProxy { } private void sendToPlayer(int playerId, ServerPlayer player) { - BoundingBoxCache boundingBoxCache = getCache(player.getDimensionId()); - if (boundingBoxCache == null) return; + for (Map.Entry entry : dimensionCache.entrySet()) { + DimensionId dimensionId = entry.getKey(); + BoundingBoxCache boundingBoxCache = entry.getValue(); + if (boundingBoxCache == null) return; - Set playerBoundingBoxes = playerBoundingBoxesCache.computeIfAbsent(playerId, k -> new HashSet<>()); + Set playerBoundingBoxes = playerBoundingBoxesCache.computeIfAbsent(playerId, k -> new HashSet<>()); - Map> boundingBoxMap = boundingBoxCache.getBoundingBoxes(); - for (AbstractBoundingBox key : boundingBoxMap.keySet()) { - if (playerBoundingBoxes.contains(key)) { - continue; - } + Map> boundingBoxMap = boundingBoxCache.getBoundingBoxes(); + for (AbstractBoundingBox key : boundingBoxMap.keySet()) { + if (playerBoundingBoxes.contains(key)) { + continue; + } - Set boundingBoxes = boundingBoxMap.get(key); - PayloadBuilder payload = AddBoundingBox.getPayload(player.getDimensionId(), key, boundingBoxes); - if (payload != null) - player.sendPacket(payload); + Set boundingBoxes = boundingBoxMap.get(key); + PayloadBuilder payload = AddBoundingBox.getPayload(dimensionId, key, boundingBoxes); + if (payload != null) + player.sendPacket(payload); - playerBoundingBoxes.add(key); + playerBoundingBoxes.add(key); + } } } - protected void removeBoundingBox(int dimensionId, AbstractBoundingBox key) { - BoundingBoxCache cache = getCache(dimensionId); - if (cache == null) return; - - 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 (Map.Entry playerEntry : players.entrySet()) { int playerId = playerEntry.getKey(); @@ -167,26 +132,16 @@ public class CommonProxy { } } - private void serverWorldTick(ServerWorldTick event) { - VillageProcessor villageProcessor = villageProcessors.get(event.getDimensionId()); - if (villageProcessor == null) return; - - villageProcessor.process(event.getWorld()); - } - - protected BoundingBoxCache getCache(int dimensionId) { + protected BoundingBoxCache getCache(DimensionId dimensionId) { return dimensionCache.get(dimensionId); } - protected BoundingBoxCache getOrCreateCache(int dimensionId) { + protected BoundingBoxCache getOrCreateCache(DimensionId dimensionId) { return dimensionCache.computeIfAbsent(dimensionId, dt -> new BoundingBoxCache()); } protected void clearCaches() { - for (VillageProcessor villageProcessor : villageProcessors.values()) { - villageProcessor.clear(); - } - villageProcessors.clear(); + structureProcessors.clear(); for (BoundingBoxCache cache : dimensionCache.values()) { cache.clear(); }