diff options
author | Roman / Linnea Gräf <nea@nea.moe> | 2023-08-02 14:23:41 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-02 14:23:41 +0200 |
commit | 6cb3282b20bf2533a8fde4f6d30e0530d53f63dd (patch) | |
tree | dee0b1bc2a7bf77ea3651c60517ef2d702c4de2f | |
parent | f7664fb62342e6b08462da634b19a2b05100c040 (diff) | |
download | skyhanni-6cb3282b20bf2533a8fde4f6d30e0530d53f63dd.tar.gz skyhanni-6cb3282b20bf2533a8fde4f6d30e0530d53f63dd.tar.bz2 skyhanni-6cb3282b20bf2533a8fde4f6d30e0530d53f63dd.zip |
Merge pull request #351
* Performance improvements for the damage indicator
* Using MutableMap.put
* Using Sequence for every entity iteration
5 files changed, 21 insertions, 20 deletions
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<MayorJson.Candidate>.bestCandidate() = maxBy { it.votes } - - private infix fun <K, V> MutableMap<K, V>.put(pairs: Pair<K, V>) { - 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<EntityLivingBase>()) { - checkEntity(entity) + if (!isEnabled()) return + data = data.editCopy { + EntityUtils.getEntities<EntityLivingBase>().mapNotNull(::checkEntity).forEach { this put it } } } - private fun checkEntity(entity: EntityLivingBase) { + private fun checkEntity(entity: EntityLivingBase): Pair<UUID, EntityData>? { 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<LorenzVec>() + private var endermanLocations = sequenceOf<LorenzVec>() @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 <reified T : Entity> getEntitiesNextToPlayer(radius: Double): List<T> = + inline fun <reified T : Entity> getEntitiesNextToPlayer(radius: Double): Sequence<T> = getEntitiesNearby<T>(LocationUtils.playerLocation(), radius) - inline fun <reified T : Entity> getEntitiesNearby(location: LorenzVec, radius: Double): List<T> = + inline fun <reified T : Entity> getEntitiesNearby(location: LorenzVec, radius: Double): Sequence<T> = getEntities<T>().filter { it.distanceTo(location) < radius } fun EntityLivingBase.isAtFullHealth() = baseMaxHealth == health.toInt() @@ -156,11 +156,11 @@ object EntityUtils { fun EntityEnderman.getBlockInHand(): IBlockState? = heldBlockState - inline fun <reified R : Entity> getEntities(): List<R> = getAllEntities().filterIsInstance<R>() + inline fun <reified R : Entity> getEntities(): Sequence<R> = getAllEntities().filterIsInstance<R>() - inline fun <reified R : Entity> getEntitiesOrNull(): List<R>? = getAllEntitiesOrNull()?.filterIsInstance<R>() + inline fun <reified R : Entity> getEntitiesOrNull(): Sequence<R>? = getAllEntitiesOrNull()?.filterIsInstance<R>() - fun getAllEntities(): List<Entity> = getAllEntitiesOrNull() ?: error("minecraft.world.loadedEntityList is null.") + fun getAllEntities(): Sequence<Entity> = getAllEntitiesOrNull() ?: emptySequence() - fun getAllEntitiesOrNull(): List<Entity>? = Minecraft.getMinecraft()?.theWorld?.loadedEntityList?.toMutableList() + fun getAllEntitiesOrNull(): Sequence<Entity>? = 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 <K, V> MutableMap<K, V>.put(pairs: Pair<K, V>) { + this[pairs.first] = pairs.second + } }
\ No newline at end of file |