mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-10-20 01:46:15 +00:00
further modifier improvements (massive changes)
This commit is contained in:
1 parent
4a8fb38380
commit
caa27642db
14 files changed
+276
-139
No files matched your search
@@ -15,6 +15,12 @@ public class TestButton : CustomActionButton
|
||||
|
||||
protected override void OnClick()
|
||||
{
|
||||
if (PlayerControl.LocalPlayer.HasModifier<ModifierParams>())
|
||||
{
|
||||
PlayerControl.LocalPlayer.RpcRemoveModifier<ModifierParams>();
|
||||
return;
|
||||
}
|
||||
|
||||
var randomPlayer = PlayerControl.AllPlayerControls.ToArray().Random();
|
||||
PlayerControl.LocalPlayer.RpcAddModifier<ModifierParams>("test", 1, randomPlayer);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace MiraAPI.Events.Vanilla;
|
||||
namespace MiraAPI.Events.Vanilla.Usables;
|
||||
|
||||
/// <summary>
|
||||
/// Event for <see cref="PlayerControl"/> using a <see cref="IUsable"/> from Vanilla Among Us. Will always be ran locally.
|
||||
|
||||
@@ -43,13 +43,14 @@ public abstract class BaseModifier
|
||||
public bool Initialized { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// Get the ID of the modifier.
|
||||
/// Gets the unique ID of the modifier.
|
||||
/// </summary>
|
||||
/// <returns>The ID of the modifier.</returns>
|
||||
public uint GetId()
|
||||
{
|
||||
return ModifierManager.GetModifierId(GetType()) ?? throw new InvalidOperationException("Modifier is not registered.");
|
||||
}
|
||||
public uint UniqueId { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the type ID of the modifier.
|
||||
/// </summary>
|
||||
public uint TypeId => ModifierManager.GetModifierTypeId(GetType()) ?? throw new InvalidOperationException("Modifier is not registered.");
|
||||
|
||||
/// <summary>
|
||||
/// Gets the HUD information for this modifier. Defaults to the modifier name. Does nothing if <see cref="HideOnUi"/> is true.
|
||||
|
||||
@@ -37,6 +37,14 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr)
|
||||
|
||||
private readonly List<BaseModifier> _toAdd = [];
|
||||
|
||||
private uint _nextUniqueId;
|
||||
|
||||
private uint GetNextUniqueId()
|
||||
{
|
||||
_nextUniqueId++;
|
||||
return _nextUniqueId;
|
||||
}
|
||||
|
||||
internal void ClearModifiers()
|
||||
{
|
||||
_toRemove.AddRange(Modifiers);
|
||||
@@ -104,14 +112,12 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr)
|
||||
return;
|
||||
}
|
||||
|
||||
var filteredModifiers = Modifiers.Where(mod => !mod.HideOnUi);
|
||||
var filteredModifiers = Modifiers.Where(mod => !mod.HideOnUi).ToArray();
|
||||
|
||||
var baseModifiers = filteredModifiers as BaseModifier[] ?? filteredModifiers.ToArray();
|
||||
|
||||
if (baseModifiers.Length != 0 && !MeetingHud.Instance)
|
||||
if (filteredModifiers.Length != 0 && !MeetingHud.Instance)
|
||||
{
|
||||
var stringBuild = new StringBuilder();
|
||||
foreach (var mod in baseModifiers)
|
||||
foreach (var mod in filteredModifiers)
|
||||
{
|
||||
stringBuild.Append(CultureInfo.InvariantCulture, $"\n{mod.GetHudString()}");
|
||||
}
|
||||
@@ -128,11 +134,11 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr)
|
||||
/// </summary>
|
||||
/// <param name="predicate">The predicate to check the modifier by.</param>
|
||||
/// <typeparam name="T">The Type of the Modifier.</typeparam>
|
||||
/// <returns>The Modifier if it is found, null otherwise.</returns>
|
||||
/// <returns>A collection of modifiers.</returns>
|
||||
[HideFromIl2Cpp]
|
||||
public IEnumerable<T> GetModifiersByType<T>(Func<T, bool>? predicate=null) where T : BaseModifier
|
||||
public IEnumerable<T> GetModifiers<T>(Func<T, bool>? predicate=null) where T : BaseModifier
|
||||
{
|
||||
return ActiveModifiers.OfType<T>().Where(x => predicate == null || predicate(x));
|
||||
return Modifiers.OfType<T>().Where(x => predicate == null || predicate(x));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -140,26 +146,26 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr)
|
||||
/// </summary>
|
||||
/// <param name="type">The modifier type.</param>
|
||||
/// <param name="predicate">The predicate to check the modifier by.</param>
|
||||
/// <returns>The Modifier if it is found, null otherwise.</returns>
|
||||
/// <returns>A collection of modifiers.</returns>
|
||||
[HideFromIl2Cpp]
|
||||
public IEnumerable<BaseModifier> GetModifiersByType(Type type, Func<BaseModifier, bool>? predicate=null)
|
||||
public IEnumerable<BaseModifier> GetModifiers(Type type, Func<BaseModifier, bool>? predicate=null)
|
||||
{
|
||||
return ActiveModifiers.Where(x => x.GetType() == type && (predicate == null || predicate(x)));
|
||||
return Modifiers.Where(x => x.GetType() == type && (predicate == null || predicate(x)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collection of modifiers by ID.
|
||||
/// Gets a collection of modifiers by type ID.
|
||||
/// </summary>
|
||||
/// <param name="id">The Modifier ID.</param>
|
||||
/// <param name="id">The modifier's type ID.</param>
|
||||
/// <param name="predicate">The predicate to check the modifier by.</param>
|
||||
/// <returns>The Modifier if it is found, null otherwise.</returns>
|
||||
/// <returns>A collection of modifiers.</returns>
|
||||
[HideFromIl2Cpp]
|
||||
public IEnumerable<BaseModifier> GetModifiersByType(uint id, Func<BaseModifier, bool>? predicate=null)
|
||||
public IEnumerable<BaseModifier> GetModifiers(uint id, Func<BaseModifier, bool>? predicate=null)
|
||||
{
|
||||
var type = ModifierManager.GetModifierType(id) ?? throw new InvalidOperationException(
|
||||
$"Cannot get modifier with id {id} because it is not registered.");
|
||||
|
||||
return GetModifiersByType(type, predicate);
|
||||
return GetModifiers(type, predicate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -191,23 +197,34 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr)
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get a modifier by its ID.
|
||||
/// Tries to get a modifier by its type ID.
|
||||
/// </summary>
|
||||
/// <param name="id">The modifier ID.</param>
|
||||
/// <param name="id">The modifier type ID.</param>
|
||||
/// <param name="modifier">The modifier or null.</param>
|
||||
/// <param name="predicate">The predicate to check the modifier by.</param>
|
||||
/// <returns>True if the modifier was found, false otherwise.</returns>
|
||||
[HideFromIl2Cpp]
|
||||
public bool TryGetModifier(uint id, out BaseModifier? modifier, Func<BaseModifier, bool>? predicate = null)
|
||||
public bool TryGetModifierByTypeId(uint id, out BaseModifier? modifier, Func<BaseModifier, bool>? predicate = null)
|
||||
{
|
||||
var type = ModifierManager.GetModifierType(id) ?? throw new InvalidOperationException(
|
||||
$"Cannot get modifier with id {id} because it is not registered.");
|
||||
|
||||
return TryGetModifier(type, out modifier, predicate);
|
||||
modifier = GetModifierByTypeId(id, predicate);
|
||||
return modifier != null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a modifier by its type, or null if the player doesn't have it.
|
||||
/// Tries to get a modifier by its unique ID.
|
||||
/// </summary>
|
||||
/// <param name="id">The modifier unique ID.</param>
|
||||
/// <param name="modifier">The modifier or null.</param>
|
||||
/// <returns>True if the modifier was found, false otherwise.</returns>
|
||||
[HideFromIl2Cpp]
|
||||
public bool TryGetModifierByUniqueId(uint id, out BaseModifier? modifier)
|
||||
{
|
||||
modifier = GetModifierByUniqueId(id);
|
||||
return modifier != null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a modifier by its type.
|
||||
/// </summary>
|
||||
/// <param name="predicate">The predicate to check the modifier by.</param>
|
||||
/// <typeparam name="T">The Type of the Modifier.</typeparam>
|
||||
@@ -215,11 +232,11 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr)
|
||||
[HideFromIl2Cpp]
|
||||
public T? GetModifier<T>(Func<T, bool>? predicate = null) where T : BaseModifier
|
||||
{
|
||||
return GetModifiersByType(predicate).FirstOrDefault();
|
||||
return GetModifiers(predicate).FirstOrDefault();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a modifier by its type, or null if the player doesn't have it.
|
||||
/// Gets a modifier by its type.
|
||||
/// </summary>
|
||||
/// <param name="type">The modifier type.</param>
|
||||
/// <param name="predicate">The predicate to check the modifier by.</param>
|
||||
@@ -227,17 +244,17 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr)
|
||||
[HideFromIl2Cpp]
|
||||
public BaseModifier? GetModifier(Type type, Func<BaseModifier, bool>? predicate = null)
|
||||
{
|
||||
return GetModifiersByType(type).FirstOrDefault(predicate ?? (_ => true));
|
||||
return GetModifiers(type).FirstOrDefault(predicate ?? (_ => true));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a modifier by its type, or null if the player doesn't have it.
|
||||
/// Gets a modifier by its type ID.
|
||||
/// </summary>
|
||||
/// <param name="id">The modifier ID.</param>
|
||||
/// <param name="predicate">The predicate to check the modifier by.</param>
|
||||
/// <returns>The Modifier if it is found, null otherwise.</returns>
|
||||
[HideFromIl2Cpp]
|
||||
public BaseModifier? GetModifier(uint id, Func<BaseModifier, bool>? predicate = null)
|
||||
public BaseModifier? GetModifierByTypeId(uint id, Func<BaseModifier, bool>? predicate = null)
|
||||
{
|
||||
var type = ModifierManager.GetModifierType(id) ?? throw new InvalidOperationException(
|
||||
$"Cannot get modifier with id {id} because it is not registered.");
|
||||
@@ -245,6 +262,17 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr)
|
||||
return GetModifier(type, predicate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a modifier by unique ID.
|
||||
/// </summary>
|
||||
/// <param name="id">The modifier's unique ID.</param>
|
||||
/// <returns>The modifier if it is found, or null.</returns>
|
||||
[HideFromIl2Cpp]
|
||||
public BaseModifier? GetModifierByUniqueId(uint id)
|
||||
{
|
||||
return Modifiers.Find(x => x.UniqueId == id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes a modifier from the player.
|
||||
/// </summary>
|
||||
@@ -280,19 +308,6 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr)
|
||||
RemoveModifier(modifier);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes a modifier from the player.
|
||||
/// </summary>
|
||||
/// <param name="modifierId">The modifier ID.</param>
|
||||
/// <param name="predicate">The predicate to check the modifier by.</param>
|
||||
[HideFromIl2Cpp]
|
||||
public void RemoveModifier(uint modifierId, Func<BaseModifier, bool>? predicate = null)
|
||||
{
|
||||
var type = ModifierManager.GetModifierType(modifierId) ?? throw new InvalidOperationException(
|
||||
$"Cannot remove modifier with id {modifierId} because it is not registered.");
|
||||
RemoveModifier(type, predicate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes a modifier from the player.
|
||||
/// </summary>
|
||||
@@ -309,6 +324,36 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr)
|
||||
_toRemove.Add(modifier);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes a modifier from the player.
|
||||
/// </summary>
|
||||
/// <param name="typeId">The modifier's type ID.</param>
|
||||
/// <param name="predicate">The predicate to check the modifier by.</param>
|
||||
[HideFromIl2Cpp]
|
||||
public void RemoveModifierByTypeId(uint typeId, Func<BaseModifier, bool>? predicate = null)
|
||||
{
|
||||
var type = ModifierManager.GetModifierType(typeId) ?? throw new InvalidOperationException(
|
||||
$"Cannot remove modifier with id {typeId} because it is not registered.");
|
||||
RemoveModifier(type, predicate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes a modifier from the player.
|
||||
/// </summary>
|
||||
/// <param name="uniqueId">The modifier's unique ID.</param>
|
||||
[HideFromIl2Cpp]
|
||||
public void RemoveModifierByUniqueId(uint uniqueId)
|
||||
{
|
||||
var modifier = GetModifierByUniqueId(uniqueId);
|
||||
if (modifier == null)
|
||||
{
|
||||
Logger<MiraApiPlugin>.Error($"Cannot remove modifier with unique id {uniqueId} because it is not active.");
|
||||
return;
|
||||
}
|
||||
|
||||
RemoveModifier(modifier);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a modifier to the player.
|
||||
/// </summary>
|
||||
@@ -329,8 +374,8 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr)
|
||||
[HideFromIl2Cpp]
|
||||
public BaseModifier? AddModifier(BaseModifier modifier)
|
||||
{
|
||||
var id = modifier.GetId();
|
||||
if (modifier.Unique && Modifiers.Find(x => x.GetId() == id) != null)
|
||||
var id = modifier.TypeId;
|
||||
if (modifier.Unique && Modifiers.Find(x => x.TypeId == id) != null)
|
||||
{
|
||||
Logger<MiraApiPlugin>.Error($"Player already has modifier with id {id}!");
|
||||
return null;
|
||||
@@ -345,6 +390,7 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr)
|
||||
_toAdd.Add(modifier);
|
||||
modifier.Player = _player;
|
||||
modifier.ModifierComponent = this;
|
||||
modifier.UniqueId = GetNextUniqueId();
|
||||
return modifier;
|
||||
}
|
||||
|
||||
@@ -398,7 +444,7 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr)
|
||||
[HideFromIl2Cpp]
|
||||
public bool HasModifier<T>(Func<T, bool>? predicate=null) where T : BaseModifier
|
||||
{
|
||||
return ActiveModifiers.Exists(x => x is T modifier && (predicate == null || predicate(modifier)));
|
||||
return Modifiers.Exists(x => x is T modifier && (predicate == null || predicate(modifier)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -410,21 +456,32 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr)
|
||||
[HideFromIl2Cpp]
|
||||
public bool HasModifier(Type type, Func<BaseModifier, bool>? predicate=null)
|
||||
{
|
||||
return ActiveModifiers.Exists(x => x.GetType() == type && (predicate == null || predicate(x)));
|
||||
return Modifiers.Exists(x => x.GetType() == type && (predicate == null || predicate(x)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a player has an active modifier by its ID.
|
||||
/// Checks if a player has an active modifier by its type ID
|
||||
/// </summary>
|
||||
/// <param name="id">The Modifier ID.</param>
|
||||
/// <param name="id">The Modifier type ID.</param>
|
||||
/// <param name="predicate">The predicate to check the modifier.</param>
|
||||
/// <returns>True if the Modifier is present, false otherwise.</returns>
|
||||
[HideFromIl2Cpp]
|
||||
public bool HasModifier(uint id, Func<BaseModifier, bool>? predicate=null)
|
||||
public bool HasModifierByTypeId(uint id, Func<BaseModifier, bool>? predicate=null)
|
||||
{
|
||||
var type = ModifierManager.GetModifierType(id) ?? throw new InvalidOperationException(
|
||||
$"Cannot get modifier with id {id} because it is not registered.");
|
||||
|
||||
return HasModifier(type, predicate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a player has an active modifier by its unique ID
|
||||
/// </summary>
|
||||
/// <param name="id">The Modifier type ID.</param>
|
||||
/// <returns>True if the Modifier is present, false otherwise.</returns>
|
||||
[HideFromIl2Cpp]
|
||||
public bool HasModifierByUniqueId(uint id)
|
||||
{
|
||||
return Modifiers.Exists(x => x.UniqueId == id);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using MiraAPI.Networking;
|
||||
using MiraAPI.Networking.Modifiers;
|
||||
using Reactor.Networking.Attributes;
|
||||
using Reactor.Networking.Rpc;
|
||||
using Reactor.Utilities;
|
||||
@@ -21,14 +22,14 @@ public static class ModifierExtensions
|
||||
/// Remote Procedure Call to add a modifier to a player.
|
||||
/// </summary>
|
||||
/// <param name="target">The player to add the modifier to.</param>
|
||||
/// <param name="modifierId">The modifier ID.</param>
|
||||
/// <param name="typeId">The modifier type ID.</param>
|
||||
/// <param name="args">The arguments to initialize the modifier constructor with.</param>
|
||||
public static void RpcAddModifier(this PlayerControl target, uint modifierId, params object[] args)
|
||||
public static void RpcAddModifier(this PlayerControl target, uint typeId, params object[] args)
|
||||
{
|
||||
_ = ModifierManager.GetModifierType(modifierId) ?? throw new InvalidOperationException(
|
||||
$"Modifier with ID {modifierId} is not registered.");
|
||||
_ = ModifierManager.GetModifierType(typeId) ?? throw new InvalidOperationException(
|
||||
$"Modifier with ID {typeId} is not registered.");
|
||||
|
||||
Rpc<AddModifierRpc>.Instance.Send(target, new ModifierData(modifierId, args));
|
||||
Rpc<AddModifierRpc>.Instance.Send(target, new ModifierData(typeId, args));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -39,7 +40,7 @@ public static class ModifierExtensions
|
||||
/// <param name="args">The arguments to initialize the modifier constructor with.</param>
|
||||
public static void RpcAddModifier(this PlayerControl player, Type type, params object[] args)
|
||||
{
|
||||
var id = ModifierManager.GetModifierId(type) ?? throw new InvalidOperationException(
|
||||
var id = ModifierManager.GetModifierTypeId(type) ?? throw new InvalidOperationException(
|
||||
$"Modifier {type.Name} is not registered.");
|
||||
|
||||
player.RpcAddModifier(id, args);
|
||||
@@ -60,28 +61,61 @@ public static class ModifierExtensions
|
||||
/// Remote Procedure Call to remove a modifier from a player.
|
||||
/// </summary>
|
||||
/// <param name="target">The player to remove the modifier from.</param>
|
||||
/// <param name="modifierId">The ID of the modifier.</param>
|
||||
/// <param name="uniqueId">The unique ID of the modifier.</param>
|
||||
[MethodRpc((uint)MiraRpc.RemoveModifier)]
|
||||
public static void RpcRemoveModifier(this PlayerControl target, uint modifierId)
|
||||
public static void RpcRemoveModifierByUniqueId(this PlayerControl target, uint uniqueId)
|
||||
{
|
||||
target.RemoveModifier(modifierId);
|
||||
target.RemoveModifierByUniqueId(uniqueId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remote Procedure Call to remove a modifier from a player by type ID.
|
||||
/// </summary>
|
||||
/// <param name="target">The player to remove the modifier from.</param>
|
||||
/// <param name="typeId">The type ID of the modifier.</param>
|
||||
/// <param name="predicate">Optional predicate to filter the modifiers.</param>
|
||||
public static void RpcRemoveModifierByTypeId(
|
||||
this PlayerControl target,
|
||||
uint typeId,
|
||||
Func<BaseModifier, bool>? predicate = null)
|
||||
{
|
||||
var modifier = target.GetModifierByTypeId(typeId, predicate);
|
||||
if (modifier is null)
|
||||
{
|
||||
Logger<MiraApiPlugin>.Error($"Player {target.PlayerId} does not have modifier with type ID {typeId}.");
|
||||
return;
|
||||
}
|
||||
|
||||
target.RpcRemoveModifierByUniqueId(modifier.UniqueId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remote Procedure Call to remove a modifier from a player.
|
||||
/// </summary>
|
||||
/// <param name="player">The player to remove the modifier from.</param>
|
||||
/// <typeparam name="T">The Type of the Modifier.</typeparam>
|
||||
public static void RpcRemoveModifier<T>(this PlayerControl player) where T : BaseModifier
|
||||
/// <param name="type">The type of the modifier.</param>
|
||||
/// <param name="predicate">Optional predicate to filter the modifiers.</param>
|
||||
public static void RpcRemoveModifier(
|
||||
this PlayerControl player,
|
||||
Type type,
|
||||
Func<BaseModifier, bool>? predicate = null)
|
||||
{
|
||||
var id = ModifierManager.GetModifierId(typeof(T));
|
||||
if (id == null)
|
||||
{
|
||||
Logger<MiraApiPlugin>.Error($"Cannot add modifier {typeof(T).Name} because it is not registered.");
|
||||
return;
|
||||
}
|
||||
var id = ModifierManager.GetModifierTypeId(type) ?? throw new InvalidOperationException(
|
||||
$"Modifier {type.Name} is not registered.");
|
||||
|
||||
player.RpcRemoveModifier(id.Value);
|
||||
player.RpcRemoveModifierByTypeId(id, predicate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remote Procedure Call to remove a modifier from a player.
|
||||
/// </summary>
|
||||
/// <param name="player">The player to remove the modifier from.</param>
|
||||
/// <param name="predicate">Optional predicate to filter the modifiers.</param>
|
||||
/// <typeparam name="T">The Type of the Modifier.</typeparam>
|
||||
public static void RpcRemoveModifier<T>(this PlayerControl player, Func<BaseModifier, bool>? predicate = null)
|
||||
where T : BaseModifier
|
||||
{
|
||||
player.RpcRemoveModifier(typeof(T), predicate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -89,7 +123,7 @@ public static class ModifierExtensions
|
||||
/// </summary>
|
||||
/// <param name="player">The PlayerControl object.</param>
|
||||
/// <returns>A ModifierComponent if there is one, null otherwise.</returns>
|
||||
public static ModifierComponent? GetModifierComponent(this PlayerControl player)
|
||||
public static ModifierComponent GetModifierComponent(this PlayerControl player)
|
||||
{
|
||||
if (ModifierComponents.TryGetValue(player, out var component))
|
||||
{
|
||||
@@ -97,9 +131,9 @@ public static class ModifierExtensions
|
||||
}
|
||||
|
||||
component = player.GetComponent<ModifierComponent>();
|
||||
if (component == null)
|
||||
if (!component)
|
||||
{
|
||||
return null;
|
||||
throw new InvalidOperationException("ModifierComponent is not attached to the player.");
|
||||
}
|
||||
|
||||
ModifierComponents[player] = component;
|
||||
@@ -115,7 +149,7 @@ public static class ModifierExtensions
|
||||
/// <returns>True if the player has the modifier, false otherwise.</returns>
|
||||
public static bool HasModifier<T>(this PlayerControl player, Func<T, bool>? predicate = null) where T : BaseModifier
|
||||
{
|
||||
return player.GetModifierComponent()!.HasModifier(predicate);
|
||||
return player.GetModifierComponent().HasModifier(predicate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -127,19 +161,33 @@ public static class ModifierExtensions
|
||||
/// <returns>True if the player has the modifier, false otherwise.</returns>
|
||||
public static bool HasModifier(this PlayerControl player, Type type, Func<BaseModifier, bool>? predicate = null)
|
||||
{
|
||||
return player.GetModifierComponent()!.HasModifier(type, predicate);
|
||||
return player.GetModifierComponent().HasModifier(type, predicate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the player has a specific modifier by ID.
|
||||
/// Checks if the player has a specific modifier by type ID.
|
||||
/// </summary>
|
||||
/// <param name="player">The PlayerControl instance.</param>
|
||||
/// <param name="id">The ID of the modifier.</param>
|
||||
/// <param name="id">The type ID of the modifier.</param>
|
||||
/// <param name="predicate">Optional predicate to filter the modifiers.</param>
|
||||
/// <returns>True if the player has the modifier, false otherwise.</returns>
|
||||
public static bool HasModifier(this PlayerControl player, uint id, Func<BaseModifier, bool>? predicate = null)
|
||||
public static bool HasModifierByTypeId(
|
||||
this PlayerControl player,
|
||||
uint id,
|
||||
Func<BaseModifier, bool>? predicate = null)
|
||||
{
|
||||
return player.GetModifierComponent()!.HasModifier(id, predicate);
|
||||
return player.GetModifierComponent().HasModifierByTypeId(id, predicate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the player has a specific modifier by unique ID.
|
||||
/// </summary>
|
||||
/// <param name="player">The PlayerControl instance.</param>
|
||||
/// <param name="id">The unique ID of the modifier.</param>
|
||||
/// <returns>True if the player has the modifier, false otherwise.</returns>
|
||||
public static bool HasModifierByUniqueId(this PlayerControl player, uint id)
|
||||
{
|
||||
return player.GetModifierComponent().HasModifierByUniqueId(id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -151,7 +199,7 @@ public static class ModifierExtensions
|
||||
/// <returns>The modifier if found, null otherwise.</returns>
|
||||
public static T? GetModifier<T>(this PlayerControl player, Func<T, bool>? predicate = null) where T : BaseModifier
|
||||
{
|
||||
return player.GetModifierComponent()!.GetModifier(predicate);
|
||||
return player.GetModifierComponent().GetModifier(predicate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -166,7 +214,22 @@ public static class ModifierExtensions
|
||||
Type type,
|
||||
Func<BaseModifier, bool>? predicate = null)
|
||||
{
|
||||
return player.GetModifierComponent()!.GetModifier(type, predicate);
|
||||
return player.GetModifierComponent().GetModifier(type, predicate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a specific modifier from the player by type ID.
|
||||
/// </summary>
|
||||
/// <param name="player">The PlayerControl instance.</param>
|
||||
/// <param name="id">The type ID of the modifier.</param>
|
||||
/// <param name="predicate">Optional predicate to filter the modifiers.</param>
|
||||
/// <returns>The modifier if found, null otherwise.</returns>
|
||||
public static BaseModifier? GetModifierByTypeId(
|
||||
this PlayerControl player,
|
||||
uint id,
|
||||
Func<BaseModifier, bool>? predicate = null)
|
||||
{
|
||||
return player.GetModifierComponent().GetModifierByTypeId(id, predicate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -174,14 +237,12 @@ public static class ModifierExtensions
|
||||
/// </summary>
|
||||
/// <param name="player">The PlayerControl instance.</param>
|
||||
/// <param name="id">The ID of the modifier.</param>
|
||||
/// <param name="predicate">Optional predicate to filter the modifiers.</param>
|
||||
/// <returns>The modifier if found, null otherwise.</returns>
|
||||
public static BaseModifier? GetModifier(
|
||||
public static BaseModifier? GetModifierByUniqueId(
|
||||
this PlayerControl player,
|
||||
uint id,
|
||||
Func<BaseModifier, bool>? predicate = null)
|
||||
uint id)
|
||||
{
|
||||
return player.GetModifierComponent()!.GetModifier(id, predicate);
|
||||
return player.GetModifierComponent().GetModifierByUniqueId(id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -191,10 +252,10 @@ public static class ModifierExtensions
|
||||
/// <param name="player">The PlayerControl instance.</param>
|
||||
/// <param name="predicate">Optional predicate to filter the modifiers.</param>
|
||||
/// <returns>An enumerable of modifiers.</returns>
|
||||
public static IEnumerable<T> GetModifiersByType<T>(this PlayerControl player, Func<T, bool>? predicate = null)
|
||||
public static IEnumerable<T> GetModifiers<T>(this PlayerControl player, Func<T, bool>? predicate = null)
|
||||
where T : BaseModifier
|
||||
{
|
||||
return player.GetModifierComponent()!.GetModifiersByType(predicate);
|
||||
return player.GetModifierComponent().GetModifiers(predicate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -204,27 +265,37 @@ public static class ModifierExtensions
|
||||
/// <param name="type">The type of the modifiers.</param>
|
||||
/// <param name="predicate">Optional predicate to filter the modifiers.</param>
|
||||
/// <returns>An enumerable of modifiers.</returns>
|
||||
public static IEnumerable<BaseModifier> GetModifiersByType(
|
||||
public static IEnumerable<BaseModifier> GetModifiers(
|
||||
this PlayerControl player,
|
||||
Type type,
|
||||
Func<BaseModifier, bool>? predicate = null)
|
||||
{
|
||||
return player.GetModifierComponent()!.GetModifiersByType(type, predicate);
|
||||
return player.GetModifierComponent().GetModifiers(type, predicate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all modifiers of a specific type from the player by ID.
|
||||
/// Gets all modifiers of a specific type from the player by type ID.
|
||||
/// </summary>
|
||||
/// <param name="player">The PlayerControl instance.</param>
|
||||
/// <param name="id">The ID of the modifiers.</param>
|
||||
/// <param name="id">The type ID of the modifiers.</param>
|
||||
/// <param name="predicate">Optional predicate to filter the modifiers.</param>
|
||||
/// <returns>An enumerable of modifiers.</returns>
|
||||
public static IEnumerable<BaseModifier> GetModifiersByType(
|
||||
public static IEnumerable<BaseModifier> GetModifiers(
|
||||
this PlayerControl player,
|
||||
uint id,
|
||||
Func<BaseModifier, bool>? predicate = null)
|
||||
{
|
||||
return player.GetModifierComponent()!.GetModifiersByType(id, predicate);
|
||||
return player.GetModifierComponent().GetModifiers(id, predicate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes a specific modifier from the player.
|
||||
/// </summary>
|
||||
/// <param name="player">The PlayerControl instance.</param>
|
||||
/// <param name="modifier">The modifier to remove.</param>
|
||||
public static void RemoveModifier(this PlayerControl player, BaseModifier modifier)
|
||||
{
|
||||
player.GetModifierComponent().RemoveModifier(modifier);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -236,7 +307,7 @@ public static class ModifierExtensions
|
||||
public static void RemoveModifier<T>(this PlayerControl player, Func<T, bool>? predicate = null)
|
||||
where T : BaseModifier
|
||||
{
|
||||
player.GetModifierComponent()!.RemoveModifier(predicate);
|
||||
player.GetModifierComponent().RemoveModifier(predicate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -247,31 +318,33 @@ public static class ModifierExtensions
|
||||
/// <param name="predicate">Optional predicate to filter the modifiers.</param>
|
||||
public static void RemoveModifier(this PlayerControl player, Type type, Func<BaseModifier, bool>? predicate = null)
|
||||
{
|
||||
player.GetModifierComponent()!.RemoveModifier(type, predicate);
|
||||
player.GetModifierComponent().RemoveModifier(type, predicate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes a specific modifier from the player by ID.
|
||||
/// </summary>
|
||||
/// <param name="player">The PlayerControl instance.</param>
|
||||
/// <param name="modifierId">The ID of the modifier.</param>
|
||||
/// <param name="typeId">The ID of the modifier.</param>
|
||||
/// <param name="predicate">Optional predicate to filter the modifiers.</param>
|
||||
public static void RemoveModifier(
|
||||
public static void RemoveModifierByTypeId(
|
||||
this PlayerControl player,
|
||||
uint modifierId,
|
||||
uint typeId,
|
||||
Func<BaseModifier, bool>? predicate = null)
|
||||
{
|
||||
player.GetModifierComponent()!.RemoveModifier(modifierId, predicate);
|
||||
player.GetModifierComponent().RemoveModifierByTypeId(typeId, predicate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes a specific modifier from the player.
|
||||
/// Removes a specific modifier from the player by ID.
|
||||
/// </summary>
|
||||
/// <param name="player">The PlayerControl instance.</param>
|
||||
/// <param name="modifier">The modifier to remove.</param>
|
||||
public static void RemoveModifier(this PlayerControl player, BaseModifier modifier)
|
||||
/// <param name="uniqueId">The ID of the modifier.</param>
|
||||
public static void RemoveModifierByUniqueId(
|
||||
this PlayerControl player,
|
||||
uint uniqueId)
|
||||
{
|
||||
player.GetModifierComponent()!.RemoveModifier(modifier);
|
||||
player.GetModifierComponent().RemoveModifierByUniqueId(uniqueId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -283,7 +356,7 @@ public static class ModifierExtensions
|
||||
/// <returns>The added modifier.</returns>
|
||||
public static T? AddModifier<T>(this PlayerControl player, params object[] args) where T : BaseModifier
|
||||
{
|
||||
return player.GetModifierComponent()!.AddModifier<T>(args);
|
||||
return player.GetModifierComponent().AddModifier<T>(args);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -294,7 +367,7 @@ public static class ModifierExtensions
|
||||
/// <returns>The added modifier.</returns>
|
||||
public static BaseModifier? AddModifier(this PlayerControl player, BaseModifier modifier)
|
||||
{
|
||||
return player.GetModifierComponent()!.AddModifier(modifier);
|
||||
return player.GetModifierComponent().AddModifier(modifier);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -306,18 +379,18 @@ public static class ModifierExtensions
|
||||
/// <returns>The added modifier.</returns>
|
||||
public static BaseModifier? AddModifier(this PlayerControl player, Type type, params object[] args)
|
||||
{
|
||||
return player.GetModifierComponent()!.AddModifier(type, args);
|
||||
return player.GetModifierComponent().AddModifier(type, args);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a specific modifier to the player by ID.
|
||||
/// Adds a specific modifier to the player by type ID.
|
||||
/// </summary>
|
||||
/// <param name="player">The PlayerControl instance.</param>
|
||||
/// <param name="id">The ID of the modifier.</param>
|
||||
/// <param name="typeId">The type ID of the modifier.</param>
|
||||
/// <param name="args">The arguments to initialize the modifier constructor with.</param>
|
||||
/// <returns>The added modifier.</returns>
|
||||
public static BaseModifier? AddModifier(this PlayerControl player, uint id, params object[] args)
|
||||
public static BaseModifier? AddModifier(this PlayerControl player, uint typeId, params object[] args)
|
||||
{
|
||||
return player.GetModifierComponent()!.AddModifier(id, args);
|
||||
return player.GetModifierComponent().AddModifier(typeId, args);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ using System.Linq;
|
||||
using System.Runtime.Serialization;
|
||||
using MiraAPI.Modifiers.Types;
|
||||
using MiraAPI.Networking;
|
||||
using MiraAPI.Networking.Modifiers;
|
||||
using MiraAPI.PluginLoading;
|
||||
using MiraAPI.Roles;
|
||||
using MiraAPI.Utilities;
|
||||
@@ -23,16 +24,16 @@ public static class ModifierManager
|
||||
private static readonly Dictionary<Type, uint> TypeToIdModifierMap = [];
|
||||
private static readonly Dictionary<int, List<uint>> PrioritiesToIdsMap = [];
|
||||
|
||||
private static uint _nextId;
|
||||
private static uint _nextTypeId;
|
||||
|
||||
private static uint GetNextId()
|
||||
private static uint GetNextTypeId()
|
||||
{
|
||||
_nextId++;
|
||||
return _nextId;
|
||||
_nextTypeId++;
|
||||
return _nextTypeId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the modifier type from the id.
|
||||
/// Gets the modifier type from the type id.
|
||||
/// </summary>
|
||||
/// <param name="id">The ID.</param>
|
||||
/// <returns>The Type of the modifier.</returns>
|
||||
@@ -46,7 +47,7 @@ public static class ModifierManager
|
||||
/// </summary>
|
||||
/// <param name="type">The Type.</param>
|
||||
/// <returns>The ID of the modifier.</returns>
|
||||
public static uint? GetModifierId(Type type)
|
||||
public static uint? GetModifierTypeId(Type type)
|
||||
{
|
||||
if (!TypeToIdModifierMap.TryGetValue(type, out var id))
|
||||
{
|
||||
@@ -63,8 +64,8 @@ public static class ModifierManager
|
||||
return false;
|
||||
}
|
||||
|
||||
IdToTypeModifierMap.Add(GetNextId(), modifierType);
|
||||
TypeToIdModifierMap.Add(modifierType, _nextId);
|
||||
IdToTypeModifierMap.Add(GetNextTypeId(), modifierType);
|
||||
TypeToIdModifierMap.Add(modifierType, _nextTypeId);
|
||||
var bm = (BaseModifier)FormatterServices.GetUninitializedObject(modifierType); // this probably isn't a great idea
|
||||
info.Modifiers.Add(bm);
|
||||
|
||||
@@ -81,7 +82,7 @@ public static class ModifierManager
|
||||
PrioritiesToIdsMap[priority] = list = [];
|
||||
}
|
||||
|
||||
list.Add(_nextId);
|
||||
list.Add(_nextTypeId);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -143,7 +144,7 @@ public static class ModifierManager
|
||||
var mod = Activator.CreateInstance(IdToTypeModifierMap[id]) as GameModifier;
|
||||
var plr = plrs.Where(x => IsGameModifierValid(x, mod!, id)).Random();
|
||||
|
||||
if (plr == null)
|
||||
if (!plr || plr == null)
|
||||
{
|
||||
Logger<MiraApiPlugin>.Warning($"Somehow valid players for modifier {mod!.ModifierName} disappeared");
|
||||
}
|
||||
@@ -158,8 +159,7 @@ public static class ModifierManager
|
||||
private static bool IsGameModifierValid(PlayerControl player, GameModifier modifier, uint modifierId)
|
||||
{
|
||||
return (player.Data.Role is not ICustomRole role || role.IsModifierApplicable(modifier)) &&
|
||||
modifier.IsModifierValidOn(player.Data.Role) &&
|
||||
!player.HasModifier(modifierId);
|
||||
modifier.IsModifierValidOn(player.Data.Role) && !player.HasModifierByTypeId(modifierId);
|
||||
}
|
||||
|
||||
internal static void SyncAllModifiers(int targetId = -1)
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ using Reactor.Networking.Attributes;
|
||||
using Reactor.Networking.Rpc;
|
||||
using Reactor.Networking.Serialization;
|
||||
|
||||
namespace MiraAPI.Networking;
|
||||
namespace MiraAPI.Networking.Modifiers;
|
||||
|
||||
/// <summary>
|
||||
/// Remote procedure call for adding a modifier.
|
||||
+1
-1
@@ -3,7 +3,7 @@ using MiraAPI.Modifiers;
|
||||
using Reactor.Networking.Attributes;
|
||||
using Reactor.Networking.Rpc;
|
||||
|
||||
namespace MiraAPI.Networking;
|
||||
namespace MiraAPI.Networking.Modifiers;
|
||||
|
||||
[RegisterCustomRpc((uint)MiraRpc.SyncModifiers)]
|
||||
internal sealed class SyncModifiersRpc(MiraApiPlugin plugin, uint id) : PlayerCustomRpc<MiraApiPlugin, NetData[]>(plugin, id)
|
||||
@@ -2,7 +2,7 @@
|
||||
using MiraAPI.Events;
|
||||
using MiraAPI.Events.Vanilla.Gameplay;
|
||||
|
||||
namespace MiraAPI.Patches;
|
||||
namespace MiraAPI.Patches.Events;
|
||||
|
||||
/// <summary>
|
||||
/// General patches for the KillAnimation class.
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
using HarmonyLib;
|
||||
using MiraAPI.Events;
|
||||
using MiraAPI.Events.Vanilla;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using MiraAPI.Events.Vanilla.Usables;
|
||||
|
||||
namespace MiraAPI.Patches.Events;
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ internal static class TaskAddButtonPatches
|
||||
if (uint.TryParse(__instance.name, out var result))
|
||||
{
|
||||
__instance.Overlay.sprite = __instance.CheckImage;
|
||||
__instance.Overlay.enabled = PlayerControl.LocalPlayer.HasModifier(result);
|
||||
__instance.Overlay.enabled = PlayerControl.LocalPlayer.HasModifierByTypeId(result);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -326,7 +326,7 @@ internal static class TaskAdderPatches
|
||||
}
|
||||
|
||||
var taskAddButton = Object.Instantiate(instance.RoleButton);
|
||||
taskAddButton.name = modifier.GetId().ToString(NumberFormatInfo.InvariantInfo);
|
||||
taskAddButton.name = modifier.TypeId.ToString(NumberFormatInfo.InvariantInfo);
|
||||
taskAddButton.role = null;
|
||||
taskAddButton.MyTask = null;
|
||||
taskAddButton.SafePositionWorld = instance.SafePositionWorld;
|
||||
@@ -336,10 +336,10 @@ internal static class TaskAdderPatches
|
||||
taskAddButton.Button.OnClick = new Button.ButtonClickedEvent();
|
||||
taskAddButton.Button.OnClick.AddListener((UnityEngine.Events.UnityAction)(() =>
|
||||
{
|
||||
var id = modifier.GetId();
|
||||
if (PlayerControl.LocalPlayer.HasModifier(id))
|
||||
var id = modifier.TypeId;
|
||||
if (PlayerControl.LocalPlayer.HasModifierByTypeId(id))
|
||||
{
|
||||
PlayerControl.LocalPlayer.RemoveModifier(id);
|
||||
PlayerControl.LocalPlayer.RemoveModifierByTypeId(id);
|
||||
taskAddButton.Overlay.enabled = false;
|
||||
}
|
||||
else
|
||||
|
||||
@@ -24,7 +24,7 @@ public static class EndGameDidWinPatch
|
||||
|
||||
var didWin = networkedPlayerInfo.Role.DidWin(gameOverReason);
|
||||
|
||||
var modifierWin = networkedPlayerInfo.Object?.GetModifierComponent()?.ActiveModifiers.OfType<GameModifier>()
|
||||
var modifierWin = networkedPlayerInfo.Object?.GetModifierComponent().GetModifiers<GameModifier>()
|
||||
.FirstOrDefault(x => x.DidWin(gameOverReason) != null)?.DidWin(gameOverReason);
|
||||
if (modifierWin != null)
|
||||
{
|
||||
|
||||
@@ -35,8 +35,8 @@ public static class VentPatches
|
||||
var canVent = role is ICustomRole customRole ? customRole.Configuration.CanUseVent : role.CanVent;
|
||||
couldUse = canVent;
|
||||
|
||||
var modifiers = @object.GetModifierComponent()?.ActiveModifiers;
|
||||
if (modifiers is { Count: > 0 })
|
||||
var modifiers = @object.GetModifierComponent().ActiveModifiers;
|
||||
if (modifiers.Count > 0)
|
||||
{
|
||||
switch (canVent)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user