/** * OxidationEventAnnouncer - Announces important events * Copyright (C) 2021 kasvoton [kasvoton@stinkfist.org] * * All Rights Reserved. * DO NOT DISTRIBUTE THIS SOFTWARE. * * You should have received a copy of the EULA along with this software. * If not, see . * * * ################################# * ### I AM AVAILABLE FOR HIRING ### * ################################# * * IF YOU WANT A CUSTOM PLUGIN FOR YOUR SERVER GET INTO CONTACT WITH ME SO * WE CAN DISCUSS YOUR NEED IN DETAIL. I CAN BUILD PLUGINS FROM SCRATCH OR * MODIFY EXISTING ONES DEPENDING ON THE COMPLEXITY. * */ using System; using System.Collections; using System.Collections.Generic; using System.Linq; using Facepunch; using Oxide.Core.Plugins; using Oxide.Game.Rust.Cui; using UnityEngine; namespace Oxide.Plugins { [Info("OxidationEventAnnouncer", "kasvoton", "1.1.1")] [Description("Project Oxidation :: Announces important events")] public class OxidationEventAnnouncer : RustPlugin { // // --- UMOD EVENTS ----------------------------------------------------- // private void Unload() { foreach (BasePlayer Player in BasePlayer.activePlayerList) UnityEngine.Object.Destroy(Player.GetComponent()); } private void OnEntitySpawned(BaseNetworkable Entity) { if (Entity is CargoShip) BroadcastEvent(Locale("CargoShip")); else if (Entity is BradleyAPC) BroadcastEvent(Locale("BradleyAPC")); else if (Entity is CargoPlane) BroadcastEvent(Locale("CargoPlane")); else if (Entity is AttackHelicopter) BroadcastEvent(Locale("AttackHelicopter")); else if (Entity is PatrolHelicopter) BroadcastEvent(Locale("PatrolHelicopter")); else if (Entity is CH47HelicopterAIController) { CH47HelicopterAIController CH47 = Entity as CH47HelicopterAIController; if (CH47.landingTarget == Vector3.zero) { BroadcastEvent(Locale("ChinookHelicopter")); return; } string Target = string.Empty; List list = Pool.GetList(); Vis.Entities(CH47.landingTarget, 25f, list); switch (list.FirstOrDefault()?.GetFrequency()) { case 4765: Target = Locale("Small"); break; case 4768: Target = Locale("Large"); break; } BroadcastEvent(Locale("Oilrig", Target)); } } // // --- API ------------------------------------------------------------- // [HookMethod("Broadcast")] private void BroadcastEvent(string Message, string Background = null, string Foreground = null) => ServerMgr.Instance.StartCoroutine(DoBroadcast(Message, Background, Foreground)); // // --- COROUTINES ------------------------------------------------------ // private IEnumerator DoBroadcast(string Message, string Background = null, string Foreground = null) { List Players = BasePlayer.activePlayerList.ToList(); foreach (BasePlayer Player in Players) { OxidationEventBehaviour Announcer = Player.GetComponent(); if (Announcer != null) UnityEngine.Object.Destroy(Announcer); yield return CoroutineEx.waitForEndOfFrame; Announcer = Player.gameObject.AddComponent(); if (Announcer == null) continue; if (Background != null) Announcer.BackgroundColor = Background; if (Foreground != null) Announcer.TextColor = Foreground; Announcer.Message = Message; Announcer.enabled = true; } } // // --- BEHAVIOURS ------------------------------------------------------ // public class OxidationEventBehaviour : MonoBehaviour { public string Message { get; set; } protected BasePlayer Player; protected float LastClientUpdate; protected CuiElementContainer Container; protected string _TextColor = "1.0 1.0 1.0 0.85"; public string TextColor { get { return _TextColor; } set { _TextColor = HexToColor(value, 0.85); return; } } public string _BackgroundColor = "0.803 0.274 0.196 0.850"; public string BackgroundColor { get { return _BackgroundColor; } set { _BackgroundColor = HexToColor(value, 0.85); return; } } internal OxidationEventBehaviour() { enabled = false; } internal void Awake() => Player = GetComponent(); internal void Start() => LastClientUpdate = Time.realtimeSinceStartup; internal void OnEnable() { Container = new CuiElementContainer(); const float FadeIn = 0.5f, FadeOut = 0.5f; Container.Add(new CuiElement { Name = $"UIEvent", Parent = "Hud", FadeOut = FadeOut, Components = { new CuiImageComponent { Color = $"{BackgroundColor}", FadeIn = FadeIn }, new CuiRectTransformComponent { AnchorMin = "0.000 0.830", AnchorMax = "1.000 0.900" } } }); Container.Add(new CuiElement { Name = $"UIEvent.border.top", Parent = $"UIEvent", FadeOut = FadeOut, Components = { new CuiImageComponent { Color = "1.0 1.0 1.0 0.85", FadeIn = FadeIn }, new CuiRectTransformComponent { AnchorMin = "0.000 0.990", AnchorMax = "1.000 1.000" } } }); Container.Add(new CuiElement { Name = $"UIEvent.border.bottom", Parent = $"UIEvent", FadeOut = FadeOut, Components = { new CuiImageComponent { Color = "1.0 1.0 1.0 0.85", FadeIn = FadeIn }, new CuiRectTransformComponent { AnchorMin = "0.000 0.000", AnchorMax = "1.000 0.010" } } }); Container.Add(new CuiElement { Name = $"UIEvent.text", Parent = $"UIEvent", FadeOut = FadeOut, Components = { new CuiTextComponent { Font = "RobotoCondensed-Bold.ttf", Align = TextAnchor.MiddleCenter, Color = $"{TextColor}", FadeIn = FadeIn, Text = Message, FontSize = 26, }, new CuiRectTransformComponent { AnchorMin = "0.000 0.000", AnchorMax = "1.000 1.000" } } }); CuiHelper.AddUi(Player, Container); } internal void FixedUpdate() { if (Time.realtimeSinceStartup - LastClientUpdate >= 5.0f) UnityEngine.Object.Destroy(this); } internal void OnDisable() { foreach (CuiElement Element in Container.Reverse()) CuiHelper.DestroyUi(Player, Element.Name); } internal static string HexToColor(string Color, double Alpha = 1.0f) { if (Color.StartsWith("#")) Color = Color.TrimStart('#'); System.Globalization.NumberStyles Style = System.Globalization.NumberStyles.AllowHexSpecifier; int R = int.Parse(Color.Substring(0, 2), Style); int G = int.Parse(Color.Substring(2, 2), Style); int B = int.Parse(Color.Substring(4, 2), Style); return $"{(double)R / 255f} {(double)G / 255f} {(double)B / 255f} {Alpha}"; } } // // --- LOCALIZATION ------------------------------------------------------------------------------------------------------ // internal string Locale(string Key, params object[] args) => string.Format(lang.GetMessage(Key, this), args); protected override void LoadDefaultMessages() { lang.RegisterMessages(new Dictionary { ["CargoShip"] = "A Cargo Ship has been spotted near shore", ["BradleyAPC"] = "A M3 Bradley is now patrolling the island", ["CargoPlane"] = "An incoming Cargo Plane has been spotted", ["AttackHelicopter"] = "A new attack helicopter was detected on the island", ["PatrolHelicopter"] = "An UH-1Y Venom has been spotted patrolling the island", ["ChinookHelicopter"] = "A CH47 Chinook has been spotted patrolling the island", ["Oilrig"] = "A CH47 Chinook has been spotted heading to the {0} Oilrig", ["Small"] = "small", ["Large"] = "large", }, this); } } }