aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/events
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-09-24 11:40:15 +0200
committerLinnea Gräf <nea@nea.moe>2024-09-24 11:40:15 +0200
commit420f2a61e1cc64d68bf03825e8fd70cf49ac6a01 (patch)
tree540f2beaf99bda96af3c145cbfe81faebba76bee /src/main/kotlin/events
parent64099bd2628490b06392766c6d1b9425f26788a3 (diff)
downloadFirmament-420f2a61e1cc64d68bf03825e8fd70cf49ac6a01.tar.gz
Firmament-420f2a61e1cc64d68bf03825e8fd70cf49ac6a01.tar.bz2
Firmament-420f2a61e1cc64d68bf03825e8fd70cf49ac6a01.zip
Use weak caches for custom textures
Diffstat (limited to 'src/main/kotlin/events')
-rw-r--r--src/main/kotlin/events/CustomItemModelEvent.kt29
-rw-r--r--src/main/kotlin/events/FinalizeResourceManagerEvent.kt27
2 files changed, 37 insertions, 19 deletions
diff --git a/src/main/kotlin/events/CustomItemModelEvent.kt b/src/main/kotlin/events/CustomItemModelEvent.kt
index 27524a9..e50eca4 100644
--- a/src/main/kotlin/events/CustomItemModelEvent.kt
+++ b/src/main/kotlin/events/CustomItemModelEvent.kt
@@ -1,24 +1,25 @@
-
-
package moe.nea.firmament.events
-import java.util.*
+import java.util.Optional
+import kotlin.jvm.optionals.getOrNull
import net.minecraft.client.render.model.BakedModel
import net.minecraft.client.render.model.BakedModelManager
import net.minecraft.client.util.ModelIdentifier
import net.minecraft.item.ItemStack
+import moe.nea.firmament.util.collections.WeakCache
data class CustomItemModelEvent(
val itemStack: ItemStack,
var overrideModel: ModelIdentifier? = null,
) : FirmamentEvent() {
companion object : FirmamentEventBus<CustomItemModelEvent>() {
- private val cache = IdentityHashMap<ItemStack?, Any>()
- private val sentinelNull = Object()
-
- fun clearCache() {
- cache.clear()
- }
+ val cache =
+ WeakCache.memoize<ItemStack, BakedModelManager, Optional<BakedModel>>("CustomItemModels") { stack, models ->
+ val modelId = getModelIdentifier(stack) ?: return@memoize Optional.empty()
+ val bakedModel = models.getModel(modelId)
+ if (bakedModel === models.missingModel) return@memoize Optional.empty()
+ Optional.of(bakedModel)
+ }
@JvmStatic
fun getModelIdentifier(itemStack: ItemStack?): ModelIdentifier? {
@@ -29,15 +30,7 @@ data class CustomItemModelEvent(
@JvmStatic
fun getModel(itemStack: ItemStack?, thing: BakedModelManager): BakedModel? {
if (itemStack == null) return null
- val cachedValue = cache.getOrPut(itemStack) {
- val modelId = getModelIdentifier(itemStack) ?: return@getOrPut sentinelNull
- val bakedModel = thing.getModel(modelId)
- if (bakedModel === thing.missingModel) return@getOrPut sentinelNull
- bakedModel
- }
- if (cachedValue === sentinelNull)
- return null
- return cachedValue as BakedModel
+ return cache.invoke(itemStack, thing).getOrNull()
}
}
}
diff --git a/src/main/kotlin/events/FinalizeResourceManagerEvent.kt b/src/main/kotlin/events/FinalizeResourceManagerEvent.kt
index c43ad3b..0d411f1 100644
--- a/src/main/kotlin/events/FinalizeResourceManagerEvent.kt
+++ b/src/main/kotlin/events/FinalizeResourceManagerEvent.kt
@@ -1,10 +1,35 @@
-
package moe.nea.firmament.events
+import java.util.concurrent.CompletableFuture
+import java.util.concurrent.Executor
import net.minecraft.resource.ReloadableResourceManagerImpl
+import net.minecraft.resource.ResourceManager
+import net.minecraft.resource.ResourceReloader
+import net.minecraft.util.profiler.Profiler
data class FinalizeResourceManagerEvent(
val resourceManager: ReloadableResourceManagerImpl,
) : FirmamentEvent() {
companion object : FirmamentEventBus<FinalizeResourceManagerEvent>()
+
+ inline fun registerOnApply(name: String, crossinline function: () -> Unit) {
+ resourceManager.registerReloader(object : ResourceReloader {
+ override fun reload(
+ synchronizer: ResourceReloader.Synchronizer,
+ manager: ResourceManager?,
+ prepareProfiler: Profiler?,
+ applyProfiler: Profiler?,
+ prepareExecutor: Executor?,
+ applyExecutor: Executor
+ ): CompletableFuture<Void> {
+ return CompletableFuture.completedFuture(Unit)
+ .thenCompose(synchronizer::whenPrepared)
+ .thenAcceptAsync({ function() }, applyExecutor)
+ }
+
+ override fun getName(): String {
+ return name
+ }
+ })
+ }
}