Files
ClaimLink/ClaimLink/ClaimLinkBufferProtection.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

82 lines
2.4 KiB
C#

using Vintagestory.API.Common;
using Vintagestory.API.MathTools;
using Vintagestory.API.Server;
namespace ClaimLink;
internal static class ClaimLinkBufferProtection
{
internal static void Register(ICoreServerAPI api)
{
api.Event.CanPlaceOrBreakBlock += OnCanPlaceOrBreakBlock;
api.Event.CanUseBlock += OnCanUseBlock;
}
private static bool OnCanUseBlock(IServerPlayer byPlayer, BlockSelection blockSel) =>
IsAllowed(byPlayer, blockSel.Position, EnumBlockAccessFlags.Use);
private static bool OnCanPlaceOrBreakBlock(
IServerPlayer byPlayer,
BlockSelection blockSel,
out string claimant
)
{
bool allowed = IsAllowed(byPlayer, blockSel.Position, EnumBlockAccessFlags.BuildOrBreak);
claimant = allowed ? null! : "custommessage-claimlink-buffer-zone";
return allowed;
}
private static bool IsAllowed(
IServerPlayer byPlayer,
BlockPos pos,
EnumBlockAccessFlags accessFlag
)
{
bool allowed = true;
foreach (int groupId in ClaimLinkModSystem.BufferIndex.CandidateGroupIds(pos))
{
if (IsMember(byPlayer, groupId))
continue;
foreach (Cuboidi buffer in ClaimLinkModSystem.BufferIndex.CachedBuffer(groupId))
if (
buffer.Contains(pos)
&& !ClaimLinkModSystem.Registry.HasPermission(groupId, byPlayer, accessFlag)
)
allowed = false;
}
return allowed || HasClaimAccess(byPlayer, pos, accessFlag);
}
private static bool IsMember(IServerPlayer player, int groupId)
{
foreach (PlayerGroupMembership membership in player.Groups)
if (membership.GroupUid == groupId)
return true;
return false;
}
private static bool HasClaimAccess(
IServerPlayer player,
BlockPos pos,
EnumBlockAccessFlags accessFlag
)
{
LandClaim[]? claims = ClaimLinkModSystem.LandClaimAPI.Get(pos);
if (claims == null)
return false;
foreach (LandClaim claim in claims)
{
if (claim.TestPlayerAccess(player, accessFlag) != EnumPlayerAccessResult.Denied)
return true;
if (accessFlag == EnumBlockAccessFlags.Use && claim.AllowUseEveryone)
return true;
}
return false;
}
}