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; } }