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