@@ -20,20 +20,22 @@ 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.ConnectionEvent
24- import com.lambda.event.listener.UnsafeListener.Companion.listenUnsafeConcurrently
23+ import com.lambda.event.events.RenderEvent
24+ import com.lambda.event.events.WorldEvent
25+ import com.lambda.event.listener.SafeListener.Companion.listen
26+ import com.lambda.event.listener.SafeListener.Companion.listenConcurrently
2527import com.lambda.network.api.v1.models.Party
2628import com.lambda.module.Module
29+ import com.lambda.module.modules.client.Network.isDiscordLinked
2730import com.lambda.module.modules.client.Network.updateToken
2831import com.lambda.module.tag.ModuleTag
2932import com.lambda.network.api.v1.endpoints.createParty
30- import com.lambda.network.api.v1.endpoints.editParty
33+ import com.lambda.network.api.v1.endpoints.deleteParty
3134import com.lambda.network.api.v1.endpoints.joinParty
3235import com.lambda.network.api.v1.endpoints.leaveParty
3336import com.lambda.network.api.v1.endpoints.linkDiscord
37+ import com.lambda.network.api.v1.endpoints.partyUpdates
3438import com.lambda.threading.runConcurrent
35- import com.lambda.threading.runSafe
36- import com.lambda.util.Communication
3739import com.lambda.util.Communication.toast
3840import com.lambda.util.Communication.warn
3941import com.lambda.util.Nameable
@@ -64,7 +66,6 @@ object Discord : Module(
6466
6567 /* Party settings */
6668 private val createByDefault by setting(" Create By Default" , true , description = " Create parties on" ) { page == Page .Party }
67- private val maxPlayers by setting(" Max Players" , 10 , 2 .. 20 ) { page == Page .Party }.onValueChange { _, _ -> if (player.isPartyOwner) edit() } // ToDo: Avoid spam requests
6869
6970 val rpc = KDiscordIPC (Lambda .APP_ID , scope = EventFlow .lambdaScope)
7071
@@ -76,72 +77,72 @@ object Discord : Module(
7677 val PlayerEntity .isPartyOwner
7778 get() = uuid == currentParty?.leader?.uuid
7879
79- val PlayerEntity .isInParty
80- get() = currentParty?.players?.any { it.uuid == uuid }
80+ val PlayerEntity .isInParty: Boolean
81+ get() = currentParty?.players?.any { it.uuid == uuid } ? : false
8182
8283 init {
8384 rpc.subscribe()
8485
85- listenUnsafeConcurrently<ConnectionEvent .Connect .Post > {
86- // FixMe: We have to wait even though this is the last event until toSafe() != null
87- // because of timing
88- delay(3000 )
89- runSafe { handleLoop() }
90- }
91-
92- onEnable { runConcurrent { startDiscord(); handleLoop() } }
93- onDisable { stopDiscord() }
94- }
95-
96- fun createParty () {
97- if (discordAuth == null ) return toast(" Can not interact with the api (are you offline?)" )
86+ // ToDo: Nametag for friends when ref/ui is merged
87+ // listen<RenderEvent.World>()
9888
99- val (party, error) = createParty(maxPlayers, true )
100- if (error != null ) toast(" Failed to create a party: ${error.message} " , Communication .LogLevel .WARN )
89+ listenConcurrently<WorldEvent .Join > {
90+ // If the player is in a party and this most likely means that the `onEnable`
91+ // block ran and is already handling the activity
92+ if (player.isInParty) return @listenConcurrently
93+ handleLoop()
94+ }
10195
102- currentParty = party
96+ onEnable { runConcurrent { start(); handleLoop() } }
97+ onDisable { stop() }
10398 }
10499
105100 /* *
106- * Joins a new party with the invitation ID
101+ * Creates a new party, leaves or delete the current party if there is one
107102 */
108- fun join (id : String ) {
109- if (discordAuth == null ) return toast(" Can not interact with the api (are you offline?)" )
103+ fun SafeContext.partyCreate () {
104+ if (! isDiscordLinked) return warn(" You did not link your discord account" )
105+ if (! player.isInParty) {
106+ if (player.isPartyOwner) deleteParty() else leaveParty()
107+ return
108+ }
110109
111- val (party, error) = joinParty(id )
112- if (error != null ) toast (" Failed to join the party: ${error.message} " , Communication . LogLevel . WARN )
110+ val (party, error) = createParty( )
111+ if (error != null ) warn (" Failed to create a party: ${error.errorData} " )
113112
114113 currentParty = party
114+ partyUpdates { currentParty = it }
115115 }
116116
117117 /* *
118- * Triggers a party edit request if you are the owner
118+ * Joins a new party with the invitation ID
119119 */
120- fun edit ( ) {
121- if (discordAuth == null ) return toast( " Can not interact with the api (are you offline?) " )
120+ fun SafeContext. partyJoin ( id : String ) {
121+ if (! isDiscordLinked ) return warn( " You did not link your discord account " )
122122
123- val (party, error) = editParty(maxPlayers )
124- if (error != null ) toast (" Failed to edit the party: ${error.message} " , Communication . LogLevel . WARN )
123+ val (party, error) = joinParty(id )
124+ if (error != null ) warn (" Failed to join the party: ${error.errorData} " )
125125
126126 currentParty = party
127+ partyUpdates { currentParty = it }
127128 }
128129
129130 /* *
130131 * Leaves the current party
131132 */
132- fun leave () {
133- if (discordAuth == null || currentParty == null ) return toast(" Can not interact with the api (are you offline?)" )
133+ fun SafeContext.partyLeave () {
134+ if (! isDiscordLinked) return warn(" You did not link your discord account" )
135+ if (! player.isInParty) return warn(" You are not in a party" )
134136
135137 val (_, error) = leaveParty()
136- if (error != null ) return toast (" Failed to edit the party: ${error.message} " , Communication . LogLevel . WARN )
138+ if (error != null ) return warn (" Failed to leave the party: ${error.errorData} " )
137139
138140 currentParty = null
139141 }
140142
141- private suspend fun startDiscord () {
143+ private suspend fun start () {
142144 if (rpc.connected) return
143145
144- rpc.subscribe()
145146 runConcurrent { rpc.connect() } // TODO: Create a function that will wait until x seconds has passed or if the connection is successful
146147 delay(1000 )
147148
@@ -152,18 +153,15 @@ object Discord : Module(
152153 return toast(" Failed to link the discord account to the minecraft auth" )
153154 }
154155
155- authResp?. let { updateToken(it) }
156+ updateToken(authResp)
156157 discordAuth = auth
157158 }
158159
159- private fun stopDiscord () {
160- if (! rpc.connected) return
161-
162- rpc.disconnect()
160+ private fun stop () {
161+ if (rpc.connected) rpc.disconnect()
163162 }
164163
165164 private fun KDiscordIPC.subscribe () {
166- // ToDO: Get party on join event
167165 on<ReadyEvent > {
168166 subscribe(DiscordEvent .VoiceChannelSelect )
169167 subscribe(DiscordEvent .VoiceStateCreate )
@@ -181,14 +179,17 @@ object Discord : Module(
181179 }
182180
183181 private suspend fun SafeContext.handleLoop () {
184- if (createByDefault) createParty(maxPlayers)
182+ if (isDiscordLinked) {
183+ if (createByDefault) createParty()
184+ partyUpdates { currentParty = it }
185+ }
185186
186187 while (rpc.connected) {
187188 update()
188189 delay(delay)
189190 }
190191
191- leave ()
192+ if (isDiscordLinked) leaveParty ()
192193 }
193194
194195 private suspend fun SafeContext.update () {
@@ -229,8 +230,6 @@ object Discord : Module(
229230 HEALTH ({ " ${mc.player?.health ? : 0 } HP" }),
230231 HUNGER ({ " ${mc.player?.hungerManager?.foodLevel ? : 0 } Hunger" }),
231232 DIMENSION ({ dimensionName }),
232- COORDINATES ({ " Coords: ${player.blockPos.toShortString()} " }),
233- SERVER ({ mc.currentServerEntry?.address ? : " Not Connected" }),
234233 FPS ({ " ${mc.currentFps} FPS" });
235234 }
236235}
0 commit comments