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;
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, BoundingBoxCache> dimensionCache = new ConcurrentHashMap<>();
+ private final Map<Integer, ServerPlayer> players = new ConcurrentHashMap<>();
+ private final Map<Integer, Set<AbstractBoundingBox>> playerBoundingBoxesCache = new HashMap<>();
+ private final Map<DimensionId, StructureProcessor> structureProcessors = new HashMap<>();
+ private final Map<DimensionId, BoundingBoxCache> 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) {
}
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) {
}
private void playerLoggedOut(PlayerLoggedOut event) {
- ServerPlayer player = event.getPlayer();
- players.remove(player);
- playerBoundingBoxesCache.remove(player);
- }
-
- 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 (ServerPlayer player : players) {
- if (player.getDimensionId() == dimensionId) {
- player.sendPacket(payload);
-
- if (playerBoundingBoxesCache.containsKey(player)) {
- playerBoundingBoxesCache.get(player).remove(boundingBox);
- }
- }
- }
+ int playerId = event.getPlayerId();
+ players.remove(playerId);
+ playerBoundingBoxesCache.remove(playerId);
}
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<DimensionId, BoundingBoxCache> entry : dimensionCache.entrySet()) {
+ DimensionId 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) {
- 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 (ServerPlayer player : players) {
- sendToPlayer(player, getCache(player.getDimensionId()));
- }
- }
+ for (Map.Entry<Integer, ServerPlayer> playerEntry : players.entrySet()) {
+ int playerId = playerEntry.getKey();
+ ServerPlayer player = playerEntry.getValue();
- private void serverWorldTick(ServerWorldTick event) {
- VillageProcessor villageProcessor = villageProcessors.get(event.getDimensionId());
- if (villageProcessor == null) return;
-
- villageProcessor.process(event.getWorld());
+ sendToPlayer(playerId, player);
+ }
}
- 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();
}