From b5f41d22449d037edd403251468c3369f9b98c07 Mon Sep 17 00:00:00 2001 From: Sollace Date: Wed, 12 Oct 2022 23:15:17 +0200 Subject: [PATCH] Improve performance by removing unneccessary nbt reads of spells when ticking --- .../ability/magic/spell/effect/HydrophobicSpell.java | 1 + .../unicopia/entity/CastSpellEntity.java | 2 +- .../com/minelittlepony/unicopia/entity/Living.java | 2 +- .../unicopia/network/datasync/EffectSync.java | 10 ++++------ .../unicopia/projectile/MagicProjectileEntity.java | 2 +- 5 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/HydrophobicSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/HydrophobicSpell.java index ff0a0c8e..64ce2943 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/HydrophobicSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/HydrophobicSpell.java @@ -48,6 +48,7 @@ public class HydrophobicSpell extends AbstractSpell { @Override public boolean tick(Caster source, Situation situation) { + if (!source.isClient()) { World world = source.getReferenceWorld(); diff --git a/src/main/java/com/minelittlepony/unicopia/entity/CastSpellEntity.java b/src/main/java/com/minelittlepony/unicopia/entity/CastSpellEntity.java index b0c1f30a..7ed61a94 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/CastSpellEntity.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/CastSpellEntity.java @@ -66,7 +66,7 @@ public class CastSpellEntity extends LightEmittingEntity implements Caster Operation.ofBoolean(spell.tick(this, Situation.GROUND_ENTITY)), true)) { + if (!getSpellSlot().forEach(spell -> Operation.ofBoolean(spell.tick(this, Situation.GROUND_ENTITY)), world.isClient)) { discard(); } } diff --git a/src/main/java/com/minelittlepony/unicopia/entity/Living.java b/src/main/java/com/minelittlepony/unicopia/entity/Living.java index 514c28b2..0b80b87e 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/Living.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/Living.java @@ -103,7 +103,7 @@ public abstract class Living implements Equine, Caste @Override public void tick() { try { - getSpellSlot().forEach(spell -> Operation.ofBoolean(spell.tick(this, Situation.BODY)), true); + getSpellSlot().forEach(spell -> Operation.ofBoolean(spell.tick(this, Situation.BODY)), entity.world.isClient); } catch (Exception e) { Unicopia.LOGGER.error("Error whilst ticking spell on entity {}", getEntity(), e); } diff --git a/src/main/java/com/minelittlepony/unicopia/network/datasync/EffectSync.java b/src/main/java/com/minelittlepony/unicopia/network/datasync/EffectSync.java index 33b71d91..6e4dd1ca 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/datasync/EffectSync.java +++ b/src/main/java/com/minelittlepony/unicopia/network/datasync/EffectSync.java @@ -66,7 +66,7 @@ public class EffectSync implements SpellContainer { @Override public boolean removeWhere(Predicate test, boolean update) { - return reduce((initial, effect) -> { + return reduce(update, (initial, effect) -> { if (!test.test(effect)) { return initial; } @@ -77,7 +77,7 @@ public class EffectSync implements SpellContainer { @Override public boolean forEach(Function test, boolean update) { - return reduce((initial, effect) -> { + return reduce(update, (initial, effect) -> { Operation op = test.apply(effect); if (op == Operation.REMOVE) { spells.removeReference(effect); @@ -122,11 +122,9 @@ public class EffectSync implements SpellContainer { return (Stream)spells.getReferences().flatMap(s -> s.findMatches(type)); } - public boolean reduce(Alteration alteration) { - spells.fromNbt(owner.getEntity().getDataTracker().get(param)); - + private boolean reduce(boolean update, Alteration alteration) { boolean initial = false; - for (Spell i : spells.getReferences().toList()) { + for (Spell i : read(null, update, false).toList()) { initial = alteration.apply(initial, i); } diff --git a/src/main/java/com/minelittlepony/unicopia/projectile/MagicProjectileEntity.java b/src/main/java/com/minelittlepony/unicopia/projectile/MagicProjectileEntity.java index b35735fc..586ad427 100644 --- a/src/main/java/com/minelittlepony/unicopia/projectile/MagicProjectileEntity.java +++ b/src/main/java/com/minelittlepony/unicopia/projectile/MagicProjectileEntity.java @@ -301,7 +301,7 @@ public class MagicProjectileEntity extends ThrownItemEntity implements Caster