From d0be9f1332f09401dd3ba72c7b102fe16948f20f Mon Sep 17 00:00:00 2001 From: iRebbok <45738951+iRebbok@users.noreply.github.com> Date: Tue, 8 Oct 2019 15:15:13 +0300 Subject: [PATCH] first rework (1.3.1) --- DeconOnDemand.cs | 345 +++++++++++++++++++++++------------------------ 1 file changed, 169 insertions(+), 176 deletions(-) diff --git a/DeconOnDemand.cs b/DeconOnDemand.cs index 4f8e477..8871eb2 100644 --- a/DeconOnDemand.cs +++ b/DeconOnDemand.cs @@ -1,177 +1,170 @@ -using Smod2; -using Smod2.Attributes; -using Smod2.Config; -using Smod2.EventHandlers; -using Smod2.Events; -using System.Linq; -using System.Reflection; -using UnityEngine; - -namespace ArithFeather.DeconOnDemand -{ - [PluginDetails( - author = "Arith", - name = "Decon On Demand", - description = "", - id = "ArithFeather.DeconOnDemand", - configPrefix = "afdod", - version = "1.3", - SmodMajor = 3, - SmodMinor = 4, - SmodRevision = 0 - )] - public class DeconOnDemand : Plugin, IEventHandlerCallCommand, IEventHandlerWaitingForPlayers - { - private const string InfoMessage = "'.decon disable' - Disables decontamination)\n" + - "'.decon #' Starts decontamination with minute value\n" + - "Announcements in:\n" + - "15 Minutes.\n" + - "10 Minutes.\n" + - "5 Minutes.\n" + - "1 Minute.\n" + - "0.5 Minutes. (30 seconds)\n"; - - public override void OnDisable() => Info("DeconOnDemand disabled."); - public override void OnEnable() => Info("DeconOnDemand enabled."); - public override void Register() - { - instance = this; - AddEventHandlers(this); - } - - [ConfigOption] private readonly string[] allowedRanks = new string[] { "ADMIN", "OWNER" }; - - private static DeconOnDemand instance; - private DecontaminationLCZ decontamination; - - public static void StartDecontamination(float timeInMinutes) => instance.InitializeDecontamination(timeInMinutes); - public static void StartDecontamination(int announcement) => instance.InitializeDecontamination(announcement); - public static void DisableDecontamination() => instance.disableDeconInfo.SetValue(instance.decontamination, true); - - private readonly FieldInfo curAnmInfo = typeof(DecontaminationLCZ).GetField("curAnm", BindingFlags.NonPublic | BindingFlags.Instance); - private readonly FieldInfo disableDeconInfo = typeof(DecontaminationLCZ).GetField("smDisableDecontamination", BindingFlags.NonPublic | BindingFlags.Instance); - - public void OnWaitingForPlayers(WaitingForPlayersEvent ev) - { - decontamination = GameObject.Find("Host").GetComponent() as DecontaminationLCZ; - } - - private void InitializeDecontamination(float minutes) - { - if (minutes >= 0f) - { - switch (minutes) - { - case 15f: - decontamination.time = 41f; - break; - case 10f: - decontamination.time = 239f; - break; - case 5f: - decontamination.time = 437f; - break; - case 1f: - decontamination.time = 635f; - break; - case 0.5f: - decontamination.time = 665f; - break; - case 0f: - decontamination.time = 703.4f; - break; - default: - decontamination.time = (11.74f - minutes) * 60; - break; - } - - disableDeconInfo.SetValue(decontamination, false); - - for (int i = 0; i < decontamination.announcements.Count; i++) - { - var annStartTime = decontamination.announcements[i].startTime; - if (decontamination.time / 60f < annStartTime) - { - curAnmInfo.SetValue(decontamination, i); - return; - } - } - } - } - - /// - /// 0 - 15 Minutes - 0.7 - /// 1 - 10 Minutes - 4 - /// 2 - 5 Minutes - 7.3 - /// 3 - 1 Minute - 10.6 - /// 4 - 30 Seconds - 11.1 - /// 5 - Decontaminate - 11.74 - /// - private void InitializeDecontamination(int announcement) - { - var anns = decontamination.announcements; - - if (announcement >= 0 && announcement <= 5) - { - disableDeconInfo.SetValue(decontamination, false); - curAnmInfo.SetValue(decontamination, announcement); - decontamination.time = anns[announcement].startTime * 60f; - } - } - - public void OnCallCommand(PlayerCallCommandEvent ev) - { - var commandToUpper = ev.Command.ToUpper(); - if (commandToUpper.StartsWith("DECON")) - { - var rank = ev.Player.GetRankName().ToUpper(); - bool allowed = false; - foreach (var r in allowedRanks) - { - if (r.ToUpper() == rank) - { - allowed = true; - break; - } - } - - if (allowed) - { - string[] inputs = ev.Command.Split(' '); - - if (inputs.Count() > 1) - { - var input = inputs[1].ToUpper(); - - if (input == "HELP") - { - ev.ReturnMessage = InfoMessage; - } - else if (input == "DISABLE") - { - disableDeconInfo.SetValue(decontamination, true); - ev.ReturnMessage = "Decontamination Disabled. Note you can not disable decontamination after decontaminate starts."; - } - else if (float.TryParse(inputs[1], out float minutes) && minutes >= 0) - { - InitializeDecontamination((float)minutes); - ev.ReturnMessage = $"Initializing Decontamination."; - } - else - { - ev.ReturnMessage = $"Wrong Input..\n{InfoMessage}"; - } - } - else - { - ev.ReturnMessage = $"Wrong Input..\n{InfoMessage}"; - } - } - else - { - ev.ReturnMessage = "You do not have the correct rank to call decontamination.\nRank must be one of: owner, admin, mod, or decon"; - } - } - } - } +using Smod2; +using Smod2.API; +using Smod2.Attributes; +using Smod2.Commands; +using Smod2.Config; +using Smod2.EventHandlers; +using Smod2.Events; +using System.Collections.Generic; +using System.Reflection; + +namespace ArithFeather.DeconOnDemand +{ + [PluginDetails( + author = "Arith", + name = "Decon On Demand", + description = "", + id = "ArithFeather.DeconOnDemand", + // configPrefix = "afdod", + version = "1.3.1", + SmodMajor = 3, + SmodMinor = 5, + SmodRevision = 0 + )] + public class DeconOnDemand : Plugin + { + private static readonly string infoMessage = string.Join("\n", new string[] { "", "Announcements in:", " - '15' Minutes", " - '10' Minutes", " - '5' Minutes", " - '1' Minutes", " - '0.5' Minutes (30 secound)" }); + + private static DeconOnDemand instance; + + public override void OnDisable() { this.Info("DeconOnDemand disabled."); } + public override void OnEnable() { instance = this; this.Info("DeconOnDemand enabled."); } + public override void Register() + { + this.AddConfig(new ConfigSetting("afgod_whitelist", new string[] { "admin", "owner" }, true, "Allowed ranks to use commands")); + this.AddConfig(new ConfigSetting("afgod_disable", false, true, "")); + + this.AddCommand("afdod_info", new InfoCommandHandler()); + this.AddCommand("afdod_decon", new DeconCommandHandler()); + } + + public class EventHandler : IEventHandlerWaitingForPlayers + { + private DeconOnDemand plugin => DeconOnDemand.instance; + internal static List allowedRanks; + + public void OnWaitingForPlayers(WaitingForPlayersEvent ev) + { + if (plugin.GetConfigBool("afgod_disable")) PluginManager.Manager.DisablePlugin(plugin); + + allowedRanks = new List(); + string[] ranks = plugin.GetConfigList("afgod_whitelist"); + foreach (string rank in ranks) if (!allowedRanks.Contains(rank)) allowedRanks.Add(rank); + + LogicManager.decontamination = PlayerManager.localPlayer.GetComponent(); + } + } + + public class InfoCommandHandler : ICommandHandler + { + public string GetCommandDescription() => "Information of decon announcements"; + public string GetUsage() => "afdod_info"; + public string[] OnCall(ICommandSender sender, string[] args) => new string[] { infoMessage }; + } + + public class DeconCommandHandler : ICommandHandler + { + public string GetCommandDescription() => ""; + public string GetUsage() => "afdod_decon