First pass trying to optimise LivingUpdateEvent handlers, see issue #484

This commit is contained in:
Electroblob77
2020-08-03 22:47:08 +01:00
parent 9d8b0b3a90
commit 7401a8e3b4
6 changed files with 17 additions and 16 deletions
@@ -352,14 +352,6 @@ public final class WizardryEventHandler {
@SubscribeEvent @SubscribeEvent
public static void onLivingUpdateEvent(LivingUpdateEvent event){ 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){ if(event.getEntityLiving().world.isRemote){
// Client-side continuous spell casting for NPCs // 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. // 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 // Right now that doesn't matter because NPCs don't use modifiers, but they might in future
((EntityLiving)event.getEntity()).getAttackTarget(), modifiers); ((EntityLiving)event.getEntity()).getAttackTarget(), modifiers);
((ISpellCaster)event.getEntity()).setSpellCounter(count + 1);
} }
} }
((ISpellCaster)event.getEntity()).setSpellCounter(count + 1);
} }
} }
} }
@@ -336,6 +336,10 @@ public class WizardData implements INBTSerializable<NBTTagCompound> {
*/ */
private void updateImbuedItems(){ 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<Imbuement> activeImbuements = new HashSet<Imbuement>(); Set<Imbuement> activeImbuements = new HashSet<Imbuement>();
// For each item in the player's inventory // For each item in the player's inventory
@@ -114,7 +114,8 @@ public class PotionContainment extends PotionMagicEffect {
@SubscribeEvent @SubscribeEvent
public static void onLivingUpdateEvent(LivingUpdateEvent event){ 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().isPotionActive(WizardryPotions.containment)){
event.getEntityLiving().getEntityData().removeTag(ENTITY_TAG); event.getEntityLiving().getEntityData().removeTag(ENTITY_TAG);
} }
@@ -46,9 +46,10 @@ public class PotionDecay extends PotionMagicEffect {
// amplifier of the potion effect, and is too slow for this purpose. // amplifier of the potion effect, and is too slow for this purpose.
EntityLivingBase target = event.getEntityLiving(); EntityLivingBase target = event.getEntityLiving();
if(!target.world.isRemote && target.isPotionActive(WizardryPotions.decay) && target.onGround // Do the timing check first, it'll cut out 95% of calls to all subsequent conditions
&& target.ticksExisted % Constants.DECAY_SPREAD_INTERVAL == 0){ if(target.ticksExisted % Constants.DECAY_SPREAD_INTERVAL == 0 && !target.world.isRemote
&& target.isPotionActive(WizardryPotions.decay) && target.onGround){
List<Entity> entities = target.world.getEntitiesWithinAABBExcludingEntity(target, List<Entity> entities = target.world.getEntitiesWithinAABBExcludingEntity(target,
target.getEntityBoundingBox()); target.getEntityBoundingBox());
@@ -119,7 +119,8 @@ public class Intimidate extends SpellAreaEffect {
@SubscribeEvent @SubscribeEvent
public static void onLivingUpdateEvent(LivingUpdateEvent event){ 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){ && event.getEntityLiving() instanceof EntityCreature){
NBTTagCompound entityNBT = event.getEntityLiving().getEntityData(); NBTTagCompound entityNBT = event.getEntityLiving().getEntityData();
@@ -196,7 +196,9 @@ public class MindControl extends SpellRay {
// more efficient (because it only fires when the entity tries to set a target) // 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 // 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. // 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(); EntityLiving entity = (EntityLiving)event.getEntityLiving();