ElDubya

RulesGUI.cs

Mar 30th, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.61 KB | None | 0 0
  1. using UnityEngine;
  2. using Rust;
  3. using Oxide.Core.Plugins;
  4. using System.Collections.Generic;
  5. using System;
  6. using System.Reflection;
  7. using Oxide.Core;
  8. using System.Linq;
  9. using Oxide.Game.Rust.Cui;
  10.  
  11. namespace Oxide.Plugins
  12. {
  13.     [Info("Rules GUI", "PaiN", "1.4.9", ResourceId = 1247)]
  14.     [Description("This plugin displays the rules on connect.")]
  15.     class RulesGUI : RustPlugin
  16.     {
  17.         private bool backroundimage;
  18.         private bool Changed;
  19.         private string text;
  20.         private bool displayoneveryconnect;
  21.         private string kickmsg;
  22.         private string backroundimageurl;
  23.        
  24.         void Loaded()  
  25.         {
  26.             permission.RegisterPermission("rulesgui.usecmd", this);
  27.             data = Interface.GetMod().DataFileSystem.ReadObject<Data>("RulesGUIdata");
  28.             LoadVariables();
  29.         }
  30.        
  31.         object GetConfig(string menu, string datavalue, object defaultValue)
  32.         {
  33.             var data = Config[menu] as Dictionary<string, object>;
  34.             if (data == null)
  35.             {
  36.                 data = new Dictionary<string, object>();
  37.                 Config[menu] = data;
  38.                 Changed = true;
  39.             }
  40.             object value;
  41.             if (!data.TryGetValue(datavalue, out value))
  42.             {
  43.                 value = defaultValue;
  44.                 data[datavalue] = value;
  45.                 Changed = true;
  46.             }
  47.             return value;  
  48.         }
  49.        
  50.         void LoadVariables()
  51.         {
  52.             backroundimageurl = Convert.ToString(GetConfig("Backround", "ImageURL", "https://i.ytimg.com/vi/yaqe1qesQ8c/maxresdefault.jpg"));
  53.             backroundimage = Convert.ToBoolean(GetConfig("Backround", "Enabled", false));
  54.             displayoneveryconnect = Convert.ToBoolean(GetConfig("Settings", "DisplayOnEveryConnect", false));
  55.             kickmsg = Convert.ToString(GetConfig("Messages", "KICK_MESSAGE", "You disagreed with the rules!"));
  56.             text = Convert.ToString(GetConfig("Messages", "RULES_MESSAGE", new List<string>{
  57.             "<color=cyan>Welcome!</color> <color=red>The following in-game activities are prohibited in the Game:</color>",
  58.             "<color=yellow>1.</color> Use of bots, use of third-party software, bugs.",
  59.             "<color=yellow>2.</color> Pretending to be a member of Administration.",
  60.             "<color=yellow>3.</color> Fraud, other dishonest actions.",
  61.             "<color=yellow>4.</color> Flooding, flaming, spam, printing in capital letters (CAPS LOCK).",
  62.             "<color=yellow>5.</color> Creating obstructions for other users.",
  63.             "<color=yellow>6.</color> Advertisement, political propaganda."
  64.             }));
  65.            
  66.             if (Changed)
  67.             {
  68.                 SaveConfig();
  69.                 Changed = false;
  70.            
  71.             }  
  72.         }
  73.        
  74.         protected override void LoadDefaultConfig()
  75.         {
  76.             Puts("Creating a new configuration file!");
  77.             Config.Clear();
  78.             LoadVariables();
  79.         }
  80.  
  81.  
  82.        
  83.  
  84.         class Data
  85.         {
  86.             public List<string> Players = new List<string>{};
  87.         }
  88.  
  89.  
  90.         Data data;
  91.  
  92.         void Unloaded()
  93.         {
  94.             foreach (BasePlayer current in BasePlayer.activePlayerList)
  95.             {
  96.                 CuiHelper.DestroyUi(current, "RulesGUI");
  97.             }
  98.         }
  99.        
  100.        
  101.         void UseUI(BasePlayer player, string msg)
  102.         {
  103.             var elements = new CuiElementContainer();
  104.  
  105.             var mainName = elements.Add(new CuiPanel
  106.             {
  107.                 Image =
  108.                 {
  109.                     Color = "0.1 0.1 0.1 1"
  110.                 },
  111.                 RectTransform =
  112.                 {
  113.                     AnchorMin = "0 0",
  114.                     AnchorMax = "1 1"
  115.                 },
  116.                 CursorEnabled = true
  117.             }, "Overlay", "RulesGUI");
  118.             if(backroundimage == true)
  119.             {
  120.                 elements.Add(new CuiElement
  121.                 {  
  122.                     Parent = "RulesGUI",
  123.                     Components =
  124.                     {
  125.                         new CuiRawImageComponent
  126.                         {
  127.                             Url = backroundimageurl,
  128.                             Sprite = "assets/content/textures/generic/fulltransparent.tga"
  129.                         },
  130.                         new CuiRectTransformComponent
  131.                         {
  132.                             AnchorMin = "0 0",
  133.                             AnchorMax = "1 1"
  134.                         }
  135.                     }
  136.                 });
  137.             }                
  138.             var Agree = new CuiButton
  139.             {
  140.                 Button =
  141.                 {
  142.                     Close = mainName,
  143.                     Color = "0 255 0 1"
  144.                 },
  145.                 RectTransform =
  146.                 {
  147.                     AnchorMin = "0.2 0.16",
  148.                     AnchorMax = "0.45 0.2"
  149.                 },
  150.                 Text =
  151.                 {
  152.                     Text = "I Agree",
  153.                     FontSize = 22,
  154.                     Align = TextAnchor.MiddleCenter
  155.                 }
  156.             };
  157.             var Disagree = new CuiButton
  158.             {
  159.                
  160.                
  161.                 Button =
  162.                 {
  163.                     Command = "global.hardestcommandtoeverguess",
  164.                     Close = mainName,
  165.                     Color = "255 0 0 1"
  166.                 },
  167.                 RectTransform =
  168.                 {
  169.                     AnchorMin = "0.5 0.16",
  170.                     AnchorMax = "0.75 0.2"
  171.                 },
  172.                 Text =
  173.                 {
  174.                     Text = "I Disagree",
  175.                     FontSize = 22,
  176.                     Align = TextAnchor.MiddleCenter
  177.                 }
  178.             };
  179.             elements.Add(new CuiLabel
  180.             {
  181.                 Text =
  182.                 {
  183.                     Text = msg,
  184.                     FontSize = 22,
  185.                     Align = TextAnchor.MiddleCenter
  186.                 },
  187.                 RectTransform =
  188.                 {
  189.                     AnchorMin = "0 0.20",
  190.                     AnchorMax = "1 0.9"
  191.                 }
  192.             }, mainName);
  193.             elements.Add(Agree, mainName);
  194.             elements.Add(Disagree, mainName);
  195.             CuiHelper.AddUi(player, elements);
  196.         }
  197.        
  198.         [ConsoleCommand("hardestcommandtoeverguess")]
  199.         void cmdHardestcmd(ConsoleSystem.Arg arg)
  200.         {
  201.             BasePlayer player = (BasePlayer)arg.Connection.player;
  202.             Network.Net.sv.Kick(player.net.connection, rust.QuoteSafe(kickmsg));
  203.         }
  204.        
  205.         [ChatCommand("rulesto")]
  206.         void cmdRulesTo(BasePlayer player, string cmd, string[] args)
  207.         {
  208.             if(!permission.UserHasPermission(player.userID.ToString(), "rulesgui.usecmd"))
  209.             {
  210.                 SendReply(player, "You do not have permission to use this command!");
  211.                 return;
  212.             }
  213.             if(args.Length != 1)
  214.             {
  215.                 SendReply(player, "Syntax: /rulesto \"target\" ");
  216.                 return;
  217.             }
  218.             BasePlayer target = BasePlayer.Find(args[0]);
  219.             if(target == null)
  220.             {
  221.                 SendReply(player, "Player not found!");
  222.                 return;
  223.             }
  224.             string msg = "";
  225.             foreach(var rule in Config["Messages", "RULES_MESSAGE"] as List<object>)
  226.             msg = msg + rule.ToString() + "\n \n";
  227.             UseUI(target, msg.ToString());
  228.             SendReply(player, "You have displayed the rules to <color=orange> " + target.displayName + "</color>");
  229.            
  230.         }      
  231.            
  232.         [ChatCommand("rule")]
  233.         void cmdRule(BasePlayer player, string cmd, string[] args)
  234.         {
  235.             string msg = "";
  236.             foreach(var rule in Config["Messages", "RULES_MESSAGE"] as List<object>)
  237.             msg = msg + rule.ToString() + "\n \n";
  238.             UseUI(player, msg.ToString());
  239.         }
  240.  
  241.         void DisplayUI(BasePlayer player)
  242.         {
  243.             if (player.HasPlayerFlag(BasePlayer.PlayerFlags.ReceivingSnapshot))
  244.             {
  245.                 timer.In(1, () => DisplayUI(player));
  246.             }
  247.             else
  248.             {
  249.                 string steamId = Convert.ToString(player.userID);
  250.                 if(displayoneveryconnect == true)
  251.                 {
  252.                     string msg = "";
  253.                     foreach(var rule in Config["Messages", "RULES_MESSAGE"] as List<object>)
  254.                     msg = msg + rule.ToString() + "\n \n";
  255.                     UseUI(player, msg.ToString());
  256.                 }
  257.                 else
  258.                 {          
  259.                     if(data.Players.Contains(steamId)) return;
  260.                     string msg = "";
  261.                     foreach(var rule in Config["Messages", "RULES_MESSAGE"] as List<object>)
  262.                     msg = msg + rule.ToString() + "\n \n";
  263.                     UseUI(player, msg.ToString());
  264.                     data.Players.Add(steamId); 
  265.                     Interface.GetMod().DataFileSystem.WriteObject("RulesGUIdata", data);
  266.                 }
  267.             }
  268.         }
  269.        
  270.        
  271.         void OnPlayerInit(BasePlayer player)       
  272.         {
  273.             DisplayUI(player);     
  274.         }
  275.     }
  276. }
Add Comment
Please, Sign In to add comment