refactor: merge land command Before/After into shared phase-aware dispatch
This commit is contained in:
@@ -190,9 +190,7 @@ public static class ClaimLinkChatCommand
|
||||
admin.EndSubCommand();
|
||||
}
|
||||
|
||||
private static readonly Dictionary<string, Func<TextCommandResult>> pendingActions = new();
|
||||
|
||||
internal static void RemovePending(string playerUid) => pendingActions.Remove(playerUid);
|
||||
internal static readonly Dictionary<string, Func<TextCommandResult>> PendingActions = new();
|
||||
|
||||
private static TextCommandResult Stage(
|
||||
string playerUid,
|
||||
@@ -200,7 +198,7 @@ public static class ClaimLinkChatCommand
|
||||
Func<TextCommandResult> action
|
||||
)
|
||||
{
|
||||
pendingActions[playerUid] = action;
|
||||
PendingActions[playerUid] = action;
|
||||
return TextCommandResult.Success(
|
||||
$"{prompt} Use /claimlink confirm to proceed, or /claimlink cancel to cancel."
|
||||
);
|
||||
@@ -279,10 +277,10 @@ public static class ClaimLinkChatCommand
|
||||
{
|
||||
string playerUid = args.Caller.Player.PlayerUID;
|
||||
|
||||
if (!pendingActions.TryGetValue(playerUid, out Func<TextCommandResult>? action))
|
||||
if (!PendingActions.TryGetValue(playerUid, out Func<TextCommandResult>? action))
|
||||
return TextCommandResult.Error("You do not have a pending action.");
|
||||
|
||||
pendingActions.Remove(playerUid);
|
||||
PendingActions.Remove(playerUid);
|
||||
return action();
|
||||
}
|
||||
|
||||
@@ -290,7 +288,7 @@ public static class ClaimLinkChatCommand
|
||||
{
|
||||
string playerUid = args.Caller.Player.PlayerUID;
|
||||
|
||||
if (!pendingActions.Remove(playerUid))
|
||||
if (!PendingActions.Remove(playerUid))
|
||||
return TextCommandResult.Error("You do not have a pending action.");
|
||||
|
||||
return TextCommandResult.Success("Pending action cancelled.");
|
||||
@@ -316,7 +314,7 @@ public static class ClaimLinkChatCommand
|
||||
|
||||
string playerUid = player.PlayerUID;
|
||||
|
||||
if (!ClaimLinkModSystem.TryResolveOwnedClaim(playerUid, claimIndex, out _, out _))
|
||||
if (!ClaimLinkModSystem.TryResolveOwnedClaim(playerUid, claimIndex, out _))
|
||||
return TextCommandResult.Error("You do not own that claim.");
|
||||
|
||||
if (ClaimLinkModSystem.Registry.IsClaimLinked(playerUid, claimIndex))
|
||||
@@ -391,7 +389,7 @@ public static class ClaimLinkChatCommand
|
||||
() =>
|
||||
{
|
||||
ClaimLinkModSystem.Registry.RemoveAllForPlayerInGroup(targetUid, group.Uid);
|
||||
RemovePending(targetUid);
|
||||
ClaimLinkCommandListener.PendingLoads.Remove(targetUid);
|
||||
|
||||
return TextCommandResult.Success(
|
||||
$"Unlinked all claims of {target.PlayerName} from '{groupName}'."
|
||||
@@ -424,7 +422,7 @@ public static class ClaimLinkChatCommand
|
||||
() =>
|
||||
{
|
||||
foreach (string uid in ClaimLinkModSystem.Registry.MemberUidsForGroup(groupId))
|
||||
RemovePending(uid);
|
||||
PendingActions.Remove(uid);
|
||||
|
||||
ClaimLinkModSystem.Registry.Remove(groupId);
|
||||
return TextCommandResult.Success($"'{groupName}' is no longer a claim link.");
|
||||
@@ -516,7 +514,9 @@ public static class ClaimLinkChatCommand
|
||||
|
||||
string notice = $"'{group.Name}' is now owned by {target.PlayerName}.";
|
||||
|
||||
HashSet<string> notifyUids = ClaimLinkModSystem.Registry.MemberUidsForGroup(link.GroupId).ToHashSet();
|
||||
HashSet<string> notifyUids = ClaimLinkModSystem
|
||||
.Registry.MemberUidsForGroup(link.GroupId)
|
||||
.ToHashSet();
|
||||
notifyUids.Add(oldOwnerUid);
|
||||
notifyUids.Add(target.PlayerUID);
|
||||
|
||||
@@ -550,7 +550,9 @@ public static class ClaimLinkChatCommand
|
||||
public static TextCommandResult List(TextCommandCallingArgs args)
|
||||
{
|
||||
List<ClaimLink> links = ClaimLinkModSystem
|
||||
.Registry.All.OrderByDescending(l => ClaimLinkModSystem.Registry.MemberCountForGroup(l.GroupId))
|
||||
.Registry.All.OrderByDescending(l =>
|
||||
ClaimLinkModSystem.Registry.MemberCountForGroup(l.GroupId)
|
||||
)
|
||||
.ToList();
|
||||
|
||||
if (links.Count == 0)
|
||||
@@ -569,20 +571,15 @@ public static class ClaimLinkChatCommand
|
||||
return TextCommandResult.Success(sb.ToString());
|
||||
}
|
||||
|
||||
private static string DescribeClaim(string ownerPlayerUid, int localIndex)
|
||||
private static string DescribeClaim(string ownerPlayerUid, int claimIndex)
|
||||
{
|
||||
if (
|
||||
!ClaimLinkModSystem.TryResolveOwnedClaim(
|
||||
ownerPlayerUid,
|
||||
localIndex,
|
||||
out _,
|
||||
out LandClaim? claim
|
||||
)
|
||||
!ClaimLinkModSystem.TryResolveOwnedClaim(ownerPlayerUid, claimIndex, out LandClaim? claim)
|
||||
|| claim == null
|
||||
)
|
||||
return $"claim {localIndex}";
|
||||
return $"claim {claimIndex}";
|
||||
|
||||
return string.IsNullOrEmpty(claim.Description) ? $"claim {localIndex}" : claim.Description;
|
||||
return string.IsNullOrEmpty(claim.Description) ? $"claim {claimIndex}" : claim.Description;
|
||||
}
|
||||
|
||||
private static string FormatInfo(string groupName, ClaimLink link)
|
||||
@@ -590,7 +587,9 @@ public static class ClaimLinkChatCommand
|
||||
int memberCount = ClaimLinkModSystem.Registry.MemberCountForGroup(link.GroupId);
|
||||
|
||||
StringBuilder sb = new();
|
||||
sb.AppendLine($"Claim link '{groupName}' ({memberCount} member{(memberCount == 1 ? "" : "s")}):");
|
||||
sb.AppendLine(
|
||||
$"Claim link '{groupName}' ({memberCount} member{(memberCount == 1 ? "" : "s")}):"
|
||||
);
|
||||
|
||||
foreach (string uid in ClaimLinkModSystem.Registry.MemberUidsForGroup(link.GroupId))
|
||||
{
|
||||
|
||||
@@ -37,28 +37,33 @@ public class ClaimLinkModSystem : ModSystem
|
||||
CommandHookModSystem.Unregister(cmdListener);
|
||||
}
|
||||
|
||||
internal static bool TryResolveOwnedClaim(string ownerPlayerUid, int localIndex, out int globalIndex, out LandClaim? claim)
|
||||
private static IEnumerable<(int claimIndex, LandClaim claim)> EnumerateOwnedClaims(
|
||||
string ownerPlayerUid
|
||||
)
|
||||
{
|
||||
globalIndex = -1;
|
||||
claim = null;
|
||||
|
||||
List<LandClaim> claims = LandClaimAPI.All;
|
||||
int count = 0;
|
||||
for (int i = 0; i < claims.Count; ++i)
|
||||
int index = 0;
|
||||
foreach (LandClaim c in LandClaimAPI.All)
|
||||
{
|
||||
if (claims[i].OwnedByPlayerUid != ownerPlayerUid)
|
||||
if (c.OwnedByPlayerUid != ownerPlayerUid)
|
||||
continue;
|
||||
|
||||
if (count == localIndex)
|
||||
yield return (index, c);
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
internal static bool TryResolveOwnedClaim(string ownerPlayerUid, int claimIndex, out LandClaim? claim)
|
||||
{
|
||||
globalIndex = i;
|
||||
claim = claims[i];
|
||||
foreach ((int index, LandClaim c) in EnumerateOwnedClaims(ownerPlayerUid))
|
||||
{
|
||||
if (index == claimIndex)
|
||||
{
|
||||
claim = c;
|
||||
return true;
|
||||
}
|
||||
|
||||
count++;
|
||||
}
|
||||
|
||||
claim = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,35 +68,35 @@ public class ClaimLinkRegistry
|
||||
Save();
|
||||
}
|
||||
|
||||
public bool IsClaimLinked(string ownerPlayerUid, int localIndex) =>
|
||||
FindLinkContaining(ownerPlayerUid, localIndex) != null;
|
||||
public bool IsClaimLinked(string ownerPlayerUid, int claimIndex) =>
|
||||
FindLinkContaining(ownerPlayerUid, claimIndex) != null;
|
||||
|
||||
public ClaimLink? FindLinkContaining(string ownerPlayerUid, int localIndex)
|
||||
public ClaimLink? FindLinkContaining(string ownerPlayerUid, int claimIndex)
|
||||
{
|
||||
if (!byPlayer.TryGetValue(ownerPlayerUid, out Dictionary<int, List<int>>? perGroup))
|
||||
return null;
|
||||
|
||||
foreach (KeyValuePair<int, List<int>> entry in perGroup)
|
||||
if (entry.Value.Contains(localIndex))
|
||||
if (entry.Value.Contains(claimIndex))
|
||||
return Get(entry.Key);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void AddEntry(string ownerPlayerUid, int groupId, int localClaimIndex)
|
||||
public void AddEntry(string ownerPlayerUid, int groupId, int claimIndex)
|
||||
{
|
||||
GetOrCreateIndices(ownerPlayerUid, groupId).Add(localClaimIndex);
|
||||
GetOrCreateIndices(ownerPlayerUid, groupId).Add(claimIndex);
|
||||
Save();
|
||||
}
|
||||
|
||||
public void RemoveEntry(string ownerPlayerUid, int groupId, int localClaimIndex)
|
||||
public void RemoveEntry(string ownerPlayerUid, int groupId, int claimIndex)
|
||||
{
|
||||
if (
|
||||
byPlayer.TryGetValue(ownerPlayerUid, out Dictionary<int, List<int>>? perGroup)
|
||||
&& perGroup.TryGetValue(groupId, out List<int>? indices)
|
||||
)
|
||||
{
|
||||
indices.Remove(localClaimIndex);
|
||||
indices.Remove(claimIndex);
|
||||
if (indices.Count == 0)
|
||||
perGroup.Remove(groupId);
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using Vintagestory.API.Common;
|
||||
using System.Collections.Generic;
|
||||
using CommandHook;
|
||||
using Vintagestory.API.Common;
|
||||
using Vintagestory.API.Server;
|
||||
|
||||
namespace ClaimLink;
|
||||
@@ -13,9 +13,9 @@ public class ClaimLinkCommandListener : ICommandHookListener
|
||||
|
||||
public CommandRegistration Registration => new(Before, After);
|
||||
|
||||
private static Dictionary<string, int> pendingLoads = new Dictionary<string, int>();
|
||||
internal static Dictionary<string, int> PendingLoads = new Dictionary<string, int>();
|
||||
|
||||
private delegate void SubHandler(Caller caller, CmdArgs args);
|
||||
private delegate void SubHandler(Caller caller, CmdArgs args, TextCommandResult? result);
|
||||
|
||||
private static readonly Dictionary<string, SubHandler> topLevel = new()
|
||||
{
|
||||
@@ -47,7 +47,7 @@ public class ClaimLinkCommandListener : ICommandHookListener
|
||||
["download"] = ClaimDownload,
|
||||
};
|
||||
|
||||
private TextCommandResult? Before(TextCommandCallingArgs args)
|
||||
private static void Dispatch(TextCommandCallingArgs args, TextCommandResult? result)
|
||||
{
|
||||
CmdArgs rawArgs = args.RawArgs.Clone();
|
||||
|
||||
@@ -58,79 +58,103 @@ public class ClaimLinkCommandListener : ICommandHookListener
|
||||
if (topLevel.TryGetValue(word, out SubHandler? handler))
|
||||
{
|
||||
rawArgs.PopWord();
|
||||
handler(args.Caller, rawArgs);
|
||||
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)
|
||||
private static void Claim(Caller caller, CmdArgs args, TextCommandResult? result)
|
||||
{
|
||||
string word = args.PeekWord();
|
||||
|
||||
if (claimSub.TryGetValue(word, out SubHandler? handler))
|
||||
{
|
||||
args.PopWord();
|
||||
handler(caller, args);
|
||||
handler(caller, args, result);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ClaimLoad(Caller caller, CmdArgs args)
|
||||
private static void ClaimLoad(Caller caller, CmdArgs args, TextCommandResult? result)
|
||||
{
|
||||
if (caller.Player == null)
|
||||
if (result == null || result.Status != EnumCommandStatus.Success || caller.Player == null)
|
||||
return;
|
||||
|
||||
int? claimIndex = args.PopInt();
|
||||
if (claimIndex == null || claimIndex < 0 || claimIndex > 999)
|
||||
return;
|
||||
|
||||
if (!ClaimLinkModSystem.TryResolveOwnedClaim(caller.Player.PlayerUID, (int)claimIndex, out int globalIndex, out _))
|
||||
return;
|
||||
|
||||
pendingLoads[caller.Player.PlayerUID] = globalIndex;
|
||||
ClaimLinkModSystem.Logger.Notification($"Claim Index Loaded = {claimIndex}");
|
||||
PendingLoads[caller.Player.PlayerUID] = (int)claimIndex;
|
||||
}
|
||||
|
||||
private static void ClaimCancel(Caller caller, CmdArgs args)
|
||||
private static void ClaimCancel(Caller caller, CmdArgs args, TextCommandResult? result)
|
||||
{
|
||||
if (caller.Player == null)
|
||||
if (result != null || caller.Player == null)
|
||||
return;
|
||||
|
||||
pendingLoads.Remove(caller.Player.PlayerUID);
|
||||
PendingLoads.Remove(caller.Player.PlayerUID);
|
||||
}
|
||||
|
||||
private static void Free(Caller caller, CmdArgs args) { }
|
||||
private static void Info(Caller caller, CmdArgs args) { }
|
||||
private static void List(Caller caller, CmdArgs args) { }
|
||||
private static void AdminFree(Caller caller, CmdArgs args) { }
|
||||
private static void Free(Caller caller, CmdArgs args, TextCommandResult? result) { }
|
||||
|
||||
private static void ClaimNew(Caller caller, CmdArgs args) { }
|
||||
private static void ClaimGrant(Caller caller, CmdArgs args) { }
|
||||
private static void ClaimRevoke(Caller caller, CmdArgs args) { }
|
||||
private static void ClaimGrantGroup(Caller caller, CmdArgs args) { }
|
||||
private static void ClaimRevokeGroup(Caller caller, CmdArgs args) { }
|
||||
private static void ClaimStart(Caller caller, CmdArgs args) { }
|
||||
private static void ClaimEnd(Caller caller, CmdArgs args) { }
|
||||
private static void ClaimGrow(Caller caller, CmdArgs args) { }
|
||||
private static void ClaimShrink(Caller caller, CmdArgs args) { }
|
||||
private static void ClaimAdd(Caller caller, CmdArgs args) { }
|
||||
private static void ClaimAllowUseEveryone(Caller caller, CmdArgs args) { }
|
||||
private static void ClaimPLevel(Caller caller, CmdArgs args) { }
|
||||
private static void ClaimFullHeight(Caller caller, CmdArgs args) { }
|
||||
private static void ClaimSave(Caller caller, CmdArgs args) { }
|
||||
private static void ClaimDownload(Caller caller, CmdArgs args) { }
|
||||
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) { }
|
||||
|
||||
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) { }
|
||||
|
||||
private static void ClaimDownload(Caller caller, CmdArgs args, TextCommandResult? result) { }
|
||||
|
||||
internal static void OnPlayerDisconnect(IServerPlayer player)
|
||||
{
|
||||
pendingLoads.Remove(player.PlayerUID);
|
||||
ClaimLinkChatCommand.RemovePending(player.PlayerUID);
|
||||
PendingLoads.Remove(player.PlayerUID);
|
||||
ClaimLinkChatCommand.PendingActions.Remove(player.PlayerUID);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user