refactor: extract guard-clause helpers for group/link resolution

This commit is contained in:
2026-07-12 19:05:27 +02:00
parent ac79d2ac94
commit 0914d55d9a
+33 -13
View File
@@ -113,16 +113,37 @@ public static class ClaimLinkChatCommand
admin.EndSubCommand();
}
private static TextCommandResult? TryResolveGroup(string groupName, out PlayerGroup group)
{
group = ClaimLinkModSystem.Groups.GetPlayerGroupByName(groupName)!;
return group == null ? TextCommandResult.Error($"No group named '{groupName}' exists.") : null;
}
private static TextCommandResult? TryResolveClaimLink(PlayerGroup group, out ClaimLink link)
{
link = ClaimLinkModSystem.Registry.Get(group.Uid)!;
return link == null ? TextCommandResult.Error($"'{group.Name}' is not a claim link.") : null;
}
private static TextCommandResult? RequireMember(IPlayer player, PlayerGroup group)
{
return player.GetGroup(group.Uid) == null ? TextCommandResult.Error($"You are not a member of '{group.Name}'.") : null;
}
private static TextCommandResult? RequireOwner(IPlayer player, PlayerGroup group)
{
return group.OwnerUID != player.PlayerUID ? TextCommandResult.Error($"You do not own the group '{group.Name}'.") : null;
}
public static TextCommandResult New(TextCommandCallingArgs args)
{
string groupName = (string)args[0];
PlayerGroup? group = ClaimLinkModSystem.Groups.GetPlayerGroupByName(groupName);
if (group == null)
return TextCommandResult.Error($"No group named '{groupName}' exists.");
TextCommandResult? err = TryResolveGroup(groupName, out PlayerGroup group);
if (err != null) return err;
if (group.OwnerUID != args.Caller.Player.PlayerUID)
return TextCommandResult.Error($"You do not own the group '{groupName}'.");
err = RequireOwner(args.Caller.Player, group);
if (err != null) return err;
if (ClaimLinkModSystem.Registry.Get(group.Uid) != null)
return TextCommandResult.Error($"'{groupName}' is already a claim link.");
@@ -130,22 +151,21 @@ public static class ClaimLinkChatCommand
ClaimLinkModSystem.Registry.Add(new ClaimLink { GroupId = group.Uid });
return TextCommandResult.Success($"'{groupName}' is now a claim link.");
}
public static TextCommandResult Link(TextCommandCallingArgs args)
{
string groupName = (string)args[0];
int claimIndex = (int)args[1];
PlayerGroup? group = ClaimLinkModSystem.Groups.GetPlayerGroupByName(groupName);
if (group == null)
return TextCommandResult.Error($"No group named '{groupName}' exists.");
TextCommandResult? err = TryResolveGroup(groupName, out PlayerGroup group);
if (err != null) return err;
ClaimLink? link = ClaimLinkModSystem.Registry.Get(group.Uid);
if (link == null)
return TextCommandResult.Error($"'{groupName}' is not a claim link.");
err = TryResolveClaimLink(group, out ClaimLink link);
if (err != null) return err;
IPlayer player = args.Caller.Player;
if (player.GetGroup(group.Uid) == null)
return TextCommandResult.Error($"You are not a member of '{groupName}'.");
err = RequireMember(player, group);
if (err != null) return err;
string playerUid = player.PlayerUID;