C#:
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using Newtonsoft.Json;
using Oxide.Core;
// https://soken.dev
// https://github.com/LewdNeko
// Neko#0013
// https://codefling.com/files/file/385-wipe-settings/
namespace Oxide.Plugins
{
[Info("WipeSettings", "Kitty", "1.0.0")]
[Description("A simple plugin to set your hostname & description when your server wipes.")]
public class WipeSettings : RustPlugin
{
#region Decs
private Configuration _Config;
private Data _Data;
private Timer _Loop;
#endregion
#region Config
private class Configuration
{
[JsonProperty("Date Format (dd, mm)")] public string DateFormat = "dd/mm";
[JsonProperty("How many days counts as 'Just Wiped'?")] public int JustWipedDays = 1;
[JsonProperty("What text should be added if the server is 'Just Wiped'?")] public string JustWipedText = "JUST WIPED";
[JsonProperty("Hostname ({wipe} goes where you want the wipe text, {just} goes where JUST WIPED should go)")] public string Hostname = "My Server | Active Admins | {wipe}";
[JsonProperty("Description ({wipe} goes where you want the wipe text, 'Just Wiped' will not be added here)")] public string Description = "Welcome to XXYY, join our discord: discord.gg/abcdef\nLast wipe was on {wipe}";
[JsonProperty("Hostname Update Interval (seconds)")] public int Interval = 3600;
private string ToJson() => JsonConvert.SerializeObject(this);
public Dictionary<string, object> ToDictionary() => JsonConvert.DeserializeObject<Dictionary<string, object>>(ToJson());
}
protected override void LoadDefaultConfig() => _Config = new Configuration();
protected override void LoadConfig()
{
base.LoadConfig();
try
{
_Config = Config.ReadObject<Configuration>();
if (_Config == null)
{
throw new JsonException();
}
if (!_Config.ToDictionary().Keys.SequenceEqual(Config.ToDictionary(x => x.Key, x => x.Value).Keys))
{
Puts("Config is outdated, updating and saving...");
SaveConfig();
}
}
catch
{
Puts("Config file is invalid, loading defaults instead");
LoadDefaultConfig();
}
}
protected override void SaveConfig()
{
Puts("Saving config...");
Config.WriteObject(_Config, true);
Puts("Config saved.");
}
#endregion
#region Data
private void LoadData<T>(out T data, string fp = null) => data = Interface.Oxide.DataFileSystem.ReadObject<T>(fp ?? Name);
private void SaveData<T>(T data, string fp = null) => Interface.Oxide.DataFileSystem.WriteObject(fp ?? Name, data);
private class Data
{
[JsonProperty("Last Wipe")] public string LastWipe;
}
#endregion
#region Hooks
[UsedImplicitly]
private void Loaded()
{
LoadData(out _Data);
SaveData(_Data);
if (_Config.Interval > 0) _Loop = timer.Every(_Config.Interval, UpdateConvars);
UpdateConvars();
}
[UsedImplicitly]
private void Unloaded()
{
SaveData(_Data);
if (_Config.Interval > 0) _Loop.Destroy();
}
[UsedImplicitly]
private void OnNewSave()
{
_Data.LastWipe = CurrentDate();
UpdateConvars();
SaveData(_Data);
}
#endregion
#region Helpers
private static DateTime GetDate(string date)
{
return DateTime.ParseExact(date, "G", null);
}
private static string CurrentDate() => DateTime.Now.ToString("G");
private string FormatText(string text, bool prependJustwiped = false)
{
// Oxide fails to compile ??= so just do it the old fashioned way
if (_Data.LastWipe == null)
{
_Data.LastWipe = CurrentDate();
SaveData(_Data);
}
var wipeDate = GetDate(_Data.LastWipe);
var dateFmt = _Config.DateFormat.Replace("dd", wipeDate.Day.ToString()).Replace("mm", wipeDate.Month.ToString());
var justWiped = prependJustwiped && (DateTime.Now - wipeDate).Days <= _Config.JustWipedDays ? _Config.JustWipedText : "";
return text.Replace("{wipe}", dateFmt).Replace("{just}", justWiped);
}
private void UpdateConvars()
{
ConVar.Server.hostname = FormatText(_Config.Hostname, true);
ConVar.Server.description = FormatText(_Config.Description);
}
#endregion
}
}