nivex

Untitled

Jun 18th, 2021 (edited)
599
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using Newtonsoft.Json;
  2. using Oxide.Core.Libraries;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using Time = UnityEngine.Time;
  7.  
  8. namespace Oxide.Plugins
  9. {
  10.     [Info("Raidable Bases Announcements", "nivex", "0.1.1")]
  11.     [Description("Outputs all Raidable Bases announcements to a Discord channel.")]
  12.     public class RaidableBasesAnnouncements : RustPlugin
  13.     {
  14.         private void Init()
  15.         {
  16.             Unsubscribe(nameof(OnPlayerPvpDelayEntry));
  17.             Unsubscribe(nameof(OnPlayerPvpDelayExpired));
  18.             Unsubscribe(nameof(OnPlayerEnteredRaidableBase));
  19.             Unsubscribe(nameof(OnPlayerExitedRaidableBase));
  20.             Unsubscribe(nameof(OnRaidableBaseDespawn));
  21.             Unsubscribe(nameof(OnRaidableBaseDespawned));
  22.             Unsubscribe(nameof(OnRaidableBaseEnded));
  23.             Unsubscribe(nameof(OnRaidableBaseStarted));
  24.             Unsubscribe(nameof(OnRaidablePlayerCorpse));
  25.         }
  26.  
  27.         private void OnServerInitialized(bool isStartup)
  28.         {
  29.             if (!CanSendDiscordMessage())
  30.             {
  31.                 return;
  32.             }
  33.  
  34.             /*if (config.OnPlayerPvpDelayEntry)
  35.             {
  36.                 Subscribe(nameof(OnPlayerPvpDelayEntry));
  37.             }
  38.  
  39.             if (config.OnPlayerPvpDelayExpired)
  40.             {
  41.                 Subscribe(nameof(OnPlayerPvpDelayExpired));
  42.             }*/
  43.  
  44.             if (config.OnPlayerEnteredRaidableBase)
  45.             {
  46.                 Subscribe(nameof(OnPlayerEnteredRaidableBase));
  47.             }
  48.  
  49.             if (config.OnPlayerExitedRaidableBase)
  50.             {
  51.                 Subscribe(nameof(OnPlayerExitedRaidableBase));
  52.             }
  53.  
  54.             if (config.OnRaidableBaseDespawn)
  55.             {
  56.                 Subscribe(nameof(OnRaidableBaseDespawn));
  57.             }
  58.  
  59.             if (config.OnRaidableBaseDespawned)
  60.             {
  61.                 Subscribe(nameof(OnRaidableBaseDespawned));
  62.             }
  63.  
  64.             if (config.OnRaidableBaseEnded)
  65.             {
  66.                 Subscribe(nameof(OnRaidableBaseEnded));
  67.             }
  68.  
  69.             if (config.OnRaidableBaseStarted)
  70.             {
  71.                 Subscribe(nameof(OnRaidableBaseStarted));
  72.             }
  73.  
  74.             if (config.OnRaidablePlayerCorpse)
  75.             {
  76.                 Subscribe(nameof(OnRaidablePlayerCorpse));
  77.             }
  78.         }
  79.  
  80.         private void OnPlayerPvpDelayEntry(BasePlayer player)
  81.         {
  82.             // set time
  83.         }
  84.  
  85.         private void OnPlayerPvpDelayExpired(BasePlayer player)
  86.         {
  87.             // reset time
  88.         }
  89.  
  90.         private void OnPlayerEnteredRaidableBase(BasePlayer player, Vector3 raidPos, bool allowPvp)
  91.         {
  92.             SendDiscordMessage(_("OnPlayerEnteredRaidableBase", player.displayName, PositionToGrid(raidPos), raidPos));
  93.         }
  94.  
  95.         private void OnPlayerExitedRaidableBase(BasePlayer player, Vector3 raidPos, bool allowPVP)
  96.         {
  97.             SendDiscordMessage(_("OnPlayerExitedRaidableBase", player.displayName, PositionToGrid(raidPos), raidPos));
  98.         }
  99.  
  100.         private void OnRaidableBaseDespawn(Vector3 raidPos, float despawnTime, string identifier)
  101.         {
  102.             SendDiscordMessage(_("OnRaidableBaseDespawn", PositionToGrid(raidPos), raidPos, despawnTime - Time.realtimeSinceStartup));
  103.         }
  104.  
  105.         private void OnRaidableBaseDespawned(Vector3 raidPos)
  106.         {
  107.             SendDiscordMessage(_("OnRaidableBaseDespawned", PositionToGrid(raidPos), raidPos));
  108.         }
  109.  
  110.         private void OnRaidableBaseEnded(Vector3 raidPos, int mode)
  111.         {
  112.             var player = BasePlayer.activePlayerList.FirstOrDefault(x => x.Distance(raidPos) <= 50f);
  113.  
  114.             SendDiscordMessage(_("OnRaidableBaseEnded", GetDifficulty(mode), player?.displayName ?? "a player", PositionToGrid(raidPos), raidPos));
  115.         }
  116.  
  117.         private void OnRaidableBaseEnded(Vector3 raidPos, int mode, BasePlayer player)
  118.         {
  119.             SendDiscordMessage(_("OnRaidableBaseEnded", GetDifficulty(mode), player.displayName, PositionToGrid(raidPos), raidPos));
  120.         }
  121.  
  122.         private void OnRaidableBaseStarted(Vector3 raidPos, int mode, float spawnTime)
  123.         {
  124.             SendDiscordMessage(_("OnRaidableBaseStarted", GetDifficulty(mode), PositionToGrid(raidPos), raidPos));
  125.         }
  126.  
  127.         private void OnRaidablePlayerCorpse(BasePlayer player, DroppedItemContainer container)
  128.         {
  129.             SendDiscordMessage(_("OnRaidablePlayerCorpse", player.displayName, PositionToGrid(container.transform.position), container.transform.position));
  130.         }
  131.  
  132.         #region Discord Messages
  133.  
  134.         private bool CanSendDiscordMessage()
  135.         {
  136.             if (string.IsNullOrEmpty(config.WebhookUrl) || config.WebhookUrl == "https://support.discordapp.com/hc/en-us/articles/228383668-Intro-to-Webhooks")
  137.             {
  138.                 return false;
  139.             }
  140.  
  141.             return true;
  142.         }
  143.  
  144.         private string GetDifficulty(int mode)
  145.         {
  146.             switch (mode)
  147.             {
  148.                 case 0:
  149.                     return "easy";
  150.                 case 1:
  151.                     return "medium";
  152.                 case 2:
  153.                     return "hard";
  154.                 case 3:
  155.                     return "expert";
  156.                 case 4:
  157.                 default:
  158.                     return "nightmare";
  159.             }
  160.         }
  161.  
  162.         private static string PositionToGrid(Vector3 position) => PhoneController.PositionToGridCoord(position);
  163.  
  164.         private void SendDiscordMessage(string text)
  165.         {
  166.             try
  167.             {
  168.                 var headers = new Dictionary<string, string>() { { "Content-Type", "application/json" } };
  169.                 var body = new DiscordMessage(text).ToJson();
  170.  
  171.                 webrequest.Enqueue(config.WebhookUrl, body, (code, response) =>
  172.                 {
  173.                     if (code != 204)
  174.                     {
  175.                         PrintError(response);
  176.                     }
  177.                 }, this, RequestMethod.POST, headers);
  178.             }
  179.             catch { }
  180.         }
  181.  
  182.         #endregion Discord Messages
  183.  
  184.         #region Configuration
  185.  
  186.         protected override void LoadDefaultMessages()
  187.         {
  188.             lang.RegisterMessages(new Dictionary<string, string>
  189.             {
  190.                 //{"OnPlayerPvpDelayEntry", ""},
  191.                 //{"OnPlayerPvpDelayExpired", ""},
  192.                 {"OnPlayerEnteredRaidableBase", "{0} entered a raid at {1} {2}"},
  193.                 {"OnPlayerExitedRaidableBase", "{0} exited a raid at {1} {2}"},
  194.                 {"OnRaidableBaseDespawn", "A raid is going to despawn at {0} {1} in {2} seconds"},
  195.                 {"OnRaidableBaseDespawned", "A raid has despawned at {0} {1}"},
  196.                 {"OnRaidableBaseEnded", "A {0} raid has been completed by {1} at {2} {3}" },
  197.                 {"OnRaidableBaseStarted", "A {0} raid has started at {1} {2}"},
  198.                 {"OnRaidablePlayerCorpse", "{0} has died in a raid at {1} {2}"},
  199.             }, this);
  200.         }
  201.  
  202.         public string _(string key, params object[] args)
  203.         {
  204.             string message = lang.GetMessage(key, this, null);
  205.  
  206.             return args.Length > 0 ? string.Format(message, args) : message;
  207.         }
  208.  
  209.         private Configuration config;
  210.  
  211.         public class Configuration
  212.         {
  213.             [JsonProperty(PropertyName = "Message - Webhook URL")]
  214.             public string WebhookUrl { get; set; } = "https://support.discordapp.com/hc/en-us/articles/228383668-Intro-to-Webhooks";
  215.  
  216.             //[JsonProperty(PropertyName = "Show PVP Delay Entry Message")]
  217.             //public bool OnPlayerPvpDelayEntry { get; set; }
  218.  
  219.             //[JsonProperty(PropertyName = "Show PVP Delay Expired Message")]
  220.             //public bool OnPlayerPvpDelayExpired { get; set; }
  221.  
  222.             [JsonProperty(PropertyName = "Show Player Entered Base Message")]
  223.             public bool OnPlayerEnteredRaidableBase { get; set; } = true;
  224.  
  225.             [JsonProperty(PropertyName = "Show Player Exited Base Message")]
  226.             public bool OnPlayerExitedRaidableBase { get; set; }
  227.  
  228.             [JsonProperty(PropertyName = "Show Despawning Message")]
  229.             public bool OnRaidableBaseDespawn { get; set; }
  230.  
  231.             [JsonProperty(PropertyName = "Show Despawned Message")]
  232.             public bool OnRaidableBaseDespawned { get; set; }
  233.  
  234.             [JsonProperty(PropertyName = "Show Raid Ended Message")]
  235.             public bool OnRaidableBaseEnded { get; set; } = true;
  236.  
  237.             [JsonProperty(PropertyName = "Show Raid Started Message")]
  238.             public bool OnRaidableBaseStarted { get; set; } = true;
  239.  
  240.             [JsonProperty(PropertyName = "Show Player Died Message")]
  241.             public bool OnRaidablePlayerCorpse { get; set; }
  242.         }
  243.  
  244.         public class DiscordMessage
  245.         {
  246.             [JsonProperty("content")]
  247.             public string Content { get; set; }
  248.  
  249.             public DiscordMessage(string content)
  250.             {
  251.                 Content = content;
  252.             }
  253.  
  254.             public string ToJson() => JsonConvert.SerializeObject(this);
  255.         }
  256.  
  257.         protected override void LoadConfig()
  258.         {
  259.             base.LoadConfig();
  260.  
  261.             try
  262.             {
  263.                 config = Config.ReadObject<Configuration>();
  264.             }
  265.             catch
  266.             {
  267.  
  268.             }
  269.  
  270.             if (config == null)
  271.             {
  272.                 LoadDefaultConfig();
  273.             }
  274.  
  275.             SaveConfig();
  276.         }
  277.  
  278.         protected override void SaveConfig() => Config.WriteObject(config);
  279.  
  280.         protected override void LoadDefaultConfig() => config = new Configuration();
  281.  
  282.         #endregion Configuration
  283.     }
  284. }
RAW Paste Data