C#:
using System;
using System.Collections.Generic;
using Oxide.Core;
using Oxide.Core.Libraries.Covalence;
using Oxide.Core.Plugins;
namespace Oxide.Plugins
{
[Info("Vip Manager", "AnotherPanda", "1.0.0")]
public class VipManager : CovalencePlugin
{
#region Configuration
private PluginConfig config;
private class PluginConfig
{
public string VipGroupName = "vip";
public int DefaultVipDays = 30;
public float CheckIntervalMinutes = 10f;
}
protected override void LoadConfig()
{
base.LoadConfig();
try
{
config = Config.ReadObject<PluginConfig>();
if (config == null)
throw new Exception("Config is null or invalid.");
}
catch
{
PrintWarning("Could not read config. Creating a new one with default values...");
LoadDefaultConfig();
}
}
protected override void LoadDefaultConfig()
{
config = new PluginConfig();
SaveConfig();
PrintWarning("New default config file created.");
}
protected override void SaveConfig() => Config.WriteObject(config, true);
#endregion
#region Data
private StoredData storedData;
private class StoredData
{
public Dictionary<ulong, double> VipUsers = new Dictionary<ulong, double>();
}
private void LoadVipData()
{
try
{
storedData = Interface.Oxide.DataFileSystem.ReadObject<StoredData>(Name);
if (storedData == null || storedData.VipUsers == null)
storedData = new StoredData();
}
catch
{
storedData = new StoredData();
}
}
private void SaveVipData() =>
Interface.Oxide.DataFileSystem.WriteObject(Name, storedData);
#endregion
#region Hooks
private void Init()
{
LoadVipData();
}
private void OnServerInitialized()
{
CheckExpiredVips();
timer.Every(config.CheckIntervalMinutes * 60f, CheckExpiredVips);
}
#endregion
#region Core Logic
private void CheckExpiredVips()
{
LoadVipData();
if (storedData?.VipUsers == null || storedData.VipUsers.Count == 0)
return;
double now = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
var expired = new List<ulong>();
foreach (var entry in storedData.VipUsers)
{
if (entry.Value <= now)
expired.Add(entry.Key);
}
if (expired.Count == 0)
return;
foreach (ulong userId in expired)
{
// Remove VIP group
permission.RemoveUserGroup(userId.ToString(), config.VipGroupName);
storedData.VipUsers.Remove(userId);
Puts($"VipManager: Removed expired VIP from user {userId}.");
}
SaveVipData();
}
private void InternalGrantVip(ulong userId, int days)
{
if (storedData == null)
LoadVipData();
if (days <= 0)
days = config.DefaultVipDays;
double now = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
double expiry = now + (days * 86400);
// Save / update expiration time
storedData.VipUsers[userId] = expiry;
SaveVipData();
// Add user to VIP group
permission.AddUserGroup(userId.ToString(), config.VipGroupName);
Puts($"VipManager: VIP granted to {userId} for {days} day(s). Expires at unix: {expiry}.");
}
private DateTime UnixToLocal(double unixTime)
{
var dt = DateTimeOffset.FromUnixTimeSeconds((long)unixTime).ToLocalTime();
return dt.DateTime;
}
#endregion
#region Public API
[HookMethod("GrantVip")]
public object GrantVip(ulong userId, int days)
{
try
{
InternalGrantVip(userId, days);
return true;
}
catch (Exception ex)
{
PrintError($"VipManager: Error while granting VIP to {userId}: {ex}");
return false;
}
}
[HookMethod("GetVipExpiryUnix")]
public object GetVipExpiryUnix(ulong userId)
{
if (storedData == null)
LoadVipData();
if (storedData.VipUsers == null)
return null;
double expiry;
if (!storedData.VipUsers.TryGetValue(userId, out expiry))
return null;
return expiry; // Unix time (seconds)
}
#endregion
}
}