diff --git a/src/main/java/com/minelittlepony/unicopia/item/ItemSpell.java b/src/main/java/com/minelittlepony/unicopia/item/ItemSpell.java index 77acfb09..1bb32617 100644 --- a/src/main/java/com/minelittlepony/unicopia/item/ItemSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/item/ItemSpell.java @@ -6,14 +6,11 @@ import javax.annotation.Nullable; import com.minelittlepony.unicopia.Predicates; import com.minelittlepony.unicopia.entity.EntitySpell; -import com.minelittlepony.unicopia.player.IPlayer; -import com.minelittlepony.unicopia.player.PlayerSpeciesList; import com.minelittlepony.unicopia.spell.IMagicEffect; import com.minelittlepony.unicopia.spell.IUseAction; import com.minelittlepony.unicopia.spell.SpellAffinity; import com.minelittlepony.unicopia.spell.SpellCastResult; import com.minelittlepony.unicopia.spell.IDispenceable; -import com.minelittlepony.unicopia.spell.IHeldEffect; import com.minelittlepony.unicopia.spell.SpellRegistry; import com.minelittlepony.util.lang.ClientLocale; import com.minelittlepony.util.vector.VecHelper; @@ -84,17 +81,7 @@ public class ItemSpell extends Item implements ICastable { } public void onUpdate(ItemStack stack, World world, Entity entity, int slot, boolean isSelected) { - if (isSelected && entity instanceof EntityPlayer) { - IPlayer player = PlayerSpeciesList.instance().getPlayer((EntityPlayer)entity); - if (player.getPlayerSpecies().canCast()) { - IHeldEffect effect = SpellRegistry.instance().getHeldFrom(stack); - - if (effect != null) { - effect.updateInHand(player, getAffinity(stack)); - } - } - } } public Item setTranslationKey(String key) { diff --git a/src/main/java/com/minelittlepony/unicopia/player/IPlayer.java b/src/main/java/com/minelittlepony/unicopia/player/IPlayer.java index 8deeaf76..fafd9a55 100644 --- a/src/main/java/com/minelittlepony/unicopia/player/IPlayer.java +++ b/src/main/java/com/minelittlepony/unicopia/player/IPlayer.java @@ -9,6 +9,7 @@ import com.minelittlepony.unicopia.UClient; import com.minelittlepony.unicopia.enchanting.IPageOwner; import com.minelittlepony.unicopia.network.ITransmittable; import com.minelittlepony.unicopia.spell.ICaster; +import com.minelittlepony.unicopia.spell.IHeldEffect; import com.mojang.authlib.GameProfile; import net.minecraft.entity.Entity; @@ -121,6 +122,15 @@ public interface IPlayer extends ICaster, IRaceContainer EFFECT = EntityDataManager .createKey(EntityPlayer.class, DataSerializers.COMPOUND_TAG); + private static final DataParameter HELD_EFFECT = EntityDataManager + .createKey(EntityPlayer.class, DataSerializers.COMPOUND_TAG); private final Map pageStates = Maps.newHashMap(); @@ -68,6 +73,7 @@ class PlayerCapabilities implements IPlayer { private final PlayerInventory inventory = new PlayerInventory(this); private final EffectSync effectDelegate = new EffectSync<>(this, EFFECT); + private final EffectSync heldEffectDelegate = new EffectSync<>(this, HELD_EFFECT); private final IInterpolator interpolator = new BasicEasingInterpolator(); @@ -86,6 +92,7 @@ class PlayerCapabilities implements IPlayer { player.getDataManager().register(EXERTION, 0F); player.getDataManager().register(ENERGY, 0F); player.getDataManager().register(EFFECT, new NBTTagCompound()); + player.getDataManager().register(HELD_EFFECT, new NBTTagCompound()); } @Override @@ -142,6 +149,26 @@ class PlayerCapabilities implements IPlayer { this.invisible = invisible; } + @Nullable + @Override + public IHeldEffect getHeldEffect(ItemStack stack) { + + if (!getPlayerSpecies().canCast()) { + heldEffectDelegate.set(null); + + return null; + } + + IHeldEffect heldEffect = heldEffectDelegate.get(IHeldEffect.class, true); + + if (heldEffect == null || !heldEffect.getName().equals(SpellRegistry.getKeyFromStack(stack))) { + heldEffect = SpellRegistry.instance().getHeldFrom(stack); + heldEffectDelegate.set(heldEffect); + } + + return heldEffect; + } + @Override public SpellAffinity getAffinity() { return SpellAffinity.NEUTRAL; @@ -224,6 +251,16 @@ class PlayerCapabilities implements IPlayer { } } + ItemStack stack = getOwner().getHeldItem(EnumHand.MAIN_HAND); + + IHeldEffect effect = getHeldEffect(stack); + + if (effect != null) { + SpellAffinity affinity = stack.getItem() instanceof ICastable ? ((ICastable)stack.getItem()).getAffinity(stack) : SpellAffinity.NEUTRAL; + + effect.updateInHand(this, affinity); + } + addExertion(-1); addEnergy(-1);