diff --git a/src/main/java/com/minelittlepony/unicopia/ability/ChangelingDisguiseAbility.java b/src/main/java/com/minelittlepony/unicopia/ability/ChangelingDisguiseAbility.java index a4b44d93..92aa1cfb 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/ChangelingDisguiseAbility.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/ChangelingDisguiseAbility.java @@ -58,12 +58,9 @@ public class ChangelingDisguiseAbility extends ChangelingFeedAbility { player.getEntityWorld().playSound(null, player.getBlockPos(), SoundEvents.ENTITY_PARROT_IMITATE_RAVAGER, SoundCategory.PLAYERS, 1.4F, 0.4F); - iplayer.getSpellSlot().get(SpellType.DISGUISE, true).orElseGet(() -> { - DisguiseSpell disc = SpellType.DISGUISE.create(); - - iplayer.setSpell(disc); - return disc; - }).setDisguise(looked); + iplayer.getSpellSlot().get(SpellType.DISGUISE, true) + .orElseGet(() -> SpellType.DISGUISE.apply(iplayer)) + .setDisguise(looked); if (!player.isCreative()) { iplayer.getMagicalReserves().getMana().multiply(0.1F); diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/AbstractSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/AbstractSpell.java index 670a5f93..a053a3fd 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/AbstractSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/AbstractSpell.java @@ -66,7 +66,6 @@ public abstract class AbstractSpell implements Spell { @Override public void onDestroyed(Caster caster) { - setDead(); } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/DisguiseSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/DisguiseSpell.java index 8768132a..10f525ab 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/DisguiseSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/DisguiseSpell.java @@ -44,8 +44,12 @@ public class DisguiseSpell extends AbstractSpell implements Attached, Suppressab @Override public void onDestroyed(Caster caster) { - super.onDestroyed(caster); caster.getEntity().calculateDimensions(); + caster.getEntity().setInvisible(false); + if (caster instanceof Pony) { + ((Pony) caster).setInvisible(false); + } + disguise.remove(); } @Override @@ -164,7 +168,7 @@ public class DisguiseSpell extends AbstractSpell implements Attached, Suppressab } } - return !source.getMaster().isDead(); + return !isDead() && !source.getMaster().isDead(); } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/NecromancySpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/NecromancySpell.java index 420aecb6..07d6d695 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/NecromancySpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/NecromancySpell.java @@ -85,7 +85,6 @@ public class NecromancySpell extends AbstractPlacedSpell { @Override public void onDestroyed(Caster caster) { - super.onDestroyed(caster); LivingEntity master = caster.getMaster(); summonedEntities.forEach(ref -> { ref.ifPresent(caster.getWorld(), e -> { diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/SpellType.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/SpellType.java index 7b0356c5..d794de7f 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/SpellType.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/SpellType.java @@ -118,13 +118,19 @@ public final class SpellType implements Affine, SpellPredicate< return null; } - public boolean apply(Caster caster) { + @Nullable + public T apply(Caster caster) { if (isEmpty()) { caster.setSpell(null); - return false; + return null; } - return create().apply(caster); + T spell = create(); + if (spell.apply(caster)) { + return spell; + } + + return null; } @Override @@ -162,8 +168,8 @@ public final class SpellType implements Affine, SpellPredicate< } @Nullable - public static Spell fromNBT(CompoundTag compound) { - if (compound.contains("effect_id")) { + public static Spell fromNBT(@Nullable CompoundTag compound) { + if (compound != null && compound.contains("effect_id")) { Spell effect = getKey(new Identifier(compound.getString("effect_id"))).create(); if (effect != null) { diff --git a/src/main/java/com/minelittlepony/unicopia/command/DisguiseCommand.java b/src/main/java/com/minelittlepony/unicopia/command/DisguiseCommand.java index 7af1c30d..bfcc4c62 100644 --- a/src/main/java/com/minelittlepony/unicopia/command/DisguiseCommand.java +++ b/src/main/java/com/minelittlepony/unicopia/command/DisguiseCommand.java @@ -61,9 +61,8 @@ public class DisguiseCommand { throw FAILED_EXCEPTION.create(); } - iplayer.getSpellSlot() - .get(SpellType.DISGUISE, true) - .orElseGet(SpellType.DISGUISE::create) + iplayer.getSpellSlot().get(SpellType.DISGUISE, true) + .orElseGet(() -> SpellType.DISGUISE.apply(iplayer)) .setDisguise(entity); if (!isSelf) { diff --git a/src/main/java/com/minelittlepony/unicopia/network/EffectSync.java b/src/main/java/com/minelittlepony/unicopia/network/EffectSync.java index db30309f..9ae4999a 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/EffectSync.java +++ b/src/main/java/com/minelittlepony/unicopia/network/EffectSync.java @@ -1,5 +1,6 @@ package com.minelittlepony.unicopia.network; +import java.util.Objects; import java.util.Optional; import javax.annotation.Nullable; @@ -12,7 +13,6 @@ import com.minelittlepony.unicopia.ability.magic.spell.SpellType; import net.minecraft.entity.data.TrackedData; import net.minecraft.nbt.CompoundTag; -import net.minecraft.util.Identifier; /** * Synchronisation class for spells. @@ -66,31 +66,39 @@ public class EffectSync implements SpellContainer { Spell effect = spell.orElse(null); - if (comp == null || !comp.contains("effect_id")) { - updateReference(null); - } else if (!checkReference() || !effect.getType().getId().equals(new Identifier(comp.getString("effect_id")))) { + if (comp == null || !comp.contains("effect_id") || !comp.contains("uuid")) { + if (effect != null) { + updateReference(null); + } + } else if (effect == null || !effect.getUuid().equals(comp.getUuid("uuid"))) { updateReference(SpellType.fromNBT(comp)); - } else if (owner.getEntity().world.isClient()) { - if (lastValue != comp || !(comp == null || comp.equals(lastValue))) { + } else if (owner.isClient()) { + if (!Objects.equals(lastValue, comp)) { lastValue = comp; effect.fromNBT(comp); } - } else if ((force || !owner.isClient()) && effect.isDirty()) { + } else if (force && effect.isDirty()) { put(effect); } } @Override public void put(@Nullable Spell effect) { + effect = effect == null || effect.isDead() ? null : effect; updateReference(effect); owner.getEntity().getDataTracker().set(param, effect == null ? new CompoundTag() : SpellType.toNBT(effect)); } private void updateReference(@Nullable Spell effect) { - if (spell.isPresent() && spell.get() != effect) { - spell.get().setDead(); - spell.get().onDestroyed(owner); + @Nullable + Spell old = spell.orElse(null); + if (old != effect) { + spell = Optional.ofNullable(effect); + + if (old != null && (effect == null || !old.getUuid().equals(effect.getUuid()))) { + old.setDead(); + old.onDestroyed(owner); + } } - spell = Optional.ofNullable(effect); } }