From 239900451df5ea05f7f8611870bf6a39748499bb Mon Sep 17 00:00:00 2001 From: Daenges <57369924+Daenges@users.noreply.github.com> Date: Sun, 18 Apr 2021 16:14:09 +0200 Subject: [PATCH] Improvements for Discord Webhook (#1544) * Improve caching and remove unused functions * Save Player UUID before leaving Save the UUID when they leave and save it until they join again. * Clear the cache after disconnect * Update MinecraftClient/config/ChatBots/DiscordWebhook.cs --- .../config/ChatBots/DiscordWebhook.cs | 48 ++++++++++++++----- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/MinecraftClient/config/ChatBots/DiscordWebhook.cs b/MinecraftClient/config/ChatBots/DiscordWebhook.cs index 42a14c66..467a8810 100644 --- a/MinecraftClient/config/ChatBots/DiscordWebhook.cs +++ b/MinecraftClient/config/ChatBots/DiscordWebhook.cs @@ -22,6 +22,7 @@ class WebhoookSettings private Dictionary> messageCache = new Dictionary>(); private Dictionary messageContains = new Dictionary(); private Dictionary messageFrom = new Dictionary(); + private Dictionary namesToUuidMojangCache = new Dictionary(); private List ignoredPlayers = new List(); #endregion @@ -96,16 +97,11 @@ class WebhoookSettings } public Dictionary GetMessageContains() { return this.messageContains; } - public void SetMessageContains(Dictionary value) { this.messageContains = value; } - public Dictionary GetMessageFrom() { return this.messageFrom; } - public void SetMessageFrom(Dictionary value) { this.messageFrom = value; } - public Dictionary> GetCachedMessages() { return this.messageCache; } - public void SetCachedMessages(Dictionary> value) { this.messageCache = value; } - public Dictionary GetSkinModes() { return this.skinModes; } - + public Dictionary GetNamesToUuidMojangCache() { return this.namesToUuidMojangCache; } + public void resetUUIDCache() { namesToUuidMojangCache.Clear(); } public List GetIgnoredPlayers() { return ignoredPlayers; } } @@ -126,12 +122,19 @@ class SkinAPI /// public string GetUUIDFromMojang(string name) { - WebClient wc = new WebClient(); - try + if (settings.GetNamesToUuidMojangCache().ContainsKey(name)) + return settings.GetNamesToUuidMojangCache()[name]; + + using (WebClient wc = new WebClient()) { - return Json.ParseJson(wc.DownloadString("https://api.mojang.com/users/profiles/minecraft/" + name)).Properties["id"].StringValue; + try + { + string uuid = Json.ParseJson(wc.DownloadString("https://api.mojang.com/users/profiles/minecraft/" + name)).Properties["id"].StringValue; + settings.GetNamesToUuidMojangCache().Add(name, uuid); + return uuid; + } + catch (Exception) { return "00000000000000000000000000000000"; } } - catch (Exception) { return "00000000000000000000000000000000"; } } /// @@ -311,6 +314,28 @@ class DiscordWebhook : ChatBot } } + public override void OnPlayerJoin(Guid uuid, string name) + { + if (settings.GetNamesToUuidMojangCache().ContainsKey(name)) + { + settings.GetNamesToUuidMojangCache().Remove(name); + } + } + + public override void OnPlayerLeave(Guid uuid, string name) + { + if (!settings.GetNamesToUuidMojangCache().ContainsKey(name)) + { + settings.GetNamesToUuidMojangCache().Add(name, uuid.ToString()); + } + } + + public override bool OnDisconnect(DisconnectReason reason, string message) + { + settings.resetUUIDCache(); + return false; + } + public override void GetText(string text) { if (settings.Togglesending) @@ -730,3 +755,4 @@ class DiscordWebhook : ChatBot else { return GetHelp(); } } } +}