diff --git a/MiraAPI/Utilities/Extensions.cs b/MiraAPI/Utilities/Extensions.cs index e53e6f0..6de68a4 100644 --- a/MiraAPI/Utilities/Extensions.cs +++ b/MiraAPI/Utilities/Extensions.cs @@ -22,7 +22,9 @@ public static class Extensions role.ParentMod.PluginConfig.TryGetEntry(role.NumConfigDefinition, out var numEntry); role.ParentMod.PluginConfig.TryGetEntry(role.ChanceConfigDefinition, out var chanceEntry); - return new NetData(RoleId.Get(role.GetType()), BitConverter.GetBytes(numEntry.Value).AddRangeToArray(BitConverter.GetBytes(chanceEntry.Value))); + return new NetData( + RoleId.Get(role.GetType()), + BitConverter.GetBytes(numEntry.Value).AddRangeToArray(BitConverter.GetBytes(chanceEntry.Value))); } /// @@ -112,7 +114,8 @@ public static class Extensions /// True if the OptionBehaviour is for a custom options, false otherwise. public static bool IsCustom(this OptionBehaviour optionBehaviour) { - return ModdedOptionsManager.ModdedOptions.Values.Any(opt => opt.OptionBehaviour && opt.OptionBehaviour == optionBehaviour); + return ModdedOptionsManager.ModdedOptions.Values.Any( + opt => opt.OptionBehaviour && opt.OptionBehaviour == optionBehaviour); } private static readonly Dictionary ModifierComponents = []; @@ -154,6 +157,7 @@ public static class Extensions randomizedList.Add(list[index]); list.RemoveAt(index); } + return randomizedList; } @@ -176,7 +180,8 @@ public static class Extensions /// True if the Modifier is present, false otherwise. public static bool HasModifier(this PlayerControl? player) where T : BaseModifier { - return player?.GetModifierComponent() != null && player.GetModifierComponent()!.ActiveModifiers.Exists(x => x is T); + return player?.GetModifierComponent() != null && + player.GetModifierComponent()!.ActiveModifiers.Exists(x => x is T); } /// @@ -187,7 +192,8 @@ public static class Extensions /// True if the Modifier is present, false otherwise. public static bool HasModifier(this PlayerControl? player, uint id) { - return player?.GetModifierComponent() != null && player.GetModifierComponent()!.ActiveModifiers.Exists(x => x.ModifierId == id); + return player?.GetModifierComponent() != null && + player.GetModifierComponent()!.ActiveModifiers.Exists(x => x.ModifierId == id); } /// @@ -233,6 +239,7 @@ public static class Extensions Logger.Error($"Cannot add modifier with id {modifierId} because it is not registered."); return; } + target.GetModifierComponent()?.AddModifier(type); } @@ -304,7 +311,9 @@ public static class Extensions /// The dead body if it is found, or null there is none within the radius. public static DeadBody? GetNearestDeadBody(this PlayerControl playerControl, float radius) { - return Helpers.GetNearestDeadBodies(playerControl.GetTruePosition(), radius, Helpers.CreateFilter(Constants.NotShipMask)).Find(component => component && !component.Reported); + return Helpers + .GetNearestDeadBodies(playerControl.GetTruePosition(), radius, Helpers.CreateFilter(Constants.NotShipMask)) + .Find(component => component && !component.Reported); } /// @@ -317,25 +326,41 @@ public static class Extensions /// Optional predicate to test if the object is valid. /// The type of the object. /// The object if it was found, or null if there is none within the radius. - public static T? GetNearestObjectOfType(this PlayerControl playerControl, float radius, ContactFilter2D filter, string? colliderTag = null, Predicate? predicate = null) where T : Component + public static T? GetNearestObjectOfType( + this PlayerControl playerControl, + float radius, + ContactFilter2D filter, + string? colliderTag = null, + Predicate? predicate = null) where T : Component { - return Helpers.GetNearestObjectsOfType(playerControl.GetTruePosition(), radius, filter, colliderTag).Find(predicate ?? (component => component)); + return Helpers.GetNearestObjectsOfType(playerControl.GetTruePosition(), radius, filter, colliderTag) + .Find(predicate ?? (component => component)); } /// - /// Gets the nearest player to a player. + /// Gets the closest player that matches the given criteria. /// /// The player object. /// Whether impostors should be included in the search. /// The radius to search within. /// Whether colliders should be ignored when searching. + /// Optional predicate to test if the object is valid. /// The closest player if there is one, false otherwise. - public static PlayerControl? GetClosestPlayer(this PlayerControl playerControl, bool includeImpostors, float distance, bool ignoreColliders = false) + public static PlayerControl? GetClosestPlayer( + this PlayerControl playerControl, + bool includeImpostors, + float distance, + bool ignoreColliders = false, + Predicate? predicate = null) { var filteredPlayers = Helpers.GetClosestPlayers(playerControl, distance, ignoreColliders) - .Where(playerInfo => !playerInfo.Data.Disconnected && playerInfo.PlayerId != playerControl.PlayerId && !playerInfo.Data.IsDead && - (includeImpostors || !playerInfo.Data.Role.IsImpostor)); + .Where( + playerInfo => !playerInfo.Data.Disconnected && + playerInfo.PlayerId != playerControl.PlayerId && + !playerInfo.Data.IsDead && + (includeImpostors || !playerInfo.Data.Role.IsImpostor)) + .ToList(); - return filteredPlayers.FirstOrDefault(); + return predicate != null ? filteredPlayers.Find(predicate) : filteredPlayers.FirstOrDefault(); } }