diff --git a/hats/API.cs b/hats/API.cs index 401f688..5b2e5a2 100644 --- a/hats/API.cs +++ b/hats/API.cs @@ -2,11 +2,13 @@ using System.Collections.Generic; using System.Linq; using Exiled.API.Features; +using hats.Components; using MapEditorReborn.API.Features; using MapEditorReborn.API.Features.Objects; using MapEditorReborn.API.Features.Serializable; using Newtonsoft.Json; using UnityEngine; +using Object = UnityEngine.Object; namespace hats { @@ -14,14 +16,10 @@ public static class API { public static Dictionary Hats { get; private set; } = new Dictionary(); - public static bool IsHat(this GameObject obj, out Hat hat) + public static bool IsHat(this GameObject obj, out HatComponent hat) { - hat = Hats.FirstOrDefault(x => x.Value.SpawnedHats.Any(x => x.gameObject == obj)).Value; - if (hat != null) - { - return true; - } - return false; + hat = Plugin.Singleton.hats.FirstOrDefault(x => x.Value.gameObject.Equals(obj)).Value; + return hat is not null; } public static void LoadHats() @@ -45,10 +43,10 @@ public static void ClearHats() { if(Hats.Count == 0) return; - foreach (var ply in Player.List.Where(x => x.SessionVariables.ContainsKey("HatWearer"))) + foreach (var ply in Player.List) { - ((SchematicObject)ply.SessionVariables["HatWearer"]).Destroy(); - ply.SessionVariables.Remove("HatWearer"); + if(ply.GameObject.TryGetComponent(out _)) + ply.RemoveHat(); } foreach (var kvp in Hats) { @@ -61,23 +59,31 @@ public static void AddHat(this Player ply, Hat hat) { if (hat == null) throw new ArgumentNullException(nameof(hat)); + if (ply.GameObject.TryGetComponent(out _)) + return; var obj = hat.SpawnHat(ply.Position); - obj.gameObject.transform.parent = ply.GameObject.transform; - obj.gameObject.transform.localPosition = hat.Offset; - ply.SessionVariables.Add("HatWearer", obj); + var comp = ply.GameObject.AddComponent(); + comp.hat = hat; + comp.ply = ply; + comp.schem = obj; + var gameObject = obj.gameObject; + gameObject.transform.parent = ply.GameObject.transform; + gameObject.transform.localPosition = hat.Offset; + Plugin.Singleton.hats.Add(ply.UserId, comp); } public static void RemoveHat(this Player ply) { - if (!ply.SessionVariables.ContainsKey("HatWearer")) + if (Plugin.Singleton.hats.Keys.All(x => x != ply.UserId)) { - throw new ArgumentException("Player isnt wearing a hat!"); + throw new ArgumentException("Player isn't wearing a hat!"); } - - if(ply.SessionVariables["HatWearer"] is SchematicObject obj && obj.gameObject.IsHat(out var hat)) + + var schem = Plugin.Singleton.hats[ply.UserId]; + if(schem.gameObject.IsHat(out var hat)) { - hat.SpawnedHats.Remove(obj); - obj.Destroy(); + hat.DoDestroy(); + Plugin.Singleton.hats.Remove(ply.UserId); } } } diff --git a/hats/Commands/AddHat.cs b/hats/Commands/AddHat.cs index 492731b..4d13a50 100644 --- a/hats/Commands/AddHat.cs +++ b/hats/Commands/AddHat.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using CommandSystem; using Exiled.API.Features; using Exiled.Permissions.Extensions; @@ -43,7 +44,7 @@ public bool Execute(ArraySegment arguments, ICommandSender sender, out s } } - if (ply.SessionVariables.ContainsKey("HatWearer")) + if (Plugin.Singleton.hats.Keys.Any(x => x == ply.UserId)) { response = "Player is already wearing a hat!"; return false; diff --git a/hats/Commands/RemoveHat.cs b/hats/Commands/RemoveHat.cs index 570bae5..9154e0f 100644 --- a/hats/Commands/RemoveHat.cs +++ b/hats/Commands/RemoveHat.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using CommandSystem; using Exiled.API.Features; using Exiled.Permissions.Extensions; @@ -31,7 +32,7 @@ public bool Execute(ArraySegment arguments, ICommandSender sender, out s } } - if (!ply.SessionVariables.ContainsKey("HatWearer")) + if (Plugin.Singleton.hats.Keys.All(x => x != ply.UserId)) { response = "Player isn't wearing a hat!"; return false; @@ -39,7 +40,7 @@ public bool Execute(ArraySegment arguments, ICommandSender sender, out s ply.RemoveHat(); - response = $"Gave hat to {ply.Nickname}!"; + response = $"Removed hat from {ply.Nickname}!"; return true; } } \ No newline at end of file diff --git a/hats/Components/HatComponent.cs b/hats/Components/HatComponent.cs new file mode 100644 index 0000000..4234126 --- /dev/null +++ b/hats/Components/HatComponent.cs @@ -0,0 +1,21 @@ +using System; +using Exiled.API.Features; +using MapEditorReborn.API.Features.Objects; +using UnityEngine; + +namespace hats.Components; + +public class HatComponent : MonoBehaviour +{ + public Hat hat; + public Player ply; + public SchematicObject schem; + + public void DoDestroy() + { + schem.Destroy(); + ply = null; + hat = null; + Destroy(this); + } +} \ No newline at end of file diff --git a/hats/EventHandler.cs b/hats/EventHandler.cs index bd1bb32..ffcca35 100644 --- a/hats/EventHandler.cs +++ b/hats/EventHandler.cs @@ -1,4 +1,5 @@ using Exiled.Events.EventArgs; +using hats.Components; using MapEditorReborn.API.Features.Objects; namespace hats @@ -6,7 +7,7 @@ namespace hats public class EventHandler { private Config cfg; - + public EventHandler(Config cfg) => this.cfg = cfg; public void WaitingForPlayers() @@ -16,7 +17,7 @@ public void WaitingForPlayers() public void OnLeave(LeftEventArgs ev) { - if(ev.Player.SessionVariables.ContainsKey("HatWearer")) + if(ev.Player.GameObject.TryGetComponent(out _)) ev.Player.RemoveHat(); } } diff --git a/hats/Hat.cs b/hats/Hat.cs index 22bdc55..483ef8a 100644 --- a/hats/Hat.cs +++ b/hats/Hat.cs @@ -1,35 +1,43 @@ using System; using System.Collections.Generic; using System.Linq; +using hats.Components; using MapEditorReborn.API.Features; using MapEditorReborn.API.Features.Objects; using MapEditorReborn.API.Features.Serializable; using MapEditorReborn.Commands.Scale; using UnityEngine; +using Object = UnityEngine.Object; namespace hats { public class Hat { - public List SpawnedHats = new List(); public string Name; public Vector3 Offset; public SchematicObjectDataList Schematic; - public SchematicObject SpawnHat(Vector3 Position, Quaternion? Rotation = null, Vector3? scale = null) + internal SchematicObject SpawnHat(Vector3 Position, Quaternion? Rotation = null, Vector3? scale = null) { SchematicObject obj = ObjectSpawner.SpawnSchematic(Name, Position, null, scale, Schematic); - SpawnedHats.Append(obj); return obj; } public void DestroyInstances() { - foreach (var hat in SpawnedHats.Where(x => x != null)) + if (Plugin.Singleton.hats.All(x => x.Value.hat != this)) + return; + List> toRemove = new List>(); + foreach (var kvp in Plugin.Singleton.hats.Where(x => x.Value.hat == this)) { - hat.Destroy(); + kvp.Value.DoDestroy(); + toRemove.Add(kvp); + } + + foreach (var kvp in toRemove) + { + Plugin.Singleton.hats.Remove(kvp.Key); } - SpawnedHats.Clear(); } public Hat(string Name, SchematicObjectDataList data, Vector3 offset) diff --git a/hats/Plugin.cs b/hats/Plugin.cs index 4d3247b..0d19b54 100644 --- a/hats/Plugin.cs +++ b/hats/Plugin.cs @@ -1,5 +1,8 @@ using System; +using System.Collections.Generic; using Exiled.API.Features; +using hats.Components; +using UnityEngine; using Player = Exiled.Events.Handlers.Player; using Server = Exiled.Events.Handlers.Server; @@ -14,9 +17,11 @@ public class Plugin : Plugin public static Plugin Singleton; public EventHandler Handler { get; private set; } - + public Dictionary hats; + public override void OnEnabled() { + hats = new Dictionary(); Singleton = this; Handler = new EventHandler(Config); @@ -33,6 +38,7 @@ public override void OnDisabled() Singleton = null; Handler = null; + hats = null; base.OnDisabled(); } } diff --git a/hats/hats.csproj b/hats/hats.csproj index 72dd322..8c95290 100644 --- a/hats/hats.csproj +++ b/hats/hats.csproj @@ -107,6 +107,7 @@ +