Skip to content

Commit

Permalink
player positions fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePlasticPotato committed Dec 18, 2024
1 parent d36dac7 commit f4662d2
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.valkyrienskies.mod.mixin.feature.entity_collision;

import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
Expand All @@ -28,13 +28,13 @@ public MixinLivingEntity(EntityType<?> entityType, Level level) {
* @reason Adjusted lerping for entities being dragged by ships.
*/
@Inject(
method = "aiStep",
at = @At(value = "HEAD")
method = "tick",
at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/LivingEntity;aiStep()V", shift = At.Shift.BEFORE)
)
private void preAiStep(CallbackInfo ci) {
// fake lerp movement gaming
if (this.level != null && this.level.isClientSide() && !firstTick) {
if (this.isControlledByLocalInstance() || (Minecraft.getInstance().player != null && Minecraft.getInstance().player.is(this))) return;
if (this.isControlledByLocalInstance() || ((Entity) this instanceof LocalPlayer)) return;
EntityDraggingInformation dragInfo = ((IEntityDraggingInformationProvider) this).getDraggingInformation();
if (dragInfo != null && dragInfo.getLastShipStoodOn() != null) {
final ClientShip ship = VSGameUtilsKt.getShipObjectWorld((ClientLevel) level).getAllShips().getById(dragInfo.getLastShipStoodOn());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private void wrapSendPosition(ClientPacketListener instance, Packet<?> arg, Oper
}
}
if (realArg instanceof ServerboundMovePlayerPacket movePacket) {
final boolean isOnGround = movePacket.isOnGround() || getDraggingInformation().getTicksSinceStoodOnShip() < 4;
final boolean isOnGround = movePacket.isOnGround() || getDraggingInformation().isEntityBeingDraggedByAShip();
if (movePacket.hasPosition() && movePacket.hasRotation()) {
//posrot
realArg = new ServerboundMovePlayerPacket.PosRot(movePacket.getX(0.0), movePacket.getY(0.0), movePacket.getZ(0.0), movePacket.getYRot(0.0f), movePacket.getXRot(0.0f), isOnGround);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import org.joml.Vector3d;
import org.slf4j.Logger;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand All @@ -37,10 +36,6 @@ public class MixinServerEntity {
@Final
private ServerLevel level;

@Shadow
@Final
private static Logger LOGGER;

/**
* @author Tomato
* @reason Intercept entity motion packets to send our own data, then cancel the original packet.
Expand All @@ -59,9 +54,11 @@ private void wrapBroadcastAccept(Consumer instance, Object t, Operation<Void> or
if (dragInfo != null && dragInfo.isEntityBeingDraggedByAShip() && dragInfo.getLastShipStoodOn() != null) {
ServerShip ship = VSGameUtilsKt.getShipObjectWorld(level).getAllShips().getById(dragInfo.getLastShipStoodOn());
if (ship != null) {
// if (!(entity instanceof Player)) LOGGER.info("Entity is rotated {}", entity.getYRot());

Vector3d position = ship.getWorldToShip().transformPosition(new Vector3d(entity.getX(), entity.getY(), entity.getZ()));
if (dragInfo.getServerRelativePlayerPosition() != null) {
position = new Vector3d(dragInfo.getServerRelativePlayerPosition());
}
Vector3d motion = ship.getTransform().transformDirectionNoScalingFromWorldToShip(new Vector3d(entity.getDeltaMovement().x(), entity.getDeltaMovement().y(), entity.getDeltaMovement().z()), new Vector3d());

double yaw;
Expand All @@ -78,7 +75,7 @@ private void wrapBroadcastAccept(Consumer instance, Object t, Operation<Void> or
motion.x, motion.y, motion.z,
yaw, pitch);
} else {
vsPacket = new PacketMobShipRotation(entity.getId(), ship.getId(), yaw);
vsPacket = new PacketMobShipRotation(entity.getId(), ship.getId(), yaw, pitch);
}

ValkyrienSkiesMod.getVsCore().getSimplePacketNetworking().sendToAllClients(vsPacket);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ import org.valkyrienskies.core.impl.networking.simple.SimplePacket
/**
* This packet is used in place of [net.minecraft.network.protocol.game.ClientboundRotateHeadPacket] to update the head rotation of a mob being dragged by a ship.
*/
data class PacketMobShipRotation(val entityID: Int, val shipID: Long, val yaw: Double): SimplePacket
data class PacketMobShipRotation(val entityID: Int, val shipID: Long, val yaw: Double, val pitch: Double): SimplePacket
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.valkyrienskies.mod.common.networking

import net.minecraft.client.Minecraft
import net.minecraft.client.player.LocalPlayer
import net.minecraft.core.Registry
import net.minecraft.resources.ResourceLocation
import net.minecraft.server.level.ServerLevel
Expand Down Expand Up @@ -72,7 +73,7 @@ object VSGamePackets {
val level = mc.level ?: return@registerClientHandler
val entity = level.getEntity(setMotion.entityID) ?: return@registerClientHandler

if (entity.isControlledByLocalInstance || (mc.player != null && mc.player!!.`is`(entity))) return@registerClientHandler
if (entity.isControlledByLocalInstance || mc.player?.id == entity.id) return@registerClientHandler

val ship = level.shipObjectWorld.allShips.getById(setMotion.shipID)
?: return@registerClientHandler
Expand Down Expand Up @@ -102,7 +103,6 @@ object VSGamePackets {
entity.setPacketCoordinates(worldPosition.x, worldPosition.y, worldPosition.z)
val worldVelocity = ship.renderTransform.shipToWorld.transformDirection(Vector3d(setMotion.xVel, setMotion.yVel, setMotion.zVel))
entity.setDeltaMovement(worldVelocity.x, worldVelocity.y, worldVelocity.z)
entity.xRot = setMotion.xRot.toFloat()
entity.draggingInformation.lerpSteps = 3

// entity.setPos(previousWorldPosition.x, previousWorldPosition.y, previousWorldPosition.z)
Expand All @@ -115,7 +115,7 @@ object VSGamePackets {
val level = mc.level ?: return@registerClientHandler
val entity = level.getEntity(setRotation.entityID) ?: return@registerClientHandler

if (entity.isControlledByLocalInstance || (entity is Player && entity.isLocalPlayer)) return@registerClientHandler
if (entity.isControlledByLocalInstance || entity is LocalPlayer) return@registerClientHandler

val ship = level.shipObjectWorld.allShips.getById(setRotation.shipID)
?: return@registerClientHandler
Expand All @@ -127,6 +127,8 @@ object VSGamePackets {
}
entity.draggingInformation.relativeHeadYawOnShip = EntityLerper.yawToShip(ship, entity.yHeadRot.toDouble())
entity.draggingInformation.lerpHeadYawOnShip = setRotation.yaw
entity.draggingInformation.relativePitchOnShip = entity.xRot.toDouble()
entity.draggingInformation.lerpPitchOnShip = setRotation.pitch
entity.draggingInformation.headLerpSteps = 3
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.valkyrienskies.mod.common.util

import net.minecraft.client.Minecraft
import net.minecraft.client.player.LocalPlayer
import net.minecraft.server.level.ServerPlayer
import net.minecraft.util.Mth
import net.minecraft.world.entity.Entity
Expand Down Expand Up @@ -123,7 +124,7 @@ object EntityDragger {
entity.yHeadRot = Mth.wrapDegrees(entity.yHeadRot + addedYRot.toFloat())
}
} else {
if (!entity.isControlledByLocalInstance && !(Minecraft.getInstance().player != null && Minecraft.getInstance().player!!.`is`(entity))) {
if (!entity.isControlledByLocalInstance && entity !is LocalPlayer) {
entity.yRot = Mth.wrapDegrees(entity.yRot + addedYRot.toFloat())
entity.yHeadRot = Mth.wrapDegrees(entity.yHeadRot + addedYRot.toFloat())
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ class EntityDraggingInformation {
var relativeVelocityOnShip: Vector3dc? = null
var lerpYawOnShip: Double? = null
var lerpHeadYawOnShip: Double? = null
var lerpPitchOnShip: Double? = null

var relativePositionOnShip: Vector3dc? = null
var previousRelativeVelocityOnShip: Vector3dc? = null
var relativeYawOnShip: Double? = null
var relativeHeadYawOnShip: Double? = null
var relativePitchOnShip: Double? = null

var lerpSteps: Int = 0
var headLerpSteps: Int = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ object EntityLerper {
val currentHeadYawWorld = yawToWorld(ship, currentHeadYaw)
val lerpHeadYawWorld = yawToWorld(ship, lerpHeadYaw)

val newHeadYaw = currentHeadYawWorld + Mth.wrapDegrees(lerpHeadYawWorld - currentHeadYawWorld) / dragInfo.headLerpSteps

val newHeadYaw = currentHeadYawWorld + Mth.wrapDegrees(lerpHeadYawWorld - currentHeadYawWorld) / dragInfo.headLerpSteps.toFloat()
entity.xRot += (dragInfo.lerpPitchOnShip!!.toFloat() - entity.xRot) / dragInfo.headLerpSteps.toFloat()
entity.yHeadRot = newHeadYaw.toFloat()
dragInfo.relativeHeadYawOnShip = yawToShip(ship, newHeadYaw.toDouble())

Expand Down

0 comments on commit f4662d2

Please sign in to comment.