Skip to content

Commit 69f74e0

Browse files
committed
fix rich presence crashing in... lots of circumstances (issue #7)
1 parent 6d5b0ea commit 69f74e0

File tree

3 files changed

+32
-27
lines changed

3 files changed

+32
-27
lines changed

src/main/java/dev/lycanea/mwonmod/Mwonmod.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void onInitializeClient() {
9595
KeyBindings.setup();
9696

9797
ClientTickEvents.END_CLIENT_TICK.register(client -> {
98-
DiscordManager.updateStatus();
98+
DiscordManager.tick();
9999
if (client.player != null && client.player.getScoreboardTeam() != null && onMelonKing()) {
100100
List<String> playerJoins = client.player.getScoreboardTeam().getPlayerList().stream()
101101
.filter(item -> !players.contains(item))

src/main/java/dev/lycanea/mwonmod/util/discord/DiscordListener.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.jagrosh.discordipc.IPCListener;
55
import com.jagrosh.discordipc.entities.*;
66
import com.jagrosh.discordipc.exceptions.NoDiscordClientException;
7+
import org.json.JSONObject;
78

89
public class DiscordListener implements IPCListener {
910
private boolean connected = false;
@@ -25,6 +26,18 @@ public void onDisconnect(IPCClient client, Throwable t) {
2526
}
2627
}
2728

29+
@Override
30+
public void onClose(IPCClient client, JSONObject json) {
31+
connected = false;
32+
if (DiscordManager.enabled) {
33+
try {
34+
client.connect(DiscordBuild.ANY);
35+
} catch (NoDiscordClientException e) {
36+
throw new RuntimeException(e);
37+
}
38+
}
39+
}
40+
2841
public boolean isConnected() {
2942
return connected;
3043
}

src/main/java/dev/lycanea/mwonmod/util/discord/DiscordManager.java

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.lycanea.mwonmod.util.discord;
22

3+
import com.jagrosh.discordipc.entities.pipe.PipeStatus;
34
import dev.lycanea.mwonmod.Mwonmod;
45
import static dev.lycanea.mwonmod.Mwonmod.LOGGER;
56
import dev.lycanea.mwonmod.Config;
@@ -22,25 +23,33 @@ public class DiscordManager {
2223
public static boolean enabled = Config.HANDLER.instance().discordRichPresence;
2324

2425
public static void initialise() {
25-
if (!enabled) return;
26-
2726
if (client == null) {
2827
client = new IPCClient(1376119105136103496L);
2928
client.setListener(listener);
3029
}
30+
}
3131

32-
try {
33-
client.connect(DiscordBuild.ANY);
34-
LOGGER.info("Connected Discord Client");
35-
} catch (NoDiscordClientException e) {
36-
throw new RuntimeException("No Discord client found", e);
32+
public static void tick() {
33+
if (client != null) {
34+
if (!listener.isConnected() && enabled) {
35+
try {
36+
client.connect(DiscordBuild.ANY);
37+
LOGGER.info("Connected Discord Client");
38+
} catch (NoDiscordClientException | RuntimeException ignored) {}
39+
}
40+
if (listener.isConnected() && !enabled) {
41+
client.close();
42+
}
43+
}
44+
if (enabled && listener.isConnected()) {
45+
DiscordManager.updateStatus();
3746
}
3847
}
3948

4049
public static void updateStatus() {
50+
if (!listener.isConnected()) return;
4151
if (GameState.melonJoin != null && Config.HANDLER.instance().discordRichPresence && Mwonmod.onMelonKing()) {
4252
// get ready for a shit ton of ternary statements
43-
setEnabled(true);
4453
if (GameState.beta_plot) {
4554
setStatus("On Beta Plot",
4655
"Playing around and breaking things",
@@ -77,15 +86,11 @@ public static void updateStatus() {
7786
"Running MwonMod " + ((FabricLoader.getInstance().getModContainer("mwonmod").map(ModContainer::getMetadata).map(ModMetadata::getVersion).map(Object::toString).orElse("0.0").equals("0.0.0")) ? "Development":FabricLoader.getInstance().getModContainer("mwonmod").map(ModContainer::getMetadata).map(ModMetadata::getVersion).map(Object::toString).orElse("0.0")),
7887
((Config.HANDLER.instance().richPresencePathIcon && GameState.currentPath != null) ? GameState.currentPath.toLowerCase():null),
7988
((Config.HANDLER.instance().richPresencePathIcon && GameState.currentPath != null) ? "Path: " + GameState.currentPath:null));
80-
} else {
81-
setEnabled(false);
8289
}
8390
}
8491

8592
public static void setStatus(String state, String details, OffsetDateTime startTimestamp, String largeImageKey, String largeImageText, String smallImageKey, String smallImageText) {
86-
if (!enabled || client == null) return;
87-
88-
if (!listener.isConnected()) return;
93+
if (!enabled || client == null || client.getStatus() == PipeStatus.CONNECTED || !listener.isConnected()) return;
8994

9095
RichPresence presence = new RichPresence.Builder()
9196
.setState(state)
@@ -97,17 +102,4 @@ public static void setStatus(String state, String details, OffsetDateTime startT
97102

98103
client.sendRichPresence(presence);
99104
}
100-
101-
public static void setEnabled(boolean value) {
102-
if (enabled == value) return; // No change
103-
104-
enabled = value;
105-
106-
if (!enabled && client != null) {
107-
client.close();
108-
client = null; // Clean up
109-
} else if (enabled && client == null) {
110-
initialise();
111-
}
112-
}
113105
}

0 commit comments

Comments
 (0)