@@ -19,15 +19,16 @@ package com.lambda.network
1919
2020import com.lambda.Lambda.LOG
2121import com.lambda.Lambda.mc
22- import com.lambda.context.SafeContext
22+ import com.lambda.config.Configurable
23+ import com.lambda.config.configurations.SecretsConfig
2324import com.lambda.core.Loadable
2425import com.lambda.event.events.WorldEvent
2526import com.lambda.event.listener.SafeListener.Companion.listen
2627import com.lambda.network.api.v1.endpoints.getCape
2728import com.lambda.network.api.v1.endpoints.getCapes
2829import com.lambda.network.api.v1.endpoints.setCape
30+ import com.lambda.threading.runGameScheduled
2931import com.lambda.threading.runIO
30- import com.lambda.threading.runSafe
3132import com.lambda.util.FileUtils.createIfNotExists
3233import com.lambda.util.FileUtils.downloadCompare
3334import com.lambda.util.FileUtils.downloadIfNotPresent
@@ -44,25 +45,19 @@ import org.lwjgl.BufferUtils
4445import java.util.*
4546import java.util.concurrent.ConcurrentHashMap
4647import kotlin.concurrent.fixedRateTimer
47- import kotlin.io.path.extension
48- import kotlin.io.path.inputStream
49- import kotlin.io.path.nameWithoutExtension
50- import kotlin.io.path.walk
5148import kotlin.time.Duration.Companion.hours
5249import kotlin.time.Duration.Companion.seconds
5350
54- @Suppress(" JavaIoSerializableObjectMustHaveReadResolve" )
55- object CapeManager : ConcurrentHashMap<UUID, String>(), Loadable {
56- // We want to cache images to reduce class B requests
57- private val images = capes.walk()
58- .filter { it.extension == " png" }
59- .associate { it.nameWithoutExtension to NativeImageBackedTexture ({ it.nameWithoutExtension }, read(it.inputStream())) }
60- .onEach { (key, value) -> mc.textureManager.registerTexture(key.asIdentifier, value) }
51+ object CapeManager : Configurable(SecretsConfig ), Loadable {
52+ override val name: String = " capes"
6153
54+ var currentCape by setting(" cape" , " " )
55+ .onValueChangeUnsafe { _, to -> updateCape(to) }
56+
57+ val cache = ConcurrentHashMap <UUID , String >()
6258 private val fetchQueue = mutableListOf<UUID >()
6359
64- // We want to cache the cape list to reduce class B requests
65- val capeList = runBlocking {
60+ val availableCapes = runBlocking {
6661 capes.resolveFile(" capes.txt" )
6762 .isOlderThan(24 .hours) {
6863 it.downloadIfNotPresent(" ${LambdaAPI .capes} .txt" )
@@ -74,26 +69,15 @@ object CapeManager : ConcurrentHashMap<UUID, String>(), Loadable {
7469 }
7570 .createIfNotExists()
7671 .readText()
77- .split(" \n " )
72+ .split(Regex ( " \\ s+ " ) )
7873 }
7974
80- /* *
81- * Sets the current player's cape
82- *
83- * @param block Lambda called once the coroutine completes, it contains the throwable if any
84- */
8575 fun updateCape (cape : String , block : (Throwable ? ) -> Unit = {}) = runIO {
8676 setCape(cape).getOrThrow()
87-
88- runSafe { fetchCape(player.uuid) }
77+ fetchCape(mc.gameProfile.id)
8978 }.invokeOnCompletion { block(it) }
9079
91- /* *
92- * Fetches the cape of the given player id
93- *
94- * @param block Lambda called once the coroutine completes, it contains the throwable if any
95- */
96- fun SafeContext.fetchCape (uuid : UUID , block : (Throwable ? ) -> Unit = {}) = runIO {
80+ fun fetchCape (uuid : UUID , block : (Throwable ? ) -> Unit = {}) = runIO {
9781 val cape = getCape(uuid).getOrNull() ? : return @runIO
9882
9983 val bytes = capes.resolveFile(" ${cape.id} .png" )
@@ -107,12 +91,12 @@ object CapeManager : ConcurrentHashMap<UUID, String>(), Loadable {
10791
10892 val image = read(NativeImage .Format .RGBA , buffer)
10993
110- mc.textureManager.registerTexture(cape.id.asIdentifier, NativeImageBackedTexture ({ cape.id }, image))
94+ runGameScheduled { mc.textureManager.registerTexture(cape.id.asIdentifier, NativeImageBackedTexture ({ cape.id }, image)) }
11195
112- put( uuid, cape.id)
96+ cache[ uuid] = cape.id
11397 }.invokeOnCompletion { block(it) }
11498
115- override fun load () = " Loaded ${images .size} cached capes and ${capeList.size} remote capes"
99+ override fun load () = " Loaded ${availableCapes .size} capes"
116100
117101 init {
118102 fixedRateTimer(
@@ -124,7 +108,7 @@ object CapeManager : ConcurrentHashMap<UUID, String>(), Loadable {
124108
125109 runBlocking {
126110 getCapes(fetchQueue)
127- .onSuccess { it.forEach { cape -> put( cape.uuid, cape.id) } }
111+ .onSuccess { it.forEach { cape -> cache[ cape.uuid] = cape.id } }
128112
129113 fetchQueue.clear()
130114 }
0 commit comments