Skip to content

Commit 2943614

Browse files
committed
Fixed texture downloading
1 parent aa90562 commit 2943614

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

common/src/main/kotlin/com/lambda/graphics/texture/TextureUtils.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ object TextureUtils {
7171
glPixelStorei(GL_UNPACK_ALIGNMENT, 4)
7272
}
7373

74+
fun readImage(
75+
bytes: ByteArray,
76+
format: NativeImage.Format = NativeImage.Format.RGBA,
77+
): NativeImage {
78+
val buffer = BufferUtils
79+
.createByteBuffer(bytes.size)
80+
.put(bytes)
81+
.flip()
82+
83+
return NativeImage.read(format, buffer)
84+
}
85+
7486
fun readImage(
7587
bufferedImage: BufferedImage,
7688
format: NativeImage.Format = NativeImage.Format.RGBA,

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package com.lambda.network
1919

2020
import com.github.kittinunf.fuel.core.FuelError
21-
import com.lambda.Lambda.LOG
2221
import com.lambda.Lambda.mc
2322
import com.lambda.context.SafeContext
2423
import com.lambda.core.Loadable
@@ -56,7 +55,10 @@ object CapeManager : ConcurrentHashMap<UUID, String>(), Loadable {
5655
fun SafeContext.fetch(uuid: UUID) = getOrPut(uuid) {
5756
getCape(uuid)
5857
.fold(
59-
success = { if (!images.contains(it.cape)) it.fetch(); put(uuid, it.cape) },
58+
success = {
59+
if (!images.contains(it.cape)) it.fetch()
60+
put(uuid, it.cape)
61+
},
6062
failure = { throw it },
6163
)
6264
}
@@ -65,8 +67,7 @@ object CapeManager : ConcurrentHashMap<UUID, String>(), Loadable {
6567

6668
init {
6769
listen<WorldEvent.Player.Join>(alwaysListen = true) {
68-
runCatching { fetch(it.uuid) }
69-
.onFailure { LOG.error(it) }
70+
fetch(it.uuid)
7071
}
7172
}
7273
}

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@ package com.lambda.network.api.v1.models
1919

2020
import com.github.kittinunf.fuel.Fuel
2121
import com.google.gson.annotations.SerializedName
22+
import com.lambda.graphics.texture.TextureUtils
2223
import com.lambda.sound.SoundManager.toIdentifier
2324
import com.lambda.threading.runSafe
25+
import com.lambda.util.Communication.logError
2426
import com.lambda.util.FolderRegister.capes
2527
import com.lambda.util.extension.resolveFile
2628
import net.minecraft.client.texture.NativeImage
2729
import net.minecraft.client.texture.NativeImageBackedTexture
30+
import org.lwjgl.BufferUtils
31+
import java.io.BufferedOutputStream
2832
import java.io.ByteArrayOutputStream
2933

3034
class Cape(
@@ -35,20 +39,19 @@ class Cape(
3539
val cape: String,
3640
) {
3741
fun fetch() = runSafe {
38-
val output = ByteArrayOutputStream(2048*1024*4)
39-
4042
Fuel.download(url)
41-
.streamDestination { _, request -> output to { request.body.toStream() } }
42-
43-
val image = NativeImage.read(output.toByteArray())
44-
val native = NativeImageBackedTexture(image)
45-
val id = cape.toIdentifier()
46-
47-
mc.textureManager.registerTexture(id, native)
48-
49-
capes.resolveFile("$cape.png")
50-
.writeBytes(output.toByteArray())
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+
}
5156
}
52-
53-
fun identifier() = cape.toIdentifier()
5457
}

0 commit comments

Comments
 (0)