From 628361e2a4ab0e3774dbbb2c4b83730250b53884 Mon Sep 17 00:00:00 2001 From: moddedmcplayer Date: Sun, 21 Aug 2022 19:15:49 +0200 Subject: [PATCH] some stuff and hopefully fixes --- hats/API.cs | 2 +- hats/Commands/AddHat.cs | 2 +- hats/Commands/HatDebug.cs | 50 +++++++++++++++++++++++++++++++++++++ hats/Commands/Parent.cs | 2 ++ hats/Commands/ReloadHats.cs | 27 ++++++++++++++++++++ hats/EventHandler.cs | 9 +++---- hats/Hat.cs | 2 +- hats/Plugin.cs | 5 +--- hats/hats.csproj | 2 ++ 9 files changed, 88 insertions(+), 13 deletions(-) create mode 100644 hats/Commands/HatDebug.cs create mode 100644 hats/Commands/ReloadHats.cs diff --git a/hats/API.cs b/hats/API.cs index 0beb964..42d0080 100644 --- a/hats/API.cs +++ b/hats/API.cs @@ -67,7 +67,7 @@ public static void AddHat(this Player ply, Hat hat) comp.ply = ply; comp.schem = obj; var gameObject = obj.gameObject; - gameObject.transform.parent = ply.GameObject.transform; + gameObject.transform.SetParent(ply.GameObject.transform); gameObject.transform.localPosition = hat.Offset; gameObject.transform.localRotation = hat.Rotation; Plugin.Singleton.hats.Add(ply.UserId, comp); diff --git a/hats/Commands/AddHat.cs b/hats/Commands/AddHat.cs index 6ed039c..06c9826 100644 --- a/hats/Commands/AddHat.cs +++ b/hats/Commands/AddHat.cs @@ -44,7 +44,7 @@ public bool Execute(ArraySegment arguments, ICommandSender sender, out s } } - if (Plugin.Singleton.hats.Keys.Any(x => x == ply.UserId)) + if (Plugin.Singleton.hats.ContainsKey(ply.UserId)) { response = "Player is already wearing a hat!"; return false; diff --git a/hats/Commands/HatDebug.cs b/hats/Commands/HatDebug.cs new file mode 100644 index 0000000..7a57d62 --- /dev/null +++ b/hats/Commands/HatDebug.cs @@ -0,0 +1,50 @@ +namespace hats.Commands +{ + using System; + using System.Linq; + using CommandSystem; + using Exiled.API.Features; + using UnityEngine; + + public class HatDebug : ICommand + { + public bool Execute(ArraySegment arguments, ICommandSender sender, out string response) + { + var ply = Player.Get(sender); + if (ply is null || ply.IsHost) + { + response = "Is host."; + return false; + } + + if (!Plugin.Singleton.hats.ContainsKey(ply.UserId)) + { + response = "Player isnt wearing a hat!"; + return false; + } + + var schem = Plugin.Singleton.hats[ply.UserId]; + if(schem.gameObject.IsHat(out var hat)) + { + var gameObject = hat.gameObject; + Transform parent; + response = $"Name: {hat.hat.Name}" + + $"Enabled: {hat.schem.enabled}" + + $"Config rotation offset: {hat.hat.Rotation}" + + $"Config position offset: {hat.hat.Offset}" + + $"Actual local rotation: {gameObject.transform.localRotation}" + + $"Actual local position: {gameObject.transform.localPosition}" + + $"Parent GO (is player): {(parent = gameObject.transform.parent).gameObject.name} ({ply.GameObject == parent.gameObject})" + + $"Hat global position: {gameObject.transform.position}"; + return true; + } + + response = "Something went wrong"; + return false; + } + + public string Command { get; } = "Debug"; + public string[] Aliases { get; } = { }; + public string Description { get; } = "Print hat debug information"; + } +} \ No newline at end of file diff --git a/hats/Commands/Parent.cs b/hats/Commands/Parent.cs index 89036b4..66a9630 100644 --- a/hats/Commands/Parent.cs +++ b/hats/Commands/Parent.cs @@ -19,6 +19,8 @@ public sealed override void LoadGeneratedCommands() RegisterCommand(new List()); RegisterCommand(new AddHat()); RegisterCommand(new RemoveHat()); + RegisterCommand(new ReloadHats()); + RegisterCommand(new HatDebug()); } protected override bool ExecuteParent(ArraySegment args, ICommandSender sender, out string response) diff --git a/hats/Commands/ReloadHats.cs b/hats/Commands/ReloadHats.cs new file mode 100644 index 0000000..fe1a526 --- /dev/null +++ b/hats/Commands/ReloadHats.cs @@ -0,0 +1,27 @@ +namespace hats.Commands +{ + using System; + using CommandSystem; + using Exiled.API.Features; + using Exiled.Permissions.Extensions; + + public class ReloadHats : ICommand + { + public bool Execute(ArraySegment arguments, ICommandSender sender, out string response) + { + if(!sender.CheckPermission("hats.reload")) + { + response = "Missing permission: hats.reload."; + return false; + } + + API.LoadHats(); + response = "reloaded hats!"; + return true; + } + + public string Command { get; } = "reload"; + public string[] Aliases { get; } = { }; + public string Description { get; } = "Reloads all hats (WILL REMOVE ALL CURRENT HATS!)"; + } +} \ No newline at end of file diff --git a/hats/EventHandler.cs b/hats/EventHandler.cs index f56e4cd..ab3bec0 100644 --- a/hats/EventHandler.cs +++ b/hats/EventHandler.cs @@ -7,14 +7,16 @@ namespace hats public class EventHandler { private Config cfg; + private bool _isLoaded = false; public EventHandler(Config cfg) => this.cfg = cfg; public void WaitingForPlayers() { - if (API.Hats.Count != 0) + if (API.Hats.Count != 0 || _isLoaded) return; API.LoadHats(); + _isLoaded = true; } public void OnLeave(LeftEventArgs ev) @@ -22,10 +24,5 @@ public void OnLeave(LeftEventArgs ev) if(ev.Player.GameObject.TryGetComponent(out _)) ev.Player.RemoveHat(); } - - public void EndingRound(EndingRoundEventArgs ev) - { - API.LoadHats(); - } } } \ No newline at end of file diff --git a/hats/Hat.cs b/hats/Hat.cs index eb2b634..05f53d9 100644 --- a/hats/Hat.cs +++ b/hats/Hat.cs @@ -31,7 +31,7 @@ public void DestroyInstances() List> toRemove = new List>(); foreach (var kvp in Plugin.Singleton.hats.Where(x => x.Value.hat == this)) { - if (kvp.Value is null || kvp.Value.gameObject.Equals(null) || !kvp.Value.schem.isActiveAndEnabled) + if (kvp.Value is null || kvp.Value.gameObject == null || !kvp.Value.schem.isActiveAndEnabled) { toRemove.Add(kvp); continue; diff --git a/hats/Plugin.cs b/hats/Plugin.cs index 7701da0..4f40cef 100644 --- a/hats/Plugin.cs +++ b/hats/Plugin.cs @@ -2,7 +2,6 @@ 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; @@ -12,7 +11,7 @@ public class Plugin : Plugin { public override string Author { get; } = "Rowpann SCP"; public override string Name { get; } = "hats"; - public override Version Version { get; } = new Version(1, 1, 0); + public override Version Version { get; } = new Version(1, 2, 0); public override Version RequiredExiledVersion { get; } = new Version(5, 2, 0); public static Plugin Singleton; @@ -26,7 +25,6 @@ public override void OnEnabled() Handler = new EventHandler(Config); Server.WaitingForPlayers += Handler.WaitingForPlayers; - Server.EndingRound += Handler.EndingRound; Player.Left += Handler.OnLeave; base.OnEnabled(); @@ -35,7 +33,6 @@ public override void OnEnabled() public override void OnDisabled() { Server.WaitingForPlayers -= Handler.WaitingForPlayers; - Server.EndingRound -= Handler.EndingRound; Player.Left -= Handler.OnLeave; Singleton = null; diff --git a/hats/hats.csproj b/hats/hats.csproj index 885ce59..5ffe0eb 100644 --- a/hats/hats.csproj +++ b/hats/hats.csproj @@ -104,8 +104,10 @@ + +