@@ -20,140 +20,57 @@ package com.lambda.module.modules.client
2020import com.lambda.Lambda
2121import com.lambda.context.SafeContext
2222import com.lambda.event.EventFlow
23- import com.lambda.event.events.RenderEvent
2423import com.lambda.event.events.WorldEvent
25- import com.lambda.event.listener.SafeListener.Companion.listen
26- import com.lambda.event.listener.SafeListener.Companion.listenConcurrently
27- import com.lambda.network.api.v1.models.Party
24+ import com.lambda.event.listener.SafeListener.Companion.listenOnce
2825import com.lambda.module.Module
29- import com.lambda.module.modules.client.Network.isDiscordLinked
3026import com.lambda.module.modules.client.Network.updateToken
3127import com.lambda.module.tag.ModuleTag
32- import com.lambda.network.api.v1.endpoints.createParty
33- import com.lambda.network.api.v1.endpoints.deleteParty
34- import com.lambda.network.api.v1.endpoints.joinParty
35- import com.lambda.network.api.v1.endpoints.leaveParty
3628import com.lambda.network.api.v1.endpoints.linkDiscord
37- import com.lambda.network.api.v1.endpoints.partyUpdates
3829import com.lambda.threading.runConcurrent
39- import com.lambda.util.Communication.toast
4030import com.lambda.util.Communication.warn
4131import com.lambda.util.Nameable
4232import com.lambda.util.extension.dimensionName
33+ import com.lambda.util.extension.worldName
4334import dev.cbyrne.kdiscordipc.KDiscordIPC
44- import dev.cbyrne.kdiscordipc.core.event.DiscordEvent
45- import dev.cbyrne.kdiscordipc.core.event.impl.ReadyEvent
4635import dev.cbyrne.kdiscordipc.core.packet.inbound.impl.AuthenticatePacket
4736import dev.cbyrne.kdiscordipc.data.activity.*
4837import kotlinx.coroutines.delay
49- import net.minecraft.entity.player.PlayerEntity
5038
5139object Discord : Module(
5240 name = " Discord" ,
5341 description = " Discord Rich Presence configuration" ,
5442 defaultTags = setOf(ModuleTag .CLIENT ),
55- // enabledByDefault = true, // ToDo: Bring this back on beta release
43+ // enabledByDefault = true, // ToDo: Bring this back on beta release
5644) {
57- private val page by setting(" Page" , Page .General )
58-
59- /* General settings */
60- private val delay by setting(" Update Delay" , 15000L , 15000L .. 30000L , 100L , unit = " ms" ) { page == Page .General }
61- private val showTime by setting(" Show Time" , true , description = " Show how long you have been playing for." ) { page == Page .General }
62- private val line1Left by setting(" Line 1 Left" , LineInfo .WORLD ) { page == Page .General }
63- private val line1Right by setting(" Line 1 Right" , LineInfo .USERNAME ) { page == Page .General }
64- private val line2Left by setting(" Line 2 Left" , LineInfo .DIMENSION ) { page == Page .General }
65- private val line2Right by setting(" Line 2 Right" , LineInfo .FPS ) { page == Page .General }
66-
67- /* Party settings */
68- private val createByDefault by setting(" Create By Default" , true , description = " Create parties on" ) { page == Page .Party }
45+ private val delay by setting(" Update Delay" , 5000L , 5000L .. 30000L , 100L , unit = " ms" )
46+ private val showTime by setting(" Show Time" , true , description = " Show how long you have been playing for." )
47+ private val line1Left by setting(" Line 1 Left" , LineInfo .WORLD )
48+ private val line1Right by setting(" Line 1 Right" , LineInfo .USERNAME )
49+ private val line2Left by setting(" Line 2 Left" , LineInfo .DIMENSION )
50+ private val line2Right by setting(" Line 2 Right" , LineInfo .FPS )
6951
7052 val rpc = KDiscordIPC (Lambda .APP_ID , scope = EventFlow .lambdaScope)
7153
7254 private var startup = System .currentTimeMillis()
7355
7456 var discordAuth: AuthenticatePacket .Data ? = null ; private set
75- var currentParty: Party ? = null ; private set
76-
77- val PlayerEntity .isPartyOwner
78- get() = uuid == currentParty?.leader?.uuid
79-
80- val PlayerEntity .isInParty: Boolean
81- get() = currentParty?.players?.any { it.uuid == uuid } ? : false
8257
8358 init {
84- rpc.subscribe()
85-
86- // ToDo: Nametag for friends when ref/ui is merged
87- // listen<RenderEvent.World>()
88-
89- listenConcurrently<WorldEvent .Join > {
59+ listenOnce<WorldEvent .Join > {
9060 // If the player is in a party and this most likely means that the `onEnable`
9161 // block ran and is already handling the activity
92- if (rpc.connected && player.isInParty) return @listenConcurrently
93-
94- start()
95- handleLoop()
96- stop()
97- }
62+ if (rpc.connected) return @listenOnce false
9863
99- onDisable { stop() }
100- onEnable { runConcurrent { start(); handleLoop() } }
101- }
64+ runConcurrent {
65+ start()
66+ handleLoop()
67+ }
10268
103- /* *
104- * Creates a new party, leaves or delete the current party if there is one
105- */
106- fun SafeContext.partyCreate () {
107- if (! isDiscordLinked) return warn(" You did not link your discord account" )
108- if (player.isInParty) {
109- if (player.isPartyOwner) deleteParty() else leaveParty()
69+ return @listenOnce true
11070 }
11171
112- val (party, error) = createParty()
113- if (error != null ) return warn(" Failed to create a party: ${error.exception} " )
114-
115- currentParty = party
116- partyUpdates { currentParty = it }
117- }
118-
119- /* *
120- * Joins a new party with the invitation ID
121- */
122- fun SafeContext.partyJoin (id : String ) {
123- if (! isDiscordLinked) return warn(" You did not link your discord account" )
124-
125- val (party, error) = joinParty(id)
126- if (error != null ) return warn(" Failed to join the party: ${error.errorData} " )
127-
128- currentParty = party
129- partyUpdates { currentParty = it }
130- }
131-
132- /* *
133- * Leaves the current party
134- */
135- fun SafeContext.partyLeave () {
136- if (! isDiscordLinked) return warn(" You did not link your discord account" )
137- if (! player.isInParty) return warn(" You are not in a party" )
138-
139- val (_, error) = leaveParty()
140- if (error != null ) return warn(" Failed to leave the party: ${error.errorData} " )
141-
142- currentParty = null
143- }
144-
145- /* *
146- * Deletes the current party
147- */
148- fun SafeContext.partyDelete () {
149- if (! isDiscordLinked) return warn(" You did not link your discord account" )
150- if (! player.isInParty) return warn(" You are not in a party" )
151-
152- val (_, error) = deleteParty()
153- if (error != null ) return warn(" Failed to delete the party: ${error.exception} " )
154-
155- currentParty = null
156-
72+ onEnable { runConcurrent { start(); handleLoop() } }
73+ onDisable { stop() }
15774 }
15875
15976 private suspend fun start () {
@@ -164,10 +81,7 @@ object Discord : Module(
16481
16582 val auth = rpc.applicationManager.authenticate()
16683 val (authResp, error) = linkDiscord(discordToken = auth.accessToken)
167- if (error != null ) {
168- warn(error.message.toString())
169- return toast(" Failed to link the discord account to the minecraft auth" )
170- }
84+ if (error != null ) return warn(" Failed to link the discord account to the minecraft auth" )
17185
17286 updateToken(authResp)
17387 discordAuth = auth
@@ -177,71 +91,29 @@ object Discord : Module(
17791 if (rpc.connected) rpc.disconnect()
17892 }
17993
180- private fun KDiscordIPC.subscribe () {
181- on<ReadyEvent > {
182- subscribe(DiscordEvent .VoiceChannelSelect )
183- subscribe(DiscordEvent .VoiceStateCreate )
184- subscribe(DiscordEvent .VoiceStateUpdate )
185- subscribe(DiscordEvent .VoiceStateDelete )
186- subscribe(DiscordEvent .VoiceSettingsUpdate )
187- subscribe(DiscordEvent .VoiceConnectionStatus )
188- subscribe(DiscordEvent .SpeakingStart )
189- subscribe(DiscordEvent .SpeakingStop )
190- subscribe(DiscordEvent .ActivityJoin )
191- subscribe(DiscordEvent .ActivityJoinRequest )
192- subscribe(DiscordEvent .OverlayUpdate )
193- // subscribe(DiscordEvent.ActivitySpectate) // Unsupported
194- }
195- }
196-
19794 private suspend fun SafeContext.handleLoop () {
198- if (isDiscordLinked) {
199- if (createByDefault) createParty()
200- partyUpdates { currentParty = it }
201- }
202-
20395 while (rpc.connected) {
20496 update()
20597 delay(delay)
20698 }
207-
208- if (isDiscordLinked) leaveParty()
20999 }
210100
211101 private suspend fun SafeContext.update () {
212- val party = currentParty
213-
214102 rpc.activityManager.setActivity {
215103 details = " ${line1Left.value(this @update)} | ${line1Right.value(this @update)} " .take(128 )
216104 state = " ${line2Left.value(this @update)} | ${line2Right.value(this @update)} " .take(128 )
217105
218106 largeImage(" lambda" , Lambda .VERSION )
219107 smallImage(" https://mc-heads.net/avatar/${mc.gameProfile.id} /nohelm" , mc.gameProfile.name)
220-
221- if (party != null ) {
222- party(party.id.toString(), party.players.size, 20 ) // Placeholder while
223- secrets(party.joinSecret)
224- } else {
225- button(" Download" , " https://github.com/lambda-client/lambda" )
226- }
108+ button(" Download" , " https://github.com/lambda-client/lambda" )
227109
228110 if (showTime) timestamps(startup)
229111 }
230112 }
231113
232- private enum class Page {
233- General , Party
234- }
235-
236114 private enum class LineInfo (val value : SafeContext .() -> String ) : Nameable {
237115 VERSION ({ Lambda .VERSION }),
238- WORLD ({
239- when {
240- mc.currentServerEntry != null -> " Multiplayer"
241- mc.isIntegratedServerRunning -> " Singleplayer"
242- else -> " Main Menu"
243- }
244- }),
116+ WORLD ({ worldName }),
245117 USERNAME ({ mc.session.username }),
246118 HEALTH ({ " ${mc.player?.health ? : 0 } HP" }),
247119 HUNGER ({ " ${mc.player?.hungerManager?.foodLevel ? : 0 } Hunger" }),
0 commit comments