| 1 | package net.minecraft.world.entity;
|
| 2 |
|
| 3 | import java.util.Set;
|
| 4 | import net.minecraft.core.PositionAndRotation;
|
| 5 | import net.minecraft.network.FriendlyByteBuf;
|
| 6 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 7 | import net.minecraft.network.codec.StreamCodec;
|
| 8 | import net.minecraft.util.Mth;
|
| 9 | import net.minecraft.world.level.portal.TeleportTransition;
|
| 10 | import net.minecraft.world.phys.Vec3;
|
| 11 |
|
| 12 | public record PositionMoveRotation(Vec3 position, Vec3 deltaMovement, float yRot, float xRot) {
|
| 13 | public static final StreamCodec<FriendlyByteBuf, PositionMoveRotation> STREAM_CODEC = StreamCodec.composite(
|
| 14 | Vec3.STREAM_CODEC,
|
| 15 | PositionMoveRotation::position,
|
| 16 | Vec3.STREAM_CODEC,
|
| 17 | PositionMoveRotation::deltaMovement,
|
| 18 | ByteBufCodecs.FLOAT,
|
| 19 | PositionMoveRotation::yRot,
|
| 20 | ByteBufCodecs.FLOAT,
|
| 21 | PositionMoveRotation::xRot,
|
| 22 | PositionMoveRotation::new
|
| 23 | );
|
| 24 |
|
| 25 | public static PositionMoveRotation of(final Entity entity) {
|
| 26 | PositionAndRotation current = entity.getClientPositionAndRotation();
|
| 27 | return new PositionMoveRotation(current.position(), entity.getKnownMovement(), current.yRot(), current.xRot());
|
| 28 | }
|
| 29 |
|
| 30 | public PositionMoveRotation withRotation(final float yRot, final float xRot) {
|
| 31 | return new PositionMoveRotation(this.position(), this.deltaMovement(), yRot, xRot);
|
| 32 | }
|
| 33 |
|
| 34 | public static PositionMoveRotation of(final TeleportTransition transition) {
|
| 35 | return new PositionMoveRotation(transition.position(), transition.deltaMovement(), transition.yRot(), transition.xRot());
|
| 36 | }
|
| 37 |
|
| 38 | public static PositionMoveRotation calculateAbsolute(final PositionMoveRotation source, final PositionMoveRotation change, final Set<Relative> relatives) {
|
| 39 | double offsetX = relatives.contains(Relative.X) ? source.position.x : 0.0;
|
| 40 | double offsetY = relatives.contains(Relative.Y) ? source.position.y : 0.0;
|
| 41 | double offsetZ = relatives.contains(Relative.Z) ? source.position.z : 0.0;
|
| 42 | float offsetYRot = relatives.contains(Relative.Y_ROT) ? source.yRot : 0.0F;
|
| 43 | float offsetXRot = relatives.contains(Relative.X_ROT) ? source.xRot : 0.0F;
|
| 44 | Vec3 absolutePosition = new Vec3(offsetX + change.position.x, offsetY + change.position.y, offsetZ + change.position.z);
|
| 45 | float absoluteYRot = offsetYRot + change.yRot;
|
| 46 | float absoluteXRot = Mth.clamp(offsetXRot + change.xRot, -90.0F, 90.0F);
|
| 47 | Vec3 rotatedCurrentMovement = source.deltaMovement;
|
| 48 | if (relatives.contains(Relative.ROTATE_DELTA)) {
|
| 49 | float diffYRot = source.yRot - absoluteYRot;
|
| 50 | float diffXRot = source.xRot - absoluteXRot;
|
| 51 | rotatedCurrentMovement = rotatedCurrentMovement.xRot((float)Math.toRadians(diffXRot));
|
| 52 | rotatedCurrentMovement = rotatedCurrentMovement.yRot((float)Math.toRadians(diffYRot));
|
| 53 | }
|
| 54 |
|
| 55 | Vec3 absoluteDeltaMovement = new Vec3(
|
| 56 | calculateDelta(rotatedCurrentMovement.x, change.deltaMovement.x, relatives, Relative.DELTA_X),
|
| 57 | calculateDelta(rotatedCurrentMovement.y, change.deltaMovement.y, relatives, Relative.DELTA_Y),
|
| 58 | calculateDelta(rotatedCurrentMovement.z, change.deltaMovement.z, relatives, Relative.DELTA_Z)
|
| 59 | );
|
| 60 | return new PositionMoveRotation(absolutePosition, absoluteDeltaMovement, absoluteYRot, absoluteXRot);
|
| 61 | }
|
| 62 |
|
| 63 | private static double calculateDelta(final double currentDelta, final double deltaChange, final Set<Relative> relatives, final Relative relative) {
|
| 64 | return relatives.contains(relative) ? currentDelta + deltaChange : deltaChange;
|
| 65 | }
|
| 66 | }
|
| 67 |
|