Skip to content

Commit

Permalink
IT LIVES!
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePlasticPotato committed Dec 16, 2024
1 parent 3c7d24b commit 9045c93
Show file tree
Hide file tree
Showing 17 changed files with 235 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.minecraft.CrashReportCategory;
import net.minecraft.ReportedException;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
Expand Down Expand Up @@ -33,6 +34,7 @@
import org.valkyrienskies.core.api.ships.properties.ShipTransform;
import org.valkyrienskies.mod.common.entity.ShipMountedToData;
import org.valkyrienskies.mod.common.VSGameUtilsKt;
import org.valkyrienskies.mod.common.util.EntityDragger;
import org.valkyrienskies.mod.common.util.EntityDraggingInformation;
import org.valkyrienskies.mod.common.util.IEntityDraggingInformationProvider;
import org.valkyrienskies.mod.common.util.VectorConversionsMCKt;
Expand Down Expand Up @@ -106,6 +108,50 @@ private void originalCheckInside(final AABBd aABB) {
private void preGetEyePosition(final float partialTicks, final CallbackInfoReturnable<Vec3> cir) {
final ShipMountedToData shipMountedToData = VSGameUtilsKt.getShipMountedToData(Entity.class.cast(this), partialTicks);
if (shipMountedToData == null) {
if (Entity.class.cast(this) instanceof final ServerPlayer sPlayer && sPlayer instanceof final IEntityDraggingInformationProvider dragProvider) {
if (dragProvider.getDraggingInformation().isEntityBeingDraggedByAShip() && dragProvider.getDraggingInformation().getServerRelativePlayerPosition() != null) {
final Ship shipDraggedBy = VSGameUtilsKt.getAllShips(level).getById(dragProvider.getDraggingInformation().getLastShipStoodOn());
if (shipDraggedBy != null) {
final Vector3dc worldPos = shipDraggedBy.getShipToWorld().transformPosition(dragProvider.getDraggingInformation().getServerRelativePlayerPosition(), new Vector3d());
cir.setReturnValue(new Vec3(worldPos.x(), worldPos.y(), worldPos.z()));
}
}
}
return;
}
final LoadedShip shipMountedTo = shipMountedToData.getShipMountedTo();

final ShipTransform shipTransform;
if (shipMountedTo instanceof ClientShip) {
shipTransform = ((ClientShip) shipMountedTo).getRenderTransform();
} else {
shipTransform = shipMountedTo.getShipTransform();
}
final Vector3dc basePos = shipTransform.getShipToWorldMatrix()
.transformPosition(shipMountedToData.getMountPosInShip(), new Vector3d());
final Vector3dc eyeRelativePos = shipTransform.getShipCoordinatesToWorldCoordinatesRotation().transform(
new Vector3d(0.0, getEyeHeight(), 0.0)
);
final Vec3 newEyePos = VectorConversionsMCKt.toMinecraft(basePos.add(eyeRelativePos, new Vector3d()));
cir.setReturnValue(newEyePos);
}

/**
* @reason Needed for players to pick blocks correctly when mounted to a ship
*/
@Inject(method = "getEyePosition()Lnet/minecraft/world/phys/Vec3;", at = @At("HEAD"), cancellable = true)
private void preGetEyePositionServer(final CallbackInfoReturnable<Vec3> cir) {
final ShipMountedToData shipMountedToData = VSGameUtilsKt.getShipMountedToData(Entity.class.cast(this), null);
if (shipMountedToData == null) {
if (Entity.class.cast(this) instanceof final ServerPlayer sPlayer && sPlayer instanceof final IEntityDraggingInformationProvider dragProvider) {
if (dragProvider.getDraggingInformation().isEntityBeingDraggedByAShip() && dragProvider.getDraggingInformation().getServerRelativePlayerPosition() != null) {
final Ship shipDraggedBy = VSGameUtilsKt.getAllShips(level).getById(dragProvider.getDraggingInformation().getLastShipStoodOn());
if (shipDraggedBy != null) {
final Vector3dc worldPos = shipDraggedBy.getShipToWorld().transformPosition(dragProvider.getDraggingInformation().getServerRelativePlayerPosition(), new Vector3d());
cir.setReturnValue(new Vec3(worldPos.x(), worldPos.y(), worldPos.z()));
}
}
}
return;
}
final LoadedShip shipMountedTo = shipMountedToData.getShipMountedTo();
Expand All @@ -132,6 +178,31 @@ private void preGetEyePosition(final float partialTicks, final CallbackInfoRetur
private void preCalculateViewVector(final float xRot, final float yRot, final CallbackInfoReturnable<Vec3> cir) {
final LoadedShip shipMountedTo = VSGameUtilsKt.getShipMountedTo(Entity.class.cast(this));
if (shipMountedTo == null) {
if (Entity.class.cast(this) instanceof final ServerPlayer sPlayer && sPlayer instanceof final IEntityDraggingInformationProvider dragProvider) {
if (dragProvider.getDraggingInformation().isEntityBeingDraggedByAShip() && dragProvider.getDraggingInformation().getServerRelativePlayerYaw() != null) {
final Ship shipDraggedBy = VSGameUtilsKt.getAllShips(level).getById(dragProvider.getDraggingInformation().getLastShipStoodOn());
if (shipDraggedBy != null) {
final float realYRot = (float) EntityDragger.INSTANCE.serversideEyeRotationOrDefault(sPlayer, yRot);
final float f = xRot * (float) (Math.PI / 180.0);
final float g = -realYRot * (float) (Math.PI / 180.0);
final float h = Mth.cos(g);
final float i = Mth.sin(g);
final float j = Mth.cos(f);
final float k = Mth.sin(f);
final Vector3dc originalViewVector = new Vector3d(i * j, -k, h * j);

final ShipTransform shipTransform;
if (shipDraggedBy instanceof ClientShip) {
shipTransform = ((ClientShip) shipDraggedBy).getRenderTransform();
} else {
shipTransform = shipDraggedBy.getShipTransform();
}
final Vec3 newViewVector = VectorConversionsMCKt.toMinecraft(
shipTransform.getShipCoordinatesToWorldCoordinatesRotation().transform(originalViewVector, new Vector3d()));
cir.setReturnValue(newViewVector);
}
}
}
return;
}
final float f = xRot * (float) (Math.PI / 180.0);
Expand Down Expand Up @@ -183,6 +254,9 @@ private void preCalculateViewVector(final float xRot, final float yRot, final Ca
@Shadow
public abstract EntityType<?> getType();

@Shadow
private float yRot;

@Override
@NotNull
public EntityDraggingInformation getDraggingInformation() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.valkyrienskies.mod.mixin.feature.distance_replace;

import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.phys.Vec3;
Expand All @@ -9,7 +8,6 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.valkyrienskies.mod.common.VSGameUtilsKt;
import org.valkyrienskies.mod.common.util.IEntityDraggingInformationProvider;

/**
* Replaces all distance checks to include ships
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import java.util.Random;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.BlockParticleOption;
import net.minecraft.core.particles.ParticleTypes;
Expand All @@ -30,7 +28,6 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
import org.valkyrienskies.core.api.ships.ClientShip;
import org.valkyrienskies.core.api.ships.Ship;
import org.valkyrienskies.mod.common.VSGameUtilsKt;
import org.valkyrienskies.mod.common.util.EntityDraggingInformation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.valkyrienskies.mod.common.util.EntityDraggingInformation;
import org.valkyrienskies.mod.common.util.EntityLerper;
import org.valkyrienskies.mod.common.util.IEntityDraggingInformationProvider;
import org.valkyrienskies.mod.util.LoggingKt;

@Mixin(LivingEntity.class)
public abstract class MixinLivingEntity extends Entity {
Expand All @@ -24,6 +23,10 @@ public MixinLivingEntity(EntityType<?> entityType, Level level) {
super(entityType, level);
}

/**
* @author Tomato
* @reason Adjusted lerping for entities being dragged by ships.
*/
@Inject(
method = "aiStep",
at = @At(value = "HEAD")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.joml.Vector3d;
import org.joml.Vector3dc;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.valkyrienskies.core.api.ships.ClientShip;
import org.valkyrienskies.mod.common.VSGameUtilsKt;
Expand All @@ -23,10 +24,17 @@

@Mixin(LocalPlayer.class)
public abstract class MixinLocalPlayer extends Entity implements IEntityDraggingInformationProvider {
@Shadow
public abstract float getViewYRot(float f);

public MixinLocalPlayer(EntityType<?> entityType, Level level) {
super(entityType, level);
}

/**
* @author Tomato
* @reason Intercept client -> server player position sending to send our own data.
*/
@WrapOperation(method = "sendPosition", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/multiplayer/ClientPacketListener;send(Lnet/minecraft/network/protocol/Packet;)V"))
private void wrapSendPosition(ClientPacketListener instance, Packet<?> arg, Operation<Void> original) {
if (getDraggingInformation().isEntityBeingDraggedByAShip()) {
Expand All @@ -36,9 +44,9 @@ private void wrapSendPosition(ClientPacketListener instance, Packet<?> arg, Oper
Vector3dc relativePosition = ship.getWorldToShip().transformPosition(
VectorConversionsMCKt.toJOML(getEyePosition()), new Vector3d());

double relativeYaw = EntityLerper.INSTANCE.yawToShip(ship, getYHeadRot());
double relativeYaw = EntityLerper.INSTANCE.yawToShip(ship, getViewYRot(1f));

PacketPlayerShipMotion packet = new PacketPlayerShipMotion(getId(), relativePosition.x(), relativePosition.y(), relativePosition.z(), relativeYaw);
PacketPlayerShipMotion packet = new PacketPlayerShipMotion(ship.getId(), relativePosition.x(), relativePosition.y(), relativePosition.z(), relativeYaw);
ValkyrienSkiesMod.getVsCore().getSimplePacketNetworking().sendToServer(packet);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.valkyrienskies.mod.mixin.feature.entity_collision;

import static org.valkyrienskies.mod.common.util.VectorConversionsMCKt.toJOML;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import java.util.function.Consumer;
Expand All @@ -12,7 +10,6 @@
import net.minecraft.server.level.ServerEntity;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import org.joml.Vector3d;
import org.slf4j.Logger;
import org.spongepowered.asm.mixin.Final;
Expand Down Expand Up @@ -44,6 +41,10 @@ public class MixinServerEntity {
@Final
private static Logger LOGGER;

/**
* @author Tomato
* @reason Intercept entity motion packets to send our own data, then cancel the original packet.
*/
@WrapOperation(
method = "sendChanges",
at = @At(
Expand All @@ -63,14 +64,6 @@ private void wrapBroadcastAccept(Consumer instance, Object t, Operation<Void> or
Vector3d position = ship.getWorldToShip().transformPosition(new Vector3d(entity.getX(), entity.getY(), entity.getZ()));
Vector3d motion = ship.getTransform().transformDirectionNoScalingFromWorldToShip(new Vector3d(entity.getDeltaMovement().x(), entity.getDeltaMovement().y(), entity.getDeltaMovement().z()), new Vector3d());

// Vector3d entityLookYawOnly = new Vector3d(Math.sin(-Math.toRadians(entity.getYRot())), 0.0, Math.cos(-Math.toRadians(entity.getYRot())));
// Vector3d newLookIdeal = ship.getTransform().getShipToWorld().transformDirection(entityLookYawOnly);
//
//// Get the X and Y rotation from [newLookIdeal]
// double newXRot = Math.asin(-newLookIdeal.y());
// double xRotCos = Math.cos(newXRot);
// double newYRot = -Math.atan2(newLookIdeal.x() / xRotCos, newLookIdeal.z() / xRotCos);

double yaw;
if (!(t instanceof ClientboundRotateHeadPacket)) {
yaw = EntityLerper.INSTANCE.yawToShip(ship, entity.getYRot());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.phys.Vec3;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.valkyrienskies.mod.common.VSGameUtilsKt;
import org.valkyrienskies.mod.common.util.EntityDragger;
import org.valkyrienskies.mod.common.util.IEntityDraggingInformationProvider;

@Mixin(AbstractContainerMenu.class)
Expand All @@ -23,17 +25,8 @@ public class MixinScreenHandler {
)
private static double includeShipsInDistanceCheck(
final Player receiver, final double x, final double y, final double z) {
double realX = x;
double realY = y;
double realZ = z;
if (receiver instanceof IEntityDraggingInformationProvider dragProvider && dragProvider.getDraggingInformation().isEntityBeingDraggedByAShip()) {
if (dragProvider.getDraggingInformation().getServerRelativePlayerPosition() != null) {
realX = dragProvider.getDraggingInformation().getServerRelativePlayerPosition().x();
realY = dragProvider.getDraggingInformation().getServerRelativePlayerPosition().y();
realZ = dragProvider.getDraggingInformation().getServerRelativePlayerPosition().z();
}
}
return VSGameUtilsKt.squaredDistanceToInclShips(receiver, realX, realY, realZ);
final Vec3 eye = EntityDragger.INSTANCE.serversideEyePosition(receiver);
return VSGameUtilsKt.squaredDistanceToInclShips(receiver, eye.x, eye.y, eye.z);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
import org.spongepowered.asm.mixin.Pseudo;
import org.spongepowered.asm.mixin.Shadow;
import org.valkyrienskies.mod.common.VSGameUtilsKt;
import org.valkyrienskies.mod.common.util.IEntityDraggingInformationProvider;
import org.valkyrienskies.mod.common.util.VectorConversionsMCKt;
import org.valkyrienskies.mod.common.util.EntityDragger;

@Pseudo
@Mixin(ReachEntityAttributes.class)
Expand All @@ -34,12 +33,7 @@ public static List<Player> getPlayersWithinReach(final Predicate<Player> viewerP
for (final Player player : world.players()) {
if (viewerPredicate.test(player)) {
final var reach = getReachDistance(player, baseReachDistance);
Vec3 eye = player.getEyePosition();
if (player instanceof IEntityDraggingInformationProvider dragProvider && dragProvider.getDraggingInformation().isEntityBeingDraggedByAShip()) {
if (dragProvider.getDraggingInformation().getServerRelativePlayerPosition() != null) {
eye = VectorConversionsMCKt.toMinecraft(dragProvider.getDraggingInformation().getServerRelativePlayerPosition());
}
}
final Vec3 eye = EntityDragger.INSTANCE.serversideEyePosition(player);
if (VSGameUtilsKt.squaredDistanceBetweenInclShips(world, x + 0.5, y + 0.5, z + 0.5, eye.x, eye.y, eye.z) <= (reach * reach)) {
playersWithinReach.add(player);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import net.minecraft.world.entity.player.Player
import net.minecraft.world.level.Level
import net.minecraft.world.phys.Vec3
import org.valkyrienskies.core.api.ships.LoadedShip
import org.valkyrienskies.mod.common.util.EntityDragger.serversideEyeRotationOrDefault
import org.valkyrienskies.mod.common.util.toJOML
import org.valkyrienskies.mod.common.util.toMinecraft
import org.valkyrienskies.mod.mixin.accessors.entity.EntityAccessor
Expand Down Expand Up @@ -38,8 +39,9 @@ object PlayerUtil {
val yaw = -atan2(direction.x, direction.z)
val pitch = -atan2(direction.y, sqrt((direction.x * direction.x) + (direction.z * direction.z)))

player.yRot = (yaw * (180 / Math.PI)).toFloat()
player.yRot = player.serversideEyeRotationOrDefault(yaw * (180 / Math.PI)).toFloat()
player.yHeadRot = player.yRot

player.xRot = (pitch * (180 / Math.PI)).toFloat()
(player as EntityAccessor).setPosNoUpdates(position.toMinecraft())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import org.valkyrienskies.core.util.expand
import org.valkyrienskies.mod.common.entity.ShipMountedToData
import org.valkyrienskies.mod.common.entity.ShipMountedToDataProvider
import org.valkyrienskies.mod.common.util.DimensionIdProvider
import org.valkyrienskies.mod.common.util.EntityDragger.serversideEyePosition
import org.valkyrienskies.mod.common.util.IEntityDraggingInformationProvider
import org.valkyrienskies.mod.common.util.MinecraftPlayer
import org.valkyrienskies.mod.common.util.set
Expand Down Expand Up @@ -120,17 +121,8 @@ val Player.playerWrapper get() = (this as PlayerDuck).vs_getPlayer()
* Like [Entity.squaredDistanceTo] except the destination is transformed into world coordinates if it is a ship
*/
fun Entity.squaredDistanceToInclShips(x: Double, y: Double, z: Double): Double {
var xEntity = this.x
var yEntity = this.y
var zEntity = this.z
if (this is ServerPlayer && this is IEntityDraggingInformationProvider && this.draggingInformation.isEntityBeingDraggedByAShip()) {
if (this.draggingInformation.serverRelativePlayerPosition != null) {
xEntity = this.draggingInformation.serverRelativePlayerPosition!!.x()
yEntity = this.draggingInformation.serverRelativePlayerPosition!!.y()
zEntity = this.draggingInformation.serverRelativePlayerPosition!!.z()
}
}
return level.squaredDistanceBetweenInclShips(x, y, z, xEntity, yEntity, zEntity)
val eyePos = this.serversideEyePosition()
return level.squaredDistanceBetweenInclShips(x, y, z, eyePos.x, eyePos.y - 1.0, eyePos.z)
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ package org.valkyrienskies.mod.common.networking

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
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ package org.valkyrienskies.mod.common.networking

import org.valkyrienskies.core.impl.networking.simple.SimplePacket

/**
* This packet is used to update the player's relative position and yaw rotation while being dragged by a ship, alongside
* [net.minecraft.network.protocol.game.ServerboundMovePlayerPacket].
*/
data class PacketPlayerShipMotion(val shipID: Long, val x: Double, val y: Double, val z: Double, val yRot: Double): SimplePacket
Loading

0 comments on commit 9045c93

Please sign in to comment.