using Newtonsoft.Json; using Oxide.Core; using System; using System.Collections.Generic; using System.IO; using System.Linq; /*v1.2.5: * Fixed a few broken things. */ /*v1.2.4: * Fixed a few broken things. */ /*v1.2.3: * Added console command "cg" to clear the groups. * Added console command "cgd" to clear the data file. */ /*v1.2.2: * Organized config. * Organized code. * Some changes to removing players. * Removed redundant .use permission. * Changed default commands. */ namespace Oxide.Plugins { [Info("Clear Groups", "Gt403cyl2", "1.2.4")] [Description("Removes players from specified groups on command.")] public class ClearGroups : RustPlugin { #region Constants private const string NoPerm = "You do not have permission to clear the data file."; private const string PlayerDataCleared = "cleared the Clear Groups data file."; private const string DataIsEmpty = "There is no data to clear."; private const string PlayersRemoved = "players from the specified groups"; private const string NoPlayersRemoved = "No players were removed from any of the specified groups."; private const string PlayerHasRemoved = "has removed the following players from the specified groups."; #endregion Constants #region config private ConfigData configData; private class ConfigData : BaseConfig { [JsonProperty("Change Commands")] public Commands Commands = new Commands(); [JsonProperty("Main Options")] public MainOptions MainOptions = new MainOptions(); [JsonProperty(PropertyName = "Group Names")] public string[] groupsList = new string[] { "", "" }; } private class BaseConfig { public string ToJson() => JsonConvert.SerializeObject(this); public Dictionary ToDictionary() => JsonConvert.DeserializeObject>(ToJson()); } protected override void LoadDefaultConfig() => configData = new ConfigData(); protected override void LoadConfig() { base.LoadConfig(); try { configData = Config.ReadObject(); if (!configData.ToDictionary().Keys.SequenceEqual(Config.ToDictionary(x => x.Key, x => x.Value).Keys)) { PrintWarning($"{Name} Configuration appears to be outdated; updating and saving"); SaveConfig(); } } catch (FileNotFoundException) { PrintWarning($"No {Name} configuration file found, creating default"); LoadDefaultConfig(); SaveConfig(); } catch (JsonReaderException) { PrintError($"{Name} Configuration file contains invalid JSON, creating default"); } } protected override void SaveConfig() { PrintWarning($"Configuration changes saved to {Name}.json"); Config.WriteObject(configData, true); } private class Commands { [JsonProperty(PropertyName = "Manually clear groups")] public string ClearGroupsCommand = "cleargroups"; [JsonProperty(PropertyName = "Delete clear groups data")] public string EmptyDataFile = "cleargroupsdata"; } private class MainOptions { [JsonProperty(PropertyName = "Ignore Admin")] public bool iadmin = true; [JsonProperty(PropertyName = "Clear Data on wipe")] public bool wipeclr = false; } #endregion config #region Data private StoredData storedData; private class StoredData { public List RemovedFromGroup = new List(); } private void Loaded() { storedData = Interface.Oxide.DataFileSystem.ReadObject("ClearGroups"); if (storedData == null) { storedData = new StoredData(); SaveData(); } else if (storedData.RemovedFromGroup == null) { storedData.RemovedFromGroup = new List(); SaveData(); } cmd.AddChatCommand(configData.Commands.ClearGroupsCommand, this, RemovePlayers); cmd.AddChatCommand(configData.Commands.EmptyDataFile, this, ClearGroupData); } private void SaveData() { Interface.Oxide.DataFileSystem.WriteObject("ClearGroups", storedData); } private void OnNewSave(string filename) { if (configData.MainOptions.wipeclr) { storedData.RemovedFromGroup.Clear(); SaveData(); Puts("Wipe Detected, The Clear Groups data file has been cleared."); } } #endregion Data #region Oxide private void Init() { permission.RegisterPermission("cleargroups.admin", this); } #endregion Oxide #region Commands private void RemovePlayers(BasePlayer player, string arg2, string[] arg3) { if (player != null && !permission.UserHasPermission(player.UserIDString, "cleargroups.admin")) { SendReply(player, $"{NoPerm}"); return; } string[] GroupsList = configData.groupsList; string DefaultGroup = "default"; int removedCount = 0; if (player != null) { storedData.RemovedFromGroup.Add($"{player.displayName} {PlayerHasRemoved}"); } foreach (var group in GroupsList) { if (permission.GroupExists(group)) { var usersInGroup = permission.GetUsersInGroup(group).ToList(); foreach (var userID in usersInGroup) { if (userID.IsSteamId() && (!configData.MainOptions.iadmin || !permission.UserHasPermission(userID, "cleargroups.admin"))) { Puts($"Removing {userID} from group {group}"); removedCount++; permission.RemoveUserGroup(userID, group); storedData.RemovedFromGroup.Add($"{userID} was removed from the group {group}"); } } } } if (removedCount == 0) { if (player != null) { SendReply(player, $"{NoPlayersRemoved}"); } } else { if (player != null) { storedData.RemovedFromGroup.Add($"{player.displayName} Removed {removedCount} {PlayersRemoved}"); SaveData(); Puts($"{player.displayName} ({player.UserIDString}) Removed {removedCount} {PlayersRemoved}"); SendReply(player, $"{player.displayName} Removed {removedCount} {PlayersRemoved}"); } else { Puts($"Removed {removedCount} {PlayersRemoved}"); } } } private void ClearGroupData(BasePlayer player, string command, string[] args) { if (player != null && !permission.UserHasPermission(player.UserIDString, "cleargroups.admin")) { SendReply(player, $"{NoPerm}"); return; } if (player != null) { if (storedData.RemovedFromGroup.IsNullOrEmpty() || (storedData.RemovedFromGroup.Count == 1 && storedData.RemovedFromGroup[0] == "")) { SendReply(player, $"{DataIsEmpty}"); return; } else { storedData.RemovedFromGroup.Clear(); SaveData(); SendReply(player, $"{PlayerDataCleared}"); } } else { if (storedData.RemovedFromGroup.IsNullOrEmpty() || (storedData.RemovedFromGroup.Count == 1 && storedData.RemovedFromGroup[0] == "")) { Puts($"{DataIsEmpty}"); return; } else { storedData.RemovedFromGroup.Clear(); SaveData(); Puts($"{PlayerDataCleared}"); } } } [ConsoleCommand("cg")] private void ConsoleClearGroupsCommand(ConsoleSystem.Arg arg) { if (arg.Connection != null && arg.Connection.authLevel < 2) return; RemovePlayers(null, null, null); var removedCount = storedData.RemovedFromGroup.Count; if (removedCount > 1) { Puts($"Removed {removedCount} {PlayersRemoved}"); } else if (removedCount == 1) { Puts($"{PlayersRemoved}: {storedData.RemovedFromGroup[0]}"); } else { Puts(NoPlayersRemoved); } } [ConsoleCommand("cgd")] private void ConsoleEmptyDataFileCommand(ConsoleSystem.Arg arg) { if (arg.Connection != null && arg.Connection.authLevel < 2) return; ClearGroupData(null, null, null); } #endregion Commands } }