@@ -6,7 +6,6 @@ import com.jagrosh.discordipc.entities.pipe.PipeStatus
6
6
import com.jagrosh.discordipc.exceptions.NoDiscordClientException
7
7
import com.lambda.capeapi.CapeType
8
8
import com.lambda.client.LambdaMod
9
- import com.lambda.client.event.events.ShutdownEvent
10
9
import com.lambda.client.module.Category
11
10
import com.lambda.client.module.Module
12
11
import com.lambda.client.util.InfoCalculator
@@ -22,7 +21,6 @@ import com.lambda.client.util.threads.BackgroundScope
22
21
import com.lambda.client.util.threads.runSafeR
23
22
import com.lambda.client.util.threads.safeListener
24
23
import com.lambda.commons.utils.MathUtils
25
- import com.lambda.event.listener.listener
26
24
import net.minecraft.client.Minecraft
27
25
import net.minecraftforge.fml.common.gameevent.TickEvent
28
26
import java.time.OffsetDateTime
@@ -42,14 +40,26 @@ object DiscordRPC : Module(
42
40
VERSION , WORLD , DIMENSION , USERNAME , HEALTH , HUNGER , SERVER_IP , COORDS , SPEED , HELD_ITEM , FPS , TPS , NONE
43
41
}
44
42
45
- private val ipc = IPCClient (LambdaMod .APP_ID )
43
+ // Not using "by lazy" to be able to catch failure in onEnable
44
+ private lateinit var ipc: IPCClient
45
+ private var initialised = false
46
46
private val rpcBuilder = RichPresence .Builder ()
47
47
.setLargeImage(" default" , " lambda-client.com" )
48
48
private val timer = TickTimer (TimeUnit .SECONDS )
49
49
private val job = BackgroundJob (" Discord RPC" , 5000L ) { updateRPC() }
50
50
51
51
init {
52
52
onEnable {
53
+ if (! initialised) {
54
+ try {
55
+ ipc = IPCClient (LambdaMod .APP_ID )
56
+ initialised = true
57
+ } catch (e: UnsatisfiedLinkError ) {
58
+ error(" Failed to initialise DiscordRPC due to missing native library" , e)
59
+ disable()
60
+ return @onEnable
61
+ }
62
+ }
53
63
start()
54
64
}
55
65
@@ -64,10 +74,6 @@ object DiscordRPC : Module(
64
74
" Do NOT use this if you do not want your coords displayed" )
65
75
}
66
76
}
67
-
68
- listener<ShutdownEvent > {
69
- end()
70
- }
71
77
}
72
78
73
79
private fun start () {
@@ -82,16 +88,17 @@ object DiscordRPC : Module(
82
88
83
89
LambdaMod .LOG .info(" Discord RPC initialised successfully" )
84
90
} catch (e: NoDiscordClientException ) {
85
- LambdaMod .LOG .error(" No discord client found for RPC, stopping" )
86
- MessageSendHelper .sendErrorMessage(" No discord client found for RPC, stopping" )
91
+ error(" No discord client found for RPC, stopping" )
87
92
disable()
88
93
}
89
94
}
90
95
91
96
private fun end () {
97
+ if (! initialised || ipc.status != PipeStatus .CONNECTED ) return
92
98
BackgroundScope .cancel(job)
93
99
94
100
LambdaMod .LOG .info(" Shutting down Discord RPC..." )
101
+ ipc.close()
95
102
}
96
103
97
104
private fun showCoordsConfirm (): Boolean {
@@ -173,6 +180,12 @@ object DiscordRPC : Module(
173
180
}
174
181
}
175
182
183
+ // Change to Throwable? if more logging is ever needed
184
+ private fun error (message : String , error : UnsatisfiedLinkError ? = null) {
185
+ MessageSendHelper .sendErrorMessage(message)
186
+ LambdaMod .LOG .error(message, error)
187
+ }
188
+
176
189
fun setCustomIcons (capeType : CapeType ? ) {
177
190
val text = when (capeType) {
178
191
CapeType .CONTRIBUTOR -> " Contributor"
0 commit comments