Placed spells are no longer lost when the player dies

This commit is contained in:
Sollace 2022-01-02 00:43:21 +02:00
parent e6b0ad9fa4
commit c27aad9154
7 changed files with 20 additions and 6 deletions

View file

@ -73,6 +73,8 @@ public interface Caster<E extends LivingEntity> extends Owned<E>, Levelled, Affi
/** /**
* Removes the desired amount of mana or health from this caster in exchange for a spell's benefits. * Removes the desired amount of mana or health from this caster in exchange for a spell's benefits.
* <p>
* @return False if the transaction has depleted the caster's reserves.
*/ */
boolean subtractEnergyCost(double amount); boolean subtractEnergyCost(double amount);

View file

@ -3,6 +3,7 @@ package com.minelittlepony.unicopia.ability.magic;
import java.util.function.Predicate; import java.util.function.Predicate;
import com.minelittlepony.unicopia.ability.magic.spell.AbstractDisguiseSpell; import com.minelittlepony.unicopia.ability.magic.spell.AbstractDisguiseSpell;
import com.minelittlepony.unicopia.ability.magic.spell.PlaceableSpell;
import com.minelittlepony.unicopia.ability.magic.spell.ProjectileSpell; import com.minelittlepony.unicopia.ability.magic.spell.ProjectileSpell;
import com.minelittlepony.unicopia.ability.magic.spell.Spell; import com.minelittlepony.unicopia.ability.magic.spell.Spell;
import com.minelittlepony.unicopia.ability.magic.spell.effect.ShieldSpell; import com.minelittlepony.unicopia.ability.magic.spell.effect.ShieldSpell;
@ -11,6 +12,7 @@ import net.minecraft.entity.Entity;
public interface SpellPredicate<T extends Spell> extends Predicate<Spell> { public interface SpellPredicate<T extends Spell> extends Predicate<Spell> {
SpellPredicate<IllusionarySpell> CAN_SUPPRESS = s -> s instanceof IllusionarySpell; SpellPredicate<IllusionarySpell> CAN_SUPPRESS = s -> s instanceof IllusionarySpell;
SpellPredicate<PlaceableSpell> IS_PLACED = s -> s instanceof PlaceableSpell;
SpellPredicate<ProjectileSpell> HAS_PROJECTILE_EVENTS = s -> s instanceof ProjectileSpell; SpellPredicate<ProjectileSpell> HAS_PROJECTILE_EVENTS = s -> s instanceof ProjectileSpell;
SpellPredicate<AbstractDisguiseSpell> IS_DISGUISE = s -> s instanceof AbstractDisguiseSpell; SpellPredicate<AbstractDisguiseSpell> IS_DISGUISE = s -> s instanceof AbstractDisguiseSpell;
SpellPredicate<ShieldSpell> IS_SHIELD_LIKE = spell -> spell instanceof ShieldSpell; SpellPredicate<ShieldSpell> IS_SHIELD_LIKE = spell -> spell instanceof ShieldSpell;

View file

@ -85,7 +85,7 @@ public class PlaceableSpell extends AbstractDelegatingSpell {
CastSpellEntity entity = UEntities.CAST_SPELL.create(source.getWorld()); CastSpellEntity entity = UEntities.CAST_SPELL.create(source.getWorld());
Vec3d pos = castEntity.getPosition().orElse(source.getOriginVector()); Vec3d pos = castEntity.getPosition().orElse(source.getOriginVector());
entity.updatePositionAndAngles(pos.x, pos.y, pos.z, 0, 0); entity.updatePositionAndAngles(pos.x, pos.y, pos.z, 0, 0);
entity.getSpellSlot().put(this); entity.getSpellSlot().put(spell.toPlaceable());
entity.setMaster(source); entity.setMaster(source);
entity.world.spawnEntity(entity); entity.world.spawnEntity(entity);
@ -107,8 +107,6 @@ public class PlaceableSpell extends AbstractDelegatingSpell {
return super.tick(source, Situation.GROUND); return super.tick(source, Situation.GROUND);
} }
this.onDestroyed(source);
return !isDead(); return !isDead();
} }

View file

@ -104,7 +104,13 @@ public class CastSpellEntity extends LightEmittingEntity implements Caster<Livin
@Override @Override
public boolean subtractEnergyCost(double amount) { public boolean subtractEnergyCost(double amount) {
return Caster.of(getMaster()).filter(c -> c.subtractEnergyCost(amount)).isPresent(); if (getMaster() == null) {
return true;
}
return Caster.of(getMaster())
.filter(c -> c.subtractEnergyCost(amount))
.isPresent();
} }
@Override @Override

View file

@ -9,6 +9,7 @@ import org.jetbrains.annotations.Nullable;
import com.minelittlepony.unicopia.util.NbtSerialisable; import com.minelittlepony.unicopia.util.NbtSerialisable;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement; import net.minecraft.nbt.NbtElement;
import net.minecraft.server.world.ServerWorld; import net.minecraft.server.world.ServerWorld;
@ -89,7 +90,7 @@ public class EntityReference<T extends Entity> implements NbtSerialisable {
private boolean checkReference(Entity e) { private boolean checkReference(Entity e) {
pos = Optional.of(e.getPos()); pos = Optional.of(e.getPos());
return !e.isRemoved(); return e instanceof PlayerEntity || !e.isRemoved();
} }
@Override @Override

View file

@ -508,7 +508,9 @@ public class Pony extends Living<PlayerEntity> implements Transmittable, Copieab
public void copyFrom(Pony oldPlayer) { public void copyFrom(Pony oldPlayer) {
speciesPersisted = oldPlayer.speciesPersisted; speciesPersisted = oldPlayer.speciesPersisted;
if (!oldPlayer.getEntity().isRemoved()) { if (!oldPlayer.getEntity().isRemoved()) {
getSpellSlot().put(oldPlayer.getSpellSlot().get(true).orElse(null)); oldPlayer.getSpellSlot().stream(true).forEach(getSpellSlot()::put);
} else {
oldPlayer.getSpellSlot().stream(true).filter(SpellPredicate.IS_PLACED).forEach(getSpellSlot()::put);
} }
oldPlayer.getSpellSlot().put(null); oldPlayer.getSpellSlot().put(null);
setSpecies(oldPlayer.getSpecies()); setSpecies(oldPlayer.getSpecies());

View file

@ -48,6 +48,9 @@ public class NetworkedReferenceSet<T> {
public boolean clear() { public boolean clear() {
dirty |= !ids.isEmpty() || !values.isEmpty(); dirty |= !ids.isEmpty() || !values.isEmpty();
ids.clear(); ids.clear();
for (NetworkedReference<T> reference : values.values()) {
reference.updateReference(null);
}
values.clear(); values.clear();
return dirty; return dirty;
} }