From ef5e02d12b87e596aa7d808b2c912e91913466d3 Mon Sep 17 00:00:00 2001 From: CalMWolfs <94038482+CalMWolfs@users.noreply.github.com> Date: Fri, 19 Apr 2024 20:04:55 +1000 Subject: Fix: Memory leak in ItemRenderBackground (#1491) --- .../skyhanni/data/ItemRenderBackground.kt | 25 +++++++++------------- 1 file changed, 10 insertions(+), 15 deletions(-) (limited to 'src/main/java/at') diff --git a/src/main/java/at/hannibal2/skyhanni/data/ItemRenderBackground.kt b/src/main/java/at/hannibal2/skyhanni/data/ItemRenderBackground.kt index 406901b9d..ba150a72b 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/ItemRenderBackground.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/ItemRenderBackground.kt @@ -2,47 +2,42 @@ package at.hannibal2.skyhanni.data import at.hannibal2.skyhanni.events.RenderRealOverlayEvent import at.hannibal2.skyhanni.utils.LorenzUtils +import at.hannibal2.skyhanni.utils.TimeLimitedCache import net.minecraft.client.Minecraft import net.minecraft.client.gui.Gui import net.minecraft.client.renderer.GlStateManager import net.minecraft.item.ItemStack import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import kotlin.time.Duration.Companion.milliseconds class ItemRenderBackground { companion object { - private val backgroundColor = mutableMapOf() - private val backgroundTime = mutableMapOf() - private val borderLineColor = mutableMapOf() - private val borderTime = mutableMapOf() + private val backgroundColour = TimeLimitedCache(60.milliseconds) + private val borderLineColour = TimeLimitedCache(60.milliseconds) var ItemStack.background: Int get() { - if (System.currentTimeMillis() > backgroundTime.getOrDefault(this, 0) + 60) return -1 - return backgroundColor.getOrDefault(this, -1) + return backgroundColour.getOrNull(this) ?: -1 } set(value) { - backgroundColor[this] = value - backgroundTime[this] = System.currentTimeMillis() + backgroundColour.put(this, value) } var ItemStack.borderLine: Int get() { - if (System.currentTimeMillis() > borderTime.getOrDefault(this, 0) + 60) return -1 - return borderLineColor.getOrDefault(this, -1) + return borderLineColour.getOrNull(this) ?: -1 } set(value) { - borderLineColor[this] = value - borderTime[this] = System.currentTimeMillis() + borderLineColour.put(this, value) } } @SubscribeEvent - fun renderOverlayLol(event: RenderRealOverlayEvent) { - val stack = event.stack + fun onRenderRealOverlay(event: RenderRealOverlayEvent) { + val stack = event.stack ?: return if (!LorenzUtils.inSkyBlock) return - if (stack == null) return val backgroundColor = stack.background if (backgroundColor != -1) { -- cgit