1717
1818package com.lambda.network
1919
20- import com.github.kittinunf.fuel.Fuel
21- import com.github.kittinunf.fuel.core.requests.CancellableRequest
2220import com.lambda.Lambda.mc
2321import com.lambda.context.SafeContext
2422import com.lambda.core.Loadable
@@ -29,13 +27,13 @@ import com.lambda.network.api.v1.endpoints.getCape
2927import com.lambda.network.api.v1.endpoints.setCape
3028import com.lambda.network.api.v1.models.Cape
3129import com.lambda.sound.SoundManager.toIdentifier
32- import com.lambda.util.Communication.info
33- import com.lambda.util.Communication.logError
30+ import com.lambda.threading.runIO
3431import com.lambda.util.FolderRegister.capes
3532import com.lambda.util.extension.get
3633import com.lambda.util.extension.resolveFile
3734import net.minecraft.client.texture.NativeImage.read
3835import net.minecraft.client.texture.NativeImageBackedTexture
36+ import java.io.ByteArrayOutputStream
3937import java.util.UUID
4038import java.util.concurrent.ConcurrentHashMap
4139import kotlin.io.path.ExperimentalPathApi
@@ -58,36 +56,35 @@ object CapeManager : ConcurrentHashMap<UUID, String>(), Loadable {
5856 /* *
5957 * Sets the current player's cape
6058 */
61- fun SafeContext.updateCape (cape : String ): CancellableRequest =
62- setCape(cape,
63- success = { fetchCape(player.uuid); info(" Successfully update your cape to $cape " ) },
64- failure = { logError(" Could not update the player cape" , it) }
65- )
59+ fun updateCape (cape : String , block : () -> Unit = {}) = runIO {
60+ setCape(cape)
61+ }.invokeOnCompletion { block() }
6662
6763 /* *
6864 * Fetches the cape of the given player id
6965 */
70- fun SafeContext.fetchCape (uuid : UUID ): CancellableRequest =
71- getCape(uuid,
72- success = { mc.textureManager.get(it.identifier) ? : download(it); put(uuid, it.id) },
73- failure = { logError(" Could not fetch the cape of the player" , it) }
74- )
66+ fun SafeContext.fetchCape (uuid : UUID , block : () -> Unit = {}) = runIO {
67+ val cape = getCape(uuid).getOrThrow()
7568
76- private fun SafeContext.download (cape : Cape ): CancellableRequest =
77- Fuel .download(cape.url)
78- .fileDestination { _, _ -> capes.resolveFile(" ${cape.id} .png" ) }
79- .response { result ->
80- result.fold(
81- success = {
82- val image = TextureUtils .readImage(it)
83- val native = NativeImageBackedTexture (image)
84- val id = cape.identifier
69+ mc.textureManager.get(cape.identifier) ? : download(cape)
70+ put(uuid, cape.id)
71+ }.invokeOnCompletion { block() }
8572
86- mc.textureManager.registerTexture(id, native)
87- },
88- failure = { logError(" Could not download the cape" , it) }
89- )
90- }
73+ private fun SafeContext.download (cape : Cape , block : () -> Unit = {}) = runIO {
74+ val destination = capes.resolveFile(" ${cape.id} .png" )
75+ val output = ByteArrayOutputStream ()
76+
77+ LambdaHttp .download(cape.url, output)
78+
79+ val bytes = output.toByteArray()
80+ destination.writeBytes(bytes)
81+
82+ val image = TextureUtils .readImage(bytes)
83+ val native = NativeImageBackedTexture (image)
84+ val id = cape.identifier
85+
86+ mc.textureManager.registerTexture(id, native)
87+ }.invokeOnCompletion { block() }
9188
9289 override fun load () = " Loaded ${images.size} cached capes"
9390
0 commit comments