From 6713d9e8361fc8098b65b6306175f5b1511271bc Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Tue, 21 Nov 2023 17:14:39 -0800 Subject: [PATCH 1/2] Update DiscordRPCManager --- src/main/java/de/hysky/skyblocker/SkyblockerMod.java | 2 +- .../skyblocker/utils/discord/DiscordRPCManager.java | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/hysky/skyblocker/SkyblockerMod.java b/src/main/java/de/hysky/skyblocker/SkyblockerMod.java index ad3b6318e7..2791a88b16 100644 --- a/src/main/java/de/hysky/skyblocker/SkyblockerMod.java +++ b/src/main/java/de/hysky/skyblocker/SkyblockerMod.java @@ -120,7 +120,7 @@ public void onInitializeClient() { containerSolverManager.init(); statusBarTracker.init(); Scheduler.INSTANCE.scheduleCyclic(Utils::update, 20); - Scheduler.INSTANCE.scheduleCyclic(DiscordRPCManager::updateDataAndPresence, 100); + Scheduler.INSTANCE.scheduleCyclic(DiscordRPCManager::updateDataAndPresence, 200); Scheduler.INSTANCE.scheduleCyclic(TicTacToe::tick, 4); Scheduler.INSTANCE.scheduleCyclic(LividColor::update, 10); Scheduler.INSTANCE.scheduleCyclic(BackpackPreview::tick, 50); diff --git a/src/main/java/de/hysky/skyblocker/utils/discord/DiscordRPCManager.java b/src/main/java/de/hysky/skyblocker/utils/discord/DiscordRPCManager.java index df10a44dc2..236b5011d0 100644 --- a/src/main/java/de/hysky/skyblocker/utils/discord/DiscordRPCManager.java +++ b/src/main/java/de/hysky/skyblocker/utils/discord/DiscordRPCManager.java @@ -75,18 +75,20 @@ private static void initAndUpdatePresence(boolean initialization) { if (SkyblockerConfigManager.get().richPresence.enableRichPresence && Utils.isOnSkyblock()) { if (!DiscordIPC.isConnected()) { if (DiscordIPC.start(934607927837356052L, null)) { - LOGGER.info("Discord RPC started successfully"); + LOGGER.info("[Skyblocker] Discord RPC started successfully"); } else { - LOGGER.error("Discord RPC failed to start"); + if (initialization) { + LOGGER.error("[Skyblocker] Discord RPC failed to start"); + } return; } } DiscordIPC.setActivity(buildPresence()); } else if (DiscordIPC.isConnected()) { DiscordIPC.stop(); - LOGGER.info("Discord RPC stopped"); + LOGGER.info("[Skyblocker] Discord RPC stopped"); } else if (initialization) { - LOGGER.info("Discord RPC is currently disabled"); + LOGGER.info("[Skyblocker] Discord RPC is currently disabled"); } }); } From dc68230f38d7af52f0e5233d410f06310c04751d Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Thu, 23 Nov 2023 15:42:12 -0800 Subject: [PATCH 2/2] Close connection properly on discord close --- .../mixin/discordipc/ConnectionMixin.java | 20 +++++++++++++++++++ .../utils/discord/DiscordRPCManager.java | 8 ++++---- src/main/resources/skyblocker.mixins.json | 3 ++- 3 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 src/main/java/de/hysky/skyblocker/mixin/discordipc/ConnectionMixin.java diff --git a/src/main/java/de/hysky/skyblocker/mixin/discordipc/ConnectionMixin.java b/src/main/java/de/hysky/skyblocker/mixin/discordipc/ConnectionMixin.java new file mode 100644 index 0000000000..47f1cadce6 --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/mixin/discordipc/ConnectionMixin.java @@ -0,0 +1,20 @@ +package de.hysky.skyblocker.mixin.discordipc; + +import de.hysky.skyblocker.utils.discord.DiscordRPCManager; +import meteordevelopment.discordipc.DiscordIPC; +import meteordevelopment.discordipc.connection.UnixConnection; +import meteordevelopment.discordipc.connection.WinConnection; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; + +import java.io.IOException; + +@Mixin(value = {UnixConnection.class, WinConnection.class}, remap = false) +public class ConnectionMixin { + @Redirect(method = "write", at = @At(value = "INVOKE", target = "Ljava/io/IOException;printStackTrace()V")) + private void write(IOException e) { + DiscordIPC.stop(); + DiscordRPCManager.LOGGER.warn("[Skyblocker] Discord RPC failed to update activity, connection lost", e); + } +} diff --git a/src/main/java/de/hysky/skyblocker/utils/discord/DiscordRPCManager.java b/src/main/java/de/hysky/skyblocker/utils/discord/DiscordRPCManager.java index 236b5011d0..b2104fdc7b 100644 --- a/src/main/java/de/hysky/skyblocker/utils/discord/DiscordRPCManager.java +++ b/src/main/java/de/hysky/skyblocker/utils/discord/DiscordRPCManager.java @@ -75,10 +75,10 @@ private static void initAndUpdatePresence(boolean initialization) { if (SkyblockerConfigManager.get().richPresence.enableRichPresence && Utils.isOnSkyblock()) { if (!DiscordIPC.isConnected()) { if (DiscordIPC.start(934607927837356052L, null)) { - LOGGER.info("[Skyblocker] Discord RPC started successfully"); + LOGGER.info("[Skyblocker] Discord RPC connected successfully"); } else { if (initialization) { - LOGGER.error("[Skyblocker] Discord RPC failed to start"); + LOGGER.error("[Skyblocker] Discord RPC failed to connect"); } return; } @@ -86,9 +86,9 @@ private static void initAndUpdatePresence(boolean initialization) { DiscordIPC.setActivity(buildPresence()); } else if (DiscordIPC.isConnected()) { DiscordIPC.stop(); - LOGGER.info("[Skyblocker] Discord RPC stopped"); + LOGGER.info("[Skyblocker] Discord RPC disconnected"); } else if (initialization) { - LOGGER.info("[Skyblocker] Discord RPC is currently disabled"); + LOGGER.info("[Skyblocker] Discord RPC is currently disabled, will not connect"); } }); } diff --git a/src/main/resources/skyblocker.mixins.json b/src/main/resources/skyblocker.mixins.json index 4e7bfe16ed..d7ae26e162 100644 --- a/src/main/resources/skyblocker.mixins.json +++ b/src/main/resources/skyblocker.mixins.json @@ -36,7 +36,8 @@ "accessor.PlayerListHudAccessor", "accessor.RecipeBookWidgetAccessor", "accessor.ScreenAccessor", - "accessor.WorldRendererAccessor" + "accessor.WorldRendererAccessor", + "discordipc.ConnectionMixin" ], "injectors": { "defaultRequire": 1