@@ -25,15 +25,13 @@ import com.lambda.event.listener.SafeListener.Companion.listen
2525import com.lambda.graphics.texture.TextureUtils
2626import com.lambda.network.api.v1.endpoints.getCape
2727import com.lambda.network.api.v1.endpoints.setCape
28- import com.lambda.network.api.v1.models.Cape
2928import com.lambda.sound.SoundManager.toIdentifier
3029import com.lambda.threading.runIO
30+ import com.lambda.util.FileUtils.downloadIfNotPresent
3131import com.lambda.util.FolderRegister.capes
32- import com.lambda.util.extension.get
3332import com.lambda.util.extension.resolveFile
3433import net.minecraft.client.texture.NativeImage.read
3534import net.minecraft.client.texture.NativeImageBackedTexture
36- import java.io.ByteArrayOutputStream
3735import java.util.UUID
3836import java.util.concurrent.ConcurrentHashMap
3937import kotlin.io.path.extension
@@ -66,26 +64,15 @@ object CapeManager : ConcurrentHashMap<UUID, String>(), Loadable {
6664 * @param block Lambda called once the coroutine completes, it contains the throwable if any
6765 */
6866 fun SafeContext.fetchCape (uuid : UUID , block : (Throwable ? ) -> Unit = {}) = runIO {
69- val cape = getCape(uuid).getOrThrow()
67+ val cape = getCape(uuid).getOrNull() ? : return @runIO
7068
71- mc.textureManager.get(cape.identifier) ? : download(cape)
72- put(uuid, cape.id)
73- }.invokeOnCompletion { block(it) }
74-
75- private fun SafeContext.download (cape : Cape , block : (Throwable ? ) -> Unit = {}) = runIO {
76- val destination = capes.resolveFile(" ${cape.id} .png" )
77- val output = ByteArrayOutputStream ()
69+ val bytes = capes.resolveFile(" ${cape.id} .png" )
70+ .downloadIfNotPresent(cape.url).getOrNull()
71+ ?.readBytes() ? : return @runIO
7872
79- LambdaHttp .download (cape.url, output )
73+ mc.textureManager.getOrDefault (cape.id.toIdentifier(), NativeImageBackedTexture ( TextureUtils .readImage(bytes)) )
8074
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)
75+ put(uuid, cape.id)
8976 }.invokeOnCompletion { block(it) }
9077
9178 override fun load () = " Loaded ${images.size} cached capes"
0 commit comments