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
+133 -55

No files matched your search

@@ -18,7 +18,7 @@ public class FreezeButton : CustomActionButton<PlayerControl>
protected override void OnClick()
{
Target.AddModifier<FreezeModifier>();
Target?.RpcAddModifier<FreezeModifier>();
}
public override PlayerControl GetTarget()
+1 -1
View File
@@ -41,7 +41,7 @@ public class MeetingButton : CustomActionButton
if (UsesLeft == 0 && PlayerControl.LocalPlayer.HasModifier<CaptainModifier>())
{
PlayerControl.LocalPlayer.RemoveModifier<CaptainModifier>();
PlayerControl.LocalPlayer.RpcRemoveModifier<CaptainModifier>();
}
}
}
+77 -44
View File
@@ -1,7 +1,5 @@
using MiraAPI.Modifiers.Types;
using MiraAPI.Networking;
using MiraAPI.Utilities;
using Reactor.Networking.Attributes;
using Reactor.Utilities;
using Reactor.Utilities.Attributes;
using System;
@@ -74,17 +72,9 @@ 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))
{
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);
var modifier = ActiveModifiers.Find(x => x.GetType() == type);
if (modifier is null)
{
@@ -92,58 +82,101 @@ public class ModifierComponent(IntPtr ptr) : MonoBehaviour(ptr)
return;
}
modifier.OnDeactivate();
modifierComponent.ActiveModifiers.Remove(modifier);
RemoveModifier(modifier);
}
if (target.AmOwner)
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();
ActiveModifiers.Remove(modifier);
if (player.AmOwner)
{
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.");
return;
Logger<MiraApiPlugin>.Error($"Player already has modifier with id {modifier.ModifierId}!");
return null;
}
if (target.HasModifier(modifierId))
{
Logger<MiraApiPlugin>.Error($"Player already has modifier with id {modifierId}!");
return;
}
var modifier = (BaseModifier)Activator.CreateInstance(type);
var modifierComponent = target.GetModifierComponent();
if (modifier is null)
{
Logger<MiraApiPlugin>.Error($"Cannot add modifier {type.Name} because it is null.");
return;
}
modifierComponent.ActiveModifiers.Add(modifier);
modifier.Player = modifierComponent.player;
modifier.ModifierId = modifierId;
ActiveModifiers.Add(modifier);
modifier.Player = player;
modifier.ModifierId = ModifierManager.TypeToIdModifiers[modifier.GetType()];
modifier.OnActivate();
if (target.AmOwner)
if (!player.AmOwner)
{
return modifier;
}
if (modifier is TimedModifier { AutoStart: true } timer)
{
timer.StartTimer();
}
HudManager.Instance.SetHudActive(true);
}
return modifier;
}
[MethodRpc((uint)MiraRpc.RemoveModifier)]
public static void RpcRemoveModifier(PlayerControl target, uint modifierId) => RemoveModifier(target, modifierId);
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;
}
[MethodRpc((uint)MiraRpc.AddModifier)]
public static void RpcAddModifier(PlayerControl target, uint modifierId) => AddModifier(target, modifierId);
if (ActiveModifiers.Find(x=>x.ModifierId == modifierId) != null)
{
Logger<MiraApiPlugin>.Error($"Player already has modifier with id {modifierId}!");
return null;
}
var modifier = (BaseModifier)Activator.CreateInstance(type);
if (modifier is null)
{
Logger<MiraApiPlugin>.Error($"Cannot add modifier {type.Name} because it is null.");
return null;
}
AddModifier(modifier);
return modifier;
}
public T AddModifier<T>() where T : BaseModifier
{
return AddModifier(typeof(T)) as T;
}
}
+8 -2
View File
@@ -106,7 +106,7 @@ public static class ModifierManager
}
shuffledModifiers.RemoveAt(0);
ModifierComponent.RpcAddModifier(plr, id);
plr.RpcAddModifier(id);
}
}
@@ -152,7 +152,13 @@ public static class ModifierManager
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;
namespace MiraAPI.Modifiers.Types;
@@ -32,7 +33,7 @@ public abstract class TimedModifier : BaseModifier
if (RemoveOnComplete)
{
ModifierComponent.RpcRemoveModifier(Player, ModifierId);
Player.RpcRemoveModifier(ModifierId);
}
}
}
+43 -5
View File
@@ -6,6 +6,7 @@ using MiraAPI.Modifiers;
using Reactor.Utilities;
using System.Linq;
using MiraAPI.Networking;
using Reactor.Networking.Attributes;
using UnityEngine;
namespace MiraAPI.Utilities;
@@ -71,9 +72,22 @@ public static class Extensions
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)
{
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)
@@ -89,16 +103,40 @@ public static class Extensions
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
{
return player?.GetModifierComponent() != null && player.GetModifierComponent()!.ActiveModifiers.Exists(x => x is T);
}
public static bool HasModifier(this PlayerControl? player, uint 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)
{
target.GetModifierComponent()?.RemoveModifier(modifierId);
}
[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))
{
@@ -106,10 +144,10 @@ public static class Extensions
return;
}
ModifierComponent.RpcAddModifier(player, id);
player.RpcAddModifier(id);
}
public static void RemoveModifier<T>(this PlayerControl player) where T : BaseModifier
public static void RpcRemoveModifier<T>(this PlayerControl player) where T : BaseModifier
{
if (!ModifierManager.TypeToIdModifiers.TryGetValue(typeof(T), out var id))
{
@@ -117,7 +155,7 @@ public static class Extensions
return;
}
ModifierComponent.RpcRemoveModifier(player, id);
player.RpcAddModifier(id);
}
public static Color DarkenColor(this Color color)