Compare commits

...
22 Commits
Author SHA1 Message Date
anth64 636a7dee98 feat: filter buffer data sent to visualizer clients by view distance 2026-07-31 07:38:56 +02:00
anth64 1e4400555c chore: bump version to 0.2.0 2026-07-31 07:18:56 +02:00
anth64 0dfcc5396a feat: add client-visualizer network endpoint for buffer geometry 2026-07-30 23:14:06 +02:00
anth64 c110ee2604 fix: normalize claim geometry before growing buffer (raw start/end marks aren't min/max ordered) 2026-07-27 07:40:12 +02:00
anth64 baf3ccbd93 feat: enforce buffer-zone non-overlap on /claimlink link 2026-07-26 15:30:35 +02:00
anth64 3cf6db1d2a fix: clear PendingLoads on /land claim new to match vanilla's unconditional reset 2026-07-26 14:36:37 +02:00
anth64 12219e6ef5 feat: add ClaimLinkConfig with configurable buffer size 2026-07-26 12:07:46 +02:00
anth64 0e0e4e0f74 chore: bump version to 0.1.0 2026-07-26 11:28:08 +02:00
anth64 154e7b7848 chore: bump version to 0.0.7 2026-07-26 11:25:27 +02:00
anth64 9e37ec11cf feat: implement admin delete/unlink/kick, fix admin unlink claim arg type 2026-07-26 11:23:21 +02:00
anth64 4d98a115ed feat: infer claim link group from chat context when omitted; drop redundant admin info 2026-07-26 11:21:13 +02:00
anth64 99238c4744 feat: hook group leave/kick to proactively cancel pending actions with a reason 2026-07-26 10:33:46 +02:00
anth64 1c079b7e44 fix: cancel pending actions on group disband even if not yet a claim link 2026-07-26 09:08:08 +02:00
anth64 b7ca9fa53b fix: track groupId per pending action for precise cleanup on delete/disband; split group hook into its own listener; self-heal orphaned registry entries in list 2026-07-26 08:51:54 +02:00
anth64 c55787ce40 feat: react to /group confirmdisband and recheck live membership on link confirm 2026-07-23 07:57:39 +02:00
anth64 b10d3a0f55 fix: cancel old owner's pending action on ownership transfer 2026-07-21 20:29:32 +02:00
anth64 b5ceba442a fix: cancel kicked player's pending action to stop stale link re-add 2026-07-21 20:25:21 +02:00
anth64 d248b772dc fix: split adminfree (by player name) from adminfreehere (by position) 2026-07-21 19:36:48 +02:00
anth64 c8fc86069b feat: gate claim link info detail by membership/admin, count members with no claims 2026-07-20 23:36:03 +02:00
anth64 2e4cc25b20 fix: guard claim-link entries against stale indices and dead groups 2026-07-20 23:28:37 +02:00
anth64 fc7e601f5b refactor: expose TryResolveOwnedClaim for future external mod consumption 2026-07-19 20:43:05 +02:00
anth64 a2e4a68f9e refactor: rebuild claim link registry around live index tracking, drop ClaimLink type 2026-07-19 20:31:10 +02:00
10 changed files with 1229 additions and 459 deletions
+9 -5
View File
@@ -1,23 +1,27 @@
using System.Collections.Generic;
using ProtoBuf;
namespace ClaimLink;
[ProtoContract]
public class ClaimLink
public class ClaimLinkSaveData
{
[ProtoMember(1)]
public int GroupId;
public List<int> Links = new();
[ProtoMember(2)]
public List<ClaimLinkEntrySaveData> Entries = new();
}
[ProtoContract]
public class ClaimLinkEntry
public class ClaimLinkEntrySaveData
{
[ProtoMember(1)]
public string OwnerPlayerUid = "";
[ProtoMember(2)]
public int ClaimIndex;
public int GroupId;
[ProtoMember(3)]
public int GroupId;
public List<int> ClaimIndicies = new();
}
+140
View File
@@ -0,0 +1,140 @@
using System.Collections.Generic;
using Vintagestory.API.Common;
using Vintagestory.API.MathTools;
namespace ClaimLink;
internal static class ClaimLinkBuffer
{
internal static List<Cuboidi> GetBuffer(int groupId)
{
List<Cuboidi> cuboids = new();
foreach (string uid in ClaimLinkModSystem.Registry.MemberUidsForGroup(groupId))
{
foreach (
int claimIndex in ClaimLinkModSystem.Registry.ClaimsForPlayerInGroup(uid, groupId)
)
{
if (
!ClaimLinkModSystem.TryResolveOwnedClaim(uid, claimIndex, out LandClaim? claim)
|| claim == null
)
continue;
foreach (Cuboidi area in claim.Areas)
cuboids.Add(GrowOne(area));
}
}
return cuboids;
}
internal static bool TryFindOverlappingClaimLink(
List<Cuboidi> areas,
ISet<int>? excludeGroupIds,
out int violatingGroupId
)
{
foreach (int groupId in ClaimLinkModSystem.Registry.All)
{
if (excludeGroupIds != null && excludeGroupIds.Contains(groupId))
continue;
foreach (Cuboidi bufferCuboid in GetBuffer(groupId))
foreach (Cuboidi area in areas)
if (area.Intersects(bufferCuboid))
{
violatingGroupId = groupId;
return true;
}
}
violatingGroupId = -1;
return false;
}
internal static bool TryFindOverlappingClaim(
List<Cuboidi> areas,
string excludeOwnerUid,
ISet<int>? excludeGroupIds,
out LandClaim? violatingClaim
)
{
HashSet<LandClaim> excludedClaims = new();
if (excludeGroupIds != null)
foreach (int groupId in excludeGroupIds)
foreach (string uid in ClaimLinkModSystem.Registry.MemberUidsForGroup(groupId))
foreach (
int claimIndex in ClaimLinkModSystem.Registry.ClaimsForPlayerInGroup(
uid,
groupId
)
)
if (
ClaimLinkModSystem.TryResolveOwnedClaim(
uid,
claimIndex,
out LandClaim? excludedClaim
)
&& excludedClaim != null
)
excludedClaims.Add(excludedClaim);
foreach (LandClaim claim in ClaimLinkModSystem.LandClaimAPI.All)
{
if (claim.OwnedByPlayerUid == excludeOwnerUid)
continue;
if (excludedClaims.Contains(claim))
continue;
foreach (Cuboidi claimArea in claim.Areas)
foreach (Cuboidi area in areas)
if (area.Intersects(claimArea))
{
violatingClaim = claim;
return true;
}
}
violatingClaim = null;
return false;
}
internal static ISet<int> MemberClaimLinkIds(string uid)
{
HashSet<int> groupIds = new();
var memberships = ClaimLinkModSystem.PlayerData.GetPlayerDataByUid(uid)?.PlayerGroupMemberships;
if (memberships == null)
return groupIds;
foreach (int groupId in memberships.Keys)
if (ClaimLinkModSystem.Registry.Exists(groupId))
groupIds.Add(groupId);
return groupIds;
}
internal static List<Cuboidi> GrowAreas(List<Cuboidi> areas)
{
List<Cuboidi> grown = new();
foreach (Cuboidi area in areas)
grown.Add(GrowOne(area));
return grown;
}
private static Cuboidi GrowOne(Cuboidi area)
{
Cuboidi normalized = new Cuboidi(
area.MinX,
area.MinY,
area.MinZ,
area.MaxX,
area.MaxY,
area.MaxZ
);
int bufferSize = ClaimLinkModSystem.Config.BufferSize;
return normalized.GrowBy(bufferSize, bufferSize, bufferSize);
}
}
+398 -121
View File
@@ -4,6 +4,7 @@ using System.Linq;
using System.Text;
using Vintagestory.API.Common;
using Vintagestory.API.Config;
using Vintagestory.API.MathTools;
using Vintagestory.API.Server;
namespace ClaimLink;
@@ -69,14 +70,18 @@ public static class ClaimLinkChatCommand
new(
new[] { "new", "n" },
"Promote a group you own to a claim link.",
new ICommandArgumentParser[] { p.Word("groupname") },
new ICommandArgumentParser[] { p.OptionalWord("groupname") },
true,
New
),
new(
new[] { "link", "l" },
"Link a claim you own to a claim link.",
new ICommandArgumentParser[] { p.Word("groupname"), p.IntRange("claim", 0, 999) },
new ICommandArgumentParser[]
{
p.IntRange("claim", 0, 999),
p.OptionalWord("groupname"),
},
true,
Link
),
@@ -104,28 +109,28 @@ public static class ClaimLinkChatCommand
new(
new[] { "kick" },
"Force-unlink all of a player's claims from the claim link.",
new ICommandArgumentParser[] { p.Word("groupname"), p.OnlinePlayer("playername") },
new ICommandArgumentParser[] { p.Word("groupname"), p.OptionalWord("playername") },
true,
Kick
),
new(
new[] { "delete" },
"Delete a claim link.",
new ICommandArgumentParser[] { p.Word("groupname") },
new ICommandArgumentParser[] { p.OptionalWord("groupname") },
true,
Delete
),
new(
new[] { "transferownership", "transfer", "to" },
"Transfer ownership of the claim link to another player.",
new ICommandArgumentParser[] { p.Word("groupname"), p.PlayerUids("playername") },
new ICommandArgumentParser[] { p.Word("groupname"), p.OptionalWord("playername") },
true,
TransferOwnership
),
new(
new[] { "info", "i" },
"Show a claim link's members and linked claims.",
new ICommandArgumentParser[] { p.Word("groupname") },
new ICommandArgumentParser[] { p.OptionalWord("groupname") },
true,
Info
),
@@ -150,38 +155,35 @@ public static class ClaimLinkChatCommand
new(
new[] { "delete", "del" },
"Delete a claim link.",
new ICommandArgumentParser[] { p.Word("groupname") },
new ICommandArgumentParser[] { p.OptionalWord("groupname") },
false,
AdminDelete
),
new(
new[] { "unlink" },
"Unlink a claim.",
new ICommandArgumentParser[] { p.OnlinePlayer("playername"), p.Word("claim") },
new ICommandArgumentParser[] { p.Word("playername"), p.IntRange("claim", 0, 999) },
false,
AdminUnlink
),
new(
new[] { "kick" },
"Force-unlink all of a player's claims.",
new ICommandArgumentParser[] { p.Word("groupname"), p.OnlinePlayer("playername") },
new ICommandArgumentParser[] { p.Word("groupname"), p.OptionalWord("playername") },
false,
AdminKick
),
new(
new[] { "transferownership", "transfer", "to" },
"Transfer ownership of any claim link to another player.",
new ICommandArgumentParser[] { p.Word("groupname"), p.PlayerUids("playername") },
new ICommandArgumentParser[]
{
p.PlayerUids("playername"),
p.OptionalWord("groupname"),
},
false,
AdminTransferOwnership
),
new(
new[] { "info", "i" },
"Show a claim link's members and linked claims.",
new ICommandArgumentParser[] { p.Word("groupname") },
false,
AdminInfo
),
};
foreach (var spec in adminCommands)
@@ -190,20 +192,53 @@ public static class ClaimLinkChatCommand
admin.EndSubCommand();
}
internal static readonly Dictionary<string, Func<TextCommandResult>> PendingActions = new();
internal static readonly Dictionary<
string,
(int GroupId, Func<TextCommandResult> Action)
> PendingActions = new();
internal static readonly Dictionary<string, string> LastCancelReason = new();
private static TextCommandResult Stage(
string playerUid,
int groupId,
string prompt,
Func<TextCommandResult> action
)
{
PendingActions[playerUid] = action;
PendingActions[playerUid] = (groupId, action);
LastCancelReason.Remove(playerUid);
return TextCommandResult.Success(
$"{prompt} Use /claimlink confirm to proceed, or /claimlink cancel to cancel."
);
}
internal static void RemovePendingActionsForGroup(int groupId, string reason)
{
List<string>? toRemove = null;
foreach (var (uid, entry) in PendingActions)
if (entry.GroupId == groupId)
(toRemove ??= new()).Add(uid);
if (toRemove == null)
return;
foreach (string uid in toRemove)
{
PendingActions.Remove(uid);
LastCancelReason[uid] = reason;
}
}
internal static void CancelPendingAction(string uid, int groupId, string reason)
{
if (!PendingActions.TryGetValue(uid, out var pending) || pending.GroupId != groupId)
return;
PendingActions.Remove(uid);
LastCancelReason[uid] = reason;
}
private static TextCommandResult? TryResolveGroup(string groupName, out PlayerGroup group)
{
group = ClaimLinkModSystem.Groups.GetPlayerGroupByName(groupName)!;
@@ -212,10 +247,27 @@ public static class ClaimLinkChatCommand
: null;
}
private static TextCommandResult? TryResolveClaimLink(PlayerGroup group, out ClaimLink link)
private static TextCommandResult? TryResolveGroupArg(
TextCommandCallingArgs args,
int argIndex,
out PlayerGroup group
)
{
link = ClaimLinkModSystem.Registry.Get(group.Uid)!;
return link == null
if (!args.Parsers[argIndex].IsMissing)
return TryResolveGroup((string)args[argIndex], out group);
int chatGroupId = args.Caller.FromChatGroupId;
if (!ClaimLinkModSystem.Groups.PlayerGroupsById.TryGetValue(chatGroupId, out group!))
return TextCommandResult.Error(
"No group specified and you are not sending this from a group chat channel."
);
return null;
}
private static TextCommandResult? TryResolveClaimLink(PlayerGroup group)
{
return !ClaimLinkModSystem.Registry.Exists(group.Uid)
? TextCommandResult.Error($"'{group.Name}' is not a claim link.")
: null;
}
@@ -247,10 +299,9 @@ public static class ClaimLinkChatCommand
public static TextCommandResult New(TextCommandCallingArgs args)
{
string groupName = (string)args[0];
string playerUid = args.Caller.Player.PlayerUID;
TextCommandResult? err = TryResolveGroup(groupName, out PlayerGroup group);
TextCommandResult? err = TryResolveGroupArg(args, 0, out PlayerGroup group);
if (err != null)
return err;
@@ -258,16 +309,21 @@ public static class ClaimLinkChatCommand
if (err != null)
return err;
if (ClaimLinkModSystem.Registry.Get(group.Uid) != null)
return TextCommandResult.Error($"'{groupName}' is already a claim link.");
if (ClaimLinkModSystem.Registry.Exists(group.Uid))
return TextCommandResult.Error($"'{group.Name}' is already a claim link.");
int groupId = group.Uid;
string groupName = group.Name;
return Stage(
playerUid,
groupId,
$"'{groupName}' will become a claim link.",
() =>
{
ClaimLinkModSystem.Registry.Add(new ClaimLink { GroupId = groupId });
if (!ClaimLinkModSystem.Groups.PlayerGroupsById.ContainsKey(groupId))
return TextCommandResult.Error($"'{groupName}' no longer exists.");
ClaimLinkModSystem.Registry.Add(groupId);
return TextCommandResult.Success($"'{groupName}' is now a claim link.");
}
);
@@ -277,11 +333,16 @@ public static class ClaimLinkChatCommand
{
string playerUid = args.Caller.Player.PlayerUID;
if (!PendingActions.TryGetValue(playerUid, out Func<TextCommandResult>? action))
if (!PendingActions.TryGetValue(playerUid, out var pending))
{
if (LastCancelReason.Remove(playerUid, out string? reason))
return TextCommandResult.Error($"You do not have a pending action ({reason}).");
return TextCommandResult.Error("You do not have a pending action.");
}
PendingActions.Remove(playerUid);
return action();
LastCancelReason.Remove(playerUid);
return pending.Action();
}
public static TextCommandResult Cancel(TextCommandCallingArgs args)
@@ -296,14 +357,13 @@ public static class ClaimLinkChatCommand
public static TextCommandResult Link(TextCommandCallingArgs args)
{
string groupName = (string)args[0];
int claimIndex = (int)args[1];
int claimIndex = (int)args[0];
TextCommandResult? err = TryResolveGroup(groupName, out PlayerGroup group);
TextCommandResult? err = TryResolveGroupArg(args, 1, out PlayerGroup group);
if (err != null)
return err;
err = TryResolveClaimLink(group, out ClaimLink link);
err = TryResolveClaimLink(group);
if (err != null)
return err;
@@ -313,22 +373,74 @@ public static class ClaimLinkChatCommand
return err;
string playerUid = player.PlayerUID;
int groupId = group.Uid;
string groupName = group.Name;
if (!ClaimLinkModSystem.TryResolveOwnedClaim(playerUid, claimIndex, out _))
return TextCommandResult.Error("You do not own that claim.");
if (
!ClaimLinkModSystem.TryResolveOwnedClaim(playerUid, claimIndex, out LandClaim? claim)
|| claim == null
)
return TextCommandResult.Error($"You don't have a claim at index {claimIndex}.");
if (ClaimLinkModSystem.Registry.IsClaimLinked(playerUid, claimIndex))
return TextCommandResult.Error("That claim is already part of a claim link.");
List<Cuboidi> grownAreas = ClaimLinkBuffer.GrowAreas(claim.Areas);
ISet<int> excludeGroupIds = new HashSet<int> { groupId };
if (
ClaimLinkBuffer.TryFindOverlappingClaimLink(
grownAreas,
excludeGroupIds,
out int violatingGroupId
)
)
{
string violatingName = ClaimLinkModSystem.Groups.PlayerGroupsById.TryGetValue(
violatingGroupId,
out PlayerGroup? violatingGroup
)
? violatingGroup!.Name
: "another claim link";
return TextCommandResult.Error(
$"Cannot link that claim, it overlaps the protected buffer of '{violatingName}'."
);
}
if (
ClaimLinkBuffer.TryFindOverlappingClaim(
grownAreas,
playerUid,
excludeGroupIds,
out LandClaim? violatingClaim
)
)
{
return TextCommandResult.Error(
$"Cannot link that claim, its buffer would overlap the claim owned by {violatingClaim!.LastKnownOwnerName}."
);
}
string claimDesc = DescribeClaim(playerUid, claimIndex);
return Stage(
playerUid,
$"{claimDesc} will be linked into '{groupName}'.",
groupId,
$"'{claimDesc}' will be linked into '{groupName}'.",
() =>
{
ClaimLinkModSystem.Registry.AddEntry(playerUid, link.GroupId, claimIndex);
bool stillMember =
ClaimLinkModSystem
.PlayerData.GetPlayerDataByUid(playerUid)
?.PlayerGroupMemberships.ContainsKey(groupId) == true;
if (!stillMember)
return TextCommandResult.Error($"You are no longer a member of '{groupName}'.");
return TextCommandResult.Success($"Linked {claimDesc} to '{groupName}'.");
bool success = ClaimLinkModSystem.Registry.AddEntry(playerUid, groupId, claimIndex);
return success
? TextCommandResult.Success($"Linked '{claimDesc}' to '{groupName}'.")
: TextCommandResult.Error(
$"Unable to link claim {claimIndex} to '{groupName}', it is currently being modified."
);
}
);
}
@@ -338,61 +450,83 @@ public static class ClaimLinkChatCommand
int claimIndex = (int)args[0];
string playerUid = args.Caller.Player.PlayerUID;
ClaimLink? link = ClaimLinkModSystem.Registry.FindLinkContaining(playerUid, claimIndex);
if (link == null)
int? groupId = ClaimLinkModSystem.Registry.FindGroupContaining(playerUid, claimIndex);
if (groupId == null)
return TextCommandResult.Error(
$"Claim {claimIndex} is not linked into any claim link by you."
);
string groupName = ClaimLinkModSystem.Groups.PlayerGroupsById[link.GroupId].Name;
string groupName = ClaimLinkModSystem.Groups.PlayerGroupsById[(int)groupId].Name;
string claimDesc = DescribeClaim(playerUid, claimIndex);
return Stage(
playerUid,
$"{claimDesc} will be unlinked from '{groupName}'.",
(int)groupId,
$"'{claimDesc}' will be unlinked from '{groupName}'.",
() =>
{
ClaimLinkModSystem.Registry.RemoveEntry(playerUid, link.GroupId, claimIndex);
return TextCommandResult.Success($"Unlinked {claimDesc} from '{groupName}'.");
ClaimLinkModSystem.Registry.RemoveEntry(playerUid, (int)groupId, claimIndex);
return TextCommandResult.Success($"Unlinked '{claimDesc}' from '{groupName}'.");
}
);
}
public static TextCommandResult Kick(TextCommandCallingArgs args)
{
string groupName = (string)args[0];
IPlayer target = (IPlayer)args[1];
string word1 = (string)args[0];
string? word2 = args.Parsers[1].IsMissing ? null : (string)args[1];
TextCommandResult? err = TryResolveGroup(groupName, out PlayerGroup group);
if (err != null)
return err;
string targetName;
PlayerGroup group;
if (word2 != null)
{
TextCommandResult? err = TryResolveGroup(word1, out group);
if (err != null)
return err;
targetName = word2;
}
else
{
int chatGroupId = args.Caller.FromChatGroupId;
if (!ClaimLinkModSystem.Groups.PlayerGroupsById.TryGetValue(chatGroupId, out group!))
return TextCommandResult.Error(
"No group specified and you are not sending this from a group chat channel."
);
targetName = word1;
}
err = TryResolveClaimLink(group, out _);
if (err != null)
return err;
TextCommandResult? linkErr = TryResolveClaimLink(group);
if (linkErr != null)
return linkErr;
err = RequireOpOrOwner(args.Caller.Player, group);
if (err != null)
return err;
linkErr = RequireOpOrOwner(args.Caller.Player, group);
if (linkErr != null)
return linkErr;
string? targetUid = ClaimLinkModSystem
.PlayerData.GetPlayerDataByLastKnownName(targetName)
?.PlayerUID;
if (targetUid == null)
return TextCommandResult.Error($"No such player '{targetName}'.");
string targetUid = target.PlayerUID;
string playerUid = args.Caller.Player.PlayerUID;
string groupName = group.Name;
if (!ClaimLinkModSystem.Registry.HasAnyEntry(targetUid, group.Uid))
return TextCommandResult.Error(
$"{target.PlayerName} has no claims linked in '{groupName}'."
);
return TextCommandResult.Error($"{targetName} has no claims linked in '{groupName}'.");
return Stage(
playerUid,
$"All of {target.PlayerName}'s claims will be unlinked from '{groupName}'.",
group.Uid,
$"All of {targetName}'s claims will be unlinked from '{groupName}'.",
() =>
{
ClaimLinkModSystem.Registry.RemoveAllForPlayerInGroup(targetUid, group.Uid);
ClaimLinkCommandListener.PendingLoads.Remove(targetUid);
CommandListener.PendingLoads.Remove(targetUid);
PendingActions.Remove(targetUid);
return TextCommandResult.Success(
$"Unlinked all claims of {target.PlayerName} from '{groupName}'."
$"Unlinked all claims of {targetName} from '{groupName}'."
);
}
);
@@ -400,14 +534,13 @@ public static class ClaimLinkChatCommand
public static TextCommandResult Delete(TextCommandCallingArgs args)
{
string groupName = (string)args[0];
string playerUid = args.Caller.Player.PlayerUID;
TextCommandResult? err = TryResolveGroup(groupName, out PlayerGroup group);
TextCommandResult? err = TryResolveGroupArg(args, 0, out PlayerGroup group);
if (err != null)
return err;
err = TryResolveClaimLink(group, out _);
err = TryResolveClaimLink(group);
if (err != null)
return err;
@@ -416,14 +549,14 @@ public static class ClaimLinkChatCommand
return err;
int groupId = group.Uid;
string groupName = group.Name;
return Stage(
playerUid,
groupId,
$"'{groupName}' will be deleted as a claim link.",
() =>
{
foreach (string uid in ClaimLinkModSystem.Registry.MemberUidsForGroup(groupId))
PendingActions.Remove(uid);
RemovePendingActionsForGroup(groupId, "claim link was deleted");
ClaimLinkModSystem.Registry.Remove(groupId);
return TextCommandResult.Success($"'{groupName}' is no longer a claim link.");
}
@@ -432,22 +565,36 @@ public static class ClaimLinkChatCommand
public static TextCommandResult TransferOwnership(TextCommandCallingArgs args)
{
string groupName = (string)args[0];
string word1 = (string)args[0];
string? word2 = args.Parsers[1].IsMissing ? null : (string)args[1];
string targetName;
PlayerGroup group;
if (word2 != null)
{
TextCommandResult? groupErr = TryResolveGroup(word1, out group);
if (groupErr != null)
return groupErr;
targetName = word2;
}
else
{
int chatGroupId = args.Caller.FromChatGroupId;
if (!ClaimLinkModSystem.Groups.PlayerGroupsById.TryGetValue(chatGroupId, out group!))
return TextCommandResult.Error(
"No group specified and you are not sending this from a group chat channel."
);
targetName = word1;
}
IPlayer player = args.Caller.Player;
string playerUid = player.PlayerUID;
TextCommandResult? err = TryResolveTargetPlayer(
(PlayerUidName[])args[1],
out IPlayer target
);
TextCommandResult? err = TryResolveOnlinePlayerByName(targetName, out IPlayer target);
if (err != null)
return err;
err = TryResolveGroup(groupName, out PlayerGroup group);
if (err != null)
return err;
err = TryResolveClaimLink(group, out ClaimLink link);
err = TryResolveClaimLink(group);
if (err != null)
return err;
@@ -463,13 +610,31 @@ public static class ClaimLinkChatCommand
if (err != null)
return err;
string groupName = group.Name;
return Stage(
playerUid,
group.Uid,
$"Ownership of '{groupName}' will be transferred to {target.PlayerName}.",
() => ExecuteTransferOwnership(group, link, target)
() => ExecuteTransferOwnership(group, target)
);
}
private static TextCommandResult? TryResolveOnlinePlayerByName(string name, out IPlayer target)
{
target = null!;
foreach (IPlayer online in ClaimLinkModSystem.World.AllOnlinePlayers)
{
if (string.Equals(online.PlayerName, name, StringComparison.OrdinalIgnoreCase))
{
target = online;
return null;
}
}
return TextCommandResult.Error($"No such player '{name}' online.");
}
private static TextCommandResult? TryResolveTargetPlayer(
PlayerUidName[] matches,
out IPlayer target
@@ -495,11 +660,7 @@ public static class ClaimLinkChatCommand
: null;
}
private static TextCommandResult ExecuteTransferOwnership(
PlayerGroup group,
ClaimLink link,
IPlayer target
)
private static TextCommandResult ExecuteTransferOwnership(PlayerGroup group, IPlayer target)
{
string oldOwnerUid = group.OwnerUID;
IPlayer? oldOwner = ClaimLinkModSystem.World.PlayerByUid(oldOwnerUid);
@@ -512,10 +673,12 @@ public static class ClaimLinkChatCommand
if (oldOwnerMembership != null)
oldOwnerMembership.Level = EnumPlayerGroupMemberShip.Op;
PendingActions.Remove(oldOwnerUid);
string notice = $"'{group.Name}' is now owned by {target.PlayerName}.";
HashSet<string> notifyUids = ClaimLinkModSystem
.Registry.MemberUidsForGroup(link.GroupId)
.Registry.MemberUidsForGroup(group.Uid)
.ToHashSet();
notifyUids.Add(oldOwnerUid);
notifyUids.Add(target.PlayerUID);
@@ -534,41 +697,70 @@ public static class ClaimLinkChatCommand
public static TextCommandResult Info(TextCommandCallingArgs args)
{
string groupName = (string)args[0];
TextCommandResult? err = TryResolveGroup(groupName, out PlayerGroup group);
TextCommandResult? err = TryResolveGroupArg(args, 0, out PlayerGroup group);
if (err != null)
return err;
err = TryResolveClaimLink(group, out ClaimLink link);
err = TryResolveClaimLink(group);
if (err != null)
return err;
return TextCommandResult.Success(FormatInfo(groupName, link));
IPlayer player = args.Caller.Player;
bool showDetails =
player.HasPrivilege(Privilege.controlserver) || player.GetGroup(group.Uid) != null;
return TextCommandResult.Success(FormatInfo(group.Name, group.Uid, showDetails));
}
public static TextCommandResult List(TextCommandCallingArgs args)
{
List<ClaimLink> links = ClaimLinkModSystem
.Registry.All.OrderByDescending(l =>
ClaimLinkModSystem.Registry.MemberCountForGroup(l.GroupId)
)
.ToList();
List<int> groupIds = new(ClaimLinkModSystem.Registry.All);
groupIds.Sort((a, b) => GroupMemberCount(b).CompareTo(GroupMemberCount(a)));
if (links.Count == 0)
if (groupIds.Count == 0)
return TextCommandResult.Success("There are no claim links.");
StringBuilder sb = new();
sb.AppendLine($"Claim links ({links.Count}):");
int shown = 0;
foreach (ClaimLink link in links)
foreach (int groupId in groupIds)
{
string groupName = ClaimLinkModSystem.Groups.PlayerGroupsById[link.GroupId].Name;
int memberCount = ClaimLinkModSystem.Registry.MemberCountForGroup(link.GroupId);
sb.AppendLine($" {groupName}: {memberCount} member{(memberCount == 1 ? "" : "s")}");
if (
!ClaimLinkModSystem.Groups.PlayerGroupsById.TryGetValue(
groupId,
out PlayerGroup? group
)
)
{
RemovePendingActionsForGroup(groupId, "group no longer exists");
ClaimLinkModSystem.Registry.Remove(groupId);
continue;
}
int memberCount = GroupMemberCount(groupId);
sb.AppendLine($" {group.Name}: {memberCount} member{(memberCount == 1 ? "" : "s")}");
shown++;
}
return TextCommandResult.Success(sb.ToString());
if (shown == 0)
return TextCommandResult.Success("There are no claim links.");
return TextCommandResult.Success($"Claim links ({shown}):\n{sb}");
}
private static IEnumerable<string> GroupMemberUids(int groupId)
{
foreach (var playerData in ClaimLinkModSystem.PlayerData.PlayerDataByUid.Values)
if (playerData.PlayerGroupMemberships.ContainsKey(groupId))
yield return playerData.PlayerUID;
}
private static int GroupMemberCount(int groupId)
{
int count = 0;
foreach (string _ in GroupMemberUids(groupId))
count++;
return count;
}
private static string DescribeClaim(string ownerPlayerUid, int claimIndex)
@@ -586,20 +778,27 @@ public static class ClaimLinkChatCommand
return string.IsNullOrEmpty(claim.Description) ? $"claim {claimIndex}" : claim.Description;
}
private static string FormatInfo(string groupName, ClaimLink link)
private static string FormatInfo(string groupName, int groupId, bool showDetails)
{
int memberCount = ClaimLinkModSystem.Registry.MemberCountForGroup(link.GroupId);
int memberCount = GroupMemberCount(groupId);
StringBuilder sb = new();
sb.AppendLine(
$"Claim link '{groupName}' ({memberCount} member{(memberCount == 1 ? "" : "s")}):"
);
foreach (string uid in ClaimLinkModSystem.Registry.MemberUidsForGroup(link.GroupId))
foreach (string uid in GroupMemberUids(groupId))
{
string name = ClaimLinkModSystem.World.PlayerByUid(uid)?.PlayerName ?? uid;
if (!showDetails)
{
sb.AppendLine($" {name}");
continue;
}
IEnumerable<string> claims = ClaimLinkModSystem
.Registry.ClaimsForPlayerInGroup(uid, link.GroupId)
.Registry.ClaimsForPlayerInGroup(uid, groupId)
.Select(i => DescribeClaim(uid, i));
sb.AppendLine($" {name}: [{string.Join(", ", claims)}]");
}
@@ -607,31 +806,112 @@ public static class ClaimLinkChatCommand
return sb.ToString();
}
public static TextCommandResult AdminDelete(TextCommandCallingArgs args) =>
TextCommandResult.Success("stub: claimlink admin delete");
public static TextCommandResult AdminDelete(TextCommandCallingArgs args)
{
TextCommandResult? err = TryResolveGroupArg(args, 0, out PlayerGroup group);
if (err != null)
return err;
public static TextCommandResult AdminUnlink(TextCommandCallingArgs args) =>
TextCommandResult.Success("stub: claimlink admin unlink");
err = TryResolveClaimLink(group);
if (err != null)
return err;
public static TextCommandResult AdminKick(TextCommandCallingArgs args) =>
TextCommandResult.Success("stub: claimlink admin kick");
int groupId = group.Uid;
string groupName = group.Name;
RemovePendingActionsForGroup(groupId, "claim link was deleted");
ClaimLinkModSystem.Registry.Remove(groupId);
return TextCommandResult.Success($"'{groupName}' is no longer a claim link.");
}
public static TextCommandResult AdminUnlink(TextCommandCallingArgs args)
{
string playerName = (string)args[0];
int claimIndex = (int)args[1];
string? targetUid = ClaimLinkModSystem
.PlayerData.GetPlayerDataByLastKnownName(playerName)
?.PlayerUID;
if (targetUid == null)
return TextCommandResult.Error($"No such player '{playerName}'.");
int? groupId = ClaimLinkModSystem.Registry.FindGroupContaining(targetUid, claimIndex);
if (groupId == null)
return TextCommandResult.Error(
$"Claim {claimIndex} is not linked into any claim link by {playerName}."
);
string groupName = ClaimLinkModSystem.Groups.PlayerGroupsById[(int)groupId].Name;
string claimDesc = DescribeClaim(targetUid, claimIndex);
ClaimLinkModSystem.Registry.RemoveEntry(targetUid, (int)groupId, claimIndex);
return TextCommandResult.Success(
$"Unlinked '{claimDesc}' from '{groupName}' (owned by {playerName})."
);
}
public static TextCommandResult AdminKick(TextCommandCallingArgs args)
{
string word1 = (string)args[0];
string? word2 = args.Parsers[1].IsMissing ? null : (string)args[1];
string targetName;
PlayerGroup group;
if (word2 != null)
{
TextCommandResult? err = TryResolveGroup(word1, out group);
if (err != null)
return err;
targetName = word2;
}
else
{
int chatGroupId = args.Caller.FromChatGroupId;
if (!ClaimLinkModSystem.Groups.PlayerGroupsById.TryGetValue(chatGroupId, out group!))
return TextCommandResult.Error(
"No group specified and you are not sending this from a group chat channel."
);
targetName = word1;
}
TextCommandResult? linkErr = TryResolveClaimLink(group);
if (linkErr != null)
return linkErr;
string? targetUid = ClaimLinkModSystem
.PlayerData.GetPlayerDataByLastKnownName(targetName)
?.PlayerUID;
if (targetUid == null)
return TextCommandResult.Error($"No such player '{targetName}'.");
string groupName = group.Name;
if (!ClaimLinkModSystem.Registry.HasAnyEntry(targetUid, group.Uid))
return TextCommandResult.Error($"{targetName} has no claims linked in '{groupName}'.");
ClaimLinkModSystem.Registry.RemoveAllForPlayerInGroup(targetUid, group.Uid);
CommandListener.PendingLoads.Remove(targetUid);
PendingActions.Remove(targetUid);
return TextCommandResult.Success(
$"Unlinked all claims of {targetName} from '{groupName}'."
);
}
public static TextCommandResult AdminTransferOwnership(TextCommandCallingArgs args)
{
string groupName = (string)args[0];
TextCommandResult? err = TryResolveTargetPlayer(
(PlayerUidName[])args[1],
(PlayerUidName[])args[0],
out IPlayer target
);
if (err != null)
return err;
err = TryResolveGroup(groupName, out PlayerGroup group);
err = TryResolveGroupArg(args, 1, out PlayerGroup group);
if (err != null)
return err;
err = TryResolveClaimLink(group, out ClaimLink link);
err = TryResolveClaimLink(group);
if (err != null)
return err;
@@ -643,12 +923,9 @@ public static class ClaimLinkChatCommand
if (err != null)
return err;
return ExecuteTransferOwnership(group, link, target);
return ExecuteTransferOwnership(group, target);
}
public static TextCommandResult AdminInfo(TextCommandCallingArgs args) =>
TextCommandResult.Success("stub: claimlink admin info");
internal static void OnPlayerDisconnect(IServerPlayer player)
{
PendingActions.Remove(player.PlayerUID);
+6
View File
@@ -0,0 +1,6 @@
namespace ClaimLink;
internal class ClaimLinkConfig
{
public int BufferSize = 16;
}
+19 -7
View File
@@ -12,23 +12,33 @@ public class ClaimLinkModSystem : ModSystem
internal static ClaimLinkRegistry Registry = null!;
internal static IGroupManager Groups = null!;
internal static IWorldAccessor World = null!;
internal ClaimLinkCommandListener? CmdListener;
internal static IPlayerDataManager PlayerData = null!;
internal static ClaimLinkConfig Config = null!;
internal CommandListener? CmdListener;
internal GroupCommandListener? GroupCmdListener;
public override bool ShouldLoad(EnumAppSide forSide) => forSide == EnumAppSide.Server;
public override void StartServerSide(ICoreServerAPI api)
{
LandClaimAPI = api.World.Claims;
Logger = api.Logger;
Registry = new ClaimLinkRegistry(api.WorldManager.SaveGame);
Groups = api.Groups;
World = api.World;
Groups = api.Groups;
Logger = api.Logger;
PlayerData = api.PlayerData;
Config = api.LoadModConfig<ClaimLinkConfig>("claimlink.json") ?? new ClaimLinkConfig();
api.StoreModConfig(Config, "claimlink.json");
Registry = new ClaimLinkRegistry(api.WorldManager.SaveGame);
CmdListener = new ClaimLinkCommandListener();
CmdListener = new CommandListener();
CommandHookModSystem.Register(CmdListener);
GroupCmdListener = new GroupCommandListener();
CommandHookModSystem.Register(GroupCmdListener);
ClaimLinkChatCommand.Register(api);
ClaimLinkVisualizerNetwork.Start(api);
api.Event.PlayerDisconnect += ClaimLinkCommandListener.OnPlayerDisconnect;
api.Event.PlayerDisconnect += CommandListener.OnPlayerDisconnect;
api.Event.PlayerDisconnect += GroupCommandListener.OnPlayerDisconnect;
api.Event.PlayerDisconnect += ClaimLinkChatCommand.OnPlayerDisconnect;
}
@@ -36,6 +46,8 @@ public class ClaimLinkModSystem : ModSystem
{
if (CmdListener != null)
CommandHookModSystem.Unregister(CmdListener);
if (GroupCmdListener != null)
CommandHookModSystem.Unregister(GroupCmdListener);
}
private static IEnumerable<(int claimIndex, LandClaim claim)> EnumerateOwnedClaims(
@@ -53,7 +65,7 @@ public class ClaimLinkModSystem : ModSystem
}
}
internal static bool TryResolveOwnedClaim(
public static bool TryResolveOwnedClaim(
string ownerPlayerUid,
int claimIndex,
out LandClaim? claim
+132 -102
View File
@@ -1,171 +1,201 @@
using System.Collections.Generic;
using System.Linq;
using ProtoBuf;
using Vintagestory.API.Server;
namespace ClaimLink;
[ProtoContract]
public class ClaimLinkData
{
[ProtoMember(1)]
public List<ClaimLink> Links = new();
[ProtoMember(2)]
public List<ClaimLinkEntry> Entries = new();
}
public class ClaimLinkRegistry
{
private const string SaveKey = "claimlink:links";
private const string SaveKey = "claimlink:claimlinkdata";
private readonly ISaveGame saveGame;
private readonly Dictionary<int, ClaimLink> byGroupId = new();
private readonly Dictionary<string, Dictionary<int, List<int>>> byPlayer = new();
private readonly List<int> claimLinks = new();
private readonly Dictionary<string, Dictionary<int, List<int>>> entries = new();
public ClaimLinkRegistry(ISaveGame saveGame)
{
this.saveGame = saveGame;
ClaimLinkData? data = saveGame.GetData<ClaimLinkData?>(SaveKey, null);
ClaimLinkSaveData? data = saveGame.GetData<ClaimLinkSaveData?>(SaveKey, null);
if (data == null)
return;
foreach (ClaimLink link in data.Links)
byGroupId[link.GroupId] = link;
claimLinks.AddRange(data.Links);
foreach (ClaimLinkEntry entry in data.Entries)
GetOrCreateIndices(entry.OwnerPlayerUid, entry.GroupId).Add(entry.ClaimIndex);
foreach (var entry in data.Entries)
{
if (!entries.ContainsKey(entry.OwnerPlayerUid))
entries[entry.OwnerPlayerUid] = new Dictionary<int, List<int>>();
entries[entry.OwnerPlayerUid][entry.GroupId] = new(entry.ClaimIndicies);
}
}
public ClaimLink? Get(int groupId) =>
byGroupId.TryGetValue(groupId, out ClaimLink? link) ? link : null;
public IReadOnlyList<int> All => claimLinks;
public IReadOnlyCollection<ClaimLink> All => byGroupId.Values;
public bool Exists(int groupId) => claimLinks.Contains(groupId);
public void Add(ClaimLink link)
public void Add(int groupId)
{
byGroupId[link.GroupId] = link;
claimLinks.Add(groupId);
Save();
}
public void Remove(int groupId)
{
byGroupId.Remove(groupId);
List<string> emptyPlayerUids = new();
foreach (KeyValuePair<string, Dictionary<int, List<int>>> playerEntry in byPlayer)
{
playerEntry.Value.Remove(groupId);
if (playerEntry.Value.Count == 0)
emptyPlayerUids.Add(playerEntry.Key);
}
foreach (string uid in emptyPlayerUids)
byPlayer.Remove(uid);
claimLinks.Remove(groupId);
foreach (var groups in entries.Values)
groups.Remove(groupId);
Prune();
Save();
}
public bool IsClaimLinked(string ownerPlayerUid, int claimIndex) =>
FindLinkContaining(ownerPlayerUid, claimIndex) != null;
public bool IsClaimLinked(string uid, int claimIndex) =>
FindGroupContaining(uid, claimIndex) != null;
public ClaimLink? FindLinkContaining(string ownerPlayerUid, int claimIndex)
public int? FindGroupContaining(string uid, int claimIndex)
{
if (!byPlayer.TryGetValue(ownerPlayerUid, out Dictionary<int, List<int>>? perGroup))
if (!entries.TryGetValue(uid, out var groups))
return null;
foreach (KeyValuePair<int, List<int>> entry in perGroup)
if (entry.Value.Contains(claimIndex))
return Get(entry.Key);
foreach (var (groupId, claims) in groups)
if (claims.Contains(claimIndex))
return groupId;
return null;
}
public void AddEntry(string ownerPlayerUid, int groupId, int claimIndex)
public bool AddEntry(string uid, int groupId, int claimIndex)
{
GetOrCreateIndices(ownerPlayerUid, groupId).Add(claimIndex);
if (CommandListener.PendingLoads.ContainsKey(uid))
return false;
if (!Exists(groupId))
return false;
if (!entries.TryGetValue(uid, out var groups))
entries[uid] = groups = new();
if (!groups.TryGetValue(groupId, out var claims))
groups[groupId] = claims = new();
claims.Add(claimIndex);
Save();
return true;
}
public void RemoveEntry(string uid, int groupId, int claimIndex)
{
if (entries.TryGetValue(uid, out var groups) && groups.TryGetValue(groupId, out var claims))
claims.Remove(claimIndex);
Prune();
Save();
}
public void RemoveEntry(string ownerPlayerUid, int groupId, int claimIndex)
public void ClaimRemoved(string uid, int removedIndex)
{
if (
byPlayer.TryGetValue(ownerPlayerUid, out Dictionary<int, List<int>>? perGroup)
&& perGroup.TryGetValue(groupId, out List<int>? indices)
)
{
indices.Remove(claimIndex);
if (indices.Count == 0)
perGroup.Remove(groupId);
if (!entries.TryGetValue(uid, out var groups))
return;
if (perGroup.Count == 0)
byPlayer.Remove(ownerPlayerUid);
foreach (var claims in groups.Values)
{
claims.Remove(removedIndex);
for (int i = 0; i < claims.Count; i++)
if (claims[i] > removedIndex)
claims[i]--;
}
Prune();
Save();
}
public void ClaimSaved(string uid, int claimIndex)
{
if (!entries.TryGetValue(uid, out var groups))
return;
int count = 0;
foreach (Vintagestory.API.Common.LandClaim c in ClaimLinkModSystem.LandClaimAPI.All)
if (c.OwnedByPlayerUid == uid)
count++;
int newIndex = count - 1;
foreach (var claims in groups.Values)
{
int i = claims.IndexOf(claimIndex);
for (int j = 0; j < claims.Count; j++)
if (j != i && claims[j] > claimIndex)
claims[j]--;
if (i >= 0)
claims[i] = newIndex;
}
Save();
}
public bool HasAnyEntry(string ownerPlayerUid, int groupId) =>
byPlayer.TryGetValue(ownerPlayerUid, out Dictionary<int, List<int>>? perGroup)
&& perGroup.TryGetValue(groupId, out List<int>? indices)
&& indices.Count > 0;
public bool HasAnyEntry(string uid, int groupId) =>
entries.TryGetValue(uid, out var groups)
&& groups.TryGetValue(groupId, out var claims)
&& claims.Count > 0;
public void RemoveAllForPlayerInGroup(string ownerPlayerUid, int groupId)
public void RemoveAllForPlayerInGroup(string uid, int groupId)
{
if (byPlayer.TryGetValue(ownerPlayerUid, out Dictionary<int, List<int>>? perGroup))
{
perGroup.Remove(groupId);
if (perGroup.Count == 0)
byPlayer.Remove(ownerPlayerUid);
}
if (entries.TryGetValue(uid, out var groups))
groups.Remove(groupId);
Prune();
Save();
}
public IEnumerable<string> MemberUidsForGroup(int groupId) =>
byPlayer
.Where(kv => kv.Value.TryGetValue(groupId, out List<int>? indices) && indices.Count > 0)
.Select(kv => kv.Key);
public void ClearAllForPlayer(string uid)
{
entries.Remove(uid);
Save();
}
public int MemberCountForGroup(int groupId) => MemberUidsForGroup(groupId).Count();
public IEnumerable<string> MemberUidsForGroup(int groupId)
{
foreach (var (uid, groups) in entries)
if (groups.ContainsKey(groupId))
yield return uid;
}
public IReadOnlyList<int> ClaimsForPlayerInGroup(string ownerPlayerUid, int groupId) =>
byPlayer.TryGetValue(ownerPlayerUid, out Dictionary<int, List<int>>? perGroup)
&& perGroup.TryGetValue(groupId, out List<int>? indices)
? indices
public int MemberCountForGroup(int groupId)
{
int count = 0;
foreach (var uid in MemberUidsForGroup(groupId))
count++;
return count;
}
public IReadOnlyList<int> ClaimsForPlayerInGroup(string uid, int groupId) =>
entries.TryGetValue(uid, out var groups) && groups.TryGetValue(groupId, out var claims)
? claims
: System.Array.Empty<int>();
private List<int> GetOrCreateIndices(string playerUid, int groupId)
private void Prune()
{
if (!byPlayer.TryGetValue(playerUid, out Dictionary<int, List<int>>? perGroup))
byPlayer[playerUid] = perGroup = new Dictionary<int, List<int>>();
if (!perGroup.TryGetValue(groupId, out List<int>? indices))
perGroup[groupId] = indices = new List<int>();
return indices;
List<string>? empty = null;
foreach (var (uid, groups) in entries)
if (groups.Count == 0)
(empty ??= new()).Add(uid);
if (empty != null)
foreach (var uid in empty)
entries.Remove(uid);
}
public void Save()
{
List<ClaimLinkEntry> entries = new();
foreach (KeyValuePair<string, Dictionary<int, List<int>>> playerEntry in byPlayer)
foreach (KeyValuePair<int, List<int>> groupEntry in playerEntry.Value)
foreach (int index in groupEntry.Value)
entries.Add(
new ClaimLinkEntry
{
OwnerPlayerUid = playerEntry.Key,
GroupId = groupEntry.Key,
ClaimIndex = index,
}
);
var entryList = new List<ClaimLinkEntrySaveData>();
foreach (var (uid, groups) in entries)
foreach (var (groupId, claims) in groups)
entryList.Add(
new ClaimLinkEntrySaveData
{
OwnerPlayerUid = uid,
GroupId = groupId,
ClaimIndicies = new(claims),
}
);
saveGame.StoreData(
SaveKey,
new ClaimLinkData { Links = new List<ClaimLink>(byGroupId.Values), Entries = entries }
new ClaimLinkSaveData { Links = claimLinks, Entries = entryList }
);
}
}
+107
View File
@@ -0,0 +1,107 @@
using System.Collections.Generic;
using ProtoBuf;
using Vintagestory.API.MathTools;
using Vintagestory.API.Server;
namespace ClaimLink;
[ProtoContract]
public class ClaimLinkBufferRequest { }
[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();
}
[ProtoContract]
public class ClaimLinkBufferResponse
{
[ProtoMember(1)]
public List<ClaimLinkBufferEntryDto> Entries = new();
}
internal static class ClaimLinkVisualizerNetwork
{
internal const string ChannelName = "claimlinkvisualizer";
internal static void Start(ICoreServerAPI api)
{
api.Network
.RegisterChannel(ChannelName)
.RegisterMessageType<ClaimLinkBufferRequest>()
.RegisterMessageType<ClaimLinkBufferResponse>()
.SetMessageHandler<ClaimLinkBufferRequest>((fromPlayer, _) =>
SendBuffers(api, fromPlayer)
);
}
private static void SendBuffers(ICoreServerAPI api, IServerPlayer toPlayer)
{
ClaimLinkBufferResponse response = new();
BlockPos playerPos = toPlayer.Entity.Pos.AsBlockPos;
int radius = toPlayer.WorldData.LastApprovedViewDistance;
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,
}
);
}
if (entry.Cuboids.Count > 0)
response.Entries.Add(entry);
}
api.Network.GetChannel(ChannelName).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;
}
+416
View File
@@ -0,0 +1,416 @@
using System.Collections.Generic;
using CommandHook;
using Vintagestory.API.Common;
using Vintagestory.API.Config;
using Vintagestory.API.MathTools;
using Vintagestory.API.Server;
namespace ClaimLink;
public class CommandListener : ICommandHookListener
{
public string ModId => "claimlink";
public IReadOnlyList<string> Commands => new[] { "land" };
public CommandRegistration Registration => new(Before, After);
internal static Dictionary<string, int> PendingLoads = new Dictionary<string, int>();
internal static Dictionary<string, List<Cuboidi>?> PendingSaveChecks =
new Dictionary<string, List<Cuboidi>?>();
private delegate void SubHandler(Caller caller, CmdArgs args);
private static readonly Dictionary<string, SubHandler> topLevel = new()
{
["claim"] = Claim,
["free"] = Free,
["adminfree"] = AdminFree,
["adminfreehere"] = AdminFreeHere,
};
private static readonly Dictionary<string, SubHandler> claimSub = new()
{
["load"] = ClaimLoad,
["new"] = ClaimNew,
["save"] = ClaimSave,
["cancel"] = ClaimCancel,
};
private static void Dispatch(TextCommandCallingArgs args)
{
CmdArgs rawArgs = args.RawArgs.Clone();
while (rawArgs.Length > 0)
{
string word = rawArgs.PeekWord();
if (topLevel.TryGetValue(word, out SubHandler? handler))
{
rawArgs.PopWord();
handler(args.Caller, rawArgs);
}
else
{
rawArgs.PopWord();
}
}
}
private TextCommandResult? Before(TextCommandCallingArgs args)
{
Dispatch(args);
return null;
}
private void After(TextCommandCallingArgs args, TextCommandResult result)
{
if (args.Caller.Player == null)
return;
string uid = args.Caller.Player.PlayerUID;
if (!PendingSaveChecks.Remove(uid, out List<Cuboidi>? snapshot))
return;
if (result.Status != EnumCommandStatus.Success)
return;
List<LandClaim> allClaims = ClaimLinkModSystem.LandClaimAPI.All;
if (allClaims.Count == 0)
return;
LandClaim savedClaim = allClaims[allClaims.Count - 1];
if (savedClaim.OwnedByPlayerUid != uid)
return;
int? linkedGroupId = null;
if (ClaimLinkModSystem.TryResolveClaimIndex(uid, savedClaim, out int savedIndex))
linkedGroupId = ClaimLinkModSystem.Registry.FindGroupContaining(uid, savedIndex);
bool isLinked = linkedGroupId != null;
List<Cuboidi> checkAreas = isLinked
? ClaimLinkBuffer.GrowAreas(savedClaim.Areas)
: savedClaim.Areas;
ISet<int> excludeGroupIds = isLinked
? new HashSet<int> { linkedGroupId!.Value }
: ClaimLinkBuffer.MemberClaimLinkIds(uid);
LandClaim? violatingClaim = null;
bool violatesClaimLink = ClaimLinkBuffer.TryFindOverlappingClaimLink(
checkAreas,
excludeGroupIds,
out int violatingGroupId
);
bool violatesClaim =
isLinked
&& !violatesClaimLink
&& ClaimLinkBuffer.TryFindOverlappingClaim(
checkAreas,
uid,
excludeGroupIds,
out violatingClaim
);
if (!violatesClaimLink && !violatesClaim)
return;
ClaimLinkModSystem.LandClaimAPI.Remove(savedClaim);
string reason = violatesClaimLink
? (
ClaimLinkModSystem.Groups.PlayerGroupsById.TryGetValue(
violatingGroupId,
out PlayerGroup? violatingGroup
)
? $"it overlaps the protected buffer of '{violatingGroup!.Name}'"
: "it overlaps the protected buffer of another claim link"
)
: $"its buffer would overlap the claim owned by {violatingClaim!.LastKnownOwnerName}";
if (snapshot != null)
{
savedClaim.Areas.Clear();
savedClaim.Areas.AddRange(snapshot);
ClaimLinkModSystem.LandClaimAPI.Add(savedClaim);
NotifyPlayer(args.Caller.Player, $"Your claim growth was reverted: {reason}.");
}
else
{
NotifyPlayer(args.Caller.Player, $"Your claim was rejected: {reason}.");
}
}
private static void NotifyPlayer(IPlayer player, string message)
{
if (player is IServerPlayer sp)
sp.SendMessage(GlobalConstants.GeneralChatGroup, message, EnumChatType.Notification);
}
private static void Claim(Caller caller, CmdArgs args)
{
string? word = args.PeekWord();
if (word == null)
return;
if (claimSub.TryGetValue(word, out SubHandler? handler))
{
args.PopWord();
handler(caller, args);
}
}
private static void ClaimSave(Caller caller, CmdArgs args)
{
if (caller.Player == null)
return;
string uid = caller.Player.PlayerUID;
PendingSaveChecks[uid] = null;
if (!PendingLoads.TryGetValue(uid, out int pendingIndex))
return;
if (
ClaimLinkModSystem.TryResolveOwnedClaim(uid, pendingIndex, out LandClaim? existingClaim)
&& existingClaim != null
)
{
List<Cuboidi> snapshot = new();
foreach (Cuboidi area in existingClaim.Areas)
snapshot.Add(area.Clone());
PendingSaveChecks[uid] = snapshot;
}
ClaimLinkModSystem.Registry.ClaimSaved(uid, pendingIndex);
PendingLoads.Remove(uid);
ClaimLinkChatCommand.PendingActions.Remove(uid);
}
private static void ClaimLoad(Caller caller, CmdArgs args)
{
int? claimIndex = args.PopInt();
if (caller.Player == null || claimIndex == null)
return;
if (
ClaimLinkModSystem.TryResolveOwnedClaim(caller.Player.PlayerUID, (int)claimIndex, out _)
)
PendingLoads[caller.Player.PlayerUID] = (int)claimIndex;
}
private static void ClaimNew(Caller caller, CmdArgs args)
{
if (caller.Player == null)
return;
PendingLoads.Remove(caller.Player.PlayerUID);
}
private static void ClaimCancel(Caller caller, CmdArgs args)
{
if (caller.Player == null)
return;
PendingLoads.Remove(caller.Player.PlayerUID);
}
private static void Free(Caller caller, CmdArgs args)
{
if (caller.Player == null)
return;
string uid = caller.Player.PlayerUID;
int? claimIndex = args.PopInt();
if (claimIndex == null)
return;
if (args.PopWord() != "confirm")
return;
ClaimLinkModSystem.Registry.ClaimRemoved(uid, (int)claimIndex);
ClaimLinkChatCommand.PendingActions.Remove(uid);
}
private static void AdminFree(Caller caller, CmdArgs args)
{
if (caller.Player == null)
return;
string? name = args.PopWord();
if (name == null)
return;
string? uid = ClaimLinkModSystem.PlayerData.GetPlayerDataByLastKnownName(name)?.PlayerUID;
if (uid == null)
return;
ClaimLinkModSystem.Registry.ClearAllForPlayer(uid);
ClaimLinkChatCommand.PendingActions.Remove(uid);
}
private static void AdminFreeHere(Caller caller, CmdArgs args)
{
if (caller.Player == null)
return;
LandClaim[] claims = ClaimLinkModSystem.LandClaimAPI.Get(
caller.Player.Entity.Pos.AsBlockPos
);
foreach (LandClaim c in claims)
if (ClaimLinkModSystem.TryResolveClaimIndex(c.OwnedByPlayerUid, c, out int claimIndex))
{
ClaimLinkModSystem.Registry.ClaimRemoved(c.OwnedByPlayerUid, claimIndex);
ClaimLinkChatCommand.PendingActions.Remove(c.OwnedByPlayerUid);
}
}
internal static void OnPlayerDisconnect(IServerPlayer player)
{
PendingLoads.Remove(player.PlayerUID);
PendingSaveChecks.Remove(player.PlayerUID);
}
}
public class GroupCommandListener : ICommandHookListener
{
public string ModId => "claimlink.group";
public IReadOnlyList<string> Commands => new[] { "group" };
public CommandRegistration Registration => new(Before, After);
internal static Dictionary<string, int> PendingDisbands = new Dictionary<string, int>();
internal static Dictionary<string, (string AffectedUid, int GroupId)> PendingMembershipChecks =
new Dictionary<string, (string, int)>();
private TextCommandResult? Before(TextCommandCallingArgs args)
{
if (args.Caller.Player == null)
return null;
string callerUid = args.Caller.Player.PlayerUID;
CmdArgs rawArgs = args.RawArgs.Clone();
string? word = rawArgs.PopWord();
switch (word)
{
case "confirmdisband":
ConfirmDisband(args, callerUid, rawArgs);
break;
case "leave":
Leave(args, callerUid, rawArgs);
break;
case "kick":
Kick(args, callerUid, rawArgs);
break;
}
return null;
}
private static void ConfirmDisband(
TextCommandCallingArgs args,
string callerUid,
CmdArgs rawArgs
)
{
string? name = rawArgs.PopWord();
int? groupId =
name != null
? ClaimLinkModSystem.Groups.GetPlayerGroupByName(name)?.Uid
: args.Caller.FromChatGroupId;
if (groupId != null)
PendingDisbands[callerUid] = (int)groupId;
}
private static void Leave(TextCommandCallingArgs args, string callerUid, CmdArgs rawArgs)
{
string? name = rawArgs.PopWord();
int? groupId =
name != null
? ClaimLinkModSystem.Groups.GetPlayerGroupByName(name)?.Uid
: args.Caller.FromChatGroupId;
if (groupId != null)
PendingMembershipChecks[callerUid] = (callerUid, (int)groupId);
}
private static void Kick(TextCommandCallingArgs args, string callerUid, CmdArgs rawArgs)
{
string? word1 = rawArgs.PopWord();
string? word2 = rawArgs.PopWord();
string? targetName;
int? groupId;
if (word2 != null)
{
groupId =
word1 != null ? ClaimLinkModSystem.Groups.GetPlayerGroupByName(word1)?.Uid : null;
targetName = word2;
}
else
{
targetName = word1;
groupId = args.Caller.FromChatGroupId;
}
if (targetName == null || groupId == null)
return;
string? targetUid = ClaimLinkModSystem
.PlayerData.GetPlayerDataByLastKnownName(targetName)
?.PlayerUID;
if (targetUid != null)
PendingMembershipChecks[callerUid] = (targetUid, (int)groupId);
}
private void After(TextCommandCallingArgs args, TextCommandResult result)
{
if (args.Caller.Player == null)
return;
string callerUid = args.Caller.Player.PlayerUID;
if (PendingDisbands.TryGetValue(callerUid, out int disbandedGroupId))
{
PendingDisbands.Remove(callerUid);
if (!ClaimLinkModSystem.Groups.PlayerGroupsById.ContainsKey(disbandedGroupId))
{
ClaimLinkChatCommand.RemovePendingActionsForGroup(
disbandedGroupId,
"group no longer exists"
);
if (ClaimLinkModSystem.Registry.Exists(disbandedGroupId))
ClaimLinkModSystem.Registry.Remove(disbandedGroupId);
}
}
if (PendingMembershipChecks.TryGetValue(callerUid, out var check))
{
PendingMembershipChecks.Remove(callerUid);
bool stillMember =
ClaimLinkModSystem
.PlayerData.GetPlayerDataByUid(check.AffectedUid)
?.PlayerGroupMemberships.ContainsKey(check.GroupId) == true;
if (!stillMember)
ClaimLinkChatCommand.CancelPendingAction(
check.AffectedUid,
check.GroupId,
"you are no longer a member of the group"
);
}
}
internal static void OnPlayerDisconnect(IServerPlayer player)
{
PendingDisbands.Remove(player.PlayerUID);
PendingMembershipChecks.Remove(player.PlayerUID);
}
}
-222
View File
@@ -1,222 +0,0 @@
using System.Collections.Generic;
using CommandHook;
using Vintagestory.API.Common;
using Vintagestory.API.Server;
namespace ClaimLink;
public class ClaimLinkCommandListener : ICommandHookListener
{
public string ModId => "claimlink";
public IReadOnlyList<string> Commands => new[] { "land" };
public CommandRegistration Registration => new(Before, After);
internal static Dictionary<string, int> PendingLoads = new Dictionary<string, int>();
private delegate void SubHandler(Caller caller, CmdArgs args, TextCommandResult? result);
private static readonly Dictionary<string, SubHandler> topLevel = new()
{
["claim"] = Claim,
["free"] = Free,
["info"] = Info,
["list"] = List,
["adminfree"] = AdminFree,
};
private static readonly Dictionary<string, SubHandler> claimSub = new()
{
["load"] = ClaimLoad,
["new"] = ClaimNew,
["grant"] = ClaimGrant,
["revoke"] = ClaimRevoke,
["grantgroup"] = ClaimGrantGroup,
["revokegroup"] = ClaimRevokeGroup,
["start"] = ClaimStart,
["end"] = ClaimEnd,
["grow"] = ClaimGrow,
["shrink"] = ClaimShrink,
["add"] = ClaimAdd,
["allowuseeveryone"] = ClaimAllowUseEveryone,
["plevel"] = ClaimPLevel,
["fullheight"] = ClaimFullHeight,
["save"] = ClaimSave,
["cancel"] = ClaimCancel,
["download"] = ClaimDownload,
};
private static void Dispatch(TextCommandCallingArgs args, TextCommandResult? result)
{
CmdArgs rawArgs = args.RawArgs.Clone();
while (rawArgs.Length > 0)
{
string word = rawArgs.PeekWord();
if (topLevel.TryGetValue(word, out SubHandler? handler))
{
rawArgs.PopWord();
handler(args.Caller, rawArgs, result);
}
else
{
rawArgs.PopWord();
}
}
}
private TextCommandResult? Before(TextCommandCallingArgs args)
{
Dispatch(args, null);
return null;
}
private void After(TextCommandCallingArgs args, TextCommandResult result)
{
Dispatch(args, result);
}
private static void Claim(Caller caller, CmdArgs args, TextCommandResult? result)
{
string? word = args.PeekWord();
if (word == null)
return;
if (claimSub.TryGetValue(word, out SubHandler? handler))
{
args.PopWord();
handler(caller, args, result);
}
}
private static void ClaimLoad(Caller caller, CmdArgs args, TextCommandResult? result)
{
int? claimIndex = args.PopInt();
if (caller.Player == null)
return;
ClaimLinkModSystem.Logger.Notification(
"CLAIM LOAD {0}",
result == null ? "BEFORE" : "AFTER"
);
if (
claimIndex != null
&& ClaimLinkModSystem.TryResolveOwnedClaim(
caller.Player.PlayerUID,
(int)claimIndex,
out _
)
)
{
PendingLoads[caller.Player.PlayerUID] = (int)claimIndex;
ClaimLinkModSystem.Logger.Notification($"Claim Index Loaded = {claimIndex}");
}
}
private static void ClaimCancel(Caller caller, CmdArgs args, TextCommandResult? result)
{
if (result != null || caller.Player == null)
return;
PendingLoads.Remove(caller.Player.PlayerUID);
}
private static void Free(Caller caller, CmdArgs args, TextCommandResult? result)
{
if (result == null || result.Status != EnumCommandStatus.Success || caller.Player == null)
return;
string uid = caller.Player.PlayerUID;
int? claimIndex = args.PopInt();
if (claimIndex == null)
return;
if (args.PopWord() != "confirm")
return;
ClaimLink? link = ClaimLinkModSystem.Registry.FindLinkContaining(uid, (int)claimIndex);
ClaimLinkModSystem.Logger.Notification(
link != null
? $"claimlink: freed claim {claimIndex} for {uid} was linked to group {link.GroupId}"
: $"claimlink: freed claim {claimIndex} for {uid} was not linked"
);
}
private static void Info(Caller caller, CmdArgs args, TextCommandResult? result) { }
private static void List(Caller caller, CmdArgs args, TextCommandResult? result) { }
private static void AdminFree(Caller caller, CmdArgs args, TextCommandResult? result)
{
if (caller.Player == null)
return;
LandClaim[] claims = ClaimLinkModSystem.LandClaimAPI.Get(
caller.Player.Entity.Pos.AsBlockPos
);
string phase = result == null ? "before" : $"after (status={result.Status})";
ClaimLinkModSystem.Logger.Notification(
$"claimlink: adminfree {phase}: {claims.Length} claim(s) at position"
);
foreach (LandClaim c in claims)
ClaimLinkModSystem.Logger.Notification($"claimlink: owned by {c.OwnedByPlayerUid}");
}
private static void ClaimNew(Caller caller, CmdArgs args, TextCommandResult? result) { }
private static void ClaimGrant(Caller caller, CmdArgs args, TextCommandResult? result) { }
private static void ClaimRevoke(Caller caller, CmdArgs args, TextCommandResult? result) { }
private static void ClaimGrantGroup(Caller caller, CmdArgs args, TextCommandResult? result) { }
private static void ClaimRevokeGroup(Caller caller, CmdArgs args, TextCommandResult? result) { }
private static void ClaimStart(Caller caller, CmdArgs args, TextCommandResult? result) { }
private static void ClaimEnd(Caller caller, CmdArgs args, TextCommandResult? result) { }
private static void ClaimGrow(Caller caller, CmdArgs args, TextCommandResult? result) { }
private static void ClaimShrink(Caller caller, CmdArgs args, TextCommandResult? result) { }
private static void ClaimAdd(Caller caller, CmdArgs args, TextCommandResult? result) { }
private static void ClaimAllowUseEveryone(
Caller caller,
CmdArgs args,
TextCommandResult? result
) { }
private static void ClaimPLevel(Caller caller, CmdArgs args, TextCommandResult? result) { }
private static void ClaimFullHeight(Caller caller, CmdArgs args, TextCommandResult? result) { }
private static void ClaimSave(Caller caller, CmdArgs args, TextCommandResult? result)
{
if (result == null || result.Status != EnumCommandStatus.Success || caller.Player == null)
return;
string uid = caller.Player.PlayerUID;
int? claimIndex = args.PopInt();
if (PendingLoads.TryGetValue(uid, out int pendingIndex) && claimIndex != pendingIndex)
ClaimLinkModSystem.Logger.Error(
$"claimlink: claim save index mismatch for {uid}: pending={pendingIndex} saved={claimIndex}"
);
PendingLoads.Remove(uid);
}
private static void ClaimDownload(Caller caller, CmdArgs args, TextCommandResult? result) { }
internal static void OnPlayerDisconnect(IServerPlayer player)
{
PendingLoads.Remove(player.PlayerUID);
}
}
+2 -2
View File
@@ -8,9 +8,9 @@
"anth64"
],
"description": "Link claims together with groups.",
"version": "0.0.6",
"version": "0.3.0",
"dependencies": {
"game": "1.22.3",
"game": "1.22.5",
"commandhook": "2.1.0"
}
}