From 36f8c65da2b47ce4e1bcf2999ca71686ddb36132 Mon Sep 17 00:00:00 2001
From: xNexusACS <83370388+xNexusACS@users.noreply.github.com>
Date: Fri, 13 Sep 2024 21:43:58 +0200
Subject: [PATCH] Starting Custom Teams

---
 .../API/Commands/CustomTeams/Parent.cs        | 51 +++++++++++
 .../API/Commands/CustomTeams/Spawn.cs         | 84 +++++++++++++++++++
 2 files changed, 135 insertions(+)
 create mode 100644 Exiled.CustomModules/API/Commands/CustomTeams/Parent.cs
 create mode 100644 Exiled.CustomModules/API/Commands/CustomTeams/Spawn.cs

diff --git a/Exiled.CustomModules/API/Commands/CustomTeams/Parent.cs b/Exiled.CustomModules/API/Commands/CustomTeams/Parent.cs
new file mode 100644
index 0000000000..f2acbbda93
--- /dev/null
+++ b/Exiled.CustomModules/API/Commands/CustomTeams/Parent.cs
@@ -0,0 +1,51 @@
+// -----------------------------------------------------------------------
+// <copyright file="Parent.cs" company="Exiled Team">
+// Copyright (c) Exiled Team. All rights reserved.
+// Licensed under the CC BY-SA 3.0 license.
+// </copyright>
+// -----------------------------------------------------------------------
+
+namespace Exiled.CustomModules.API.Commands.CustomTeams
+{
+    using System;
+
+    using CommandSystem;
+
+    /// <summary>
+    /// The main parent command for custom teams.
+    /// </summary>
+    [CommandHandler(typeof(RemoteAdminCommandHandler))]
+    [CommandHandler(typeof(GameConsoleCommandHandler))]
+    internal sealed class Parent : ParentCommand
+    {
+        /// <summary>
+        /// Initializes a new instance of the <see cref="Parent"/> class.
+        /// </summary>
+        public Parent()
+        {
+            LoadGeneratedCommands();
+        }
+
+        /// <inheritdoc/>
+        public override string Command { get; } = "teams";
+
+        /// <inheritdoc/>
+        public override string[] Aliases { get; } = { "tms" };
+
+        /// <inheritdoc/>
+        public override string Description { get; } = string.Empty;
+
+        /// <inheritdoc/>
+        public override void LoadGeneratedCommands()
+        {
+            RegisterCommand(Spawn.Instance);
+        }
+
+        /// <inheritdoc/>
+        protected override bool ExecuteParent(ArraySegment<string> arguments, ICommandSender sender, out string response)
+        {
+            response = "Invalid subcommand! Available: ";
+            return false;
+        }
+    }
+}
\ No newline at end of file
diff --git a/Exiled.CustomModules/API/Commands/CustomTeams/Spawn.cs b/Exiled.CustomModules/API/Commands/CustomTeams/Spawn.cs
new file mode 100644
index 0000000000..b49113f1a3
--- /dev/null
+++ b/Exiled.CustomModules/API/Commands/CustomTeams/Spawn.cs
@@ -0,0 +1,84 @@
+// -----------------------------------------------------------------------
+// <copyright file="Spawn.cs" company="Exiled Team">
+// Copyright (c) Exiled Team. All rights reserved.
+// Licensed under the CC BY-SA 3.0 license.
+// </copyright>
+// -----------------------------------------------------------------------
+
+namespace Exiled.CustomModules.API.Commands.CustomTeams
+{
+    using System;
+    using System.Linq;
+
+    using CommandSystem;
+    using Exiled.API.Features;
+    using Exiled.CustomModules.API.Features.CustomRoles;
+    using Exiled.Permissions.Extensions;
+    using PlayerRoles;
+
+    /// <summary>
+    /// The command to spawn a custom team.
+    /// </summary>
+    internal sealed class Spawn : ICommand
+    {
+        private Spawn()
+        {
+        }
+
+        /// <summary>
+        /// Gets the <see cref="Spawn"/> instance.
+        /// </summary>
+        public static Spawn Instance { get; } = new();
+
+        /// <inheritdoc/>
+        public string Command { get; } = "spawn";
+
+        /// <inheritdoc/>
+        public string[] Aliases { get; } = { "s" };
+
+        /// <inheritdoc/>
+        public string Description { get; } = "Spawns the specified custom team.";
+
+        /// <inheritdoc/>
+        public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out string response)
+        {
+            try
+            {
+                if (!sender.CheckPermission("customteams.spawn"))
+                {
+                    response = "Permission denied, customteams.spawn is required.";
+                    return false;
+                }
+
+                if (arguments.Count < 1)
+                {
+                    response = "spawn <Custom Team ID>";
+                    return false;
+                }
+
+                if (!CustomTeam.TryGet(arguments.At(0), out CustomTeam team) && (!uint.TryParse(arguments.At(0), out uint id) || !CustomTeam.TryGet(id, out team)) && team is null)
+                {
+                    response = $"Custom Team {arguments.At(0)} not found!";
+                    return false;
+                }
+
+                if (!Player.Get(Team.Dead).Any())
+                {
+                    response = "There are no dead players to spawn the custom team.";
+                    return false;
+                }
+
+                team.Respawn(true);
+
+                response = $"Custom Team {team.Name} has been spawned.";
+                return true;
+            }
+            catch (Exception ex)
+            {
+                Log.Error(ex);
+                response = "An error occurred when executing the command, check server console for more details.";
+                return false;
+            }
+        }
+    }
+}
\ No newline at end of file