Skip to content

Commit

Permalink
Announcement Code Cleanup
Browse files Browse the repository at this point in the history
- Additional code cleanup for Announcements mod
  • Loading branch information
data-bomb committed Feb 15, 2024
1 parent 6317dd9 commit 18536c7
Showing 1 changed file with 36 additions and 15 deletions.
51 changes: 36 additions & 15 deletions Si_Announcements/Si_Announcements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ You should have received a copy of the GNU General Public License
using SilicaAdminMod;
using System.Linq;

[assembly: MelonInfo(typeof(Announcements), "Server Announcements", "1.1.6", "databomb", "https://github.com/data-bomb/Silica")]
[assembly: MelonInfo(typeof(Announcements), "Server Announcements", "1.1.7", "databomb", "https://github.com/data-bomb/Silica")]
[assembly: MelonGame("Bohemia Interactive", "Silica")]
[assembly: MelonOptionalDependencies("Admin Mod")]

Expand Down Expand Up @@ -84,10 +84,11 @@ public override void OnInitializeMelon()

announcementFileLine.Add(announcement);
}

announcementsText = announcementFileLine.ToArray();
}

MelonLogger.Msg("Loaded announcements.txt file with " + announcementsText.Length + " announcements");
MelonLogger.Msg("Loaded announcements.txt file with " + announcementsText.Length + " announcements.");

double interval = _Announcements_SecondsBetweenMessages.Value * 1000.0f;
announcementTimer = new System.Timers.Timer(interval);
Expand Down Expand Up @@ -142,29 +143,25 @@ private static void Postfix(MusicJukeboxHandler __instance)
{
timerExpired = false;

if (_Announcements_ShowIfLastChatWasAnnouncement != null && !_Announcements_ShowIfLastChatWasAnnouncement.Value && lastChatMessage != null && announcementsText != null)
if (announcementsText == null)
{
return;
}

if (!_Announcements_ShowIfLastChatWasAnnouncement.Value)
{
// check if the last chat message was an announcement
bool lastMessageWasAnnouncement = announcementsText.Any(m => m == lastChatMessage);
if (lastMessageWasAnnouncement)
if (IsPreviousChatMessageAnnouncement(lastChatMessage))
{
MelonLogger.Msg("Skipping Announcement - Repeated Message");
return;
}
}

announcementCount++;

if (announcementsText == null)
{
return;
}

String thisAnnouncement = announcementsText[announcementCount % announcementsText.Length];
MelonLogger.Msg("Announcement: " + thisAnnouncement);
string nextAnnouncement = GetNextAnnouncement();

Player broadcastPlayer = HelperMethods.FindBroadcastPlayer();
broadcastPlayer.SendChatMessage(thisAnnouncement);
broadcastPlayer.SendChatMessage(nextAnnouncement);
}
}
catch (Exception exception)
Expand Down Expand Up @@ -207,11 +204,35 @@ public static void Postfix(Silica.UI.Chat __instance, Player __0, string __1, bo
}
}

private static bool IsPreviousChatMessageAnnouncement(string? previousChat)
{
if (previousChat == null || announcementsText == null)
{
return false;
}

return announcementsText.Any(m => m == previousChat);
}

private static void CreateStarterAnnouncementsFile(string filePath)
{
File.WriteAllText(filePath, "<color=#cc33ff>Server mods by databomb. Report issues in Discord or on GitHub.</color>\n");
}

private static string GetNextAnnouncement()
{
if (announcementsText == null)
{
return "";
}

announcementCount++;
string nextAnnouncement = announcementsText[announcementCount % announcementsText.Length];

MelonLogger.Msg("Announcement: " + nextAnnouncement);
return nextAnnouncement;
}

private static bool IsValidAnnouncement(string announcementText)
{
// ignore a line with only LF/CR
Expand Down

0 comments on commit 18536c7

Please sign in to comment.