using System; using System.Collections.Generic; namespace Oxide.Plugins { [Info("Automatic Countdown Save", "Farmer Elias", "2.1.1")] [Description("Initiate a countdown before the server saves.")] class AutomaticCountdownSave : RustPlugin { private int countdownInSeconds; private int serverSaveInterval; private Timer saveTimer; private bool useGameTips; protected override void LoadDefaultConfig() { Config["CountdownInSeconds"] = 10; Config["ServerSaveInterval"] = 600; Config["UseGameTips"] = true; SaveConfig(); } private void Init() { LoadConfigValues(); LoadDefaultMessages(); } private void LoadConfigValues() { countdownInSeconds = Convert.ToInt32(Config["CountdownInSeconds"]); serverSaveInterval = Convert.ToInt32(Config["ServerSaveInterval"]); useGameTips = Convert.ToBoolean(Config["UseGameTips"]); } private void LoadDefaultMessages() { lang.RegisterMessages(new Dictionary { ["CountdownMessage"] = "The Server saves in {0} Second{1}", ["SaveMessage"] = "The Server saves now. Expect some lag" }, this); } private string GetMessage(string key, string playerId = null) => lang.GetMessage(key, this, playerId); private void OnServerSave() { Puts($"Waiting for the next server save..."); timer.Once(1f, () => StartCountdown()); } private void StartCountdown() { int initialCountdown = serverSaveInterval - countdownInSeconds; if (initialCountdown <= 0) { initialCountdown = 1; } Puts($"Server save countdown started! Saving in {initialCountdown} second{(initialCountdown != 1 ? "s" : "")}."); if (saveTimer != null && !saveTimer.Destroyed) { saveTimer.Destroy(); HideGameTipsForAll(); } saveTimer = timer.Once(initialCountdown, () => StartCountdownTimer()); } private void StartCountdownTimer() { saveTimer = timer.Repeat(1f, countdownInSeconds, () => { string countdownMessage = string.Format(GetMessage("CountdownMessage"), countdownInSeconds, countdownInSeconds != 1 ? "s" : ""); if (useGameTips) { CreateGameTipAll(countdownMessage); } else { PrintToChat(countdownMessage); } countdownInSeconds--; if (countdownInSeconds <= 0) { if (!useGameTips) { string saveMessage = GetMessage("SaveMessage"); PrintToChat(saveMessage); } else { CreateGameTipAll(GetMessage("SaveMessage")); } Puts("Save Countdown finished. Server Saving..."); countdownInSeconds = Convert.ToInt32(Config["CountdownInSeconds"]); timer.Once(1f, () => HideGameTipsForAll()); } }); } private void CreateGameTipAll(string text, float length = 30f) { foreach (BasePlayer player in BasePlayer.activePlayerList) { CreateGameTip(text, player, length); } } private void CreateGameTip(string text, BasePlayer player, float length = 30f) { if (player == null) return; player.SendConsoleCommand("gametip.hidegametip"); player.SendConsoleCommand("gametip.showgametip", text); timer.Once(length, () => player?.SendConsoleCommand("gametip.hidegametip")); } private void HideGameTipsForAll() { foreach (BasePlayer player in BasePlayer.activePlayerList) { player.SendConsoleCommand("gametip.hidegametip"); } } [ConsoleCommand("gametipfalse")] private void gametipfalse(ConsoleSystem.Arg args) { if (args.Connection != null && args.Connection.authLevel < 2) { args.ReplyWith("You don't have permission to use this command."); return; } Config["UseGameTips"] = false; SaveConfig(); covalence.Server.Command("o.reload AutomaticCountdownSave"); } [ConsoleCommand("gametiptrue")] private void gametiptrue(ConsoleSystem.Arg args) { if (args.Connection != null && args.Connection.authLevel < 2) { args.ReplyWith("You don't have permission to use this command."); return; } Config["UseGameTips"] = true; SaveConfig(); covalence.Server.Command("o.reload AutomaticCountdownSave"); } [ConsoleCommand("saveintervalrefresh")] private void CmdSaveIntervalRefresh(ConsoleSystem.Arg args) { if (args.Connection != null && args.Connection.authLevel < 2) { args.ReplyWith("You don't have permission to use this command."); return; } float saveInterval = ConVar.Server.saveinterval; Config["ServerSaveInterval"] = saveInterval; SaveConfig(); covalence.Server.Command("o.reload AutomaticCountdownSave"); Puts($"Server save interval has been refreshed in the plugin config and set to: {saveInterval}"); } [ConsoleCommand("countdown5s")] private void countdown5s(ConsoleSystem.Arg args) { if (args.Connection != null && args.Connection.authLevel < 2) { args.ReplyWith("You don't have permission to use this command."); return; } Config["CountdownInSeconds"] = 5; SaveConfig(); covalence.Server.Command("o.reload AutomaticCountdownSave"); args.ReplyWith("Countdown set to 5 seconds."); } [ConsoleCommand("countdown10s")] private void countdown10s(ConsoleSystem.Arg args) { if (args.Connection != null && args.Connection.authLevel < 2) { args.ReplyWith("You don't have permission to use this command."); return; } Config["CountdownInSeconds"] = 10; SaveConfig(); covalence.Server.Command("o.reload AutomaticCountdownSave"); args.ReplyWith("Countdown set to 10 seconds."); } [ConsoleCommand("countdown15s")] private void countdown15s(ConsoleSystem.Arg args) { if (args.Connection != null && args.Connection.authLevel < 2) { args.ReplyWith("You don't have permission to use this command."); return; } Config["CountdownInSeconds"] = 15; SaveConfig(); covalence.Server.Command("o.reload AutomaticCountdownSave"); args.ReplyWith("Countdown set to 15 seconds."); } [ConsoleCommand("countdown30s")] private void Countdown30s(ConsoleSystem.Arg args) { if (args.Connection != null && args.Connection.authLevel < 2) { args.ReplyWith("You don't have permission to use this command."); return; } Config["CountdownInSeconds"] = 30; SaveConfig(); covalence.Server.Command("o.reload AutomaticCountdownSave"); args.ReplyWith("Countdown set to 30 seconds."); } const string CUI = @"[{ ""name"":""menu"", ""parent"":""Overlay"", ""components"":[ { ""type"":""NeedsCursor"" }, { ""type"":""RectTransform"", ""anchormin"":""0.3432308 0.3259261"", ""anchormax"":""0.6401055 0.6481483"", ""offsetmin"":""0 0"", ""offsetmax"":""0 0"" } ] }, { ""name"":""top"", ""parent"":""menu"", ""components"":[ { ""type"":""UnityEngine.UI.Image"", ""color"":""0.04313726 0.04313726 0.04313726 1"" }, { ""type"":""RectTransform"", ""anchormin"":""-5.662441E-06 0.8218389"", ""anchormax"":""1 1"", ""offsetmin"":""0 0"", ""offsetmax"":""0 0"" } ] }, { ""name"":""exit_button"", ""parent"":""top"", ""components"":[ { ""type"":""UnityEngine.UI.Button"", ""close"":""menu"", ""color"":""0.7647059 0.1882353 0.1882353 1"" }, { ""type"":""RectTransform"", ""anchormin"":""0.9280667 0.2612895"", ""anchormax"":""0.9842069 0.777418"", ""offsetmin"":""0 0"", ""offsetmax"":""0 0"" } ] }, { ""name"":""exit_button_text"", ""parent"":""exit_button"", ""components"":[ { ""type"":""UnityEngine.UI.Text"", ""text"":""x"", ""fontSize"":21, ""font"":""robotocondensed-bold.ttf"", ""align"":""MiddleCenter"", ""color"":""0 0 0 1"" }, { ""type"":""RectTransform"", ""anchormin"":""-0.2812483 -0.1625018"", ""anchormax"":""1.28124 1.243747"", ""offsetmin"":""0 0"", ""offsetmax"":""0 0"" } ] }, { ""name"":""topbar_text"", ""parent"":""top"", ""components"":[ { ""type"":""UnityEngine.UI.Text"", ""text"":""Save Countdown Announcer - Editor"" }, { ""type"":""RectTransform"", ""anchormin"":""0.04385996 0.2709669"", ""anchormax"":""0.6543832 0.67742"", ""offsetmin"":""0 0"", ""offsetmax"":""0 0"" } ] }, { ""name"":""bottom"", ""parent"":""menu"", ""components"":[ { ""type"":""UnityEngine.UI.Image"", ""color"":""0.08627451 0.08627451 0.08627451 1"" }, { ""type"":""RectTransform"", ""anchormin"":""-5.890615E-08 -0.08908099"", ""anchormax"":""1.000006 0.8298852"", ""offsetmax"":""0 0"" } ] }, { ""name"":""cluster"", ""parent"":""bottom"", ""components"":[ { ""type"":""RectTransform"", ""anchormin"":""0.2333323 0.2157597"", ""anchormax"":""0.7666632 0.7817382"", ""offsetmax"":""0 0"" } ] }, { ""name"":""gametips"", ""parent"":""cluster"", ""components"":[ { ""type"":""UnityEngine.UI.Image"", ""color"":""0.1098039 0.1098039 0.1098039 1"" }, { ""type"":""RectTransform"", ""anchormin"":""0 0.6839765"", ""anchormax"":""1.006568 1"", ""offsetmax"":""0 0"" } ] }, { ""name"":""text_gametips"", ""parent"":""gametips"", ""components"":[ { ""type"":""UnityEngine.UI.Text"", ""text"":""Gametips"", ""fontSize"":13, ""align"":""MiddleCenter"" }, { ""type"":""RectTransform"", ""anchormin"":""0 0"", ""anchormax"":""0.4182947 0.9964995"", ""offsetmax"":""0 0"" } ] }, { ""name"":""true"", ""parent"":""gametips"", ""components"":[ { ""type"":""UnityEngine.UI.Button"", ""command"":""gametiptrue"", ""close"":""hide_saved"", ""color"":""0.3529412 0.4862745 0.3529412 1"" }, { ""type"":""RectTransform"", ""anchormin"":""0.4594804 0.300702"", ""anchormax"":""0.6993428 0.6853152"", ""offsetmin"":""0 0"", ""offsetmax"":""0 0"" } ] }, { ""name"":""text_true"", ""parent"":""true"", ""components"":[ { ""type"":""UnityEngine.UI.Text"", ""text"":""true"", ""fontSize"":11, ""align"":""MiddleCenter"" }, { ""type"":""RectTransform"", ""anchormin"":""0.005413651 0.04545313"", ""anchormax"":""1 1.045456"", ""offsetmax"":""0 0"" } ] }, { ""name"":""false"", ""parent"":""gametips"", ""components"":[ { ""type"":""UnityEngine.UI.Button"", ""command"":""gametipfalse"", ""close"":""hide_saved"", ""color"":""0.8039216 0.345098 0.2313726 1"" }, { ""type"":""RectTransform"", ""anchormin"":""0.7143847 0.300702"", ""anchormax"":""0.9542471 0.6853153"", ""offsetmax"":""0 0"" } ] }, { ""name"":""text_false"", ""parent"":""false"", ""components"":[ { ""type"":""UnityEngine.UI.Text"", ""text"":""false"", ""fontSize"":11, ""align"":""MiddleCenter"" }, { ""type"":""RectTransform"", ""anchormin"":""0.01903913 0.04545316"", ""anchormax"":""1.019039 0.9999998"", ""offsetmax"":""0 0"" } ] }, { ""name"":""interval"", ""parent"":""cluster"", ""components"":[ { ""type"":""UnityEngine.UI.Image"", ""color"":""0.1098039 0.1098039 0.1098039 1"" }, { ""type"":""RectTransform"", ""anchormin"":""0 0.3414343"", ""anchormax"":""1.006568 0.6574578"", ""offsetmin"":""0 0"", ""offsetmax"":""0 0"" } ] }, { ""name"":""text_interval"", ""parent"":""interval"", ""components"":[ { ""type"":""UnityEngine.UI.Text"", ""text"":""Save Interval"", ""fontSize"":13, ""align"":""MiddleCenter"" }, { ""type"":""RectTransform"", ""anchormin"":""0 0"", ""anchormax"":""0.4182947 0.9964994"", ""offsetmin"":""0 0"", ""offsetmax"":""0 0"" } ] }, { ""name"":""refresh"", ""parent"":""interval"", ""components"":[ { ""type"":""UnityEngine.UI.Button"", ""command"":""saveintervalrefresh"", ""close"":""hide_refreshed"", ""color"":""0.2216572 0.4356023 0.7565105 1"" }, { ""type"":""RectTransform"", ""anchormin"":""0.5477172 0.2832204"", ""anchormax"":""0.8745183 0.667834"", ""offsetmax"":""0 0"" } ] }, { ""name"":""text_refresh"", ""parent"":""refresh"", ""components"":[ { ""type"":""UnityEngine.UI.Text"", ""text"":""Refresh"", ""fontSize"":11, ""align"":""MiddleCenter"" }, { ""type"":""RectTransform"", ""anchormin"":""5.364418E-07 4.410744E-06"", ""anchormax"":""1 1"", ""offsetmax"":""0 0"" } ] }, { ""name"":""countdown"", ""parent"":""cluster"", ""components"":[ { ""type"":""UnityEngine.UI.Image"", ""color"":""0.1098039 0.1098039 0.1098039 1"" }, { ""type"":""RectTransform"", ""anchormin"":""0 0"", ""anchormax"":""1.006568 0.3160233"", ""offsetmin"":""0 0"", ""offsetmax"":""0 0"" } ] }, { ""name"":""text_countdown"", ""parent"":""countdown"", ""components"":[ { ""type"":""UnityEngine.UI.Text"", ""text"":""Countdown"", ""fontSize"":13, ""align"":""MiddleCenter"" }, { ""type"":""RectTransform"", ""anchormin"":""0 0"", ""anchormax"":""0.4182947 0.9964994"", ""offsetmin"":""0 0"", ""offsetmax"":""0 0"" } ] }, { ""name"":""countdown5s"", ""parent"":""countdown"", ""components"":[ { ""type"":""UnityEngine.UI.Button"", ""command"":""countdown5s"", ""close"":""hide_changed"", ""color"":""0.3529412 0.4862745 0.3529412 1"" }, { ""type"":""RectTransform"", ""anchormin"":""0.4594809 0.2748239"", ""anchormax"":""0.552283 0.6594381"", ""offsetmax"":""0 0"" } ] }, { ""name"":""text_5s"", ""parent"":""countdown5s"", ""components"":[ { ""type"":""UnityEngine.UI.Text"", ""text"":""5s"", ""fontSize"":10, ""align"":""MiddleCenter"" }, { ""type"":""RectTransform"", ""anchormin"":""0.1 0.1"", ""anchormax"":""1.000002 0.9672718"", ""offsetmax"":""0 0"" } ] }, { ""name"":""countdown10s"", ""parent"":""countdown"", ""components"":[ { ""type"":""UnityEngine.UI.Button"", ""command"":""countdown10s"", ""close"":""hide_changed"", ""color"":""0.3529412 0.4862745 0.3529412 1"" }, { ""type"":""RectTransform"", ""anchormin"":""0.5673251 0.2748239"", ""anchormax"":""0.6830034 0.6594381"", ""offsetmin"":""0 0"", ""offsetmax"":""0 0"" } ] }, { ""name"":""text_10s"", ""parent"":""countdown10s"", ""components"":[ { ""type"":""UnityEngine.UI.Text"", ""text"":""10s"", ""fontSize"":10, ""align"":""MiddleCenter"" }, { ""type"":""RectTransform"", ""anchormin"":""0.1 0.1"", ""anchormax"":""0.9717538 0.967272"", ""offsetmin"":""0 0"", ""offsetmax"":""0 0"" } ] }, { ""name"":""countdown15s"", ""parent"":""countdown"", ""components"":[ { ""type"":""UnityEngine.UI.Button"", ""command"":""countdown15s"", ""close"":""hide_changed"", ""color"":""0.3529412 0.4862745 0.3529412 1"" }, { ""type"":""RectTransform"", ""anchormin"":""0.6980453 0.2748239"", ""anchormax"":""0.8137236 0.6594381"", ""offsetmin"":""0 0"", ""offsetmax"":""0 0"" } ] }, { ""name"":""text_15s"", ""parent"":""countdown15s"", ""components"":[ { ""type"":""UnityEngine.UI.Text"", ""text"":""15s"", ""fontSize"":10, ""align"":""MiddleCenter"" }, { ""type"":""RectTransform"", ""anchormin"":""0.1 0.1"", ""anchormax"":""0.9717538 0.967272"", ""offsetmin"":""0 0"", ""offsetmax"":""0 0"" } ] }, { ""name"":""countdown30s"", ""parent"":""countdown"", ""components"":[ { ""type"":""UnityEngine.UI.Button"", ""command"":""countdown30s"", ""close"":""hide_changed"", ""color"":""0.3529412 0.4862745 0.3529412 1"" }, { ""type"":""RectTransform"", ""anchormin"":""0.8287655 0.2748239"", ""anchormax"":""0.9444438 0.6594381"", ""offsetmin"":""0 0"", ""offsetmax"":""0 0"" } ] }, { ""name"":""text_30s"", ""parent"":""countdown30s"", ""components"":[ { ""type"":""UnityEngine.UI.Text"", ""text"":""30s"", ""fontSize"":10, ""align"":""MiddleCenter"" }, { ""type"":""RectTransform"", ""anchormin"":""0.1 0.1"", ""anchormax"":""0.9717538 0.967272"", ""offsetmin"":""0 0"", ""offsetmax"":""0 0"" } ] }, { ""name"":""text_saved"", ""parent"":""bottom"", ""components"":[ { ""type"":""UnityEngine.UI.Text"", ""text"":""Saved."", ""fontSize"":13, ""align"":""MiddleCenter"", ""color"":""0.2662668 0.5423159 0.2601298 1"" }, { ""type"":""RectTransform"", ""anchormin"":""0.7771899 0.6128826"", ""anchormax"":""0.9526277 0.7723573"", ""offsetmax"":""0 0"" } ] }, { ""name"":""text_refreshed"", ""parent"":""bottom"", ""components"":[ { ""type"":""UnityEngine.UI.Text"", ""text"":""Refreshed!"", ""fontSize"":13, ""align"":""MiddleCenter"", ""color"":""0.2627451 0.5411765 0.2588235 1"" }, { ""type"":""RectTransform"", ""anchormin"":""0.7771899 0.4190115"", ""anchormax"":""0.9526277 0.5784863"", ""offsetmin"":""0 0"", ""offsetmax"":""0 0"" } ] }, { ""name"":""text_changed"", ""parent"":""bottom"", ""components"":[ { ""type"":""UnityEngine.UI.Text"", ""text"":""Changed."", ""fontSize"":13, ""align"":""MiddleCenter"", ""color"":""0.2627451 0.5411765 0.254902 1"" }, { ""type"":""RectTransform"", ""anchormin"":""0.7771899 0.2220137"", ""anchormax"":""0.9526277 0.3814878"", ""offsetmin"":""0 0"", ""offsetmax"":""0 0"" } ] }, { ""name"":""hide_saved"", ""parent"":""menu"", ""components"":[ { ""type"":""UnityEngine.UI.Image"", ""color"":""0.08627451 0.08627451 0.08627451 1"" }, { ""type"":""RectTransform"", ""anchormin"":""0.7824569 0.4879309"", ""anchormax"":""0.9578955 0.6373563"", ""offsetmax"":""0 0"" } ], ""fadeOut"":1.0 }, { ""name"":""hide_changed"", ""parent"":""menu"", ""components"":[ { ""type"":""UnityEngine.UI.Image"", ""color"":""0.08627451 0.08627451 0.08627451 1"" }, { ""type"":""RectTransform"", ""anchormin"":""0.7824569 0.1028737"", ""anchormax"":""0.9578956 0.2522988"", ""offsetmin"":""0 0"", ""offsetmax"":""0 0"" } ], ""fadeOut"":1.0 }, { ""name"":""hide_refreshed"", ""parent"":""menu"", ""components"":[ { ""type"":""UnityEngine.UI.Image"", ""color"":""0.08627451 0.08627451 0.08627451 1"" }, { ""type"":""RectTransform"", ""anchormin"":""0.7842112 0.2982759"", ""anchormax"":""0.9596498 0.4477012"", ""offsetmin"":""0 0"", ""offsetmax"":""0 0"" } ], ""fadeOut"":1.0 } ]"; [ChatCommand("sca")] private void ShowGUI(BasePlayer player, string command, string[] args) { CommunityEntity.ServerInstance.ClientRPCEx(new Network.SendInfo { connection = player.net.connection }, null, "AddUI", CUI); } private void Unload() { HideGameTipsForAll(); } } }