using Oxide.Core.Plugins; using Oxide.Core.Libraries.Covalence; using Oxide.Core; using Oxide.Core.Configuration; using System.Collections.Generic; using System.Linq; using System; using Newtonsoft.Json; namespace Oxide.Plugins { [Info("AdminTeamManager", "HardRock", "1.0.4")] [Description("Allows admins to manage players and teams via commands.")] public class AdminTeamManager : CovalencePlugin { private const string permissionUse = "adminteammanager.use"; private int maxTeamSize = 8; private StoredData storedData; private DynamicConfigFile dataFile; #region Hooks and Initialization private void Init() { permission.RegisterPermission(permissionUse, this); dataFile = Interface.Oxide.DataFileSystem.GetFile("AdminTeamManager_Data"); LoadData(); LoadConfig(); } protected override void LoadDefaultConfig() { Config["MaxTeamSize"] = maxTeamSize; SaveConfig(); } private void LoadConfig() { maxTeamSize = Convert.ToInt32(Config["MaxTeamSize"] ?? maxTeamSize); } private void OnServerInitialized() { ScanAllPlayers(); } private void OnPlayerConnected(BasePlayer player) { UpdatePlayerData(player); } #endregion #region Data Management private class StoredData { public Dictionary PlayerLookup = new Dictionary(); } private void LoadData() { try { storedData = dataFile.Exists() ? dataFile.ReadObject() : new StoredData(); } catch { storedData = new StoredData(); } } private void SaveData() { dataFile.WriteObject(storedData); } private void UpdatePlayerData(BasePlayer player) { if (!storedData.PlayerLookup.ContainsKey(player.displayName)) { storedData.PlayerLookup[player.displayName] = player.userID; SaveData(); } } private void ScanAllPlayers() { int addedCount = 0; foreach (var player in BasePlayer.activePlayerList) { if (!storedData.PlayerLookup.ContainsKey(player.displayName)) { storedData.PlayerLookup[player.displayName] = player.userID; addedCount++; } } foreach (var sleeper in BasePlayer.sleepingPlayerList) { if (!storedData.PlayerLookup.ContainsKey(sleeper.displayName)) { storedData.PlayerLookup[sleeper.displayName] = sleeper.userID; addedCount++; } } SaveData(); Puts($"Scanned all players and sleepers. Added {addedCount} new entries to the data file."); } private ulong? FindPlayerByName(string name) { var matches = storedData.PlayerLookup .Where(kvp => kvp.Key.IndexOf(name, StringComparison.OrdinalIgnoreCase) >= 0) .ToList(); if (matches.Count == 0) { return null; } if (matches.Count > 1) { PrintWarning($"Multiple players found matching '{name}': {string.Join(", ", matches.Select(m => m.Key))}"); return null; } return matches.First().Value; } #endregion #region Utility Functions private bool HasPermissionAndValidArgs(IPlayer player, string[] args, int requiredArgs, string usageMessage) { if (!player.HasPermission(permissionUse)) { player.Reply("You do not have permission to use this command."); return false; } if (args.Length != requiredArgs) { player.Reply(usageMessage); return false; } return true; } private BasePlayer GetPlayerByNameOrID(string input) { ulong? playerID = null; if (ulong.TryParse(input, out ulong id)) { playerID = id; } else { playerID = FindPlayerByName(input); } return playerID != null ? BasePlayer.FindAwakeOrSleeping(playerID.ToString()) : null; } #endregion #region Commands [Command("addteam", "Add a player to another player's team")] private void AddTeamCommand(IPlayer admin, string command, string[] args) { if (!HasPermissionAndValidArgs(admin, args, 2, "Usage: /addteam ")) return; BasePlayer teamOwner = GetPlayerByNameOrID(args[0]); BasePlayer playerToAdd = GetPlayerByNameOrID(args[1]); if (teamOwner == null || playerToAdd == null) { admin.Reply("One or both players were not found."); return; } if (teamOwner.currentTeam == 0) { admin.Reply($"{teamOwner.displayName} does not have a team."); return; } var team = RelationshipManager.ServerInstance?.FindTeam(teamOwner.currentTeam); if (team == null || team.members.Contains(playerToAdd.userID)) { admin.Reply($"{playerToAdd.displayName} is already in {teamOwner.displayName}'s team."); return; } if (team.members.Count >= maxTeamSize) { admin.Reply($"{teamOwner.displayName}'s team is full."); return; } team.AddPlayer(playerToAdd); admin.Reply($"{playerToAdd.displayName} has been added to {teamOwner.displayName}'s team."); } [Command("removeplayer", "Remove a player from a team")] private void RemoveTeamPlayerCommand(IPlayer admin, string command, string[] args) { if (!HasPermissionAndValidArgs(admin, args, 2, "Usage: /removeplayer ")) return; BasePlayer teamLeader = GetPlayerByNameOrID(args[0]); BasePlayer playerToRemove = GetPlayerByNameOrID(args[1]); if (teamLeader == null || playerToRemove == null) { admin.Reply("One or both players were not found."); return; } var team = RelationshipManager.ServerInstance?.FindTeam(teamLeader.currentTeam); if (team == null || !team.members.Contains(playerToRemove.userID)) { admin.Reply($"{playerToRemove.displayName} is not in {teamLeader.displayName}'s team."); return; } team.RemovePlayer(playerToRemove.userID); admin.Reply($"{playerToRemove.displayName} has been removed from the team."); } [Command("changeleader", "Change the leader of a team")] private void ChangeLeaderCommand(IPlayer admin, string command, string[] args) { if (!HasPermissionAndValidArgs(admin, args, 2, "Usage: /changeleader ")) return; BasePlayer currentLeader = GetPlayerByNameOrID(args[0]); BasePlayer newLeader = GetPlayerByNameOrID(args[1]); if (currentLeader == null || newLeader == null) { admin.Reply("One or both players were not found."); return; } var team = RelationshipManager.ServerInstance?.FindTeam(currentLeader.currentTeam); if (team == null) { admin.Reply($"{currentLeader.displayName} does not have a team."); return; } if (!team.members.Contains(newLeader.userID)) { admin.Reply($"{newLeader.displayName} is not a member of {currentLeader.displayName}'s team."); return; } team.SetTeamLeader(newLeader.userID); admin.Reply($"Team leadership has been transferred from {currentLeader.displayName} to {newLeader.displayName}."); } [Command("scanplayers", "Scan all players and sleepers to update the data file")] private void ScanPlayersCommand(IPlayer admin, string command, string[] args) { if (!admin.HasPermission(permissionUse)) { admin.Reply("You do not have permission to use this command."); return; } ScanAllPlayers(); admin.Reply("Scanned all active players and sleepers. Data file has been updated."); } #endregion } }