From 33a26a50595b595864cc10dc9851e7d24be4a7ab Mon Sep 17 00:00:00 2001 From: quentin452 <42176772+quentin452@users.noreply.github.com> Date: Fri, 5 May 2023 20:02:07 +0200 Subject: [PATCH] Fix tps lag in orespawn dimensions caused by the onSpawn methods (#28) * Fix tps lag introduced by onSpawn methods Especially in Orespawn dimensions like unstable dimension theses methods destroys tps before Tested in my modpack spark profiler before : ~ 13 ms https://spark.lucko.me/6KkG1Tq4Su spark profiler after : ~ 8.5 ms https://spark.lucko.me/6m2vv9Hb1X However, originals versions may be more appropriate in some situations where you need to find all players in a given area rather than the closest player. * Revert "Fix tps lag introduced by onSpawn methods" This reverts commit 76a23a28dbd09698106539578cd8bff4d5c01523. * Fix tps lags in orespawn dimension --- .../common/brew/potion/PotionBloodthirst.java | 14 +++++++++----- .../common/brew/potion/PotionEmptiness.java | 15 ++++++++++----- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/main/java/vazkii/botania/common/brew/potion/PotionBloodthirst.java b/src/main/java/vazkii/botania/common/brew/potion/PotionBloodthirst.java index 44eadad2b..075ce7cb0 100644 --- a/src/main/java/vazkii/botania/common/brew/potion/PotionBloodthirst.java +++ b/src/main/java/vazkii/botania/common/brew/potion/PotionBloodthirst.java @@ -11,6 +11,7 @@ package vazkii.botania.common.brew.potion; import java.util.List; +import java.util.stream.Collectors; import net.minecraft.entity.monster.IMob; import net.minecraft.entity.player.EntityPlayer; @@ -31,17 +32,20 @@ public class PotionBloodthirst extends PotionMod { super(ConfigHandler.potionIDBloodthirst, LibPotionNames.BLOODTHIRST, false, 0xC30000, 3); MinecraftForge.EVENT_BUS.register(this); } - @SubscribeEvent public void onSpawn(LivingSpawnEvent.CheckSpawn event) { - if(event.getResult() != Result.ALLOW && event.entityLiving instanceof IMob) { - List players = event.world.getEntitiesWithinAABB(EntityPlayer.class, AxisAlignedBB.getBoundingBox(event.x - RANGE, event.y - RANGE, event.z - RANGE, event.x + RANGE, event.y + RANGE, event.z + RANGE)); - for(EntityPlayer player : players) + if (event.getResult() != Result.ALLOW && event.entityLiving instanceof IMob) { + double rangeSq = RANGE * RANGE; + List players = (List) event.world.playerEntities.stream() + .filter(player -> ((EntityPlayer) player).getDistanceSq(event.x, event.y, event.z) <= rangeSq) + .map(player -> (EntityPlayer) player) + .collect(Collectors.toList()); + for (EntityPlayer player : players) { if(hasEffect(player) && !hasEffect(player, ModPotions.emptiness)) { event.setResult(Result.ALLOW); return; } + } } } - } diff --git a/src/main/java/vazkii/botania/common/brew/potion/PotionEmptiness.java b/src/main/java/vazkii/botania/common/brew/potion/PotionEmptiness.java index 6d2b77ea0..3217a3d43 100644 --- a/src/main/java/vazkii/botania/common/brew/potion/PotionEmptiness.java +++ b/src/main/java/vazkii/botania/common/brew/potion/PotionEmptiness.java @@ -11,6 +11,7 @@ package vazkii.botania.common.brew.potion; import java.util.List; +import java.util.stream.Collectors; import net.minecraft.entity.monster.IMob; import net.minecraft.entity.player.EntityPlayer; @@ -33,14 +34,18 @@ public class PotionEmptiness extends PotionMod { @SubscribeEvent public void onSpawn(LivingSpawnEvent.CheckSpawn event) { - if(event.getResult() != Result.ALLOW && event.entityLiving instanceof IMob) { - List players = event.world.getEntitiesWithinAABB(EntityPlayer.class, AxisAlignedBB.getBoundingBox(event.x - RANGE, event.y - RANGE, event.z - RANGE, event.x + RANGE, event.y + RANGE, event.z + RANGE)); - for(EntityPlayer player : players) - if(hasEffect(player)) { + if (event.getResult() != Result.ALLOW && event.entityLiving instanceof IMob) { + double rangeSq = RANGE * RANGE; + List players = (List) event.world.playerEntities.stream() + .filter(player -> ((EntityPlayer) player).getDistanceSq(event.x, event.y, event.z) <= rangeSq) + .map(player -> (EntityPlayer) player) + .collect(Collectors.toList()); + for (EntityPlayer player : players) { + if (hasEffect(player)) { event.setResult(Result.DENY); return; } + } } } - }