C#:
using Newtonsoft.Json;
using Oxide.Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
/*v1.2.6:
* Fixed not removing players.
* Code clean-up and work.
*/
/*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.6")]
[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 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
#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() { return JsonConvert.SerializeObject(this); }
public Dictionary<string, object> ToDictionary() { return JsonConvert.DeserializeObject<Dictionary<string, object>>(ToJson()); }
}
protected override void LoadDefaultConfig()
{
configData = new ConfigData();
}
protected override void LoadConfig()
{
base.LoadConfig();
try
{
configData = Config.ReadObject<ConfigData>();
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
#region Data
private StoredData storedData;
private class StoredData
{
public List<string> RemovedFromGroup = new List<string>();
}
private void Loaded()
{
storedData = Interface.Oxide.DataFileSystem.ReadObject<StoredData>("ClearGroups") ?? new StoredData();
if (storedData.RemovedFromGroup == null)
{
storedData.RemovedFromGroup = new List<string>();
}
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
#region Oxide
private void Init()
{
permission.RegisterPermission("cleargroups.admin", this);
}
#endregion
#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;
int removedCount = 0;
HashSet<string> uniqueUsersRemoved = new HashSet<string>();
if (player != null)
{
storedData.RemovedFromGroup.Add(player.displayName + " " + PlayerHasRemoved);
}
foreach (string group in groupsList)
{
if (!permission.GroupExists(group))
{
continue;
}
string[] usersInGroup = permission.GetUsersInGroup(group);
if (usersInGroup == null || usersInGroup.Length == 0)
{
continue;
}
foreach (string userID in usersInGroup.ToList())
{
var match = Regex.Match(userID, "(\\d{17})");
if (!match.Success)
{
continue;
}
var cleanId = match.Groups[1].Value;
if (configData.MainOptions.iadmin && permission.UserHasPermission(cleanId, "cleargroups.admin"))
{
continue;
}
permission.RemoveUserGroup(cleanId, group);
removedCount++;
uniqueUsersRemoved.Add(cleanId);
storedData.RemovedFromGroup.Add(cleanId + " was removed from group '" + group + "'");
}
}
if (removedCount == 0)
{
if (player != null)
{
SendReply(player, NoPlayersRemoved);
}
}
else
{
int usersCount = uniqueUsersRemoved.Count;
string output = usersCount + " player" + (usersCount == 1 ? " was" : "s were") + " removed from " + removedCount + " group" + (removedCount == 1 ? "" : "s");
if (player != null)
{
storedData.RemovedFromGroup.Add(player.displayName + " removed " + output);
SendReply(player, player.displayName + " removed " + output);
Puts(player.displayName + " (" + player.UserIDString + ") removed " + output);
}
else
{
Puts("Removed " + output);
}
SaveData();
}
}
[ConsoleCommand("cg")]
private void ConsoleClearGroupsCommand(ConsoleSystem.Arg arg)
{
if (arg.Connection != null && arg.Connection.authLevel < 2)
{
return;
}
RemovePlayers(null, null, null);
}
[ConsoleCommand("cgd")]
private void ConsoleEmptyDataFileCommand(ConsoleSystem.Arg arg)
{
if (arg.Connection != null && arg.Connection.authLevel < 2)
{
return;
}
ClearGroupData(null, null, null);
}
private void ClearGroupData(BasePlayer player, string command, string[] args)
{
if (player != null && !permission.UserHasPermission(player.UserIDString, "cleargroups.admin"))
{
SendReply(player, NoPerm);
return;
}
if (storedData.RemovedFromGroup == null || storedData.RemovedFromGroup.Count == 0 ||
(storedData.RemovedFromGroup.Count == 1 && storedData.RemovedFromGroup[0] == ""))
{
if (player != null)
{
SendReply(player, DataIsEmpty);
}
else
{
Puts(DataIsEmpty);
}
return;
}
storedData.RemovedFromGroup.Clear();
SaveData();
if (player != null)
{
SendReply(player, PlayerDataCleared);
}
else
{
Puts(PlayerDataCleared);
}
}
#endregion
}
}