using UnityEngine;
using Rust;
using Oxide.Core.Plugins;
using System.Collections.Generic;
using System;
using Oxide.Core;
using Oxide.Game.Rust.Cui;
using Newtonsoft.Json;
namespace Oxide.Plugins
{
[Info("Rules GUI", "Mike Hawke", "2.0.1")]
[Description("This plugin displays the rules on connect.")]
class RulesGUI : RustPlugin
{
private ConfigData configData;
private StoredData storedData;
private class ConfigData
{
[JsonProperty(PropertyName = "Background Image true/false")]
public bool BackgroundImage { get; set; } = false;
[JsonProperty(PropertyName = "Background Color RGBA")]
public string BackgroundColor { get; set; } = "0.1 0.1 0.1 1";
[JsonProperty(PropertyName = "Background Image URL")]
public string BackgroundImageUrl { get; set; } = "https://i.ytimg.com/vi/yaqe1qesQ8c/maxresdefault.jpg";
[JsonProperty(PropertyName = "Text Font Size")]
public int MessageFontSize { get; set; } = 22;
[JsonProperty(PropertyName = "Message text")]
public string Text { get; set; } = "Welcome! The following in-game activities are prohibited in the Game:\n\n1. Use of bots, use of third-party software, bugs.\n\n2. Pretending to be a member of Administration.\n\n3. Fraud, other dishonest actions.\n\n4. Flooding, flaming, spam, printing in capital letters (CAPS LOCK).\n\n5. Creating obstructions for other users, Greifing and bullying\n\n6. Advertisement, political propaganda.\n\n7. Racism! \n\n8. Failure to comply will land you in prison, continuing to break rules will land you a Ban!";
[JsonProperty(PropertyName = "Display on every connection true/false")]
public bool DisplayOnEveryConnect { get; set; } = true;
[JsonProperty(PropertyName = "Disagree Kick Reason")]
public string KickMessage { get; set; } = "You disagreed with the rules!";
}
private class StoredData
{
public List DisplayedTo { get; set; } = new List();
}
private void Init()
{
LoadConfigVariables();
}
private void Loaded()
{
permission.RegisterPermission("rulesgui.usecmd", this);
storedData = Interface.Oxide.DataFileSystem.ReadObject("RuleGUIData");
Interface.Oxide.DataFileSystem.WriteObject("RuleGUIData", storedData);
}
private void LoadConfigVariables()
{
configData = Config.ReadObject();
SaveConfig(configData);
}
protected override void LoadDefaultConfig()
{
Puts("Creating new config file.");
var config = new ConfigData();
SaveConfig(config);
}
private void SaveConfig(ConfigData config)
{
Config.WriteObject(config, true);
}
private void SaveData()
{
Interface.Oxide.DataFileSystem.WriteObject("RuleGUIData", storedData);
}
private void OnNewSave(string filename)
{
storedData.DisplayedTo.Clear();
SaveData();
}
private void Unloaded()
{
foreach (BasePlayer current in BasePlayer.activePlayerList)
{
CuiHelper.DestroyUi(current, "RulesGUI");
}
}
private void Destroy(BasePlayer player, bool all)
{
CuiHelper.DestroyUi(player, "RulesGUI");
}
private void UseUI(BasePlayer player)
{
CuiHelper.DestroyUi(player, "RulesGUI");
var elements = new CuiElementContainer();
var mainName = elements.Add(new CuiPanel { Image = { Color = configData.BackgroundColor }, RectTransform = { AnchorMin = "0 0", AnchorMax = "1 1" }, CursorEnabled = true }, "Overlay", "RulesGUI");
if (configData.BackgroundImage)
{
elements.Add(new CuiElement { Parent = "RulesGUI", Components = { new CuiRawImageComponent { Url = configData.BackgroundImageUrl, Sprite = "assets/content/textures/generic/fulltransparent.tga" }, new CuiRectTransformComponent { AnchorMin = "0 0", AnchorMax = "1 1" } } });
}
var Agree = new CuiButton { Button = { Command = "global.agreed", Color = "0 255 0 1" }, RectTransform = { AnchorMin = "0.2 0.16", AnchorMax = "0.45 0.2" }, Text = { Text = "I Agree", FontSize = 22, Align = TextAnchor.MiddleCenter } };
var Disagree = new CuiButton { Button = { Command = "global.hardestcommandtoeverguess", Close = mainName, Color = "255 0 0 1" }, RectTransform = { AnchorMin = "0.5 0.16", AnchorMax = "0.75 0.2" }, Text = { Text = "I Disagree", FontSize = 22, Align = TextAnchor.MiddleCenter } };
elements.Add(new CuiLabel { Text = { Text = configData.Text, FontSize = configData.MessageFontSize, Align = TextAnchor.MiddleCenter }, RectTransform = { AnchorMin = "0 0.20", AnchorMax = "1 0.9" } }, mainName);
elements.Add(Agree, mainName);
elements.Add(Disagree, mainName);
CuiHelper.AddUi(player, elements);
}
[ConsoleCommand("agreed")]
private void agreed(ConsoleSystem.Arg arg)
{
var player = arg.Connection.player as BasePlayer;
if (player == null) return;
Destroy(player, true);
}
[ConsoleCommand("hardestcommandtoeverguess")]
private void cmdHardestcmd(ConsoleSystem.Arg arg)
{
BasePlayer player = (BasePlayer)arg.Connection.player;
Network.Net.sv.Kick(player.net.connection, rust.QuoteSafe(configData.KickMessage));
}
[ChatCommand("rulesto")]
private void cmdRulesTo(BasePlayer player, string cmd, string[] args)
{
if (!permission.UserHasPermission(player.UserIDString, "rulesgui.usecmd"))
{
SendReply(player, "You do not have permission to use this command!");
return;
}
if (args.Length != 1)
{
SendReply(player, "Syntax: /rulesto \"target\" ");
return;
}
BasePlayer target = BasePlayer.Find(args[0]);
if (target == null)
{
SendReply(player, "Player not found!");
return;
}
UseUI(player);
SendReply(player, $"You have displayed the rules to {target.displayName}");
}
[ChatCommand("rules")]
private void cmdRule(BasePlayer player, string cmd, string[] args)
{
UseUI(player);
}
[ConsoleCommand("sendrules")]
private void conRuleto(ConsoleSystem.Arg arg)
{
if (arg.IsAdmin && arg.Args?.Length > 0)
{
var player = BasePlayer.Find(arg.Args[0]) ?? BasePlayer.FindSleeping(arg.Args[0]);
if (player == null)
{
PrintWarning($"We can't find player with that name/ID! {arg.Args[0]}");
return;
}
UseUI(player);
PrintWarning($"Player {arg.Args[0]} was sent the rules");
}
}
private void DisplayUI(BasePlayer player)
{
if (player.HasPlayerFlag(BasePlayer.PlayerFlags.ReceivingSnapshot))
{
timer.In(1, () => DisplayUI(player));
}
else
{
if (configData.DisplayOnEveryConnect)
{
UseUI(player);
}
else
{
if (storedData.DisplayedTo.Contains(player.userID)) return;
UseUI(player);
storedData.DisplayedTo.Add(player.userID);
SaveData();
}
}
}
private void OnPlayerConnected(BasePlayer player)
{
DisplayUI(player);
}
}
}