Held effects are now properly persisted

This commit is contained in:
Sollace 2019-03-03 23:51:36 +02:00
parent e8b2b13a38
commit 74f02b045c
3 changed files with 47 additions and 13 deletions

View file

@ -6,14 +6,11 @@ import javax.annotation.Nullable;
import com.minelittlepony.unicopia.Predicates; import com.minelittlepony.unicopia.Predicates;
import com.minelittlepony.unicopia.entity.EntitySpell; 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.IMagicEffect;
import com.minelittlepony.unicopia.spell.IUseAction; import com.minelittlepony.unicopia.spell.IUseAction;
import com.minelittlepony.unicopia.spell.SpellAffinity; import com.minelittlepony.unicopia.spell.SpellAffinity;
import com.minelittlepony.unicopia.spell.SpellCastResult; import com.minelittlepony.unicopia.spell.SpellCastResult;
import com.minelittlepony.unicopia.spell.IDispenceable; import com.minelittlepony.unicopia.spell.IDispenceable;
import com.minelittlepony.unicopia.spell.IHeldEffect;
import com.minelittlepony.unicopia.spell.SpellRegistry; import com.minelittlepony.unicopia.spell.SpellRegistry;
import com.minelittlepony.util.lang.ClientLocale; import com.minelittlepony.util.lang.ClientLocale;
import com.minelittlepony.util.vector.VecHelper; 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) { 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) { public Item setTranslationKey(String key) {

View file

@ -9,6 +9,7 @@ import com.minelittlepony.unicopia.UClient;
import com.minelittlepony.unicopia.enchanting.IPageOwner; import com.minelittlepony.unicopia.enchanting.IPageOwner;
import com.minelittlepony.unicopia.network.ITransmittable; import com.minelittlepony.unicopia.network.ITransmittable;
import com.minelittlepony.unicopia.spell.ICaster; import com.minelittlepony.unicopia.spell.ICaster;
import com.minelittlepony.unicopia.spell.IHeldEffect;
import com.mojang.authlib.GameProfile; import com.mojang.authlib.GameProfile;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
@ -121,6 +122,15 @@ public interface IPlayer extends ICaster<EntityPlayer>, IRaceContainer<EntityPla
*/ */
void onFall(float distance, float damageMultiplier); void onFall(float distance, float damageMultiplier);
/**
* Gets the held effect for the given item.
* Updates it if the current held effect doesn't match or is empty.
*
* Returns null if the passed item has no held effect.
*/
@Nullable
IHeldEffect getHeldEffect(ItemStack stack);
/** /**
* Event triggered when this player is hit by a projectile. * Event triggered when this player is hit by a projectile.
* @param projectile The projectile doing the hitting. * @param projectile The projectile doing the hitting.

View file

@ -12,9 +12,11 @@ import com.minelittlepony.unicopia.Unicopia;
import com.minelittlepony.unicopia.enchanting.PageState; import com.minelittlepony.unicopia.enchanting.PageState;
import com.minelittlepony.unicopia.init.UEffects; import com.minelittlepony.unicopia.init.UEffects;
import com.minelittlepony.unicopia.init.UItems; import com.minelittlepony.unicopia.init.UItems;
import com.minelittlepony.unicopia.item.ICastable;
import com.minelittlepony.unicopia.network.EffectSync; import com.minelittlepony.unicopia.network.EffectSync;
import com.minelittlepony.unicopia.network.MsgPlayerCapabilities; import com.minelittlepony.unicopia.network.MsgPlayerCapabilities;
import com.minelittlepony.unicopia.spell.IAttachedEffect; import com.minelittlepony.unicopia.spell.IAttachedEffect;
import com.minelittlepony.unicopia.spell.IHeldEffect;
import com.minelittlepony.unicopia.spell.IMagicEffect; import com.minelittlepony.unicopia.spell.IMagicEffect;
import com.minelittlepony.unicopia.spell.SpellAffinity; import com.minelittlepony.unicopia.spell.SpellAffinity;
import com.minelittlepony.unicopia.spell.SpellDisguise; import com.minelittlepony.unicopia.spell.SpellDisguise;
@ -34,6 +36,7 @@ import net.minecraft.network.datasync.EntityDataManager;
import net.minecraft.network.play.server.SPacketSetPassengers; import net.minecraft.network.play.server.SPacketSetPassengers;
import net.minecraft.potion.PotionEffect; import net.minecraft.potion.PotionEffect;
import net.minecraft.stats.StatList; import net.minecraft.stats.StatList;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentTranslation; import net.minecraft.util.text.TextComponentTranslation;
@ -52,6 +55,8 @@ class PlayerCapabilities implements IPlayer {
private static final DataParameter<NBTTagCompound> EFFECT = EntityDataManager private static final DataParameter<NBTTagCompound> EFFECT = EntityDataManager
.createKey(EntityPlayer.class, DataSerializers.COMPOUND_TAG); .createKey(EntityPlayer.class, DataSerializers.COMPOUND_TAG);
private static final DataParameter<NBTTagCompound> HELD_EFFECT = EntityDataManager
.createKey(EntityPlayer.class, DataSerializers.COMPOUND_TAG);
private final Map<ResourceLocation, PageState> pageStates = Maps.newHashMap(); private final Map<ResourceLocation, PageState> pageStates = Maps.newHashMap();
@ -68,6 +73,7 @@ class PlayerCapabilities implements IPlayer {
private final PlayerInventory inventory = new PlayerInventory(this); private final PlayerInventory inventory = new PlayerInventory(this);
private final EffectSync<EntityPlayer> effectDelegate = new EffectSync<>(this, EFFECT); private final EffectSync<EntityPlayer> effectDelegate = new EffectSync<>(this, EFFECT);
private final EffectSync<EntityPlayer> heldEffectDelegate = new EffectSync<>(this, HELD_EFFECT);
private final IInterpolator interpolator = new BasicEasingInterpolator(); private final IInterpolator interpolator = new BasicEasingInterpolator();
@ -86,6 +92,7 @@ class PlayerCapabilities implements IPlayer {
player.getDataManager().register(EXERTION, 0F); player.getDataManager().register(EXERTION, 0F);
player.getDataManager().register(ENERGY, 0F); player.getDataManager().register(ENERGY, 0F);
player.getDataManager().register(EFFECT, new NBTTagCompound()); player.getDataManager().register(EFFECT, new NBTTagCompound());
player.getDataManager().register(HELD_EFFECT, new NBTTagCompound());
} }
@Override @Override
@ -142,6 +149,26 @@ class PlayerCapabilities implements IPlayer {
this.invisible = invisible; 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 @Override
public SpellAffinity getAffinity() { public SpellAffinity getAffinity() {
return SpellAffinity.NEUTRAL; 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); addExertion(-1);
addEnergy(-1); addEnergy(-1);