aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-01-04 17:45:29 +0100
committerLinnea Gräf <nea@nea.moe>2024-01-04 17:55:39 +0100
commit61a5635e993b46d3ae2e76edd1b104987ff92a3f (patch)
tree3091150988d9ab47ff37aac482263aa0ce0e797f /src/main/kotlin/moe/nea
parent62519450e2dc3434560ab2e9db1f308c0ca79f0c (diff)
downloadneuhax-61a5635e993b46d3ae2e76edd1b104987ff92a3f.tar.gz
neuhax-61a5635e993b46d3ae2e76edd1b104987ff92a3f.tar.bz2
neuhax-61a5635e993b46d3ae2e76edd1b104987ff92a3f.zip
Add Optifine cache
Diffstat (limited to 'src/main/kotlin/moe/nea')
-rw-r--r--src/main/kotlin/moe/nea/sky/NEUHax.kt4
-rw-r--r--src/main/kotlin/moe/nea/sky/features/fopt/OptifineCustomItemCacheKey.kt101
-rw-r--r--src/main/kotlin/moe/nea/sky/util/chatutils.kt4
3 files changed, 109 insertions, 0 deletions
diff --git a/src/main/kotlin/moe/nea/sky/NEUHax.kt b/src/main/kotlin/moe/nea/sky/NEUHax.kt
index 93e1a6f..734fdab 100644
--- a/src/main/kotlin/moe/nea/sky/NEUHax.kt
+++ b/src/main/kotlin/moe/nea/sky/NEUHax.kt
@@ -11,8 +11,10 @@
package moe.nea.sky
import io.github.moulberry.notenoughupdates.NotEnoughUpdates
+import io.github.moulberry.notenoughupdates.util.brigadier.BrigadierRoot
import moe.nea.sky.commands.NEUHaxCommand
import moe.nea.sky.config.HaxConfigNeuConfig
+import moe.nea.sky.features.fopt.OptifineCustomItemCache
import moe.nea.sky.features.gui.Enchanting
import moe.nea.sky.features.gui.Melody
import moe.nea.sky.features.world.AutoFishing
@@ -51,11 +53,13 @@ object NEUHax {
AutoFishing,
YawSnapping,
Melody,
+ OptifineCustomItemCache,
).forEach {
MinecraftForge.EVENT_BUS.register(it)
}
ClientCommandHandler.instance.registerCommand(NEUHaxCommand)
ClientCommandHandler.instance.registerCommand(CommandActionRegistry)
+ BrigadierRoot.updateHooks()
}
} \ No newline at end of file
diff --git a/src/main/kotlin/moe/nea/sky/features/fopt/OptifineCustomItemCacheKey.kt b/src/main/kotlin/moe/nea/sky/features/fopt/OptifineCustomItemCacheKey.kt
new file mode 100644
index 0000000..ce922a3
--- /dev/null
+++ b/src/main/kotlin/moe/nea/sky/features/fopt/OptifineCustomItemCacheKey.kt
@@ -0,0 +1,101 @@
+package moe.nea.sky.features.fopt
+
+import io.github.moulberry.notenoughupdates.deps.com.mojang.brigadier.arguments.IntegerArgumentType
+import io.github.moulberry.notenoughupdates.events.RegisterBrigadierCommandEvent
+import io.github.moulberry.notenoughupdates.util.brigadier.get
+import io.github.moulberry.notenoughupdates.util.brigadier.thenArgumentExecute
+import io.github.moulberry.notenoughupdates.util.brigadier.thenExecute
+import io.github.moulberry.notenoughupdates.util.brigadier.withHelp
+import moe.nea.sky.util.showMessage
+import net.minecraft.command.ICommandSender
+import net.minecraft.item.ItemStack
+import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
+import net.minecraftforge.fml.common.gameevent.TickEvent
+import net.optifine.CustomItemProperties
+
+class OptifineCustomItemCacheKey(val itemstack: ItemStack, val type: Int) {
+ override fun equals(other: Any?): Boolean {
+ if (other !is OptifineCustomItemCacheKey) return false
+ return itemstack === other.itemstack && type == other.type
+ }
+
+ override fun hashCode(): Int {
+ return System.identityHashCode(itemstack) + type * 31
+ }
+}
+
+class CacheResult(val customItemProperties: CustomItemProperties?)
+
+class Histogram<T>(val maxSize: Int) : Iterable<T> {
+ private val dequeue = ArrayDeque<T>()
+ fun append(element: T) {
+ dequeue.addLast(element)
+ if (dequeue.size > maxSize) {
+ dequeue.removeFirst()
+ }
+ }
+
+ val size get() = dequeue.size
+
+ override fun iterator(): Iterator<T> {
+ return dequeue.iterator()
+ }
+}
+
+data class CacheStats(var cacheHits: Int = 0, var cacheMisses: Int = 0, var cacheCount: Int = 0)
+object OptifineCustomItemCache {
+ private val map: MutableMap<OptifineCustomItemCacheKey, CacheResult> = mutableMapOf()
+ private val histogram = Histogram<CacheStats>(100)
+ private var cacheStats = CacheStats()
+
+ fun dumpStats(target: ICommandSender, length: Int) {
+ target.showMessage {
+ text("Optifine Cache Size:")
+ histogram.forEachIndexed { index, stats ->
+ val ago = histogram.size - index
+ if (ago <= length)
+ text("§b${ago}§e tick${s(ago)} ago: §a${stats.cacheHits}§e-§c${stats.cacheMisses}§e-§b${stats.cacheCount}")
+ }
+ }
+ }
+
+ @SubscribeEvent
+ fun onCommands(event: RegisterBrigadierCommandEvent) {
+ event.command("nhopticache") {
+ thenExecute {
+ dumpStats(source, Int.MAX_VALUE)
+ }
+ thenArgumentExecute("count", IntegerArgumentType.integer()) {
+ dumpStats(source, this[it])
+ }.withHelp("Show stats about the last count ticks")
+ }.withHelp("Show stats about the optifine item cache")
+ }
+
+
+ @SubscribeEvent
+ fun onTick(event: TickEvent.ClientTickEvent) {
+ if (event.phase == TickEvent.Phase.END) {
+ cacheStats.cacheCount = map.size
+ histogram.append(cacheStats)
+ cacheStats = CacheStats()
+ map.clear()
+ }
+ }
+
+ @JvmStatic
+ fun retrieveCacheHit(itemstack: ItemStack, type: Int): CacheResult? {
+ val cacheResult = map[OptifineCustomItemCacheKey(itemstack, type)]
+ if (cacheResult == null) {
+ cacheStats.cacheMisses++
+ } else {
+ cacheStats.cacheHits++
+ }
+ return cacheResult
+ }
+
+ @JvmStatic
+ fun storeCacheElement(itemstack: ItemStack, type: Int, properties: CacheResult) {
+ map[OptifineCustomItemCacheKey(itemstack, type)] = properties
+ }
+
+} \ No newline at end of file
diff --git a/src/main/kotlin/moe/nea/sky/util/chatutils.kt b/src/main/kotlin/moe/nea/sky/util/chatutils.kt
index df54cc8..025ee92 100644
--- a/src/main/kotlin/moe/nea/sky/util/chatutils.kt
+++ b/src/main/kotlin/moe/nea/sky/util/chatutils.kt
@@ -80,6 +80,10 @@ object CommandActionRegistry : CommandBase() {
class MessageTarget {
val aggregate = mutableListOf<IChatComponent>()
+ fun s(count: Int): String {
+ return if (count == 1) "" else "s"
+ }
+
fun text(string: String): ChatComponentText {
return component(ChatComponentText(string))
}