| 1 | package net.minecraft.network.protocol.game;
|
| 2 |
|
| 3 | import net.minecraft.commands.arguments.EntityAnchorArgument;
|
| 4 | import net.minecraft.network.FriendlyByteBuf;
|
| 5 | import net.minecraft.network.codec.StreamCodec;
|
| 6 | import net.minecraft.network.protocol.Packet;
|
| 7 | import net.minecraft.network.protocol.PacketType;
|
| 8 | import net.minecraft.world.entity.Entity;
|
| 9 | import net.minecraft.world.level.Level;
|
| 10 | import net.minecraft.world.phys.Vec3;
|
| 11 | import org.jspecify.annotations.Nullable;
|
| 12 |
|
| 13 | public class ClientboundPlayerLookAtPacket implements Packet<ClientGamePacketListener> {
|
| 14 | public static final StreamCodec<FriendlyByteBuf, ClientboundPlayerLookAtPacket> STREAM_CODEC = Packet.codec(
|
| 15 | ClientboundPlayerLookAtPacket::write, ClientboundPlayerLookAtPacket::new
|
| 16 | );
|
| 17 | private final double x;
|
| 18 | private final double y;
|
| 19 | private final double z;
|
| 20 | private final int entity;
|
| 21 | private final EntityAnchorArgument.Anchor fromAnchor;
|
| 22 | private final EntityAnchorArgument.Anchor toAnchor;
|
| 23 | private final boolean atEntity;
|
| 24 |
|
| 25 | public ClientboundPlayerLookAtPacket(final EntityAnchorArgument.Anchor fromAnchor, final double x, final double y, final double z) {
|
| 26 | this.fromAnchor = fromAnchor;
|
| 27 | this.x = x;
|
| 28 | this.y = y;
|
| 29 | this.z = z;
|
| 30 | this.entity = 0;
|
| 31 | this.atEntity = false;
|
| 32 | this.toAnchor = null;
|
| 33 | }
|
| 34 |
|
| 35 | public ClientboundPlayerLookAtPacket(final EntityAnchorArgument.Anchor fromAnchor, final Entity entity, final EntityAnchorArgument.Anchor toAnchor) {
|
| 36 | this.fromAnchor = fromAnchor;
|
| 37 | this.entity = entity.getId();
|
| 38 | this.toAnchor = toAnchor;
|
| 39 | Vec3 pos = toAnchor.apply(entity);
|
| 40 | this.x = pos.x;
|
| 41 | this.y = pos.y;
|
| 42 | this.z = pos.z;
|
| 43 | this.atEntity = true;
|
| 44 | }
|
| 45 |
|
| 46 | private ClientboundPlayerLookAtPacket(final FriendlyByteBuf input) {
|
| 47 | this.fromAnchor = input.readEnum(EntityAnchorArgument.Anchor.class);
|
| 48 | this.x = input.readDouble();
|
| 49 | this.y = input.readDouble();
|
| 50 | this.z = input.readDouble();
|
| 51 | this.atEntity = input.readBoolean();
|
| 52 | if (this.atEntity) {
|
| 53 | this.entity = input.readVarInt();
|
| 54 | this.toAnchor = input.readEnum(EntityAnchorArgument.Anchor.class);
|
| 55 | } else {
|
| 56 | this.entity = 0;
|
| 57 | this.toAnchor = null;
|
| 58 | }
|
| 59 | }
|
| 60 |
|
| 61 | private void write(final FriendlyByteBuf output) {
|
| 62 | output.writeEnum(this.fromAnchor);
|
| 63 | output.writeDouble(this.x);
|
| 64 | output.writeDouble(this.y);
|
| 65 | output.writeDouble(this.z);
|
| 66 | output.writeBoolean(this.atEntity);
|
| 67 | if (this.atEntity) {
|
| 68 | output.writeVarInt(this.entity);
|
| 69 | output.writeEnum(this.toAnchor);
|
| 70 | }
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | public PacketType<ClientboundPlayerLookAtPacket> type() {
|
| 75 | return GamePacketTypes.CLIENTBOUND_PLAYER_LOOK_AT;
|
| 76 | }
|
| 77 |
|
| 78 | public void handle(final ClientGamePacketListener listener) {
|
| 79 | listener.handleLookAt(this);
|
| 80 | }
|
| 81 |
|
| 82 | public EntityAnchorArgument.Anchor getFromAnchor() {
|
| 83 | return this.fromAnchor;
|
| 84 | }
|
| 85 |
|
| 86 | public @Nullable Vec3 getPosition(final Level level) {
|
| 87 | if (this.atEntity) {
|
| 88 | Entity entity = level.getEntity(this.entity);
|
| 89 | return entity == null ? new Vec3(this.x, this.y, this.z) : this.toAnchor.apply(entity);
|
| 90 | } else {
|
| 91 | return new Vec3(this.x, this.y, this.z);
|
| 92 | }
|
| 93 | }
|
| 94 | }
|
| 95 |
|