From 6cb3282b20bf2533a8fde4f6d30e0530d53f63dd Mon Sep 17 00:00:00 2001 From: Roman / Linnea Gräf Date: Wed, 2 Aug 2023 14:23:41 +0200 Subject: Merge pull request #351 * Performance improvements for the damage indicator * Using MutableMap.put * Using Sequence for every entity iteration --- .../java/at/hannibal2/skyhanni/data/MayorElection.kt | 5 +---- .../features/damageindicator/DamageIndicatorManager.kt | 18 +++++++++--------- .../slayer/enderman/EndermanSlayerHideParticles.kt | 2 +- .../java/at/hannibal2/skyhanni/utils/EntityUtils.kt | 12 ++++++------ .../java/at/hannibal2/skyhanni/utils/LorenzUtils.kt | 4 ++++ 5 files changed, 21 insertions(+), 20 deletions(-) (limited to 'src/main/java/at/hannibal2') diff --git a/src/main/java/at/hannibal2/skyhanni/data/MayorElection.kt b/src/main/java/at/hannibal2/skyhanni/data/MayorElection.kt index a0b8ae9bc..92f7bebb3 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/MayorElection.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/MayorElection.kt @@ -5,6 +5,7 @@ import at.hannibal2.skyhanni.config.ConfigManager import at.hannibal2.skyhanni.events.LorenzTickEvent import at.hannibal2.skyhanni.utils.APIUtil import at.hannibal2.skyhanni.utils.LorenzUtils +import at.hannibal2.skyhanni.utils.LorenzUtils.put import at.hannibal2.skyhanni.utils.jsonobjects.MayorJson import io.github.moulberry.notenoughupdates.util.SkyBlockTime import kotlinx.coroutines.Dispatchers @@ -74,8 +75,4 @@ class MayorElection { private fun MayorJson.Election.getPairs() = year + 1 to candidates.bestCandidate() private fun List.bestCandidate() = maxBy { it.votes } - - private infix fun MutableMap.put(pairs: Pair) { - this[pairs.first] = pairs.second - } } \ No newline at end of file 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..83a2abbec 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/damageindicator/DamageIndicatorManager.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/damageindicator/DamageIndicatorManager.kt @@ -13,6 +13,7 @@ import at.hannibal2.skyhanni.utils.LocationUtils.distanceToPlayer import at.hannibal2.skyhanni.utils.LorenzUtils.baseMaxHealth import at.hannibal2.skyhanni.utils.LorenzUtils.between import at.hannibal2.skyhanni.utils.LorenzUtils.editCopy +import at.hannibal2.skyhanni.utils.LorenzUtils.put import at.hannibal2.skyhanni.utils.LorenzUtils.round import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators import at.hannibal2.skyhanni.utils.RenderUtils.drawDynamicText @@ -297,15 +298,15 @@ 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.getEntities().mapNotNull(::checkEntity).forEach { this put it } } } - 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 +332,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 +351,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/features/slayer/enderman/EndermanSlayerHideParticles.kt b/src/main/java/at/hannibal2/skyhanni/features/slayer/enderman/EndermanSlayerHideParticles.kt index d6ae27f3b..d2abd8bf1 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/slayer/enderman/EndermanSlayerHideParticles.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/slayer/enderman/EndermanSlayerHideParticles.kt @@ -14,7 +14,7 @@ import net.minecraftforge.fml.common.eventhandler.SubscribeEvent class EndermanSlayerHideParticles { - private var endermanLocations = listOf() + private var endermanLocations = sequenceOf() @SubscribeEvent fun onTick(event: LorenzTickEvent) { diff --git a/src/main/java/at/hannibal2/skyhanni/utils/EntityUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/EntityUtils.kt index 9bfb9e1bb..42a5b5583 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/EntityUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/EntityUtils.kt @@ -126,10 +126,10 @@ object EntityUtils { ?.value } - inline fun getEntitiesNextToPlayer(radius: Double): List = + inline fun getEntitiesNextToPlayer(radius: Double): Sequence = getEntitiesNearby(LocationUtils.playerLocation(), radius) - inline fun getEntitiesNearby(location: LorenzVec, radius: Double): List = + inline fun getEntitiesNearby(location: LorenzVec, radius: Double): Sequence = getEntities().filter { it.distanceTo(location) < radius } fun EntityLivingBase.isAtFullHealth() = baseMaxHealth == health.toInt() @@ -156,11 +156,11 @@ object EntityUtils { fun EntityEnderman.getBlockInHand(): IBlockState? = heldBlockState - inline fun getEntities(): List = getAllEntities().filterIsInstance() + inline fun getEntities(): Sequence = getAllEntities().filterIsInstance() - inline fun getEntitiesOrNull(): List? = getAllEntitiesOrNull()?.filterIsInstance() + inline fun getEntitiesOrNull(): Sequence? = getAllEntitiesOrNull()?.filterIsInstance() - fun getAllEntities(): List = getAllEntitiesOrNull() ?: error("minecraft.world.loadedEntityList is null.") + fun getAllEntities(): Sequence = getAllEntitiesOrNull() ?: emptySequence() - fun getAllEntitiesOrNull(): List? = Minecraft.getMinecraft()?.theWorld?.loadedEntityList?.toMutableList() + fun getAllEntitiesOrNull(): Sequence? = Minecraft.getMinecraft()?.theWorld?.loadedEntityList?.asSequence() } \ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt index f84d135ef..64281ddf8 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt @@ -422,4 +422,8 @@ object LorenzUtils { } return false } + + infix fun MutableMap.put(pairs: Pair) { + this[pairs.first] = pairs.second + } } \ No newline at end of file -- cgit