1717
1818package com.lambda.module.modules.client
1919
20- import com.lambda.Lambda
2120import com.lambda.Lambda.LOG
2221import com.lambda.Lambda.gson
2322import com.lambda.Lambda.mc
@@ -34,7 +33,6 @@ import com.lambda.network.api.v1.models.Authentication
3433import com.lambda.module.Module
3534import com.lambda.module.tag.ModuleTag
3635import com.lambda.network.api.v1.models.Authentication.Data
37- import com.lambda.util.Communication.info
3836import com.lambda.util.extension.isOffline
3937import net.minecraft.client.network.AllowedAddressResolver
4038import net.minecraft.client.network.ClientLoginNetworkHandler
@@ -53,14 +51,14 @@ object Network : Module(
5351 defaultTags = setOf(ModuleTag .CLIENT ),
5452 enabledByDefault = true ,
5553) {
56- val authServer by setting(" Auth Server" , " auth.lambda-client.org" )
57- val apiUrl by setting(" API Server" , " https://api.lambda-client.org" )
58- val apiVersion by setting(" API Version" , ApiVersion .V1 )
54+ val authServer by setting(" Auth Server" , " auth.lambda-client.org" )
55+ val apiUrl by setting(" API Server" , " https://api.lambda-client.org" )
56+ val apiVersion by setting(" API Version" , ApiVersion .V1 )
5957
60- var apiAuth : Authentication ? = null ; private set // TODO: Cache
61- var deserialized: Data ? = null ; private set // gson is too stupid
58+ private var auth : Authentication ? = null // TODO: Cache
59+ private var deserialized: Data ? = null
6260 val accessToken: String
63- get() = apiAuth ?.accessToken ? : " "
61+ get() = auth ?.accessToken ? : " "
6462
6563 val SafeContext .isDiscordLinked: Boolean
6664 get() = deserialized?.data?.discordId != null
@@ -80,53 +78,42 @@ object Network : Module(
8078 }
8179
8280 listenOnceUnsafe<ConnectionEvent .Connect .Post > {
83- if (! ::hash.isInitialized) {
84- if (! authenticate()) return @listenOnceUnsafe false
85- }
81+ if (mc.gameProfile.isOffline) return @listenOnceUnsafe true
8682
8783 // If we log in right as the client responds to the encryption request, we start
8884 // a race condition where the game server haven't acknowledged the packets
8985 // and posted to the sessionserver api
90- val (authResponse , error) = login(mc.session.username, hash)
86+ val (resp , error) = login(mc.session.username, hash)
9187 if (error != null ) {
9288 LOG .debug(" Unable to authenticate: ${error.message} " )
9389 return @listenOnceUnsafe false
9490 }
9591
96- apiAuth = authResponse
92+ auth = resp
9793 deserialized = gson.fromJson(String (Base64 .getUrlDecoder().decode(accessToken.split(" ." )[1 ])), Data ::class .java)
9894
99- LOG .info(" Successfully authenticated" )
10095 true
10196 }
10297
10398 listenUnsafeConcurrently<ClientEvent .Startup > { authenticate() }
10499 }
105100
106- private fun authenticate (): Boolean {
107- if (mc.gameProfile.isOffline) return true
108-
109- val addddd = ServerAddress .parse(authServer)
101+ private fun authenticate () {
102+ val address = ServerAddress .parse(authServer)
110103 val connection = ClientConnection (CLIENTBOUND )
111- val addr = AllowedAddressResolver .DEFAULT .resolve(addddd )
104+ val resolved = AllowedAddressResolver .DEFAULT .resolve(address )
112105 .map { it.inetSocketAddress }.get()
113106
114- runCatching {
115- ClientConnection .connect(addr, mc.options.shouldUseNativeTransport(), connection)
116- .syncUninterruptibly()
117- }.onFailure {
118- return false
119- }
107+ ClientConnection .connect(resolved, mc.options.shouldUseNativeTransport(), connection)
108+ .syncUninterruptibly()
120109
121110 val handler = ClientLoginNetworkHandler (connection, mc, null , null , false , null ) { Text .empty() }
122111
123- connection.connect(addr .hostName, addr .port, handler)
112+ connection.connect(resolved .hostName, resolved .port, handler)
124113 connection.send(LoginHelloC2SPacket (mc.session.username, mc.session.uuidOrNull))
125-
126- return true
127114 }
128115
129- internal fun updateToken (auth : Authentication ? ) { apiAuth = auth }
116+ internal fun updateToken (auth : Authentication ? ) { this .auth = auth }
130117
131118 enum class ApiVersion (val value : String ) {
132119 // We can use @Deprecated("Not supported") to remove old API versions in the future
0 commit comments