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.
This commit is contained in:
1 parent
b14eb5c538
commit
62c94bc775
7 files changed
+240
-97
No files matched your search
@@ -1,13 +1,11 @@
|
||||
using System.Collections.Generic;
|
||||
using ProtoBuf;
|
||||
using Vintagestory.API.Common;
|
||||
using Vintagestory.API.MathTools;
|
||||
using Vintagestory.API.Server;
|
||||
|
||||
namespace ClaimLink;
|
||||
|
||||
[ProtoContract]
|
||||
public class ClaimLinkBufferRequest { }
|
||||
|
||||
[ProtoContract]
|
||||
public class ClaimLinkBufferCuboidDto
|
||||
{
|
||||
@@ -38,6 +36,9 @@ public class ClaimLinkBufferEntryDto
|
||||
|
||||
[ProtoMember(2)]
|
||||
public List<ClaimLinkBufferCuboidDto> Cuboids = new();
|
||||
|
||||
[ProtoMember(3)]
|
||||
public int GrantedAccess;
|
||||
}
|
||||
|
||||
[ProtoContract]
|
||||
@@ -45,62 +46,80 @@ 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)
|
||||
{
|
||||
api.Network.RegisterChannel(ChannelName)
|
||||
.RegisterMessageType<ClaimLinkBufferRequest>()
|
||||
.RegisterMessageType<ClaimLinkBufferResponse>()
|
||||
.SetMessageHandler<ClaimLinkBufferRequest>(
|
||||
(fromPlayer, _) => SendBuffers(api, fromPlayer)
|
||||
);
|
||||
channel = api.Network.RegisterChannel(ChannelName).RegisterMessageType<ClaimLinkBufferResponse>();
|
||||
|
||||
api.Event.PlayerNowPlaying += SendFullSync;
|
||||
}
|
||||
|
||||
private static void SendBuffers(ICoreServerAPI api, IServerPlayer toPlayer)
|
||||
private static void SendFullSync(IServerPlayer toPlayer)
|
||||
{
|
||||
ClaimLinkBufferResponse response = new();
|
||||
|
||||
BlockPos playerPos = toPlayer.Entity.Pos.AsBlockPos;
|
||||
int radius = toPlayer.WorldData.LastApprovedViewDistance;
|
||||
ClaimLinkBufferResponse response = new() { IsFullSync = true };
|
||||
|
||||
foreach (int groupId in ClaimLinkModSystem.Registry.All)
|
||||
{
|
||||
ClaimLinkBufferEntryDto entry = new() { GroupId = groupId };
|
||||
foreach (Cuboidi cuboid in ClaimLinkBuffer.GetBuffer(groupId))
|
||||
{
|
||||
if (!IsNearby(cuboid, playerPos, radius))
|
||||
continue;
|
||||
|
||||
entry.Cuboids.Add(
|
||||
new ClaimLinkBufferCuboidDto
|
||||
{
|
||||
MinX = cuboid.MinX,
|
||||
MinY = cuboid.MinY,
|
||||
MinZ = cuboid.MinZ,
|
||||
MaxX = cuboid.MaxX,
|
||||
MaxY = cuboid.MaxY,
|
||||
MaxZ = cuboid.MaxZ,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
ClaimLinkBufferEntryDto entry = BuildEntry(groupId, toPlayer);
|
||||
if (entry.Cuboids.Count > 0)
|
||||
response.Entries.Add(entry);
|
||||
}
|
||||
|
||||
api.Network.GetChannel(ChannelName).SendPacket(response, toPlayer);
|
||||
channel.SendPacket(response, toPlayer);
|
||||
}
|
||||
|
||||
private static bool IsNearby(Cuboidi cuboid, BlockPos playerPos, int radius) =>
|
||||
cuboid.MinX <= playerPos.X + radius
|
||||
&& cuboid.MaxX >= playerPos.X - radius
|
||||
&& cuboid.MinY <= playerPos.Y + radius
|
||||
&& cuboid.MaxY >= playerPos.Y - radius
|
||||
&& cuboid.MinZ <= playerPos.Z + radius
|
||||
&& cuboid.MaxZ >= playerPos.Z - radius;
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user