88 lines
3.4 KiB
C#
88 lines
3.4 KiB
C#
using OrekiWoofsBees.Common;
|
|
using RoamingBees.Particles.Catchup;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Numerics;
|
|
using Vintagestory.API.Common;
|
|
using Vintagestory.API.MathTools;
|
|
using Vintagestory.API.Server;
|
|
|
|
namespace RoamingBees.Particles;
|
|
|
|
internal class BeeSpawnPacketDistributor(RoamingBeesModSystem modSystem, EnumAppSide appSide)
|
|
{
|
|
private readonly RoamingBeesModSystem modSystem = modSystem;
|
|
private readonly EnumAppSide appSide = appSide;
|
|
|
|
private Dictionary<Vector3, IBeeSpawnHandler> spawnHandlers { get; } = [];
|
|
private Dictionary<Vector3, IBeeSpawnCatchup> catchupHandlers { get; } = [];
|
|
|
|
public int SpawnHandlersCount => spawnHandlers.Count;
|
|
|
|
public void Register(BlockPos position, object manager)
|
|
{
|
|
if (manager is IBeeSpawnHandler spawnHandler)
|
|
spawnHandlers.TryAdd(new(position.X, position.Y, position.Z), spawnHandler);
|
|
if (manager is IBeeSpawnCatchup beeSpawnCatchup)
|
|
catchupHandlers.TryAdd(new(position.X, position.Y, position.Z), beeSpawnCatchup);
|
|
}
|
|
|
|
public void Unregister(BlockPos position)
|
|
{
|
|
var pos = new Vector3(position.X, position.Y, position.Z);
|
|
spawnHandlers.Remove(pos);
|
|
catchupHandlers.Remove(pos);
|
|
}
|
|
|
|
public void HandleBeeParticleSpawn(BeeSpawnPacket packet)
|
|
{
|
|
if (!Config.Instance.ReceiveParticles || appSide.HasFlag(EnumAppSide.Server))
|
|
return;
|
|
|
|
if (spawnHandlers.TryGetValue(new(packet.HivePosition.X, packet.HivePosition.Y, packet.HivePosition.Z), out var hive))
|
|
hive.HandleBeeParticleSpawn(packet);
|
|
}
|
|
|
|
public void HandleBeeParticleCatchup(BeeCatchupPacket catchupPacket)
|
|
{
|
|
if (modSystem.Mod.Info.Version.Contains("dev"))
|
|
{
|
|
modSystem.Mod.Logger.Event($"HandleBeeParticleCatchup, Pos: {catchupPacket.HivePosition.ToVec3f().ToVector3()}, Count: {catchupPacket.SpawnPackets.Count()}");
|
|
}
|
|
|
|
if (!Config.Instance.ReceiveParticles || appSide.HasFlag(EnumAppSide.Server))
|
|
return;
|
|
|
|
if (!spawnHandlers.TryGetValue(new(catchupPacket.HivePosition.X, catchupPacket.HivePosition.Y, catchupPacket.HivePosition.Z), out var hive))
|
|
return;
|
|
foreach (var spawnPacket in catchupPacket.SpawnPackets)
|
|
hive.HandleBeeParticleSpawn(spawnPacket, catchup: true);
|
|
}
|
|
|
|
internal void OnBeeCatchupRequest(IServerPlayer fromPlayer, BeeCatchupRequestPacket catchupRequest)
|
|
{
|
|
if (!appSide.HasFlag(EnumAppSide.Server))
|
|
return;
|
|
|
|
if (!catchupHandlers.TryGetValue(new(catchupRequest.HivePosition.X, catchupRequest.HivePosition.Y, catchupRequest.HivePosition.Z), out var hive))
|
|
return;
|
|
|
|
var spawnPackets = hive.ActiveBeesPackets.ToList();
|
|
if (spawnPackets.Count is 0)
|
|
return;
|
|
|
|
var message = new BeeCatchupPacket
|
|
{
|
|
HivePosition = catchupRequest.HivePosition,
|
|
SpawnPackets = spawnPackets,
|
|
};
|
|
|
|
if (modSystem.Mod.Info.Version.Contains("dev"))
|
|
modSystem.Mod.Logger.Event($"OnBeeCatchupRequest, Pos: {catchupRequest.HivePosition.ToVec3f().ToVector3()}, Count: {message.SpawnPackets.Count()}");
|
|
|
|
modSystem.ServerChannel?.SendPacket(message, fromPlayer);
|
|
}
|
|
|
|
public void Clear() => spawnHandlers.Clear();
|
|
}
|