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,22 +27,20 @@ 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
41- import kotlin.io.path.ExperimentalPathApi
4239import kotlin.io.path.extension
4340import kotlin.io.path.inputStream
4441import kotlin.io.path.nameWithoutExtension
4542import kotlin.io.path.walk
4643
47- @OptIn(ExperimentalPathApi ::class )
4844@Suppress(" JavaIoSerializableObjectMustHaveReadResolve" )
4945object CapeManager : ConcurrentHashMap<UUID, String>(), Loadable {
5046 /* *
@@ -57,37 +53,40 @@ object CapeManager : ConcurrentHashMap<UUID, String>(), Loadable {
5753
5854 /* *
5955 * Sets the current player's cape
56+ *
57+ * @param block Lambda called once the coroutine completes, it contains the throwable if any
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 : (Throwable ? ) -> Unit = {}) = runIO {
60+ setCape(cape).getOrThrow()
61+ }.invokeOnCompletion { block(it) }
6662
6763 /* *
6864 * Fetches the cape of the given player id
65+ *
66+ * @param block Lambda called once the coroutine completes, it contains the throwable if any
6967 */
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- )
68+ fun SafeContext.fetchCape (uuid : UUID , block : (Throwable ? ) -> Unit = {}) = runIO {
69+ val cape = getCape(uuid).getOrThrow()
7570
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
71+ mc.textureManager.get(cape.identifier) ? : download(cape)
72+ put(uuid, cape.id)
73+ }.invokeOnCompletion { block(it) }
8574
86- mc.textureManager.registerTexture(id, native)
87- },
88- failure = { logError(" Could not download the cape" , it) }
89- )
90- }
75+ private fun SafeContext.download (cape : Cape , block : (Throwable ? ) -> Unit = {}) = runIO {
76+ val destination = capes.resolveFile(" ${cape.id} .png" )
77+ val output = ByteArrayOutputStream ()
78+
79+ LambdaHttp .download(cape.url, output)
80+
81+ val bytes = output.toByteArray()
82+ destination.writeBytes(bytes)
83+
84+ val image = TextureUtils .readImage(bytes)
85+ val native = NativeImageBackedTexture (image)
86+ val id = cape.identifier
87+
88+ mc.textureManager.registerTexture(id, native)
89+ }.invokeOnCompletion { block(it) }
9190
9291 override fun load () = " Loaded ${images.size} cached capes"
9392
0 commit comments