refactor access and management methods for modifiers

This commit is contained in:
XtraCube committed 2024-08-28 12:39:05 -04:00
1 parent 5f1996b457
commit dd8d3df2b0
6 files changed
+139 -61

No files matched your search

@@ -18,7 +18,7 @@ public class FreezeButton : CustomActionButton<PlayerControl>
protected override void OnClick() protected override void OnClick()
{ {
Target.AddModifier<FreezeModifier>(); Target?.RpcAddModifier<FreezeModifier>();
} }
public override PlayerControl GetTarget() public override PlayerControl GetTarget()
+1 -1
View File
@@ -41,7 +41,7 @@ public class MeetingButton : CustomActionButton
if (UsesLeft == 0 && PlayerControl.LocalPlayer.HasModifier<CaptainModifier>()) if (UsesLeft == 0 && PlayerControl.LocalPlayer.HasModifier<CaptainModifier>())
{ {
PlayerControl.LocalPlayer.RemoveModifier<CaptainModifier>(); PlayerControl.LocalPlayer.RpcRemoveModifier<CaptainModifier>();
} }
} }
} }
+76 -43
View File
@@ -1,7 +1,5 @@
using MiraAPI.Modifiers.Types; using MiraAPI.Modifiers.Types;
using MiraAPI.Networking;
using MiraAPI.Utilities; using MiraAPI.Utilities;
using Reactor.Networking.Attributes;
using Reactor.Utilities; using Reactor.Utilities;
using Reactor.Utilities.Attributes; using Reactor.Utilities.Attributes;
using System; using System;
@@ -54,7 +52,7 @@ public class ModifierComponent(IntPtr ptr) : MonoBehaviour(ptr)
var filteredModifiers = ActiveModifiers.Where(mod => !mod.HideOnUi); var filteredModifiers = ActiveModifiers.Where(mod => !mod.HideOnUi);
var baseModifiers = filteredModifiers as BaseModifier[] ?? filteredModifiers.ToArray(); var baseModifiers = filteredModifiers as BaseModifier[] ?? filteredModifiers.ToArray();
if (player.AmOwner && baseModifiers.Any()) if (player.AmOwner && baseModifiers.Any())
{ {
var stringBuild = new StringBuilder(); var stringBuild = new StringBuilder();
@@ -74,76 +72,111 @@ public class ModifierComponent(IntPtr ptr) : MonoBehaviour(ptr)
} }
} }
public static void RemoveModifier(PlayerControl target, uint modifierId) public void RemoveModifier(Type type)
{ {
if (!ModifierManager.IdToTypeModifiers.TryGetValue(modifierId, out var type)) var modifier = ActiveModifiers.Find(x => x.GetType() == type);
{
Logger<MiraApiPlugin>.Error($"Cannot remove modifier with id {modifierId} because it is not registered.");
return;
}
var modifierComponent = target.GetModifierComponent();
var modifier = modifierComponent?.ActiveModifiers.Find(x => x.GetType() == type);
if (modifier is null) if (modifier is null)
{ {
Logger<MiraApiPlugin>.Error($"Cannot remove modifier {type.Name} because it is not active."); Logger<MiraApiPlugin>.Error($"Cannot remove modifier {type.Name} because it is not active.");
return; return;
} }
RemoveModifier(modifier);
}
public void RemoveModifier<T>() where T : BaseModifier
{
RemoveModifier(typeof(T));
}
public void RemoveModifier(uint modifierId)
{
var modifier = ActiveModifiers.Find(x => x.ModifierId == modifierId);
if (modifier is null)
{
Logger<MiraApiPlugin>.Error($"Cannot remove modifier with id {modifierId} because it is not active.");
return;
}
RemoveModifier(modifier);
}
public void RemoveModifier(BaseModifier modifier)
{
if (!ActiveModifiers.Contains(modifier))
{
Logger<MiraApiPlugin>.Error($"Cannot remove modifier {modifier.ModifierName} because it is not active on this player.");
return;
}
modifier.OnDeactivate(); modifier.OnDeactivate();
modifierComponent.ActiveModifiers.Remove(modifier); ActiveModifiers.Remove(modifier);
if (target.AmOwner) if (player.AmOwner)
{ {
HudManager.Instance.SetHudActive(true); HudManager.Instance.SetHudActive(true);
} }
} }
public static void AddModifier(PlayerControl target, uint modifierId) public BaseModifier AddModifier(BaseModifier modifier)
{ {
if (!ModifierManager.IdToTypeModifiers.TryGetValue(modifierId, out var type)) if (ActiveModifiers.Contains(modifier))
{ {
Logger<MiraApiPlugin>.Error($"Cannot add modifier with id {modifierId} because it is not registered."); Logger<MiraApiPlugin>.Error($"Player already has modifier with id {modifier.ModifierId}!");
return; return null;
}
ActiveModifiers.Add(modifier);
modifier.Player = player;
modifier.ModifierId = ModifierManager.TypeToIdModifiers[modifier.GetType()];
modifier.OnActivate();
if (!player.AmOwner)
{
return modifier;
} }
if (target.HasModifier(modifierId)) if (modifier is TimedModifier { AutoStart: true } timer)
{
timer.StartTimer();
}
HudManager.Instance.SetHudActive(true);
return modifier;
}
public BaseModifier AddModifier(Type type)
{
if (!ModifierManager.TypeToIdModifiers.TryGetValue(type, out var modifierId))
{
Logger<MiraApiPlugin>.Error($"Cannot add modifier {type.Name} because it is not registered.");
return null;
}
if (ActiveModifiers.Find(x=>x.ModifierId == modifierId) != null)
{ {
Logger<MiraApiPlugin>.Error($"Player already has modifier with id {modifierId}!"); Logger<MiraApiPlugin>.Error($"Player already has modifier with id {modifierId}!");
return; return null;
} }
var modifier = (BaseModifier)Activator.CreateInstance(type); var modifier = (BaseModifier)Activator.CreateInstance(type);
var modifierComponent = target.GetModifierComponent();
if (modifier is null) if (modifier is null)
{ {
Logger<MiraApiPlugin>.Error($"Cannot add modifier {type.Name} because it is null."); Logger<MiraApiPlugin>.Error($"Cannot add modifier {type.Name} because it is null.");
return; return null;
} }
modifierComponent.ActiveModifiers.Add(modifier); AddModifier(modifier);
modifier.Player = modifierComponent.player;
modifier.ModifierId = modifierId; return modifier;
modifier.OnActivate();
if (target.AmOwner)
{
if (modifier is TimedModifier { AutoStart: true } timer)
{
timer.StartTimer();
}
HudManager.Instance.SetHudActive(true);
}
} }
[MethodRpc((uint)MiraRpc.RemoveModifier)] public T AddModifier<T>() where T : BaseModifier
public static void RpcRemoveModifier(PlayerControl target, uint modifierId) => RemoveModifier(target, modifierId); {
return AddModifier(typeof(T)) as T;
[MethodRpc((uint)MiraRpc.AddModifier)] }
public static void RpcAddModifier(PlayerControl target, uint modifierId) => AddModifier(target, modifierId);
} }
+8 -2
View File
@@ -106,7 +106,7 @@ public static class ModifierManager
} }
shuffledModifiers.RemoveAt(0); shuffledModifiers.RemoveAt(0);
ModifierComponent.RpcAddModifier(plr, id); plr.RpcAddModifier(id);
} }
} }
@@ -152,7 +152,13 @@ public static class ModifierManager
foreach (var id in ids) foreach (var id in ids)
{ {
ModifierComponent.AddModifier(plr, id); if (!IdToTypeModifiers.TryGetValue(id, out var type))
{
Logger<MiraApiPlugin>.Error($"Cannot add modifier with id {id} because it is not registered.");
continue;
}
modifierComponent.AddModifier(type);
} }
} }
} }
+3 -2
View File
@@ -1,4 +1,5 @@
using Reactor.Utilities; using MiraAPI.Utilities;
using Reactor.Utilities;
using UnityEngine; using UnityEngine;
namespace MiraAPI.Modifiers.Types; namespace MiraAPI.Modifiers.Types;
@@ -32,7 +33,7 @@ public abstract class TimedModifier : BaseModifier
if (RemoveOnComplete) if (RemoveOnComplete)
{ {
ModifierComponent.RpcRemoveModifier(Player, ModifierId); Player.RpcRemoveModifier(ModifierId);
} }
} }
} }
+50 -12
View File
@@ -6,6 +6,7 @@ using MiraAPI.Modifiers;
using Reactor.Utilities; using Reactor.Utilities;
using System.Linq; using System.Linq;
using MiraAPI.Networking; using MiraAPI.Networking;
using Reactor.Networking.Attributes;
using UnityEngine; using UnityEngine;
namespace MiraAPI.Utilities; namespace MiraAPI.Utilities;
@@ -71,9 +72,22 @@ public static class Extensions
return ModdedOptionsManager.ModdedOptions.Values.Any(opt => opt.OptionBehaviour && opt.OptionBehaviour.Equals(optionBehaviour)); return ModdedOptionsManager.ModdedOptions.Values.Any(opt => opt.OptionBehaviour && opt.OptionBehaviour.Equals(optionBehaviour));
} }
public static readonly Dictionary<PlayerControl, ModifierComponent> ModifierComponents = new();
public static ModifierComponent? GetModifierComponent(this PlayerControl player) public static ModifierComponent? GetModifierComponent(this PlayerControl player)
{ {
return player.GetComponent<ModifierComponent>(); if (ModifierComponents.TryGetValue(player, out var component))
{
return component;
}
component = player.GetComponent<ModifierComponent>();
if (component)
{
ModifierComponents[player] = component;
}
return component;
} }
public static List<T> Randomize<T>(this List<T> list) public static List<T> Randomize<T>(this List<T> list)
@@ -88,28 +102,52 @@ public static class Extensions
} }
return randomizedList; return randomizedList;
} }
public static T? GetModifier<T>(this PlayerControl? player) where T : BaseModifier
{
return player?.GetModifierComponent()?.ActiveModifiers.Find(x => x is T) as T;
}
public static bool HasModifier<T>(this PlayerControl? player) where T : BaseModifier public static bool HasModifier<T>(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);
} }
public static bool HasModifier(this PlayerControl? player, uint id) 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);
} }
public static void AddModifier<T>(this PlayerControl player) where T : BaseModifier [MethodRpc((uint)MiraRpc.RemoveModifier)]
public static void RpcRemoveModifier(this PlayerControl target, uint modifierId)
{ {
if (!ModifierManager.TypeToIdModifiers.TryGetValue(typeof(T), out var id)) target.GetModifierComponent()?.RemoveModifier(modifierId);
{
Logger<MiraApiPlugin>.Error($"Cannot add modifier {typeof(T).Name} because it is not registered.");
return;
}
ModifierComponent.RpcAddModifier(player, id);
} }
public static void RemoveModifier<T>(this PlayerControl player) where T : BaseModifier [MethodRpc((uint)MiraRpc.AddModifier)]
public static BaseModifier? RpcAddModifier(this PlayerControl target, uint modifierId)
{
if (ModifierManager.IdToTypeModifiers.TryGetValue(modifierId, out var type))
{
return target.GetModifierComponent()?.AddModifier(type);
}
Logger<MiraApiPlugin>.Error($"Cannot add modifier with id {modifierId} because it is not registered.");
return null;
}
public static void RpcAddModifier<T>(this PlayerControl player) where T : BaseModifier
{
if (!ModifierManager.TypeToIdModifiers.TryGetValue(typeof(T), out var id))
{
Logger<MiraApiPlugin>.Error($"Cannot add modifier {typeof(T).Name} because it is not registered.");
return;
}
player.RpcAddModifier(id);
}
public static void RpcRemoveModifier<T>(this PlayerControl player) where T : BaseModifier
{ {
if (!ModifierManager.TypeToIdModifiers.TryGetValue(typeof(T), out var id)) if (!ModifierManager.TypeToIdModifiers.TryGetValue(typeof(T), out var id))
{ {
@@ -117,7 +155,7 @@ public static class Extensions
return; return;
} }
ModifierComponent.RpcRemoveModifier(player, id); player.RpcAddModifier(id);
} }
public static Color DarkenColor(this Color color) public static Color DarkenColor(this Color color)