Skip to content

Commit a2f9a66

Browse files
committed
Updated logic and fixed bugs
1 parent e757afc commit a2f9a66

File tree

5 files changed

+55
-70
lines changed

5 files changed

+55
-70
lines changed

common/src/main/kotlin/com/lambda/module/modules/client/Discord.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,15 @@ object Discord : Module(
8989
listenConcurrently<WorldEvent.Join> {
9090
// If the player is in a party and this most likely means that the `onEnable`
9191
// block ran and is already handling the activity
92-
if (player.isInParty) return@listenConcurrently
92+
if (rpc.connected && player.isInParty) return@listenConcurrently
93+
94+
start()
9395
handleLoop()
96+
stop()
9497
}
9598

96-
onEnable { runConcurrent { start(); handleLoop() } }
9799
onDisable { stop() }
100+
onEnable { runConcurrent { start(); handleLoop() } }
98101
}
99102

100103
/**
@@ -104,11 +107,10 @@ object Discord : Module(
104107
if (!isDiscordLinked) return warn("You did not link your discord account")
105108
if (player.isInParty) {
106109
if (player.isPartyOwner) deleteParty() else leaveParty()
107-
return
108110
}
109111

110112
val (party, error) = createParty()
111-
if (error != null) return warn("Failed to create a party: ${error.errorData}")
113+
if (error != null) return warn("Failed to create a party: ${error.exception}")
112114

113115
currentParty = party
114116
partyUpdates { currentParty = it }
@@ -148,7 +150,7 @@ object Discord : Module(
148150
if (!player.isInParty) return warn("You are not in a party")
149151

150152
val (_, error) = deleteParty()
151-
if (error != null) return warn("Failed to delete the party: ${error.errorData}")
153+
if (error != null) return warn("Failed to delete the party: ${error.exception}")
152154

153155
currentParty = null
154156

@@ -217,7 +219,7 @@ object Discord : Module(
217219
smallImage("https://mc-heads.net/avatar/${mc.gameProfile.id}/nohelm", mc.gameProfile.name)
218220

219221
if (party != null) {
220-
party(party.id.toString(), party.players.size, party.settings.maxPlayers)
222+
party(party.id.toString(), party.players.size, 20) // Placeholder while
221223
secrets(party.joinSecret)
222224
} else {
223225
button("Download", "https://github.com/lambda-client/lambda")

common/src/main/kotlin/com/lambda/module/modules/client/Network.kt

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package com.lambda.module.modules.client
1919

20+
import com.lambda.Lambda
2021
import com.lambda.Lambda.LOG
2122
import com.lambda.Lambda.gson
2223
import com.lambda.Lambda.mc
@@ -33,6 +34,7 @@ import com.lambda.network.api.v1.models.Authentication
3334
import com.lambda.module.Module
3435
import com.lambda.module.tag.ModuleTag
3536
import com.lambda.network.api.v1.models.Authentication.Data
37+
import com.lambda.util.Communication.info
3638
import com.lambda.util.extension.isOffline
3739
import net.minecraft.client.network.AllowedAddressResolver
3840
import net.minecraft.client.network.ClientLoginNetworkHandler
@@ -78,38 +80,50 @@ object Network : Module(
7880
}
7981

8082
listenOnceUnsafe<ConnectionEvent.Connect.Post> {
83+
if (!::hash.isInitialized) {
84+
if (!authenticate()) return@listenOnceUnsafe false
85+
}
86+
8187
// If we log in right as the client responds to the encryption request, we start
8288
// a race condition where the game server haven't acknowledged the packets
8389
// and posted to the sessionserver api
8490
val (authResponse, error) = login(mc.session.username, hash)
8591
if (error != null) {
86-
LOG.debug("Unable to authenticate with the API: ${error.message}")
92+
LOG.debug("Unable to authenticate: ${error.message}")
8793
return@listenOnceUnsafe false
8894
}
8995

9096
apiAuth = authResponse
9197
deserialized = gson.fromJson(String(Base64.getUrlDecoder().decode(accessToken.split(".")[1])), Data::class.java)
9298

93-
// Destroy the listener
99+
LOG.info("Successfully authenticated")
94100
true
95101
}
96102

97-
listenUnsafeConcurrently<ClientEvent.Startup> {
98-
if (mc.gameProfile.isOffline) return@listenUnsafeConcurrently
103+
listenUnsafeConcurrently<ClientEvent.Startup> { authenticate() }
104+
}
105+
106+
private fun authenticate(): Boolean {
107+
if (mc.gameProfile.isOffline) return true
99108

100-
val addddd = ServerAddress.parse(authServer)
101-
val connection = ClientConnection(CLIENTBOUND)
102-
val addr = AllowedAddressResolver.DEFAULT.resolve(addddd)
103-
.map { it.inetSocketAddress }.get()
109+
val addddd = ServerAddress.parse(authServer)
110+
val connection = ClientConnection(CLIENTBOUND)
111+
val addr = AllowedAddressResolver.DEFAULT.resolve(addddd)
112+
.map { it.inetSocketAddress }.get()
104113

114+
runCatching {
105115
ClientConnection.connect(addr, mc.options.shouldUseNativeTransport(), connection)
106116
.syncUninterruptibly()
117+
}.onFailure {
118+
return false
119+
}
107120

108-
val handler = ClientLoginNetworkHandler(connection, mc, null, null, false, null) { Text.empty() }
121+
val handler = ClientLoginNetworkHandler(connection, mc, null, null, false, null) { Text.empty() }
109122

110-
connection.connect(addr.hostName, addr.port, handler)
111-
connection.send(LoginHelloC2SPacket(mc.session.username, mc.session.uuidOrNull))
112-
}
123+
connection.connect(addr.hostName, addr.port, handler)
124+
connection.send(LoginHelloC2SPacket(mc.session.username, mc.session.uuidOrNull))
125+
126+
return true
113127
}
114128

115129
internal fun updateToken(auth: Authentication?) { apiAuth = auth }

common/src/main/kotlin/com/lambda/network/api/v1/endpoints/PartyUpdates.kt

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,35 @@
1818
package com.lambda.network.api.v1.endpoints
1919

2020
import com.lambda.Lambda
21+
import com.lambda.module.modules.client.Network.accessToken
2122
import com.lambda.module.modules.client.Network.apiUrl
2223
import com.lambda.module.modules.client.Network.apiVersion
2324
import com.lambda.network.api.v1.models.Party
25+
import com.lambda.threading.runConcurrent
2426
import java.net.URI
2527
import java.net.http.HttpClient
2628
import java.net.http.HttpRequest
2729
import java.net.http.HttpResponse
2830

2931
// Waiting for https://github.com/kittinunf/fuel/issues/989 before changing this
30-
fun partyUpdates(block: (Party) -> Unit) {
31-
HttpClient.newHttpClient().sendAsync(
32-
HttpRequest.newBuilder()
33-
.uri(URI.create("${apiUrl}/api/${apiVersion.value}/party/listen"))
34-
.header("Accept", "text/event-stream")
35-
.build(),
36-
HttpResponse.BodyHandlers.ofLines()
37-
).thenAccept { response ->
38-
response.body().forEach {
39-
if (!it.startsWith("data:")) return@forEach
32+
fun partyUpdates(block: (Party?) -> Unit) {
33+
runConcurrent {
34+
HttpClient.newHttpClient().sendAsync(
35+
HttpRequest.newBuilder()
36+
.uri(URI.create("${apiUrl}/api/${apiVersion.value}/party/listen"))
37+
.header("Accept", "text/event-stream")
38+
.header("Authorization", "Bearer $accessToken")
39+
.build(),
40+
HttpResponse.BodyHandlers.ofLines()
41+
).thenAccept { response ->
42+
response.body().forEach {
43+
if (!it.startsWith("data:")) return@forEach
4044

41-
val data = it.substring(5).trim()
42-
val party = Lambda.gson.fromJson(data, Party::class.java)
43-
block(party)
44-
}
45-
}.join()
45+
val data = it.substring(5).trim()
46+
val party = runCatching { Lambda.gson.fromJson(data, Party::class.java) }.getOrNull()
47+
48+
block(party)
49+
}
50+
}.join()
51+
}
4652
}

common/src/main/kotlin/com/lambda/network/api/v1/models/Party.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,4 @@ data class Party(
4343
// The list of players in the party.
4444
@SerializedName("players")
4545
val players: List<Player>,
46-
47-
// The settings of the party
48-
@SerializedName("settings")
49-
val settings: Settings,
5046
)

common/src/main/kotlin/com/lambda/network/api/v1/models/Settings.kt

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)