diff options
author | Linnea Gräf <nea@nea.moe> | 2024-03-30 19:44:32 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-03-30 19:44:32 +0100 |
commit | 0414b87e02e51b51cf9ef0c165e5ed61e5193160 (patch) | |
tree | 0d1a0144c44b252dcabc2c1d685a7d2b27e6adee /src/main/java/moe/nea/caelo/optifine | |
download | veloxcaelo-0414b87e02e51b51cf9ef0c165e5ed61e5193160.tar.gz veloxcaelo-0414b87e02e51b51cf9ef0c165e5ed61e5193160.tar.bz2 veloxcaelo-0414b87e02e51b51cf9ef0c165e5ed61e5193160.zip |
Initial commit
Diffstat (limited to 'src/main/java/moe/nea/caelo/optifine')
-rw-r--r-- | src/main/java/moe/nea/caelo/optifine/OptifineCustomItemCache.kt | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/src/main/java/moe/nea/caelo/optifine/OptifineCustomItemCache.kt b/src/main/java/moe/nea/caelo/optifine/OptifineCustomItemCache.kt new file mode 100644 index 0000000..0bd6d78 --- /dev/null +++ b/src/main/java/moe/nea/caelo/optifine/OptifineCustomItemCache.kt @@ -0,0 +1,78 @@ +package moe.nea.caelo.optifine + +import moe.nea.caelo.CaeloCommand +import moe.nea.caelo.event.NeaTickEvent +import moe.nea.caelo.util.Histogram +import moe.nea.caelo.util.MC +import net.minecraft.item.ItemStack +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import net.optifine.CustomItemProperties +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable + +object OptifineCustomItemCache { + + init { + CaeloCommand.subcommand("opticache") { args -> + val cache = cacheSizeHistory.lastOrNull() ?: CacheStats() + MC.display("OptiCache stats:") + MC.display("- History: §3${cacheSizeHistory.size}") + MC.display("- Misses: §c${cache.cacheMisses}") + MC.display("- Hits: §a${cache.cacheHits}") + MC.display("- Entries: §b${cache.uniquePropertyBearingStacks}") + } + } + + class CacheKey(val itemStack: ItemStack, val type: Int) { + override fun equals(other: Any?): Boolean { + if (other !is CacheKey) return false + return itemStack === other.itemStack && type == other.type + } + + override fun hashCode(): Int { + return System.identityHashCode(itemStack) + type * 31 + } + } + + data class CacheStats( + var cacheHits: Int = 0, + var cacheMisses: Int = 0, + var uniquePropertyBearingStacks: Int = 0, + ) + + private val map = mutableMapOf<CacheKey, CustomItemProperties?>() + private val cacheSizeHistory = Histogram<CacheStats>(1000) + private var cacheStats = CacheStats() + + @SubscribeEvent + fun onTick(event: NeaTickEvent) { + cacheSizeHistory.append(cacheStats) + cacheStats = CacheStats() + map.clear() + } + + @JvmStatic + fun retrieveCacheHit( + itemStack: ItemStack, + type: Int, + cir: CallbackInfoReturnable<CustomItemProperties?> + ) { + val key = CacheKey(itemStack, type) + if (!map.containsKey(key)) { + cacheStats.cacheMisses++ + return + } + cacheStats.cacheHits++ + cir.returnValue = map[key] + } + + @JvmStatic + fun storeCustomItemProperties(itemStack: ItemStack, type: Int, cip: CustomItemProperties) { + map[CacheKey(itemStack, type)] = cip + cacheStats.uniquePropertyBearingStacks++ + } + + @JvmStatic + fun storeNoCustomItemProperties(itemStack: ItemStack, type: Int) { + map[CacheKey(itemStack, type)] = null + } +}
\ No newline at end of file |