diff --git a/src/main/java/at/petrak/hexcasting/HexMod.kt b/src/main/java/at/petrak/hexcasting/HexMod.kt index 7b9e463d..14161533 100644 --- a/src/main/java/at/petrak/hexcasting/HexMod.kt +++ b/src/main/java/at/petrak/hexcasting/HexMod.kt @@ -17,6 +17,7 @@ import at.petrak.hexcasting.common.items.HexItems import at.petrak.hexcasting.common.lib.HexCapabilityHandler import at.petrak.hexcasting.common.lib.HexSounds import at.petrak.hexcasting.common.misc.Brainsweeping +import at.petrak.hexcasting.common.misc.PlayerPositionRecorder import at.petrak.hexcasting.common.network.HexMessages import at.petrak.hexcasting.common.particles.HexParticles import at.petrak.hexcasting.common.recipe.HexComposting @@ -85,6 +86,7 @@ object HexMod { modBus.register(HexComposting::class.java) + evBus.register(PlayerPositionRecorder::class.java) evBus.register(HexCommands::class.java) evBus.register(TickScheduler) evBus.register(HexCapabilityHandler::class.java) diff --git a/src/main/java/at/petrak/hexcasting/common/casting/operators/OpEntityVelocity.kt b/src/main/java/at/petrak/hexcasting/common/casting/operators/OpEntityVelocity.kt index 845f759f..5231aa7d 100644 --- a/src/main/java/at/petrak/hexcasting/common/casting/operators/OpEntityVelocity.kt +++ b/src/main/java/at/petrak/hexcasting/common/casting/operators/OpEntityVelocity.kt @@ -5,9 +5,9 @@ import at.petrak.hexcasting.api.spell.Operator.Companion.getChecked import at.petrak.hexcasting.api.spell.Operator.Companion.spellListOf import at.petrak.hexcasting.api.spell.SpellDatum import at.petrak.hexcasting.api.spell.casting.CastingContext +import at.petrak.hexcasting.common.misc.PlayerPositionRecorder +import net.minecraft.server.level.ServerPlayer import net.minecraft.world.entity.Entity -import net.minecraft.world.entity.player.Player -import net.minecraft.world.phys.Vec3 object OpEntityVelocity : ConstManaOperator { override val argc = 1 @@ -17,8 +17,8 @@ object OpEntityVelocity : ConstManaOperator { ctx.assertEntityInRange(e) // Player velocity is jank. Really jank. This is the best we can do. - if (e is Player) { - val prevPosition = Vec3(e.xOld, e.yOld, e.zOld) + if (e is ServerPlayer) { + val prevPosition = PlayerPositionRecorder.getLastPosition(e) return spellListOf(e.position().subtract(prevPosition)) } diff --git a/src/main/java/at/petrak/hexcasting/common/misc/PlayerPositionRecorder.java b/src/main/java/at/petrak/hexcasting/common/misc/PlayerPositionRecorder.java new file mode 100644 index 00000000..597b8d0c --- /dev/null +++ b/src/main/java/at/petrak/hexcasting/common/misc/PlayerPositionRecorder.java @@ -0,0 +1,26 @@ +package at.petrak.hexcasting.common.misc; + +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.phys.Vec3; +import net.minecraftforge.event.entity.living.LivingEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; + +import java.util.Map; +import java.util.WeakHashMap; + +public final class PlayerPositionRecorder { + private static final Map LAST_POSITION_MAP = new WeakHashMap<>(); + + @SubscribeEvent + public static void onEntityUpdate(LivingEvent.LivingUpdateEvent evt) { + if (evt.getEntityLiving() instanceof ServerPlayer player) { + LAST_POSITION_MAP.put(player, player.position()); + } + } + + public static Vec3 getLastPosition(ServerPlayer player) { + Vec3 vec = LAST_POSITION_MAP.get(player); + return vec == null ? player.position() : vec; + } +}