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(); 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) public static TextCommandResult New(TextCommandCallingArgs args)
{ {
string groupName = (string)args[0]; string groupName = (string)args[0];
PlayerGroup? group = ClaimLinkModSystem.Groups.GetPlayerGroupByName(groupName); TextCommandResult? err = TryResolveGroup(groupName, out PlayerGroup group);
if (group == null) if (err != null) return err;
return TextCommandResult.Error($"No group named '{groupName}' exists.");
if (group.OwnerUID != args.Caller.Player.PlayerUID) err = RequireOwner(args.Caller.Player, group);
return TextCommandResult.Error($"You do not own the group '{groupName}'."); if (err != null) return err;
if (ClaimLinkModSystem.Registry.Get(group.Uid) != null) if (ClaimLinkModSystem.Registry.Get(group.Uid) != null)
return TextCommandResult.Error($"'{groupName}' is already a claim link."); 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 }); ClaimLinkModSystem.Registry.Add(new ClaimLink { GroupId = group.Uid });
return TextCommandResult.Success($"'{groupName}' is now a claim link."); return TextCommandResult.Success($"'{groupName}' is now a claim link.");
} }
public static TextCommandResult Link(TextCommandCallingArgs args) public static TextCommandResult Link(TextCommandCallingArgs args)
{ {
string groupName = (string)args[0]; string groupName = (string)args[0];
int claimIndex = (int)args[1]; int claimIndex = (int)args[1];
PlayerGroup? group = ClaimLinkModSystem.Groups.GetPlayerGroupByName(groupName); TextCommandResult? err = TryResolveGroup(groupName, out PlayerGroup group);
if (group == null) if (err != null) return err;
return TextCommandResult.Error($"No group named '{groupName}' exists.");
ClaimLink? link = ClaimLinkModSystem.Registry.Get(group.Uid); err = TryResolveClaimLink(group, out ClaimLink link);
if (link == null) if (err != null) return err;
return TextCommandResult.Error($"'{groupName}' is not a claim link.");
IPlayer player = args.Caller.Player; IPlayer player = args.Caller.Player;
if (player.GetGroup(group.Uid) == null) err = RequireMember(player, group);
return TextCommandResult.Error($"You are not a member of '{groupName}'."); if (err != null) return err;
string playerUid = player.PlayerUID; string playerUid = player.PlayerUID;