Files
ClaimLink/ClaimLink/ClaimLinkVisualizerNetwork.cs
T
anth64 62c94bc775 feat: push-based buffer/permission sync for the client-side TestBlockAccess pre-check
The server now pushes buffer and permission state to clients continuously: a full sync when a player joins, and an incremental delta on every registry mutation, mirroring vanilla's own land claim sync model. ClaimLinkBufferIndex is now instantiable, so the client and server each get their own region-indexed lookup built from the same algorithm.
2026-08-26 00:01:58 +02:00

126 lines
3.4 KiB
C#

using System.Collections.Generic;
using ProtoBuf;
using Vintagestory.API.Common;
using Vintagestory.API.MathTools;
using Vintagestory.API.Server;
namespace ClaimLink;
[ProtoContract]
public class ClaimLinkBufferCuboidDto
{
[ProtoMember(1)]
public int MinX;
[ProtoMember(2)]
public int MinY;
[ProtoMember(3)]
public int MinZ;
[ProtoMember(4)]
public int MaxX;
[ProtoMember(5)]
public int MaxY;
[ProtoMember(6)]
public int MaxZ;
}
[ProtoContract]
public class ClaimLinkBufferEntryDto
{
[ProtoMember(1)]
public int GroupId;
[ProtoMember(2)]
public List<ClaimLinkBufferCuboidDto> Cuboids = new();
[ProtoMember(3)]
public int GrantedAccess;
}
[ProtoContract]
public class ClaimLinkBufferResponse
{
[ProtoMember(1)]
public List<ClaimLinkBufferEntryDto> Entries = new();
[ProtoMember(2)]
public bool IsFullSync;
}
internal static class ClaimLinkVisualizerNetwork
{
internal const string ChannelName = "claimlinkvisualizer";
private static IServerNetworkChannel channel = null!;
internal static void Start(ICoreServerAPI api)
{
channel = api.Network.RegisterChannel(ChannelName).RegisterMessageType<ClaimLinkBufferResponse>();
api.Event.PlayerNowPlaying += SendFullSync;
}
private static void SendFullSync(IServerPlayer toPlayer)
{
ClaimLinkBufferResponse response = new() { IsFullSync = true };
foreach (int groupId in ClaimLinkModSystem.Registry.All)
{
ClaimLinkBufferEntryDto entry = BuildEntry(groupId, toPlayer);
if (entry.Cuboids.Count > 0)
response.Entries.Add(entry);
}
channel.SendPacket(response, toPlayer);
}
internal static void BroadcastDelta(IReadOnlyCollection<int> affectedGroupIds)
{
if (affectedGroupIds.Count == 0 || channel == null)
return;
foreach (IPlayer onlinePlayer in ClaimLinkModSystem.World.AllOnlinePlayers)
{
if (onlinePlayer is not IServerPlayer serverPlayer)
continue;
ClaimLinkBufferResponse response = new() { IsFullSync = false };
foreach (int groupId in affectedGroupIds)
response.Entries.Add(BuildEntry(groupId, serverPlayer));
channel.SendPacket(response, serverPlayer);
}
}
private static ClaimLinkBufferEntryDto BuildEntry(int groupId, IServerPlayer forPlayer)
{
EnumBlockAccessFlags granted = EnumBlockAccessFlags.None;
if (ClaimLinkModSystem.Registry.HasPermission(groupId, forPlayer, EnumBlockAccessFlags.BuildOrBreak))
granted |= EnumBlockAccessFlags.BuildOrBreak;
if (ClaimLinkModSystem.Registry.HasPermission(groupId, forPlayer, EnumBlockAccessFlags.Use))
granted |= EnumBlockAccessFlags.Use;
ClaimLinkBufferEntryDto entry = new() { GroupId = groupId, GrantedAccess = (int)granted };
foreach (Cuboidi cuboid in ClaimLinkBuffer.GetBuffer(groupId))
{
entry.Cuboids.Add(
new ClaimLinkBufferCuboidDto
{
MinX = cuboid.MinX,
MinY = cuboid.MinY,
MinZ = cuboid.MinZ,
MaxX = cuboid.MaxX,
MaxY = cuboid.MaxY,
MaxZ = cuboid.MaxZ,
}
);
}
return entry;
}
}