using UnityEngine;
using System.Collections.Generic;
using System.Globalization;
using Oxide.Core;
using Oxide.Game.Rust.Cui;
namespace Oxide.Plugins
{
[Info("Relocated", "Death", "1.0.1")]
[Description("Display a GUI screen with information to new server location. Useful when changing server IPs.")]
class Relocated : RustPlugin
{
#region Hooks
void Init()
{
LoadConfigVariables();
}
void Unload()
{
foreach (var player in BasePlayer.activePlayerList)
{
CuiHelper.DestroyUi(player, "main");
}
}
void OnPlayerSleepEnded(BasePlayer player)
{
CuiElementContainer container = UI.Container("main", UI.Color(configData.Design.BodyPanel_Color, configData.Design.BodyPanel_Opacity), new UI4(0, 0, 1, 1), true);
UI.Label(ref container, "main", $"{configData.Info.HeaderText.Replace("{0}", configData.Info.ServerName)}", 25, new UI4(0.2f, 0.7f, 0.8f, 1), TextAnchor.MiddleCenter);
UI.Label(ref container, "main", configData.Info.SubHeaderText.Replace("{0}", $"{player.displayName}").Replace("{1}", Interface.Oxide.Config.Options.Modded ? "modded" : "community"), 16, new UI4(0.2f, 0.6f, 0.8f, 0.7f), TextAnchor.MiddleCenter);
UI.Label(ref container, "main", configData.Info.ConnectInfo.Replace("{0}", $"connect {configData.Info.NewServerIP}").Replace("{1}", $"{configData.Info.ServerName}").Replace("{2}", Interface.Oxide.Config.Options.Modded ? "modded" : "community"), 16, new UI4(0.2f, 0.3f, 0.8f, 0.4f), TextAnchor.MiddleCenter);
UI.Button(ref container, "main", UI.Color(configData.Design.Button_Color, configData.Design.Button_Opacity), "Print To Console", 10, new UI4(0.38f, 0.17f, 0.48f, 0.2f), $"r_print");
UI.Button(ref container, "main", UI.Color(configData.Design.Button_Color, configData.Design.Button_Opacity), "Exit", 10, new UI4(0.52f, 0.17f, 0.62f, 0.2f), "r_exit");
CuiHelper.AddUi(player, container);
}
#endregion
#region Functions
#region CallBacks
[ConsoleCommand("r_exit")]
void Exit(ConsoleSystem.Arg arg)
{
if (arg == null || arg.Player() == null)
{
return;
}
if (configData.Info.KickOnExit)
{
arg.Player().Kick(configData.Info.KickReason.Replace("{0}", configData.Info.NewServerIP));
}
else
{
CuiHelper.DestroyUi(arg.Player(), "main");
}
}
[ConsoleCommand("r_print")]
void Print(ConsoleSystem.Arg arg)
{
if (arg == null || arg.Player() == null)
{
return;
}
arg.Player().SendConsoleCommand($"echo connect {configData.Info.NewServerIP}");
}
#endregion
#region Config
private ConfigData configData;
class ConfigData
{
public Info Info = new Info();
public Design Design = new Design();
}
class Info
{
public string ServerName = ConVar.Server.hostname;
public string NewServerIP = "123.456.789:1234";
public string HeaderText = "{0} has moved!";
public string SubHeaderText = "Greetings {0}! Our server has moved locations and is under a new IP address. Because of this, you'll have to search for our server under the {1} tab or connect with the information below.";
public string ConnectInfo = "Connect to our new server by pressing F1 and typing {0} or searching {1} under the {2} tab!";
public string PrintInfo = "connect {0}";
public bool KickOnExit = true;
public string KickReason = "Our server has moved to {0}!";
}
class Design
{
public string ImportantText_Color = "#188FF3";
public string BodyText_Color = "#fffffff";
public string BodyPanel_Color = "000000";
public float BodyPanel_Opacity = 0.9f;
public string Button_Color = "008000";
public float Button_Opacity = 0.9f;
}
private void LoadConfigVariables()
{
configData = Config.ReadObject();
SaveConfig(configData);
}
protected override void LoadDefaultConfig()
{
var config = new ConfigData();
SaveConfig(config);
}
void SaveConfig(ConfigData config)
{
Config.WriteObject(config, true);
}
#endregion
#region GUI - Framework
public static class UI
{
static public CuiElementContainer Container(string panel, string color, UI4 dimensions, bool useCursor = false, string parent = "Overlay")
{
CuiElementContainer container = new CuiElementContainer()
{
{
new CuiPanel
{
Image = {Color = color},
RectTransform = {AnchorMin = dimensions.GetMin(), AnchorMax = dimensions.GetMax()},
CursorEnabled = useCursor
},
new CuiElement().Parent = parent,
panel
}
};
return container;
}
static public void Label(ref CuiElementContainer container, string panel, string text, int size, UI4 dimensions, TextAnchor align = TextAnchor.MiddleCenter)
{
container.Add(new CuiLabel
{
Text = { FontSize = size, Align = align, Text = text },
RectTransform = { AnchorMin = dimensions.GetMin(), AnchorMax = dimensions.GetMax() }
},
panel);
}
static public void Button(ref CuiElementContainer container, string panel, string color, string text, int size, UI4 dimensions, string command, TextAnchor align = TextAnchor.MiddleCenter)
{
container.Add(new CuiButton
{
Button = { Color = color, Command = command, FadeIn = 0f },
RectTransform = { AnchorMin = dimensions.GetMin(), AnchorMax = dimensions.GetMax() },
Text = { Text = text, FontSize = size, Align = align }
},
panel);
}
public static string Color(string hexColor, float alpha)
{
if (hexColor.StartsWith("#"))
hexColor = hexColor.Substring(1);
int red = int.Parse(hexColor.Substring(0, 2), NumberStyles.AllowHexSpecifier);
int green = int.Parse(hexColor.Substring(2, 2), NumberStyles.AllowHexSpecifier);
int blue = int.Parse(hexColor.Substring(4, 2), NumberStyles.AllowHexSpecifier);
return $"{(double)red / 255} {(double)green / 255} {(double)blue / 255} {alpha}";
}
}
public class UI4
{
public float xMin, yMin, xMax, yMax;
public UI4(float xMin, float yMin, float xMax, float yMax)
{
this.xMin = xMin;
this.yMin = yMin;
this.xMax = xMax;
this.yMax = yMax;
}
public string GetMin() => $"{xMin} {yMin}";
public string GetMax() => $"{xMax} {yMax}";
}
#endregion
#endregion
}
}