From e41b7e4bd2526950625b0d72f6cf1af0fb7f2224 Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal002@users.noreply.github.com> Date: Sun, 26 May 2024 11:38:17 +0200 Subject: Fix: Small Memory Leak (#1890) Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com> --- .../hannibal2/skyhanni/features/slayer/HideMobNames.kt | 17 ++++++++++------- .../skyhanni/features/summonings/SummoningSoulsName.kt | 15 +++++++++------ 2 files changed, 19 insertions(+), 13 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/at/hannibal2/skyhanni/features/slayer/HideMobNames.kt b/src/main/java/at/hannibal2/skyhanni/features/slayer/HideMobNames.kt index 28b03f597..c2534b02b 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/slayer/HideMobNames.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/slayer/HideMobNames.kt @@ -5,16 +5,18 @@ import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent import at.hannibal2.skyhanni.events.SkyHanniRenderEntityEvent import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher +import at.hannibal2.skyhanni.utils.TimeLimitedCache import net.minecraft.entity.EntityLivingBase import net.minecraft.entity.item.EntityArmorStand import net.minecraftforge.fml.common.eventhandler.EventPriority import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import java.util.regex.Pattern +import kotlin.time.Duration.Companion.minutes class HideMobNames { - private val lastMobName = mutableMapOf() - private val mobNamesHidden = mutableListOf() + private val lastMobName = TimeLimitedCache(2.minutes) + private val mobNamesHidden = mutableListOf() private val patterns = mutableListOf() init { @@ -56,19 +58,20 @@ class HideMobNames { if (!entity.hasCustomName()) return val name = entity.name - if (lastMobName.getOrDefault(entity, "abc") == name) { - if (entity in mobNamesHidden) { + val id = entity.entityId + if (lastMobName.getOrNull(id) == name) { + if (id in mobNamesHidden) { event.isCanceled = true } return } - lastMobName[entity] = name - mobNamesHidden.remove(entity) + lastMobName.put(id, name) + mobNamesHidden.remove(id) if (shouldNameBeHidden(name)) { event.isCanceled = true - mobNamesHidden.add(entity) + mobNamesHidden.add(id) } } diff --git a/src/main/java/at/hannibal2/skyhanni/features/summonings/SummoningSoulsName.kt b/src/main/java/at/hannibal2/skyhanni/features/summonings/SummoningSoulsName.kt index ce4a5e406..cabd8dd4e 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/summonings/SummoningSoulsName.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/summonings/SummoningSoulsName.kt @@ -11,10 +11,12 @@ import at.hannibal2.skyhanni.utils.EntityUtils.hasSkullTexture import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.LorenzVec import at.hannibal2.skyhanni.utils.RenderUtils.drawString +import at.hannibal2.skyhanni.utils.TimeLimitedCache import at.hannibal2.skyhanni.utils.getLorenzVec import net.minecraft.entity.EntityLiving import net.minecraft.entity.item.EntityArmorStand import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import kotlin.time.Duration.Companion.minutes class SummoningSoulsName { @@ -27,8 +29,8 @@ class SummoningSoulsName { "NjkxNzc4ZDVlOTU4NDAxNzAyMjdlYjllM2UyOTQzYmVhODUzOTI5Y2U5MjNjNTk4OWFkIgogICAgfQogIH0KfQ" private val souls = mutableMapOf() - private val mobsLastLocation = mutableMapOf() - private val mobsName = mutableMapOf() + private val mobsLastLocation = TimeLimitedCache(6.minutes) + private val mobsName = TimeLimitedCache(6.minutes) @SubscribeEvent fun onTick(event: LorenzTickEvent) { @@ -45,7 +47,7 @@ class SummoningSoulsName { if (entity.hasSkullTexture(texture)) { val soulLocation = entity.getLorenzVec() - val map = mutableMapOf() + val map = mutableMapOf() for ((mob, loc) in mobsLastLocation) { val distance = loc.distance(soulLocation) map[mob] = distance @@ -53,16 +55,17 @@ class SummoningSoulsName { val nearestMob = map.sorted().firstNotNullOfOrNull { it.key } if (nearestMob != null) { - souls[entity] = mobsName[nearestMob]!! + souls[entity] = mobsName.getOrNull(nearestMob)!! } } } for (entity in EntityUtils.getEntities()) { + val id = entity.entityId val consumer = entity.getNameTagWith(2, "§c❤") if (consumer != null && !consumer.name.contains("§e0")) { - mobsLastLocation[entity] = entity.getLorenzVec() - mobsName[entity] = consumer.name + mobsLastLocation.put(id, entity.getLorenzVec()) + mobsName.put(id, consumer.name) } } -- cgit