From b372620a04a346b47b2c129ffab328f9a44dbe99 Mon Sep 17 00:00:00 2001 From: Sollace Date: Sun, 30 Apr 2023 21:42:07 +0100 Subject: [PATCH] Fixed placed spells not using the location specified when casting them with /cast --- .../ability/magic/spell/PlaceableSpell.java | 23 +++++++++++++++++-- .../unicopia/command/CastCommand.java | 14 +++-------- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/PlaceableSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/PlaceableSpell.java index 8c68fff7..ea6f89da 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/PlaceableSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/PlaceableSpell.java @@ -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 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) { diff --git a/src/main/java/com/minelittlepony/unicopia/command/CastCommand.java b/src/main/java/com/minelittlepony/unicopia/command/CastCommand.java index 7ab5fca8..9614bffb 100644 --- a/src/main/java/com/minelittlepony/unicopia/command/CastCommand.java +++ b/src/main/java/com/minelittlepony/unicopia/command/CastCommand.java @@ -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; }