From 240d518f447676bc1a897dc9e2fac1f6b01fe625 Mon Sep 17 00:00:00 2001 From: nea Date: Tue, 1 Aug 2023 19:25:46 +0200 Subject: Performance improvements for the damage indicator --- .../damageindicator/DamageIndicatorManager.kt | 20 +++++++++++--------- .../java/at/hannibal2/skyhanni/utils/EntityUtils.kt | 1 + 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/main/java/at/hannibal2/skyhanni/features/damageindicator/DamageIndicatorManager.kt b/src/main/java/at/hannibal2/skyhanni/features/damageindicator/DamageIndicatorManager.kt index d46bae435..883d69c42 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/damageindicator/DamageIndicatorManager.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/damageindicator/DamageIndicatorManager.kt @@ -297,15 +297,18 @@ class DamageIndicatorManager { @SubscribeEvent fun onTick(event: LorenzTickEvent) { - if (!LorenzUtils.inSkyBlock) return - for (entity in EntityUtils.getEntities()) { - checkEntity(entity) + if (!isEnabled()) return + data = data.editCopy { + EntityUtils.getEntitiesSequence().mapNotNull(::checkEntity) + .forEach { (uuid, entityData) -> + this[uuid] = entityData + } } } - private fun checkEntity(entity: EntityLivingBase) { + private fun checkEntity(entity: EntityLivingBase): Pair? { try { - val entityData = grabData(entity) ?: return + val entityData = grabData(entity) ?: return null if (LorenzUtils.inDungeons) { checkFinalBoss(entityData.finalDungeonBoss, entity.entityId) } @@ -331,7 +334,7 @@ class DamageIndicatorManager { } "§cDead" } else { - getCustomHealth(entityData, health, entity, maxHealth) ?: return + getCustomHealth(entityData, health, entity, maxHealth) ?: return null } if (data.containsKey(entity.uniqueID)) { @@ -350,11 +353,10 @@ class DamageIndicatorManager { entityData.healthText = color.getChatColor() + NumberUtil.format(health) } entityData.timeLastTick = System.currentTimeMillis() - data = data.editCopy { this[entity.uniqueID] = entityData } - - + return entity.uniqueID to entityData } catch (e: Throwable) { e.printStackTrace() + return null } } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/EntityUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/EntityUtils.kt index 9bfb9e1bb..24f838cfc 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/EntityUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/EntityUtils.kt @@ -157,6 +157,7 @@ object EntityUtils { fun EntityEnderman.getBlockInHand(): IBlockState? = heldBlockState inline fun getEntities(): List = getAllEntities().filterIsInstance() + inline fun getEntitiesSequence(): Sequence = Minecraft.getMinecraft()?.theWorld?.loadedEntityList?.asSequence()?.filterIsInstance() ?: emptySequence() inline fun getEntitiesOrNull(): List? = getAllEntitiesOrNull()?.filterIsInstance() -- cgit