@@ -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,23 +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- private val images = capes.walk()
57- .filter { it.extension == " png" }
58- .associate { it.nameWithoutExtension to NativeImageBackedTexture ({ it.nameWithoutExtension }, read(it.inputStream())) }
59- .onEach { (key, value) -> mc.textureManager.registerTexture(key.asIdentifier, value) }
51+ object CapeManager : Configurable(SecretsConfig ), Loadable {
52+ override val name: String = " capes"
6053
54+ var currentCape by setting(" cape" , " " )
55+ .onValueChangeUnsafe { _, to -> updateCape(to) }
56+
57+ val cache = ConcurrentHashMap <UUID , String >()
6158 private val fetchQueue = mutableListOf<UUID >()
6259
63- val capeList = runBlocking {
60+ val availableCapes = runBlocking {
6461 capes.resolveFile(" capes.txt" )
6562 .isOlderThan(24 .hours) {
6663 it.downloadIfNotPresent(" ${LambdaAPI .capes} .txt" )
@@ -72,26 +69,15 @@ object CapeManager : ConcurrentHashMap<UUID, String>(), Loadable {
7269 }
7370 .createIfNotExists()
7471 .readText()
75- .split(" \n " )
72+ .split(Regex ( " \\ s+ " ) )
7673 }
7774
78- /* *
79- * Sets the current player's cape
80- *
81- * @param block Lambda called once the coroutine completes, it contains the throwable if any
82- */
8375 fun updateCape (cape : String , block : (Throwable ? ) -> Unit = {}) = runIO {
8476 setCape(cape).getOrThrow()
85-
86- runSafe { fetchCape(player.uuid) }
77+ fetchCape(mc.gameProfile.id)
8778 }.invokeOnCompletion { block(it) }
8879
89- /* *
90- * Fetches the cape of the given player id
91- *
92- * @param block Lambda called once the coroutine completes, it contains the throwable if any
93- */
94- fun SafeContext.fetchCape (uuid : UUID , block : (Throwable ? ) -> Unit = {}) = runIO {
80+ fun fetchCape (uuid : UUID , block : (Throwable ? ) -> Unit = {}) = runIO {
9581 val cape = getCape(uuid).getOrNull() ? : return @runIO
9682
9783 val bytes = capes.resolveFile(" ${cape.id} .png" )
@@ -105,12 +91,12 @@ object CapeManager : ConcurrentHashMap<UUID, String>(), Loadable {
10591
10692 val image = read(NativeImage .Format .RGBA , buffer)
10793
108- mc.textureManager.registerTexture(cape.id.asIdentifier, NativeImageBackedTexture ({ cape.id }, image))
94+ runGameScheduled { mc.textureManager.registerTexture(cape.id.asIdentifier, NativeImageBackedTexture ({ cape.id }, image)) }
10995
110- put( uuid, cape.id)
96+ cache[ uuid] = cape.id
11197 }.invokeOnCompletion { block(it) }
11298
113- override fun load () = " Loaded ${images .size} cached capes and ${capeList.size} remote capes"
99+ override fun load () = " Loaded ${availableCapes .size} capes"
114100
115101 init {
116102 fixedRateTimer(
@@ -122,7 +108,7 @@ object CapeManager : ConcurrentHashMap<UUID, String>(), Loadable {
122108
123109 runBlocking {
124110 getCapes(fetchQueue)
125- .onSuccess { it.forEach { cape -> put( cape.uuid, cape.id) } }
111+ .onSuccess { it.forEach { cape -> cache[ cape.uuid] = cape.id } }
126112
127113 fetchQueue.clear()
128114 }
0 commit comments