Fixed crash due to null spell slot

This commit is contained in:
Sollace 2021-03-05 19:22:27 +02:00
parent 51ed3fe2e2
commit 1cb6c6ac00
6 changed files with 55 additions and 19 deletions

View file

@ -10,7 +10,6 @@ import com.minelittlepony.unicopia.EquinePredicates;
import com.minelittlepony.unicopia.Owned; import com.minelittlepony.unicopia.Owned;
import com.minelittlepony.unicopia.entity.Physics; import com.minelittlepony.unicopia.entity.Physics;
import com.minelittlepony.unicopia.entity.PonyContainer; import com.minelittlepony.unicopia.entity.PonyContainer;
import com.minelittlepony.unicopia.network.EffectSync;
import com.minelittlepony.unicopia.particle.ParticleSource; import com.minelittlepony.unicopia.particle.ParticleSource;
import com.minelittlepony.unicopia.util.VecHelper; import com.minelittlepony.unicopia.util.VecHelper;
@ -28,7 +27,7 @@ public interface Caster<E extends LivingEntity> extends Owned<E>, Levelled, Affi
Physics getPhysics(); Physics getPhysics();
EffectSync getSpellSlot(); SpellContainer getSpellSlot();
default void setSpell(@Nullable Spell spell) { default void setSpell(@Nullable Spell spell) {
getSpellSlot().put(spell); getSpellSlot().put(spell);

View file

@ -0,0 +1,44 @@
package com.minelittlepony.unicopia.ability.magic;
import java.util.Optional;
import javax.annotation.Nullable;
import com.minelittlepony.unicopia.ability.magic.spell.SpellPredicate;
public interface SpellContainer {
SpellContainer EMPTY = new SpellContainer() {
@Override
public <T extends Spell> Optional<T> get(SpellPredicate<T> type, boolean update) {
return Optional.empty();
}
@Override
public void put(Spell effect) {
}
};
/**
* Gets the active effect for this caster updating it if needed.
*/
default <T extends Spell> Optional<T> get(boolean update) {
return get(null, update);
}
/**
* Returns true if this caster has an active effect attached to it.
*/
default boolean isPresent() {
return get(true).isPresent();
}
/**
* Gets the active effect for this caster updating it if needed.
*/
<T extends Spell> Optional<T> get(@Nullable SpellPredicate<T> type, boolean update);
/**
* Sets the active effect.
*/
void put(@Nullable Spell effect);
}

View file

@ -28,7 +28,7 @@ public interface Thrown extends Spell, ProjectileDelegate {
* *
* @param source The entity we are currently attached to. * @param source The entity we are currently attached to.
*/ */
boolean onThrownTick(Caster<?> source); boolean onThrownTick(MagicProjectileEntity projectile);
@Override @Override
default void onImpact(MagicProjectileEntity projectile, BlockPos pos, BlockState state) { default void onImpact(MagicProjectileEntity projectile, BlockPos pos, BlockState state) {

View file

@ -6,6 +6,7 @@ import java.util.stream.Stream;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import com.minelittlepony.unicopia.ability.magic.Caster; import com.minelittlepony.unicopia.ability.magic.Caster;
import com.minelittlepony.unicopia.ability.magic.SpellContainer;
import com.minelittlepony.unicopia.ability.magic.spell.SpellPredicate; import com.minelittlepony.unicopia.ability.magic.spell.SpellPredicate;
import com.minelittlepony.unicopia.ability.magic.spell.SpellType; import com.minelittlepony.unicopia.ability.magic.spell.SpellType;
import com.minelittlepony.unicopia.item.UItems; import com.minelittlepony.unicopia.item.UItems;
@ -59,7 +60,7 @@ public abstract class Living<T extends LivingEntity> implements Equine<T>, Caste
} }
@Override @Override
public EffectSync getSpellSlot() { public SpellContainer getSpellSlot() {
return effectDelegate; return effectDelegate;
} }

View file

@ -6,6 +6,7 @@ import javax.annotation.Nullable;
import com.minelittlepony.unicopia.ability.magic.Caster; import com.minelittlepony.unicopia.ability.magic.Caster;
import com.minelittlepony.unicopia.ability.magic.Spell; import com.minelittlepony.unicopia.ability.magic.Spell;
import com.minelittlepony.unicopia.ability.magic.SpellContainer;
import com.minelittlepony.unicopia.ability.magic.spell.SpellPredicate; import com.minelittlepony.unicopia.ability.magic.spell.SpellPredicate;
import com.minelittlepony.unicopia.ability.magic.spell.SpellType; import com.minelittlepony.unicopia.ability.magic.spell.SpellType;
@ -20,7 +21,7 @@ import net.minecraft.util.Identifier;
* *
* @param <T> The owning entity * @param <T> The owning entity
*/ */
public class EffectSync { public class EffectSync implements SpellContainer {
private Optional<Spell> spell = Optional.empty(); private Optional<Spell> spell = Optional.empty();
@ -36,16 +37,7 @@ public class EffectSync {
this.param = param; this.param = param;
} }
/** @Override
* Gets the active effect for this caster updating it if needed.
*/
public <T extends Spell> Optional<T> get(boolean update) {
return get(null, update);
}
/**
* Gets the active effect for this caster updating it if needed.
*/
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T extends Spell> Optional<T> get(@Nullable SpellPredicate<T> type, boolean update) { public <T extends Spell> Optional<T> get(@Nullable SpellPredicate<T> type, boolean update) {
if (update) { if (update) {
@ -59,9 +51,7 @@ public class EffectSync {
return Optional.empty(); return Optional.empty();
} }
/** @Override
* Returns true if this caster has an active effect attached to it.
*/
public boolean isPresent() { public boolean isPresent() {
sync(false); sync(false);
return checkReference(); return checkReference();
@ -90,6 +80,7 @@ public class EffectSync {
} }
} }
@Override
public void put(@Nullable Spell effect) { public void put(@Nullable Spell effect) {
updateReference(effect); updateReference(effect);
owner.getEntity().getDataTracker().set(param, effect == null ? new CompoundTag() : SpellType.toNBT(effect)); owner.getEntity().getDataTracker().set(param, effect == null ? new CompoundTag() : SpellType.toNBT(effect));

View file

@ -7,6 +7,7 @@ import com.minelittlepony.unicopia.ability.magic.Caster;
import com.minelittlepony.unicopia.ability.magic.Levelled; import com.minelittlepony.unicopia.ability.magic.Levelled;
import com.minelittlepony.unicopia.ability.magic.Magical; import com.minelittlepony.unicopia.ability.magic.Magical;
import com.minelittlepony.unicopia.ability.magic.Spell; import com.minelittlepony.unicopia.ability.magic.Spell;
import com.minelittlepony.unicopia.ability.magic.SpellContainer;
import com.minelittlepony.unicopia.ability.magic.spell.SpellPredicate; import com.minelittlepony.unicopia.ability.magic.spell.SpellPredicate;
import com.minelittlepony.unicopia.ability.magic.spell.SpellType; import com.minelittlepony.unicopia.ability.magic.spell.SpellType;
import com.minelittlepony.unicopia.entity.EntityPhysics; import com.minelittlepony.unicopia.entity.EntityPhysics;
@ -116,7 +117,7 @@ public class MagicProjectileEntity extends ThrownItemEntity implements Magical,
} }
@Override @Override
public EffectSync getSpellSlot() { public SpellContainer getSpellSlot() {
return effectDelegate; return effectDelegate;
} }