From 7401a8e3b4683fe79a5a996b6dc6c018a6e8cc18 Mon Sep 17 00:00:00 2001 From: Electroblob77 <35599699+Electroblob77@users.noreply.github.com> Date: Mon, 3 Aug 2020 22:47:08 +0100 Subject: [PATCH] First pass trying to optimise LivingUpdateEvent handlers, see issue #484 --- .../electroblob/wizardry/WizardryEventHandler.java | 12 ++---------- .../java/electroblob/wizardry/data/WizardData.java | 4 ++++ .../wizardry/potion/PotionContainment.java | 3 ++- .../electroblob/wizardry/potion/PotionDecay.java | 7 ++++--- .../java/electroblob/wizardry/spell/Intimidate.java | 3 ++- .../java/electroblob/wizardry/spell/MindControl.java | 4 +++- 6 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/main/java/electroblob/wizardry/WizardryEventHandler.java b/src/main/java/electroblob/wizardry/WizardryEventHandler.java index e5227c85..e9f1b62e 100644 --- a/src/main/java/electroblob/wizardry/WizardryEventHandler.java +++ b/src/main/java/electroblob/wizardry/WizardryEventHandler.java @@ -352,14 +352,6 @@ public final class WizardryEventHandler { @SubscribeEvent public static void onLivingUpdateEvent(LivingUpdateEvent event){ - // Experimental animation feature -// if(event.getEntityLiving().isHandActive() && event.getEntityLiving().getActiveItemStack().getItemUseAction() == WizardryUtilities.POINT){ -// event.getEntityLiving().isSwingInProgress = true; -// event.getEntityLiving().swingProgress = 1f; -// event.getEntityLiving().prevSwingProgress = 1; -// event.getEntityLiving().swingingHand = event.getEntityLiving().getActiveHand(); -// } - if(event.getEntityLiving().world.isRemote){ // Client-side continuous spell casting for NPCs @@ -379,10 +371,10 @@ public final class WizardryEventHandler { // TODO: This implementation of modifiers relies on them being accessible client-side. // Right now that doesn't matter because NPCs don't use modifiers, but they might in future ((EntityLiving)event.getEntity()).getAttackTarget(), modifiers); + + ((ISpellCaster)event.getEntity()).setSpellCounter(count + 1); } } - - ((ISpellCaster)event.getEntity()).setSpellCounter(count + 1); } } } diff --git a/src/main/java/electroblob/wizardry/data/WizardData.java b/src/main/java/electroblob/wizardry/data/WizardData.java index 50d9ecc1..7ef5d858 100644 --- a/src/main/java/electroblob/wizardry/data/WizardData.java +++ b/src/main/java/electroblob/wizardry/data/WizardData.java @@ -336,6 +336,10 @@ public class WizardData implements INBTSerializable { */ private void updateImbuedItems(){ + // FIXME: Iterating through the entire inventory each tick is an AWFUL way of doing this, and is super-slow! + // Change the imbuement durations map to be ItemStack -> Integer and just decrement them, adding entries + // whenever an item with an imbuement enters the inventory and removing them when the time expires. + Set activeImbuements = new HashSet(); // For each item in the player's inventory diff --git a/src/main/java/electroblob/wizardry/potion/PotionContainment.java b/src/main/java/electroblob/wizardry/potion/PotionContainment.java index 6fc5a19a..79c47d9f 100644 --- a/src/main/java/electroblob/wizardry/potion/PotionContainment.java +++ b/src/main/java/electroblob/wizardry/potion/PotionContainment.java @@ -114,7 +114,8 @@ public class PotionContainment extends PotionMagicEffect { @SubscribeEvent public static void onLivingUpdateEvent(LivingUpdateEvent event){ - if(event.getEntityLiving().getEntityData().hasKey(ENTITY_TAG) + // This is LAST-RESORT CLEANUP. It does NOT need checking every tick! We always check for the actual potion anyway. + if(event.getEntity().ticksExisted % 20 == 0 && event.getEntityLiving().getEntityData().hasKey(ENTITY_TAG) && !event.getEntityLiving().isPotionActive(WizardryPotions.containment)){ event.getEntityLiving().getEntityData().removeTag(ENTITY_TAG); } diff --git a/src/main/java/electroblob/wizardry/potion/PotionDecay.java b/src/main/java/electroblob/wizardry/potion/PotionDecay.java index 7ce204ff..085f151e 100644 --- a/src/main/java/electroblob/wizardry/potion/PotionDecay.java +++ b/src/main/java/electroblob/wizardry/potion/PotionDecay.java @@ -46,9 +46,10 @@ public class PotionDecay extends PotionMagicEffect { // amplifier of the potion effect, and is too slow for this purpose. EntityLivingBase target = event.getEntityLiving(); - - if(!target.world.isRemote && target.isPotionActive(WizardryPotions.decay) && target.onGround - && target.ticksExisted % Constants.DECAY_SPREAD_INTERVAL == 0){ + + // Do the timing check first, it'll cut out 95% of calls to all subsequent conditions + if(target.ticksExisted % Constants.DECAY_SPREAD_INTERVAL == 0 && !target.world.isRemote + && target.isPotionActive(WizardryPotions.decay) && target.onGround){ List entities = target.world.getEntitiesWithinAABBExcludingEntity(target, target.getEntityBoundingBox()); diff --git a/src/main/java/electroblob/wizardry/spell/Intimidate.java b/src/main/java/electroblob/wizardry/spell/Intimidate.java index 6b6bd101..33594e7c 100644 --- a/src/main/java/electroblob/wizardry/spell/Intimidate.java +++ b/src/main/java/electroblob/wizardry/spell/Intimidate.java @@ -119,7 +119,8 @@ public class Intimidate extends SpellAreaEffect { @SubscribeEvent public static void onLivingUpdateEvent(LivingUpdateEvent event){ - if(event.getEntityLiving().isPotionActive(WizardryPotions.fear) + // No need to do this every tick either + if(event.getEntity().ticksExisted % 50 == 0 && event.getEntityLiving().isPotionActive(WizardryPotions.fear) && event.getEntityLiving() instanceof EntityCreature){ NBTTagCompound entityNBT = event.getEntityLiving().getEntityData(); diff --git a/src/main/java/electroblob/wizardry/spell/MindControl.java b/src/main/java/electroblob/wizardry/spell/MindControl.java index 2417fa79..6ce3f206 100644 --- a/src/main/java/electroblob/wizardry/spell/MindControl.java +++ b/src/main/java/electroblob/wizardry/spell/MindControl.java @@ -196,7 +196,9 @@ public class MindControl extends SpellRay { // more efficient (because it only fires when the entity tries to set a target) // Of course, in survival this code is unlikely to be used much because the entity will always try to target the // player and hence will rarely have no target. - if(event.getEntityLiving().isPotionActive(WizardryPotions.mind_control) && event.getEntityLiving() instanceof EntityLiving){ + // No need to do it every tick either! + if(event.getEntity().ticksExisted % 50 == 0 && event.getEntityLiving().isPotionActive(WizardryPotions.mind_control) + && event.getEntityLiving() instanceof EntityLiving){ EntityLiving entity = (EntityLiving)event.getEntityLiving();