Skip to content

Commit 13e692f

Browse files
committed
Cape feature
1 parent 2943614 commit 13e692f

File tree

11 files changed

+89
-78
lines changed

11 files changed

+89
-78
lines changed

common/src/main/kotlin/com/lambda/command/commands/CapeCommand.kt

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@
1717

1818
package com.lambda.command.commands
1919

20-
import com.lambda.brigadier.CommandResult.Companion.failure
2120
import com.lambda.brigadier.CommandResult.Companion.success
2221
import com.lambda.brigadier.argument.string
2322
import com.lambda.brigadier.argument.value
2423
import com.lambda.brigadier.executeWithResult
2524
import com.lambda.brigadier.required
2625
import com.lambda.command.LambdaCommand
26+
import com.lambda.network.CapeManager.updateCape
2727
import com.lambda.network.NetworkManager
28-
import com.lambda.network.api.v1.endpoints.setCape
28+
import com.lambda.threading.runSafe
29+
import com.lambda.util.Communication.info
2930
import com.lambda.util.extension.CommandBuilder
3031

3132
object CapeCommand : LambdaCommand(
@@ -43,12 +44,18 @@ object CapeCommand : LambdaCommand(
4344
}
4445

4546
executeWithResult {
46-
val cape = id().value()
47-
setCape(cape)
48-
.fold(
49-
success = { NetworkManager.cape = cape; success() },
50-
failure = { failure(it) },
51-
)
47+
runSafe {
48+
val cape = id().value()
49+
50+
// FixMe:
51+
// try-catch is stupid -
52+
// cannot propagate errors correctly -
53+
// spam the user and fuck off
54+
updateCape(cape)
55+
this@CapeCommand.info("Successfully updated the cape")
56+
57+
success()
58+
}!!
5259
}
5360
}
5461
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,10 @@ object Discord : Module(
8181

8282
val auth = rpc.applicationManager.authenticate()
8383

84-
linkDiscord(discordToken = auth.accessToken)
85-
.fold(
86-
success = { updateToken(it); discordAuth = auth },
87-
failure = { warn("Failed to link the discord account to the minecraft auth") },
88-
)
84+
linkDiscord(discordToken = auth.accessToken,
85+
success = { updateToken(it); discordAuth = auth },
86+
failure = { warn("Failed to link the discord account to the minecraft auth") }
87+
)
8988
}
9089

9190
private fun stop() {

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,10 @@ object Network : Module(
7373
// If we log in right as the client responds to the encryption request, we start
7474
// a race condition where the game server haven't acknowledged the packets
7575
// and posted to the sessionserver api
76-
login(mc.session.username, hash)
77-
.fold(
78-
success = { updateToken(it) },
79-
failure = { LOG.warn("Unable to authenticate: $it") },
80-
)
76+
login(mc.session.username, hash,
77+
success = { updateToken(it) },
78+
failure = { LOG.warn("Unable to authenticate: $it") }
79+
)
8180
}
8281
}
8382

common/src/main/kotlin/com/lambda/network/CapeManager.kt

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,22 @@
1717

1818
package com.lambda.network
1919

20-
import com.github.kittinunf.fuel.core.FuelError
20+
import com.github.kittinunf.fuel.Fuel
21+
import com.github.kittinunf.fuel.core.requests.CancellableRequest
2122
import com.lambda.Lambda.mc
2223
import com.lambda.context.SafeContext
2324
import com.lambda.core.Loadable
2425
import com.lambda.event.events.WorldEvent
2526
import com.lambda.event.listener.SafeListener.Companion.listen
27+
import com.lambda.graphics.texture.TextureUtils
2628
import com.lambda.network.api.v1.endpoints.getCape
29+
import com.lambda.network.api.v1.endpoints.setCape
30+
import com.lambda.network.api.v1.models.Cape
2731
import com.lambda.sound.SoundManager.toIdentifier
32+
import com.lambda.util.Communication.logError
2833
import com.lambda.util.FolderRegister.capes
34+
import com.lambda.util.extension.get
35+
import com.lambda.util.extension.resolveFile
2936
import net.minecraft.client.texture.NativeImage.read
3037
import net.minecraft.client.texture.NativeImageBackedTexture
3138
import java.util.UUID
@@ -47,27 +54,45 @@ object CapeManager : ConcurrentHashMap<UUID, String>(), Loadable {
4754
.associate { it.nameWithoutExtension to NativeImageBackedTexture(read(it.inputStream())) }
4855
.onEach { (key, value) -> mc.textureManager.registerTexture(key.toIdentifier(), value) }
4956

57+
/**
58+
* Sets the current player's cape
59+
*/
60+
fun SafeContext.updateCape(cape: String) =
61+
setCape(cape,
62+
success = { fetchCape(player.uuid) },
63+
failure = { logError("Could not update the player cape", it) }
64+
)//.join()
65+
5066
/**
5167
* Fetches the cape of the given player id
52-
*
53-
* @throws FuelError if something wrong happens
5468
*/
55-
fun SafeContext.fetch(uuid: UUID) = getOrPut(uuid) {
56-
getCape(uuid)
57-
.fold(
58-
success = {
59-
if (!images.contains(it.cape)) it.fetch()
60-
put(uuid, it.cape)
61-
},
62-
failure = { throw it },
63-
)
64-
}
69+
fun SafeContext.fetchCape(uuid: UUID): CancellableRequest =
70+
getCape(uuid,
71+
success = { mc.textureManager.get(it.identifier) ?: download(it); put(uuid, it.id) },
72+
failure = { logError("Could not fetch the cape of the player", it) }
73+
)
74+
75+
private fun SafeContext.download(cape: Cape): CancellableRequest =
76+
Fuel.download(cape.url)
77+
.fileDestination { _, _ -> capes.resolveFile("${cape.id}.png") }
78+
.response { result ->
79+
result.fold(
80+
success = {
81+
val image = TextureUtils.readImage(it)
82+
val native = NativeImageBackedTexture(image)
83+
val id = cape.identifier
84+
85+
mc.textureManager.registerTexture(id, native)
86+
},
87+
failure = { logError("Could not download the cape", it) }
88+
)
89+
}
6590

6691
override fun load() = "Loaded ${images.size} cached capes"
6792

6893
init {
6994
listen<WorldEvent.Player.Join>(alwaysListen = true) {
70-
fetch(it.uuid)
95+
fetchCape(it.uuid)
7196
}
7297
}
7398
}

common/src/main/kotlin/com/lambda/network/NetworkManager.kt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,10 @@ object NetworkManager : Configurable(UserConfig), Loadable {
3232
override val name = "network"
3333

3434
var accessToken by setting("authentication", ""); private set
35-
private var _cape by setting("cape", "")
3635

3736
val isDiscordLinked: Boolean
3837
get() = deserialized?.data?.discordId != null
3938

40-
/**
41-
* Returns the current cape id or null if there are none
42-
*/
43-
var cape: String? = null
44-
get() = _cape.ifEmpty { null }
45-
set(value) { value?.let { CapeManager.put(mc.gameProfile.id, it) }; field = value }
46-
4739
/**
4840
* Returns whether the auth has expired
4941
*/

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
package com.lambda.network.api.v1.endpoints
1919

2020
import com.github.kittinunf.fuel.Fuel
21+
import com.github.kittinunf.fuel.core.FuelError
22+
import com.github.kittinunf.fuel.core.ResultHandler
2123
import com.github.kittinunf.fuel.gson.responseObject
24+
import com.github.kittinunf.result.failure
25+
import com.github.kittinunf.result.success
2226
import com.lambda.module.modules.client.Network.apiUrl
2327
import com.lambda.module.modules.client.Network.apiVersion
2428
import com.lambda.network.api.v1.models.Cape
@@ -32,7 +36,6 @@ import java.util.UUID
3236
*
3337
* response: [Cape] or error
3438
*/
35-
fun getCape(uuid: UUID) =
39+
fun getCape(uuid: UUID, success: (Cape) -> Unit, failure: (FuelError) -> Unit) =
3640
Fuel.get("$apiUrl/api/${apiVersion.value}/cape?id=$uuid")
37-
.responseObject<Cape>()
38-
.third
41+
.responseObject<Cape> { _, _, result -> result.fold(success, failure) }

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.lambda.network.api.v1.endpoints
1919

2020
import com.github.kittinunf.fuel.Fuel
21+
import com.github.kittinunf.fuel.core.FuelError
2122
import com.github.kittinunf.fuel.core.extensions.authentication
2223
import com.github.kittinunf.fuel.core.extensions.jsonBody
2324
import com.github.kittinunf.fuel.gson.responseObject
@@ -34,9 +35,9 @@ import com.lambda.network.api.v1.models.Authentication
3435
*
3536
* response: [Authentication] or error
3637
*/
37-
fun linkDiscord(discordToken: String) =
38+
fun linkDiscord(discordToken: String, success: (Authentication) -> Unit, failure: (FuelError) -> Unit) =
3839
Fuel.post("${apiUrl}/api/${apiVersion.value}/link/discord")
3940
.jsonBody("""{ "token": "$discordToken" }""")
4041
.authentication()
4142
.bearer(NetworkManager.accessToken)
42-
.responseObject<Authentication>().third
43+
.responseObject<Authentication> { _, _, result -> result.fold(success, failure) }

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.lambda.network.api.v1.endpoints
1919

2020
import com.github.kittinunf.fuel.Fuel
21+
import com.github.kittinunf.fuel.core.FuelError
2122
import com.github.kittinunf.fuel.core.extensions.jsonBody
2223
import com.github.kittinunf.fuel.gson.responseObject
2324
import com.lambda.module.modules.client.Network.apiUrl
@@ -33,7 +34,7 @@ import com.lambda.network.api.v1.models.Authentication
3334
*
3435
* response: [Authentication] or error
3536
*/
36-
fun login(username: String, hash: String) =
37+
fun login(username: String, hash: String, success: (Authentication) -> Unit, failure: (FuelError) -> Unit) =
3738
Fuel.post("${apiUrl}/api/${apiVersion.value}/login")
3839
.jsonBody("""{ "username": "$username", "hash": "$hash" }""")
39-
.responseObject<Authentication>().third
40+
.responseObject<Authentication> { _, _, result -> result.fold(success, failure) }

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
package com.lambda.network.api.v1.endpoints
1919

2020
import com.github.kittinunf.fuel.Fuel
21+
import com.github.kittinunf.fuel.core.FuelError
22+
import com.github.kittinunf.fuel.core.awaitResult
2123
import com.github.kittinunf.fuel.core.extensions.authentication
22-
import com.github.kittinunf.fuel.core.responseUnit
2324
import com.lambda.module.modules.client.Network.apiUrl
2425
import com.lambda.module.modules.client.Network.apiVersion
2526
import com.lambda.network.NetworkManager
@@ -32,8 +33,8 @@ import com.lambda.network.NetworkManager
3233
*
3334
* response: [Unit] or error
3435
*/
35-
fun setCape(id: String) =
36+
fun setCape(id: String, success: (ByteArray) -> Unit, failure: (FuelError) -> Unit) =
3637
Fuel.put("$apiUrl/api/${apiVersion.value}/cape?id=$id")
3738
.authentication()
3839
.bearer(NetworkManager.accessToken)
39-
.responseUnit().third
40+
.response { _, _, resp -> resp.fold(success, failure) }

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

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,18 @@
1717

1818
package com.lambda.network.api.v1.models
1919

20-
import com.github.kittinunf.fuel.Fuel
2120
import com.google.gson.annotations.SerializedName
22-
import com.lambda.graphics.texture.TextureUtils
2321
import com.lambda.sound.SoundManager.toIdentifier
24-
import com.lambda.threading.runSafe
25-
import com.lambda.util.Communication.logError
26-
import com.lambda.util.FolderRegister.capes
27-
import com.lambda.util.extension.resolveFile
28-
import net.minecraft.client.texture.NativeImage
29-
import net.minecraft.client.texture.NativeImageBackedTexture
30-
import org.lwjgl.BufferUtils
31-
import java.io.BufferedOutputStream
32-
import java.io.ByteArrayOutputStream
22+
import net.minecraft.util.Identifier
23+
import java.util.UUID
3324

3425
class Cape(
3526
@SerializedName("url")
3627
val url: String,
3728

3829
@SerializedName("type")
39-
val cape: String,
30+
val id: String,
4031
) {
41-
fun fetch() = runSafe {
42-
Fuel.download(url)
43-
.fileDestination { _, _ -> capes.resolveFile("$cape.png") }
44-
.response { result ->
45-
result.fold(
46-
success = {
47-
val image = TextureUtils.readImage(it)
48-
val native = NativeImageBackedTexture(image)
49-
val id = cape.toIdentifier()
50-
51-
mc.textureManager.registerTexture(id, native)
52-
},
53-
failure = { logError("Error while downloading capes", it) }
54-
)
55-
}
56-
}
32+
val identifier: Identifier
33+
get() = id.toIdentifier()
5734
}

0 commit comments

Comments
 (0)