From f828a6c9ec9d7219f58d11a5c260ae3897500f69 Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Sun, 12 Jan 2025 14:53:29 +0100 Subject: fix: Missing cache for custom item models causing lags --- src/main/kotlin/events/CustomItemModelEvent.kt | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'src/main/kotlin/events') diff --git a/src/main/kotlin/events/CustomItemModelEvent.kt b/src/main/kotlin/events/CustomItemModelEvent.kt index 11528fd..e7b6eb8 100644 --- a/src/main/kotlin/events/CustomItemModelEvent.kt +++ b/src/main/kotlin/events/CustomItemModelEvent.kt @@ -1,7 +1,10 @@ package moe.nea.firmament.events +import java.util.Optional +import kotlin.jvm.optionals.getOrNull import net.minecraft.item.ItemStack import net.minecraft.util.Identifier +import moe.nea.firmament.util.collections.WeakCache // TODO: assert an order on these events data class CustomItemModelEvent( @@ -9,11 +12,17 @@ data class CustomItemModelEvent( var overrideModel: Identifier? = null, ) : FirmamentEvent() { companion object : FirmamentEventBus() { + val cache = WeakCache.memoize("ItemModelIdentifier", ::getModelIdentifier0) + @JvmStatic fun getModelIdentifier(itemStack: ItemStack?): Identifier? { - // TODO: Re-add memoization and add an error / warning if the model does not exist if (itemStack == null) return null - return publish(CustomItemModelEvent(itemStack)).overrideModel + return cache.invoke(itemStack).getOrNull() + } + + fun getModelIdentifier0(itemStack: ItemStack): Optional { + // TODO: add an error / warning if the model does not exist + return Optional.ofNullable(publish(CustomItemModelEvent(itemStack)).overrideModel) } } -- cgit