Fix the displacement spell. Fixes #456

This commit is contained in:
Sollace 2024-09-18 15:13:47 +01:00
parent aa7c4511e9
commit 956dbd26b9
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB
3 changed files with 62 additions and 36 deletions

View file

@ -77,39 +77,38 @@ public class PlacementControlSpell extends AbstractSpell implements OrientedSpel
@Override
public boolean apply(Caster<?> caster) {
if (delegate == null) {
return false;
}
boolean result = super.apply(caster);
if (result) {
if (dimension.isEmpty()) {
setDimension(caster.asWorld().getRegistryKey());
}
if (position.isEmpty()) {
setPosition(caster.asEntity().getPos());
}
if (delegate instanceof PlacementDelegate) {
((PlacementDelegate)delegate).onPlaced(caster, this);
}
CastSpellEntity entity = new CastSpellEntity(caster.asWorld(), caster, this);
Vec3d pos = position.get();
Vec3d rot = orientation.orElse(Vec3d.ZERO);
entity.updatePositionAndAngles(pos.x, pos.y, pos.z, (float)rot.y, (float)rot.x);
entity.getWorld().spawnEntity(entity);
placedEntityId = entity.getUuid();
}
return result;
return delegate != null && super.apply(caster);
}
@Override
public boolean tick(Caster<?> source, Situation situation) {
if (!source.isClient() && getConnection(source) == null) {
setDead();
if (!source.isClient()) {
if (placedEntityId == null) {
if (dimension.isEmpty()) {
setDimension(source.asWorld().getRegistryKey());
}
if (position.isEmpty()) {
setPosition(source.asEntity().getPos());
}
System.out.println("Creating placed spell");
CastSpellEntity entity = new CastSpellEntity(source.asWorld(), source, this);
Vec3d pos = position.get();
Vec3d rot = orientation.orElse(Vec3d.ZERO);
entity.updatePositionAndAngles(pos.x, pos.y, pos.z, (float)rot.y, (float)rot.x);
entity.getWorld().spawnEntity(entity);
placedEntityId = entity.getUuid();
setDirty();
} else {
if (getConnection(source) == null) {
setDead();
}
}
}
return !isDead();
}

View file

@ -18,13 +18,13 @@ import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.hit.EntityHitResult;
import net.minecraft.util.math.Vec3d;
public class DisplacementSpell extends AbstractSpell implements HomingSpell, ProjectileDelegate.EntityHitListener {
public class DisplacementSpell extends AbstractSpell implements HomingSpell, ProjectileDelegate.HitListener {
private static final SpellAttribute<Float> DAMAGE_TO_TARGET = SpellAttribute.create(SpellAttributeType.DAMAGE_TO_TARGET, AttributeFormat.REGULAR, AttributeFormat.PERCENTAGE, Trait.BLOOD, blood -> blood);
static final TooltipFactory TOOLTIP = DAMAGE_TO_TARGET;
private final EntityReference<Entity> target = new EntityReference<>();
private final EntityReference<Entity> target = dataTracker.startTracking(new EntityReference<>());
private int ticks = 10;
@ -34,7 +34,7 @@ public class DisplacementSpell extends AbstractSpell implements HomingSpell, Pro
@Override
public Spell prepareForCast(Caster<?> caster, CastingMethod method) {
return toPlaceable();
return method.isIndirectCause() ? this : toPlaceable();
}
@Override
@ -43,14 +43,23 @@ public class DisplacementSpell extends AbstractSpell implements HomingSpell, Pro
originator.asEntity().setGlowing(true);
if (situation == Situation.PROJECTILE) {
return !isDead();
}
ticks--;
if (originator.isClient()) {
return !isDead() || ticks >= -10;
}
if (ticks == 0) {
target.ifPresent(originator.asWorld(), target -> apply(originator, target));
if (!originator.isClient()) {
target.ifPresent(originator.asWorld(), target -> {
target.setGlowing(true);
if (ticks == 0) {
apply(originator, target);
}
});
}
return ticks >= -10;
@ -58,7 +67,18 @@ public class DisplacementSpell extends AbstractSpell implements HomingSpell, Pro
@Override
public void onImpact(MagicProjectileEntity projectile, EntityHitResult hit) {
Caster.of(projectile.getMaster()).ifPresent(originator -> apply(originator, hit.getEntity()));
HitListener.super.onImpact(projectile, hit);
Caster.of(projectile.getMaster()).ifPresent(originator -> {
apply(originator, hit.getEntity());
});
}
@Override
public void onImpact(MagicProjectileEntity projectile) {
if (projectile.getMaster() instanceof Entity owner) {
owner.setGlowing(false);
}
target.ifPresent(projectile.asWorld(), e -> e.setGlowing(false));
}
private void apply(Caster<?> originator, Entity target) {
@ -107,11 +127,13 @@ public class DisplacementSpell extends AbstractSpell implements HomingSpell, Pro
public void toNBT(NbtCompound compound) {
super.toNBT(compound);
compound.putInt("ticks", ticks);
compound.put("target", target.toNBT());
}
@Override
public void fromNBT(NbtCompound compound) {
super.fromNBT(compound);
ticks = compound.getInt("ticks");
target.fromNBT(compound.getCompound("target"));
}
}

View file

@ -11,6 +11,7 @@ import com.minelittlepony.unicopia.ability.magic.SpellSlots;
import com.minelittlepony.unicopia.ability.magic.spell.PlacementControlSpell;
import com.minelittlepony.unicopia.ability.magic.spell.Situation;
import com.minelittlepony.unicopia.ability.magic.spell.Spell;
import com.minelittlepony.unicopia.ability.magic.spell.PlacementControlSpell.PlacementDelegate;
import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType;
import com.minelittlepony.unicopia.entity.EntityPhysics;
import com.minelittlepony.unicopia.entity.EntityReference;
@ -70,8 +71,12 @@ public class CastSpellEntity extends LightEmittingEntity implements Caster<CastS
this.controllingEntityUuid = caster.asEntity().getUuid();
this.controllingSpellUuid = control.getUuid();
setCaster(caster);
Spell spell = Spell.copy(control.getDelegate());
spells.getSlots().put(spell);
if (control.getDelegate() instanceof PlacementDelegate delegate) {
delegate.onPlaced(caster, control);
}
spells.getSlots().put(Spell.copy(control.getDelegate()));
}
public CastSpellEntity(EntityType<?> type, World world) {