Skip to content

Commit 2bc41f0

Browse files
committed
Added bulk cape get
1 parent 401c02d commit 2bc41f0

File tree

3 files changed

+51
-24
lines changed

3 files changed

+51
-24
lines changed

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

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,13 @@ import com.lambda.event.listener.SafeListener.Companion.listen
2525
import com.lambda.graphics.texture.TextureUtils
2626
import com.lambda.network.api.v1.endpoints.getCape
2727
import com.lambda.network.api.v1.endpoints.setCape
28-
import com.lambda.network.api.v1.models.Cape
2928
import com.lambda.sound.SoundManager.toIdentifier
3029
import com.lambda.threading.runIO
30+
import com.lambda.util.FileUtils.downloadIfNotPresent
3131
import com.lambda.util.FolderRegister.capes
32-
import com.lambda.util.extension.get
3332
import com.lambda.util.extension.resolveFile
3433
import net.minecraft.client.texture.NativeImage.read
3534
import net.minecraft.client.texture.NativeImageBackedTexture
36-
import java.io.ByteArrayOutputStream
3735
import java.util.UUID
3836
import java.util.concurrent.ConcurrentHashMap
3937
import 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"
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.lambda.network.api.v1.endpoints
2+
3+
import com.lambda.module.modules.client.Network
4+
import com.lambda.module.modules.client.Network.apiUrl
5+
import com.lambda.module.modules.client.Network.apiVersion
6+
import com.lambda.network.LambdaHttp
7+
import com.lambda.network.api.v1.models.Cape
8+
import io.ktor.client.call.*
9+
import io.ktor.client.request.*
10+
import io.ktor.http.*
11+
import java.util.*
12+
13+
/**
14+
* Gets the cape of the given player UUIDs
15+
*
16+
* Example:
17+
* - id: ab24f5d6-dcf1-45e4-897e-b50a7c5e7422
18+
* - id: 4f332cd7-cf93-427e-a282-53f45f6bb113
19+
* - id: fdee323e-7f0c-4c15-8d1c-0f277442342a
20+
*
21+
* @return results of capes
22+
*/
23+
suspend fun getCapes(vararg uuid: UUID) = getCapes(uuid.toSet())
24+
25+
/**
26+
* Gets the cape of the given player UUIDs
27+
*
28+
* Example:
29+
* - id: ab24f5d6-dcf1-45e4-897e-b50a7c5e7422
30+
* - id: 4f332cd7-cf93-427e-a282-53f45f6bb113
31+
* - id: fdee323e-7f0c-4c15-8d1c-0f277442342a
32+
*
33+
* @return results of capes
34+
*/
35+
suspend fun getCapes(uuids: Set<UUID>) = runCatching {
36+
LambdaHttp.get("$apiUrl/api/$apiVersion/capes") {
37+
contentType(ContentType.Application.Json)
38+
setBody("""{ "players": [${uuids.joinToString(prefix = "\"", postfix = "\"", separator = "\",\"")}] }""")
39+
}.body<List<Cape>>()
40+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ import net.minecraft.util.Identifier
2323
import java.util.UUID
2424

2525
class Cape(
26-
@SerializedName("url")
27-
val url: String,
26+
@SerializedName("uuid")
27+
val uuid: String,
2828

2929
@SerializedName("type")
3030
val id: String,
3131
) {
32-
val identifier: Identifier
33-
get() = id.toIdentifier()
32+
val url: String
33+
get() = "https://cdn.lambda-client.org/$id.png"
3434
}

0 commit comments

Comments
 (0)