From efc37ccbea80e841c1e96c379d5aff84e2825597 Mon Sep 17 00:00:00 2001 From: PintTheDragon Date: Thu, 9 Jul 2020 22:45:55 -0700 Subject: [PATCH] Added command handler until I actually fix the bug --- Buddy.cs | 13 ++++++++++++- Buddy.csproj | 2 ++ BuddyAcceptCommand.cs | 18 +++++++++--------- BuddyCommand.cs | 16 ++++++++-------- CommandHandler.cs | 34 ++++++++++++++++++++++++++++++++++ EventHandlers.cs | 5 +++++ ICommandPint.cs | 20 ++++++++++++++++++++ UnBuddyCommand.cs | 18 +++++++++--------- 8 files changed, 99 insertions(+), 27 deletions(-) create mode 100644 CommandHandler.cs create mode 100644 ICommandPint.cs diff --git a/Buddy.cs b/Buddy.cs index fb8bd09..9c9d7cb 100644 --- a/Buddy.cs +++ b/Buddy.cs @@ -1,5 +1,6 @@ using Exiled.API.Enums; using Exiled.API.Features; +using RemoteAdmin; using System; using System.Collections.Generic; using System.Linq; @@ -10,7 +11,7 @@ class Buddy : Plugin { public override PluginPriority Priority => PluginPriority.Medium; - public override Version Version { get; } = new Version("1.1.6"); + public override Version Version { get; } = new Version("1.1.7"); public EventHandlers EventHandlers; @@ -63,6 +64,9 @@ public override void OnDisabled() Exiled.Events.Handlers.Server.RoundStarted -= EventHandlers.OnRoundStart; Exiled.Events.Handlers.Player.Joined -= EventHandlers.OnPlayerJoin; Exiled.Events.Handlers.Server.RestartingRound -= EventHandlers.OnRoundRestart; + Exiled.Events.Handlers.Server.SendingConsoleCommand -= EventHandlers.OnConsoleCommand; + CommandHandler.isEnabled = false; + Log.Info("Buddy v"+Version+" (by PintTheDragon) has unloaded."); } @@ -83,6 +87,13 @@ public override void OnEnabled() Exiled.Events.Handlers.Server.RoundStarted += EventHandlers.OnRoundStart; Exiled.Events.Handlers.Player.Joined += EventHandlers.OnPlayerJoin; Exiled.Events.Handlers.Server.RestartingRound += EventHandlers.OnRoundRestart; + Exiled.Events.Handlers.Server.SendingConsoleCommand += EventHandlers.OnConsoleCommand; + + CommandHandler.isEnabled = true; + CommandHandler.Register(new BuddyCommand()); + CommandHandler.Register(new BuddyAcceptCommand()); + CommandHandler.Register(new UnBuddyCommand()); + Log.Info("Buddy v" + Version + " (by PintTheDragon) has loaded."); } diff --git a/Buddy.csproj b/Buddy.csproj index cee4456..d8077bd 100644 --- a/Buddy.csproj +++ b/Buddy.csproj @@ -35,8 +35,10 @@ + + diff --git a/BuddyAcceptCommand.cs b/BuddyAcceptCommand.cs index f739127..8decf34 100644 --- a/BuddyAcceptCommand.cs +++ b/BuddyAcceptCommand.cs @@ -6,25 +6,25 @@ namespace Buddy { - [CommandHandler(typeof(GameConsoleCommandHandler))] - class BuddyAcceptCommand : ICommand + //[CommandHandler(typeof(GameConsoleCommandHandler))] + class BuddyAcceptCommand : ICommandPint { public string Command => Buddy.singleton.buddyAcceptCommand; - public string[] Aliases => new string[] { }; + public string[] Aliases => null; public string Description => "A command to accept a pending buddy request."; - public bool Execute(ArraySegment arguments, ICommandSender sender, out string response) + public bool Execute(ArraySegment arguments, /*ICommandSender sender*/ Player player, out string response) { response = ""; string[] args = arguments.ToArray(); - if (sender is PlayerCommandSender p) - { - Player player = Player.Get(p.Processor._hub); + //if (sender is PlayerCommandSender p) + //{ + //Player player = Player.Get(p.Processor._hub); response = handleBuddyAcceptCommand(player, new string[] { }); - return true; - } + //return true; + //} return true; } diff --git a/BuddyCommand.cs b/BuddyCommand.cs index ea20ad2..78864a3 100644 --- a/BuddyCommand.cs +++ b/BuddyCommand.cs @@ -6,22 +6,22 @@ namespace Buddy { - [CommandHandler(typeof(GameConsoleCommandHandler))] - class BuddyCommand : ICommand + //[CommandHandler(typeof(GameConsoleCommandHandler))] + class BuddyCommand : ICommandPint { public string Command => Buddy.singleton.buddyCommand; - public string[] Aliases => new string[] { }; + public string[] Aliases => null; public string Description => "Allows you to pair up with another player and play on the same team."; - public bool Execute(ArraySegment arguments, ICommandSender sender, out string response) + public bool Execute(ArraySegment arguments, /*ICommandSender sender*/ Player player, out string response) { response = ""; string[] args = arguments.ToArray(); - if(sender is PlayerCommandSender p) - { - Player player = Player.Get(p.Processor._hub); + //if(sender is PlayerCommandSender p) + //{ + //Player player = Player.Get(p.Processor._hub); if (args.Length != 1) { response = Buddy.singleton.invalidUsage; @@ -37,7 +37,7 @@ public bool Execute(ArraySegment arguments, ICommandSender sender, out s Log.Error(e.ToString()); response = Buddy.singleton.errorMessage; } - } + //} return true; } diff --git a/CommandHandler.cs b/CommandHandler.cs new file mode 100644 index 0000000..ad43614 --- /dev/null +++ b/CommandHandler.cs @@ -0,0 +1,34 @@ +using CommandSystem; +using Exiled.Events.EventArgs; +using RemoteAdmin; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Buddy +{ + class CommandHandler + { + private static Dictionary commands = new Dictionary(); + + public static bool isEnabled = false; + + public static void Register(ICommandPint cmd) + { + string commandStr = cmd.Command; + commands.Add(commandStr, cmd); + } + + public static void RunCommand(SendingConsoleCommandEventArgs ev) + { + if (!isEnabled) return; + ICommandPint cmd = null; + if (!commands.TryGetValue(ev.Name, out cmd) || cmd == null) return; + string outText = ev.ReturnMessage; + cmd.Execute(new ArraySegment(ev.Arguments.ToArray()), ev.Player, out outText); + ev.ReturnMessage = outText; + } + } +} diff --git a/EventHandlers.cs b/EventHandlers.cs index 779946c..18ab32a 100644 --- a/EventHandlers.cs +++ b/EventHandlers.cs @@ -181,5 +181,10 @@ private IEnumerator doTheSCPThing() } } + + public void OnConsoleCommand(SendingConsoleCommandEventArgs ev) + { + CommandHandler.RunCommand(ev); + } } } diff --git a/ICommandPint.cs b/ICommandPint.cs new file mode 100644 index 0000000..5f91078 --- /dev/null +++ b/ICommandPint.cs @@ -0,0 +1,20 @@ +using Exiled.API.Features; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Buddy +{ + interface ICommandPint + { + string Command { get; } + + string[] Aliases { get; } + + string Description { get; } + + bool Execute(ArraySegment arguments, Player sender, out string response); + } +} diff --git a/UnBuddyCommand.cs b/UnBuddyCommand.cs index 15c326a..72ec4e0 100644 --- a/UnBuddyCommand.cs +++ b/UnBuddyCommand.cs @@ -6,25 +6,25 @@ namespace Buddy { - [CommandHandler(typeof(GameConsoleCommandHandler))] - class UnBuddyCommand : ICommand + //[CommandHandler(typeof(GameConsoleCommandHandler))] + class UnBuddyCommand : ICommandPint { public string Command => Buddy.singleton.buddyUnbuddyCommand; - public string[] Aliases => new string[] { }; + public string[] Aliases => null; public string Description => "A command to remove your current buddy."; - public bool Execute(ArraySegment arguments, ICommandSender sender, out string response) + public bool Execute(ArraySegment arguments, /*ICommandSender sender*/ Player player, out string response) { response = ""; string[] args = arguments.ToArray(); - if (sender is PlayerCommandSender p) - { - Player player = Player.Get(p.Processor._hub); + //if (sender is PlayerCommandSender p) + //{ + //Player player = Player.Get(p.Processor._hub); response = handleUnBuddyCommand(player); - return true; - } + //return true; + //} return true; }