@@ -6,7 +6,6 @@ import com.jagrosh.discordipc.entities.pipe.PipeStatus
66import com.jagrosh.discordipc.exceptions.NoDiscordClientException
77import com.lambda.capeapi.CapeType
88import com.lambda.client.LambdaMod
9- import com.lambda.client.event.events.ShutdownEvent
109import com.lambda.client.module.Category
1110import com.lambda.client.module.Module
1211import com.lambda.client.util.InfoCalculator
@@ -22,7 +21,6 @@ import com.lambda.client.util.threads.BackgroundScope
2221import com.lambda.client.util.threads.runSafeR
2322import com.lambda.client.util.threads.safeListener
2423import com.lambda.commons.utils.MathUtils
25- import com.lambda.event.listener.listener
2624import net.minecraft.client.Minecraft
2725import net.minecraftforge.fml.common.gameevent.TickEvent
2826import java.time.OffsetDateTime
@@ -42,14 +40,26 @@ object DiscordRPC : Module(
4240 VERSION , WORLD , DIMENSION , USERNAME , HEALTH , HUNGER , SERVER_IP , COORDS , SPEED , HELD_ITEM , FPS , TPS , NONE
4341 }
4442
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
4646 private val rpcBuilder = RichPresence .Builder ()
4747 .setLargeImage(" default" , " lambda-client.com" )
4848 private val timer = TickTimer (TimeUnit .SECONDS )
4949 private val job = BackgroundJob (" Discord RPC" , 5000L ) { updateRPC() }
5050
5151 init {
5252 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+ }
5363 start()
5464 }
5565
@@ -64,10 +74,6 @@ object DiscordRPC : Module(
6474 " Do NOT use this if you do not want your coords displayed" )
6575 }
6676 }
67-
68- listener<ShutdownEvent > {
69- end()
70- }
7177 }
7278
7379 private fun start () {
@@ -82,16 +88,17 @@ object DiscordRPC : Module(
8288
8389 LambdaMod .LOG .info(" Discord RPC initialised successfully" )
8490 } 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" )
8792 disable()
8893 }
8994 }
9095
9196 private fun end () {
97+ if (! initialised || ipc.status != PipeStatus .CONNECTED ) return
9298 BackgroundScope .cancel(job)
9399
94100 LambdaMod .LOG .info(" Shutting down Discord RPC..." )
101+ ipc.close()
95102 }
96103
97104 private fun showCoordsConfirm (): Boolean {
@@ -173,6 +180,12 @@ object DiscordRPC : Module(
173180 }
174181 }
175182
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+
176189 fun setCustomIcons (capeType : CapeType ? ) {
177190 val text = when (capeType) {
178191 CapeType .CONTRIBUTOR -> " Contributor"
0 commit comments