Fixed placed spells not using the location specified when casting them with /cast

This commit is contained in:
Sollace 2023-04-30 21:42:07 +01:00
parent c8524a3024
commit b372620a04
2 changed files with 24 additions and 13 deletions

View file

@ -14,6 +14,7 @@ import com.minelittlepony.unicopia.particle.ParticleHandle;
import com.minelittlepony.unicopia.particle.UParticles;
import com.minelittlepony.unicopia.particle.ParticleHandle.Attachment;
import com.minelittlepony.unicopia.server.world.Ether;
import com.minelittlepony.unicopia.util.NbtSerialisable;
import net.minecraft.nbt.*;
import net.minecraft.registry.*;
@ -52,6 +53,8 @@ public class PlaceableSpell extends AbstractDelegatingSpell implements OrientedS
public float pitch;
public float yaw;
private Optional<Vec3d> position = Optional.empty();
public PlaceableSpell(CustomisedSpellType<?> type) {
super(type);
}
@ -120,7 +123,7 @@ public class PlaceableSpell extends AbstractDelegatingSpell implements OrientedS
private void spawnPlacedEntity(Caster<?> source) {
CastSpellEntity entity = UEntities.CAST_SPELL.create(source.asWorld());
Vec3d pos = castEntity.getPosition().orElse(source.getOriginVector());
Vec3d pos = castEntity.getPosition().orElse(position.orElse(source.getOriginVector()));
entity.updatePositionAndAngles(pos.x, pos.y, pos.z, source.asEntity().getYaw(), source.asEntity().getPitch());
PlaceableSpell copy = spell.toPlaceable();
if (spell instanceof PlacementDelegate delegate) {
@ -140,7 +143,19 @@ public class PlaceableSpell extends AbstractDelegatingSpell implements OrientedS
this.pitch = -90 - pitch;
this.yaw = -yaw;
getDelegates(spell -> spell instanceof OrientedSpell o ? o : null)
.forEach(oriented -> oriented.setOrientation(pitch, yaw));
.forEach(spell -> spell.setOrientation(pitch, yaw));
setDirty();
}
public void setPosition(Caster<?> source, Vec3d position) {
this.position = Optional.of(position);
getWorld(source).ifPresent(world -> {
castEntity.ifPresent(world, entity -> {
entity.updatePositionAndAngles(position.x, position.y, position.z, entity.getYaw(), entity.getPitch());
});
});
getDelegates(spell -> spell instanceof PlaceableSpell o ? o : null)
.forEach(spell -> spell.setPosition(source ,position));
setDirty();
}
@ -188,6 +203,9 @@ public class PlaceableSpell extends AbstractDelegatingSpell implements OrientedS
super.toNBT(compound);
compound.putFloat("pitch", pitch);
compound.putFloat("yaw", yaw);
position.ifPresent(pos -> {
compound.put("position", NbtSerialisable.writeVector(pos));
});
if (dimension != null) {
compound.putString("dimension", dimension.getValue().toString());
}
@ -200,6 +218,7 @@ public class PlaceableSpell extends AbstractDelegatingSpell implements OrientedS
super.fromNBT(compound);
pitch = compound.getFloat("pitch");
yaw = compound.getFloat("yaw");
position = compound.contains("position") ? Optional.of(NbtSerialisable.readVector(compound.getList("position", NbtElement.FLOAT_TYPE))) : Optional.empty();
if (compound.contains("dimension", NbtElement.STRING_TYPE)) {
Identifier id = Identifier.tryParse(compound.getString("dimension"));
if (id != null) {

View file

@ -4,11 +4,9 @@ import java.util.Optional;
import com.minelittlepony.unicopia.ability.magic.Caster;
import com.minelittlepony.unicopia.ability.magic.spell.PlaceableSpell;
import com.minelittlepony.unicopia.ability.magic.spell.Situation;
import com.minelittlepony.unicopia.ability.magic.spell.effect.CustomisedSpellType;
import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType;
import com.minelittlepony.unicopia.ability.magic.spell.trait.SpellTraits;
import com.minelittlepony.unicopia.entity.CastSpellEntity;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.FloatArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
@ -62,12 +60,12 @@ public class CastCommand {
CommandManager.literal("place").executes(c -> placed(c, traitsFunc, Optional.empty(), c.getSource().getRotation())).then(
CommandManager.argument("loc", BlockPosArgumentType.blockPos()).executes(c -> placed(c,
traitsFunc,
Optional.of(BlockPosArgumentType.getBlockPos(c, "location").toCenterPos()),
Optional.of(BlockPosArgumentType.getBlockPos(c, "loc").toCenterPos()),
c.getSource().getRotation()
)).then(
CommandManager.argument("rot", RotationArgumentType.rotation()).executes(c -> placed(c,
traitsFunc,
Optional.of(BlockPosArgumentType.getBlockPos(c, "location").toCenterPos()),
Optional.of(BlockPosArgumentType.getBlockPos(c, "loc").toCenterPos()),
RotationArgumentType.getRotation(c, "rot").toAbsoluteRotation(c.getSource())
))
)
@ -105,15 +103,9 @@ public class CastCommand {
Caster<?> caster = Caster.of(player).orElseThrow();
spell.setOrientation(rotation.x, rotation.y);
position.ifPresent(pos -> spell.setPosition(caster, pos));
spell.apply(caster);
position.ifPresent(pos -> {
spell.tick(caster, Situation.BODY);
CastSpellEntity entity = spell.getSpellEntity(caster).orElseThrow();
entity.setPosition(pos);
});
return 0;
}