11package dev .lycanea .mwonmod .util .discord ;
22
3+ import com .jagrosh .discordipc .entities .pipe .PipeStatus ;
34import dev .lycanea .mwonmod .Mwonmod ;
45import static dev .lycanea .mwonmod .Mwonmod .LOGGER ;
56import 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