diff --git a/AutoBroadcast.cs b/AutoBroadcast.cs index c8616cd..dfe44d7 100644 --- a/AutoBroadcast.cs +++ b/AutoBroadcast.cs @@ -1,34 +1,31 @@ -using Exiled.API.Enums; -using Exiled.API.Features; +using LabApi.Loader.Features.Plugins; +using LabApi.Loader.Features.Plugins.Enums; namespace AutoBroadcast; public class AutoBroadcast : Plugin { - public static AutoBroadcast Instance { get; set; } = null!; + private Handler? _eventHandler; + + public static AutoBroadcast Instance { get; private set; } = null!; public override string Name => "AutoBroadcast"; - public override string Prefix => "AutoBroadcast"; + public override string Description => "Auto-Broadcast plugin"; + public override string Author => "@misfiy"; - public override Version RequiredExiledVersion => new(9, 2, 1); public override Version Version => new(1, 6, 3); - public override PluginPriority Priority => PluginPriority.Last; - - private Handler eventHandler { get; set; } = null!; + public override Version RequiredApiVersion { get; } = new(LabApi.Features.LabApiProperties.CompiledVersion); + public override LoadPriority Priority => LoadPriority.Lowest; - public override void OnEnabled() + public override void Enable() { Instance = this; - eventHandler = new(); - - base.OnEnabled(); + _eventHandler = new Handler(); } - public override void OnDisabled() + public override void Disable() { - eventHandler = null!; + _eventHandler = null!; Instance = null!; - - base.OnDisabled(); } } \ No newline at end of file diff --git a/AutoBroadcast.csproj b/AutoBroadcast.csproj index 8e8ff8c..584b292 100644 --- a/AutoBroadcast.csproj +++ b/AutoBroadcast.csproj @@ -8,17 +8,19 @@ x64 - - - - - - - - - - + + + + + + + + + + + + \ No newline at end of file diff --git a/Config.cs b/Config.cs index a5f7de2..604f191 100644 --- a/Config.cs +++ b/Config.cs @@ -1,9 +1,9 @@ -using Exiled.API.Interfaces; +using AutoBroadcast.Objects; using PlayerRoles; namespace AutoBroadcast; -public class Config : IConfig +public class Config { public bool IsEnabled { get; set; } = true; public bool Debug { get; set; } = false; @@ -20,11 +20,11 @@ public class Config : IConfig public BroadCassie? RoundStart { get; set; } = new() { - Broadcast = new() + Broadcast = new BroadcastSystem { Message = "Round has started!", }, - Cassie = new() + Cassie = new CassieBroadcast { Message = "Containment breach detected", } @@ -32,11 +32,11 @@ public class Config : IConfig public BroadCassie? LastPlayerOnTeam { get; set; } = new() { - Broadcast = new() + Broadcast = new BroadcastSystem { Message = "You are the last remaining on your team!", }, - Cassie = new() + Cassie = new CassieBroadcast { Message = "There is 1 {role} remaining!" } @@ -45,9 +45,9 @@ public class Config : IConfig public Dictionary Delayed { get; set; } = new() { { - 60, new() + 60, new BroadCassie { - Broadcast = new() + Broadcast = new BroadcastSystem { Message = "60 seconds have passed!", } @@ -58,9 +58,9 @@ public class Config : IConfig public Dictionary Intervals { get; set; } = new() { { - 30, new() + 30, new BroadCassie { - Broadcast = new() + Broadcast = new BroadcastSystem { Message = "I show every 30 seconds!", UseHints = true, @@ -72,9 +72,9 @@ public class Config : IConfig public Dictionary SpawnBroadcasts { get; set; } = new() { { - RoleTypeId.Spectator, new() + RoleTypeId.Spectator, new BroadCassie { - Broadcast = new() + Broadcast = new BroadcastSystem { Message = "You died!", Duration = 5, @@ -83,9 +83,9 @@ public class Config : IConfig } }, { - RoleTypeId.Overwatch, new() + RoleTypeId.Overwatch, new BroadCassie { - Broadcast = new() + Broadcast = new BroadcastSystem { Message = "Overwatch initiated.", Duration = 6, diff --git a/Handler.cs b/Handler.cs index 91b5bdd..0a3b0c8 100644 --- a/Handler.cs +++ b/Handler.cs @@ -1,51 +1,54 @@ -using Exiled.Events.EventArgs.Player; -using Exiled.Events.EventArgs.Server; -using Exiled.API.Features; +using AutoBroadcast.Objects; using MEC; -using Exiled.Events.EventArgs.Map; +using GameCore; +using LabApi.Events.Arguments.PlayerEvents; +using LabApi.Events.Arguments.ServerEvents; +using LabApi.Events.Handlers; +using LabApi.Features.Console; +using LabApi.Features.Wrappers; using PlayerRoles; namespace AutoBroadcast; public class Handler { - public static List Coroutines = new(); - - private static Config config => AutoBroadcast.Instance.Config; + public static readonly List Coroutines = new(); + + private static Config? Config => AutoBroadcast.Instance.Config; public Handler() { - Exiled.Events.Handlers.Server.RoundEnded += OnRoundEnded; - Exiled.Events.Handlers.Server.RoundStarted += OnRoundStart; - Exiled.Events.Handlers.Player.Verified += OnVerified; - Exiled.Events.Handlers.Player.Spawned += OnSpawned; - Exiled.Events.Handlers.Player.Died += OnDied; + ServerEvents.RoundEnded += OnRoundEnded; + ServerEvents.RoundStarted += OnRoundStart; + PlayerEvents.Joined += OnJoined; + PlayerEvents.Spawned += OnSpawned; + PlayerEvents.Death += OnDied; - if (AutoBroadcast.Instance.Config.NtfAnnouncementCassie != "DISABLED" && AutoBroadcast.Instance.Config.NtfAnnouncementCassieNoScps != "DISABLED") + if (Config?.NtfAnnouncementCassie != "DISABLED" && Config?.NtfAnnouncementCassieNoScps != "DISABLED") Exiled.Events.Handlers.Map.AnnouncingNtfEntrance += OnAnnouncingNtf; } ~Handler() { - Exiled.Events.Handlers.Server.RoundEnded -= OnRoundEnded; - Exiled.Events.Handlers.Server.RoundStarted -= OnRoundStart; - Exiled.Events.Handlers.Player.Verified -= OnVerified; - Exiled.Events.Handlers.Player.Spawned -= OnSpawned; - Exiled.Events.Handlers.Player.Died -= OnDied; + ServerEvents.RoundEnded -= OnRoundEnded; + ServerEvents.RoundStarted -= OnRoundStart; + PlayerEvents.Joined -= OnJoined; + PlayerEvents.Spawned -= OnSpawned; + PlayerEvents.Death -= OnDied; - if (AutoBroadcast.Instance.Config.NtfAnnouncementCassie != "DISABLED" || AutoBroadcast.Instance.Config.NtfAnnouncementCassieNoScps != "DISABLED") + if (Config?.NtfAnnouncementCassie != "DISABLED" || Config?.NtfAnnouncementCassieNoScps != "DISABLED") Exiled.Events.Handlers.Map.AnnouncingNtfEntrance -= OnAnnouncingNtf; } public void OnRoundStart() { - Log.Debug("Round started"); - config.RoundStart?.Broadcast?.Show(); - config.RoundStart?.Cassie?.Send(); + Logger.Debug("Round started"); + Config?.RoundStart?.Broadcast?.Show(); + Config?.RoundStart?.Cassie?.Send(); - foreach (KeyValuePair kvp in config.Delayed) + foreach (KeyValuePair kvp in Config?.Delayed ?? new Dictionary()) { - Log.Debug("Running coroutine"); + Logger.Debug("Running coroutine"); Coroutines.Add(Timing.CallDelayed(kvp.Key, delegate { kvp.Value.Broadcast?.Show(); @@ -53,33 +56,33 @@ public void OnRoundStart() })); } - foreach (KeyValuePair kvp in config.Intervals) + foreach (KeyValuePair kvp in Config.Intervals) { - Log.Debug("Running coroutine"); + Logger.Debug("Running coroutine"); Coroutines.Add(Timing.RunCoroutine(DoIntervalBroadcast(kvp.Key, kvp.Value))); } } public void OnRoundEnded(RoundEndedEventArgs ev) { - Log.Debug("Killing coroutines"); + Logger.Debug("Killing coroutines"); foreach(CoroutineHandle coroutine in Coroutines) { Timing.KillCoroutines(coroutine); - Log.Debug("Killed a coroutine successfully!"); + Logger.Debug("Killed a coroutine successfully!"); } Coroutines.Clear(); - Log.Debug("Killed all coroutines"); + Logger.Debug("Killed all coroutines"); } - public void OnVerified(VerifiedEventArgs ev) + public void OnJoined(PlayerJoinedEventArgs ev) { - if (config.JoinMessage != null && config.JoinMessage.Duration > 0 && !config.JoinMessage.Message.IsEmpty()) + if (Config?.JoinMessage != null && Config.JoinMessage.Duration > 0 && !Config.JoinMessage.Message.IsEmpty()) { - Log.Debug("Showing Verified message to " + ev.Player.Nickname); - string message = config.JoinMessage.Message.Replace("%name%", ev.Player.Nickname); - ev.Player.Broadcast(config.JoinMessage.Duration, message, default, config.JoinMessage.Override); + Logger.Debug("Showing Verified message to " + ev.Player.Nickname); + string message = Config.JoinMessage.Message.Replace("%name%", ev.Player.Nickname); + Config.JoinMessage.Show(ev.Player); }; } @@ -87,15 +90,15 @@ public void OnAnnouncingNtf(AnnouncingNtfEntranceEventArgs ev) { string cassieMessage = string.Empty; - if (ev.ScpsLeft == 0 && AutoBroadcast.Instance.Config.NtfAnnouncementCassieNoScps != "DISABLED") + if (ev.ScpsLeft == 0 && Config.NtfAnnouncementCassieNoScps != "DISABLED") { - Log.Debug("No SCPs cassie"); + Logger.Debug("No SCPs cassie"); ev.IsAllowed = false; - cassieMessage = AutoBroadcast.Instance.Config.NtfAnnouncementCassieNoScps; + cassieMessage = Config.NtfAnnouncementCassieNoScps; } - else if (ev.ScpsLeft >= 1 && AutoBroadcast.Instance.Config.NtfAnnouncementCassie != "DISABLED") + else if (ev.ScpsLeft >= 1 && Config.NtfAnnouncementCassie != "DISABLED") { - Log.Debug("1 or more SCPs cassie"); + Logger.Debug("1 or more SCPs cassie"); ev.IsAllowed = false; cassieMessage = AutoBroadcast.Instance.Config.NtfAnnouncementCassie; } @@ -111,33 +114,33 @@ public void OnAnnouncingNtf(AnnouncingNtfEntranceEventArgs ev) Cassie.Message(cassieMessage, isSubtitles: true); } - public void OnSpawned(SpawnedEventArgs ev) + public void OnSpawned(PlayerSpawnedEventArgs ev) { - if (config.SpawnBroadcasts.TryGetValue(ev.Player.Role.Type, out BroadCassie broadcast)) + if (Config.SpawnBroadcasts.TryGetValue(ev.Player.Role, out BroadCassie broadcast)) { - Log.Debug($"Spawn Broadcast showing for {ev.Player.Role.Type}"); + Logger.Debug($"Spawn Broadcast showing for {ev.Player.Role}"); broadcast.Broadcast?.Show(ev.Player); broadcast.Cassie?.Send(); } } - public void OnDied(DiedEventArgs ev) + public void OnDied(PlayerDeathEventArgs ev) { if (ev.TargetOldRole.GetTeam() is not Team.SCPs and not Team.OtherAlive and not Team.Dead) { List playerTeam = Player.Get(ev.TargetOldRole.GetTeam()).ToList(); if (playerTeam.Count == 1) { - Log.Debug($"{ev.TargetOldRole.GetTeam()} has 1 person remaining on their team, starting LastPlayerAlive Broadcast/Cassie"); - config.LastPlayerOnTeam?.Broadcast?.Show(playerTeam.FirstOrDefault()); - config.LastPlayerOnTeam?.Cassie?.Send(ev.TargetOldRole); + Logger.Debug($"{ev.TargetOldRole.GetTeam()} has 1 person remaining on their team, starting LastPlayerAlive Broadcast/Cassie"); + Config?.LastPlayerOnTeam?.Broadcast?.Show(playerTeam.FirstOrDefault()); + Config?.LastPlayerOnTeam?.Cassie?.Send(ev.TargetOldRole); } } } public IEnumerator DoIntervalBroadcast(int interval, BroadCassie message) { - while (Round.IsStarted) + while (Round.IsRoundStarted) { yield return Timing.WaitForSeconds(interval); diff --git a/Objects.cs b/Objects.cs deleted file mode 100644 index dc92b56..0000000 --- a/Objects.cs +++ /dev/null @@ -1,72 +0,0 @@ -using Exiled.API.Features; -using PlayerRoles; -using System.ComponentModel; -using Exiled.API.Extensions; - -namespace AutoBroadcast; - -public class BroadCassie -{ - public BroadcastSystem? Broadcast { get; set; } - public CassieBroadcast? Cassie { get; set; } -} - -public class BroadcastSystem -{ - [Description("How long the hint/broadcast should show")] - public ushort Duration { get; set; } = 3; - [Description("The message shown on the broadcast")] - public string Message { get; set; } = string.Empty; - [Description("Override broadcasts")] - public bool Override { get; set; } - [Description("Whether or not to use hints instead")] - public bool UseHints { get; set; } - - public void Show() - { - if (UseHints) - Map.ShowHint(Message, Duration); - else - Map.Broadcast(Duration, Message, default, Override); - } - - public void Show(Player player) - { - if (UseHints) - player.ShowHint(Message, Duration); - else - player.Broadcast(Duration, Message, default, Override); - } -} - -public class CassieBroadcast -{ - [Description("The CASSIE message to be sent")] - public string Message { get; set; } = ""; - [Description("The text to be shown")] - public string Translation { get; set; } = string.Empty; - [Description("Whether or not to hide the subtitle for the cassie message")] - public bool ShowSubtitles { get; set; } = true; - - public void Send() - { - Cassie.MessageTranslated(Message,Translation.IsEmpty() ? Message : Translation, isSubtitles: ShowSubtitles); - } - - public void Send(Player player) - { - player.MessageTranslated(Message, Translation.IsEmpty() ? Message : Translation, isSubtitles: ShowSubtitles); - } - - /// - /// Sends the CASSIE to all players replacing "{role}" with 's full name. - /// - /// Used by - public void Send(RoleTypeId role) - { - Cassie.MessageTranslated( - Message.Replace("{role}", RoleExtensions.GetTeam(role).ToString()), - Translation.IsEmpty() ? Message.Replace("{role}", RoleExtensions.GetTeam(role).ToString()) : Translation.Replace("{role}", role.GetFullName()), - isSubtitles: ShowSubtitles); - } -} \ No newline at end of file diff --git a/Objects/BroadCassie.cs b/Objects/BroadCassie.cs new file mode 100644 index 0000000..591f98c --- /dev/null +++ b/Objects/BroadCassie.cs @@ -0,0 +1,7 @@ +namespace AutoBroadcast.Objects; + +public class BroadCassie +{ + public BroadcastSystem? Broadcast { get; set; } + public CassieBroadcast? Cassie { get; set; } +} \ No newline at end of file diff --git a/Objects/BroadcastSystem.cs b/Objects/BroadcastSystem.cs new file mode 100644 index 0000000..97f0a5a --- /dev/null +++ b/Objects/BroadcastSystem.cs @@ -0,0 +1,33 @@ +using System.ComponentModel; +using LabApi.Features.Wrappers; + +namespace AutoBroadcast.Objects; + +public class BroadcastSystem +{ + [Description("How long the hint/broadcast should show")] + public ushort Duration { get; set; } = 3; + + [Description("The message shown on the broadcast")] + public string Message { get; set; } = string.Empty; + + [Description("Whether it should override - Only works for Broadcast")] + public bool Override { get; set; } + + [Description("Whether or not to use hints instead")] + public bool UseHints { get; set; } + + public void Show() + { + foreach (Player player in Player.List) + Show(player); + } + + public void Show(Player player) + { + if (UseHints) + player.SendHint(Message, Duration); + else + player.SendBroadcast(Message, Duration, default, Override); + } +} \ No newline at end of file diff --git a/Objects/CassieBroadcast.cs b/Objects/CassieBroadcast.cs new file mode 100644 index 0000000..07c2b6d --- /dev/null +++ b/Objects/CassieBroadcast.cs @@ -0,0 +1,40 @@ +using System.ComponentModel; +using LabApi.Features.Extensions; +using LabApi.Features.Wrappers; +using PlayerRoles; + +namespace AutoBroadcast.Objects; + +public class CassieBroadcast +{ + [Description("The CASSIE message to be sent")] + public string Message { get; set; } = ""; + + [Description("The text to be shown")] + public string Translation { get; set; } = string.Empty; + + [Description("Whether or not to hide the subtitle for the cassie message")] + public bool ShowSubtitles { get; set; } = true; + + public void Send() + { + Cassie.Message(Message, isSubtitles: ShowSubtitles, customSubtitles: Translation); + } + + public void Send(Player player) + { + player.MessageTranslated(Message, Translation.IsEmpty() ? Message : Translation, isSubtitles: ShowSubtitles); + } + + /// + /// Sends the CASSIE to all players replacing "{role}" with 's full name. + /// + /// Used by + public void Send(RoleTypeId role) + { + Cassie.Message( + Message.Replace("{role}", role.GetTeam().ToString()), + isSubtitles: ShowSubtitles, + customSubtitles: Translation.Replace("{role}", role.GetFullName())); + } +} \ No newline at end of file