C#:
using Oxide.Core;
using Oxide.Core.Plugins;
using Oxide.Core.Libraries.Covalence;
using System.Collections.Generic;
using System.Linq;
using System;
namespace Oxide.Plugins
{
[Info("AutoUnload", "TheGhostOfChaos", "1.0.1")]
[Description("Automatically unloads specified plugins when this plugin is loaded and on a timer.")]
public class AutoUnload : CovalencePlugin
{
#region Configuration
private ConfigData config;
private class ConfigData
{
public List<string> PluginsToUnload { get; set; } = new List<string> { "ExamplePlugin1", "ExamplePlugin2" };
public bool Enabled { get; set; } = true;
public int UnloadTimerInMinutes { get; set; } = 15;
}
protected override void LoadDefaultConfig() => config = new ConfigData();
protected override void LoadConfig()
{
base.LoadConfig();
try
{
config = Config.ReadObject<ConfigData>();
if (config?.PluginsToUnload == null)
throw new Exception();
if (config.UnloadTimerInMinutes < 1)
{
PrintWarning("UnloadTimerInMinutes in config is less than 1, resetting to 15 minutes.");
config.UnloadTimerInMinutes = 15;
SaveConfig();
}
}
catch
{
PrintWarning("Creating new config file...");
LoadDefaultConfig();
SaveConfig();
}
}
protected override void SaveConfig() => Config.WriteObject(config);
#endregion
#region Initialization
private const string AdminPerm = "autounload.admin";
private const string ChatPrefix = "<color=orange>[Auto Unload]</color><color=yellow>: </color>";
private Timer _timer;
private void Init()
{
permission.RegisterPermission(AdminPerm, this);
}
private void OnServerInitialized()
{
if (config.Enabled)
{
UnloadConfiguredPlugins();
StartUnloadCheckTimer();
}
}
private void UnloadConfiguredPlugins()
{
foreach (string pluginName in config.PluginsToUnload)
{
var foundPlugin = plugins.Find(pluginName);
if (foundPlugin != null && foundPlugin.IsLoaded)
{
Puts($"[AutoUnload] Unloading plugin: {pluginName}");
SendToAdmins($"{ChatPrefix}<color=yellow>Unloading plugin <color=orange>{pluginName}</color>.</color>");
Interface.Oxide.UnloadPlugin(pluginName);
}
}
}
private void StartUnloadCheckTimer()
{
_timer?.Destroy();
float intervalSeconds = config.UnloadTimerInMinutes * 60f;
_timer = timer.Every(intervalSeconds, () =>
{
if (!config.Enabled) return;
foreach (var pluginName in config.PluginsToUnload)
{
var plugin = plugins.Find(pluginName);
if (plugin != null && plugin.IsLoaded)
{
Puts($"[AutoUnload] Timer check: unloading plugin {pluginName}");
SendToAdmins($"{ChatPrefix}<color=yellow>Timer check: unloading plugin <color=orange>{pluginName}</color>.</color>");
Interface.Oxide.UnloadPlugin(pluginName);
}
}
});
}
private void StopUnloadCheckTimer()
{
_timer?.Destroy();
_timer = null;
}
private void SendToAdmins(string message)
{
foreach (var player in players.Connected)
{
if (player.HasPermission(AdminPerm))
player.Message(message);
}
}
#endregion
#region Commands
[Command("autounload.enable")]
private void EnableCommand(IPlayer player, string command, string[] args)
{
if (!player.HasPermission(AdminPerm))
{
player.Reply($"{ChatPrefix}<color=yellow>You do not have permission to use this command.</color>");
return;
}
if (config.Enabled)
{
player.Reply($"{ChatPrefix}<color=yellow>AutoUnload is already enabled.</color>");
return;
}
config.Enabled = true;
SaveConfig();
player.Reply($"{ChatPrefix}<color=yellow>AutoUnload has been <color=green>enabled</color>.</color>");
UnloadConfiguredPlugins();
StartUnloadCheckTimer();
}
[Command("autounload.disable")]
private void DisableCommand(IPlayer player, string command, string[] args)
{
if (!player.HasPermission(AdminPerm))
{
player.Reply($"{ChatPrefix}<color=yellow>You do not have permission to use this command.</color>");
return;
}
if (!config.Enabled)
{
player.Reply($"{ChatPrefix}<color=yellow>AutoUnload is already disabled.</color>");
return;
}
config.Enabled = false;
SaveConfig();
player.Reply($"{ChatPrefix}<color=yellow>AutoUnload has been <color=red>disabled</color>.</color>");
StopUnloadCheckTimer();
}
[Command("autounload.check")]
private void CheckCommand(IPlayer player, string command, string[] args)
{
if (!player.HasPermission(AdminPerm))
{
player.Reply($"{ChatPrefix}<color=yellow>You do not have permission to use this command.</color>");
return;
}
if (config.PluginsToUnload.Count == 0)
{
player.Reply($"{ChatPrefix}<color=yellow>No plugins are configured to be auto-unloaded.</color>");
return;
}
player.Reply($"{ChatPrefix}<color=yellow>Plugins set to unload:</color>");
foreach (var pluginName in config.PluginsToUnload)
{
player.Reply(pluginName);
}
}
[Command("autounload.help")]
private void HelpCommand(IPlayer player, string command, string[] args)
{
if (!player.HasPermission(AdminPerm))
{
player.Reply($"{ChatPrefix}<color=yellow>You do not have permission to use this command.</color>");
return;
}
player.Reply($"{ChatPrefix}<color=yellow>Available Commands:</color>");
player.Reply("<color=yellow>- <color=orange>/autounload.enable</color>: Enable automatic unloading</color>");
player.Reply("<color=yellow>- <color=orange>/autounload.disable</color>: Disable automatic unloading</color>");
player.Reply("<color=yellow>- <color=orange>/autounload.check</color>: Show plugins set to auto-unload</color>");
player.Reply("<color=yellow>- <color=orange>/autounload.add <pluginname></color>: Add a plugin to auto-unload list</color>");
player.Reply("<color=yellow>- <color=orange>/autounload.remove <pluginname></color>: Remove a plugin from auto-unload list</color>");
player.Reply("<color=yellow>- <color=orange>/autounload.help</color>: Show this help message</color>");
}
[Command("autounload.add")]
private void AddCommand(IPlayer player, string command, string[] args)
{
if (!player.HasPermission(AdminPerm))
{
player.Reply($"{ChatPrefix}<color=yellow>You do not have permission to use this command.</color>");
return;
}
if (args.Length == 0)
{
player.Reply($"{ChatPrefix}<color=yellow>Please specify a plugin name to add.</color>");
return;
}
string pluginName = args[0];
if (config.PluginsToUnload.Contains(pluginName, StringComparer.OrdinalIgnoreCase))
{
player.Reply($"{ChatPrefix}<color=yellow>Plugin <color=orange>{pluginName}</color> is already in the unload list.</color>");
return;
}
config.PluginsToUnload.Add(pluginName);
SaveConfig();
player.Reply($"{ChatPrefix}<color=yellow>Plugin <color=orange>{pluginName}</color> added to unload list.</color>");
}
[Command("autounload.remove")]
private void RemoveCommand(IPlayer player, string command, string[] args)
{
if (!player.HasPermission(AdminPerm))
{
player.Reply($"{ChatPrefix}<color=yellow>You do not have permission to use this command.</color>");
return;
}
if (args.Length == 0)
{
player.Reply($"{ChatPrefix}<color=yellow>Please specify a plugin name to remove.</color>");
return;
}
string pluginName = args[0];
var existing = config.PluginsToUnload.FirstOrDefault(p => p.Equals(pluginName, StringComparison.OrdinalIgnoreCase));
if (existing == null)
{
player.Reply($"{ChatPrefix}<color=yellow>Plugin <color=orange>{pluginName}</color> is not in the unload list.</color>");
return;
}
config.PluginsToUnload.Remove(existing);
SaveConfig();
player.Reply($"{ChatPrefix}<color=yellow>Plugin <color=orange>{pluginName}</color> removed from unload list.</color>");
}
#endregion
#region Unload cleanup
private void Unload()
{
StopUnloadCheckTimer();
}
#endregion
}
}