Improve performance by removing unneccessary nbt reads of spells when ticking

This commit is contained in:
Sollace 2022-10-12 23:15:17 +02:00
parent 7cb560802f
commit b5f41d2244
5 changed files with 8 additions and 9 deletions

View file

@ -48,6 +48,7 @@ public class HydrophobicSpell extends AbstractSpell {
@Override @Override
public boolean tick(Caster<?> source, Situation situation) { public boolean tick(Caster<?> source, Situation situation) {
if (!source.isClient()) { if (!source.isClient()) {
World world = source.getReferenceWorld(); World world = source.getReferenceWorld();

View file

@ -66,7 +66,7 @@ public class CastSpellEntity extends LightEmittingEntity implements Caster<Livin
return; return;
} }
if (!getSpellSlot().forEach(spell -> Operation.ofBoolean(spell.tick(this, Situation.GROUND_ENTITY)), true)) { if (!getSpellSlot().forEach(spell -> Operation.ofBoolean(spell.tick(this, Situation.GROUND_ENTITY)), world.isClient)) {
discard(); discard();
} }
} }

View file

@ -103,7 +103,7 @@ public abstract class Living<T extends LivingEntity> implements Equine<T>, Caste
@Override @Override
public void tick() { public void tick() {
try { 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) { } catch (Exception e) {
Unicopia.LOGGER.error("Error whilst ticking spell on entity {}", getEntity(), e); Unicopia.LOGGER.error("Error whilst ticking spell on entity {}", getEntity(), e);
} }

View file

@ -66,7 +66,7 @@ public class EffectSync implements SpellContainer {
@Override @Override
public boolean removeWhere(Predicate<Spell> test, boolean update) { public boolean removeWhere(Predicate<Spell> test, boolean update) {
return reduce((initial, effect) -> { return reduce(update, (initial, effect) -> {
if (!test.test(effect)) { if (!test.test(effect)) {
return initial; return initial;
} }
@ -77,7 +77,7 @@ public class EffectSync implements SpellContainer {
@Override @Override
public boolean forEach(Function<Spell, Operation> test, boolean update) { public boolean forEach(Function<Spell, Operation> test, boolean update) {
return reduce((initial, effect) -> { return reduce(update, (initial, effect) -> {
Operation op = test.apply(effect); Operation op = test.apply(effect);
if (op == Operation.REMOVE) { if (op == Operation.REMOVE) {
spells.removeReference(effect); spells.removeReference(effect);
@ -122,11 +122,9 @@ public class EffectSync implements SpellContainer {
return (Stream<T>)spells.getReferences().flatMap(s -> s.findMatches(type)); return (Stream<T>)spells.getReferences().flatMap(s -> s.findMatches(type));
} }
public boolean reduce(Alteration alteration) { private boolean reduce(boolean update, Alteration alteration) {
spells.fromNbt(owner.getEntity().getDataTracker().get(param));
boolean initial = false; boolean initial = false;
for (Spell i : spells.getReferences().toList()) { for (Spell i : read(null, update, false).toList()) {
initial = alteration.apply(initial, i); initial = alteration.apply(initial, i);
} }

View file

@ -301,7 +301,7 @@ public class MagicProjectileEntity extends ThrownItemEntity implements Caster<Li
consumer.accept((ProjectileDelegate)spell); consumer.accept((ProjectileDelegate)spell);
} }
return Operation.SKIP; return Operation.SKIP;
}, true); }, world.isClient);
if (getItem().getItem() instanceof ProjectileDelegate) { if (getItem().getItem() instanceof ProjectileDelegate) {
consumer.accept(((ProjectileDelegate)getItem().getItem())); consumer.accept(((ProjectileDelegate)getItem().getItem()));
} }