| 1 | package net.minecraft.network.protocol.game;
|
| 2 |
|
| 3 | import net.minecraft.network.FriendlyByteBuf;
|
| 4 | import net.minecraft.network.codec.StreamCodec;
|
| 5 | import net.minecraft.network.protocol.Packet;
|
| 6 | import net.minecraft.network.protocol.PacketType;
|
| 7 | import net.minecraft.util.Mth;
|
| 8 | import net.minecraft.world.entity.Entity;
|
| 9 | import net.minecraft.world.level.Level;
|
| 10 | import org.jspecify.annotations.Nullable;
|
| 11 |
|
| 12 | public abstract class ClientboundMoveEntityPacket implements MovementPacket<ClientGamePacketListener> {
|
| 13 | private static final int ON_GROUND_FLAG = 1;
|
| 14 | private static final int STEP_COUNT_OFFSET = 1;
|
| 15 | protected final int entityId;
|
| 16 | protected final VecDelta delta;
|
| 17 | protected final byte yRot;
|
| 18 | protected final byte xRot;
|
| 19 | protected final boolean onGround;
|
| 20 | protected final boolean hasRot;
|
| 21 | protected final boolean hasPos;
|
| 22 |
|
| 23 | protected static boolean unpackOnGround(final int properties) {
|
| 24 | return (properties & 1) != 0;
|
| 25 | }
|
| 26 |
|
| 27 | protected static int unpackStepCount(final int properties) {
|
| 28 | return properties >>> 1;
|
| 29 | }
|
| 30 |
|
| 31 | protected static int packProperties(final boolean onGround, final int stepCount) {
|
| 32 | return (onGround ? 1 : 0) | stepCount << 1;
|
| 33 | }
|
| 34 |
|
| 35 | protected ClientboundMoveEntityPacket(
|
| 36 | final int entityId, final VecDelta delta, final byte yRot, final byte xRot, final boolean onGround, final boolean hasRot, final boolean hasPos
|
| 37 | ) {
|
| 38 | this.entityId = entityId;
|
| 39 | this.delta = delta;
|
| 40 | this.yRot = yRot;
|
| 41 | this.xRot = xRot;
|
| 42 | this.onGround = onGround;
|
| 43 | this.hasRot = hasRot;
|
| 44 | this.hasPos = hasPos;
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | public abstract PacketType<? extends ClientboundMoveEntityPacket> type();
|
| 49 |
|
| 50 | public void handle(final ClientGamePacketListener listener) {
|
| 51 | listener.handleMoveEntity(this);
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | public String toString() {
|
| 56 | return "Entity_" + super.toString();
|
| 57 | }
|
| 58 |
|
| 59 | public @Nullable Entity getEntity(final Level level) {
|
| 60 | return level.getEntity(this.entityId);
|
| 61 | }
|
| 62 |
|
| 63 | public VecDelta getPositionDelta() {
|
| 64 | return this.delta;
|
| 65 | }
|
| 66 |
|
| 67 | public float getYRot() {
|
| 68 | return Mth.unpackDegrees(this.yRot);
|
| 69 | }
|
| 70 |
|
| 71 | public float getXRot() {
|
| 72 | return Mth.unpackDegrees(this.xRot);
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | public boolean hasRotation() {
|
| 77 | return this.hasRot;
|
| 78 | }
|
| 79 |
|
| 80 | @Override
|
| 81 | public boolean hasPosition() {
|
| 82 | return this.hasPos;
|
| 83 | }
|
| 84 |
|
| 85 | public boolean isOnGround() {
|
| 86 | return this.onGround;
|
| 87 | }
|
| 88 |
|
| 89 | public static class Pos extends ClientboundMoveEntityPacket {
|
| 90 | public static final StreamCodec<FriendlyByteBuf, ClientboundMoveEntityPacket.Pos> STREAM_CODEC = Packet.codec(
|
| 91 | ClientboundMoveEntityPacket.Pos::write, ClientboundMoveEntityPacket.Pos::read
|
| 92 | );
|
| 93 |
|
| 94 | public Pos(final int id, final VecDelta delta, final boolean onGround) {
|
| 95 | super(id, delta, (byte)0, (byte)0, onGround, false, true);
|
| 96 | }
|
| 97 |
|
| 98 | private static ClientboundMoveEntityPacket.Pos read(final FriendlyByteBuf input) {
|
| 99 | int entityId = input.readVarInt();
|
| 100 | int properties = input.readVarInt();
|
| 101 | VecDelta delta = VecDelta.read(input, unpackStepCount(properties));
|
| 102 | return new ClientboundMoveEntityPacket.Pos(entityId, delta, unpackOnGround(properties));
|
| 103 | }
|
| 104 |
|
| 105 | private void write(final FriendlyByteBuf output) {
|
| 106 | output.writeVarInt(this.entityId);
|
| 107 | output.writeVarInt(packProperties(this.onGround, this.delta.stepCount()));
|
| 108 | VecDelta.write(output, this.delta);
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | public PacketType<ClientboundMoveEntityPacket.Pos> type() {
|
| 113 | return GamePacketTypes.CLIENTBOUND_MOVE_ENTITY_POS;
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 | public static class PosRot extends ClientboundMoveEntityPacket {
|
| 118 | public static final StreamCodec<FriendlyByteBuf, ClientboundMoveEntityPacket.PosRot> STREAM_CODEC = Packet.codec(
|
| 119 | ClientboundMoveEntityPacket.PosRot::write, ClientboundMoveEntityPacket.PosRot::read
|
| 120 | );
|
| 121 |
|
| 122 | public PosRot(final int id, final VecDelta delta, final byte yRot, final byte xRot, final boolean onGround) {
|
| 123 | super(id, delta, yRot, xRot, onGround, true, true);
|
| 124 | }
|
| 125 |
|
| 126 | private static ClientboundMoveEntityPacket.PosRot read(final FriendlyByteBuf input) {
|
| 127 | int entityId = input.readVarInt();
|
| 128 | int properties = input.readVarInt();
|
| 129 | VecDelta delta = VecDelta.read(input, unpackStepCount(properties));
|
| 130 | byte yRot = input.readByte();
|
| 131 | byte xRot = input.readByte();
|
| 132 | return new ClientboundMoveEntityPacket.PosRot(entityId, delta, yRot, xRot, unpackOnGround(properties));
|
| 133 | }
|
| 134 |
|
| 135 | private void write(final FriendlyByteBuf output) {
|
| 136 | output.writeVarInt(this.entityId);
|
| 137 | output.writeVarInt(packProperties(this.onGround, this.delta.stepCount()));
|
| 138 | VecDelta.write(output, this.delta);
|
| 139 | output.writeByte(this.yRot);
|
| 140 | output.writeByte(this.xRot);
|
| 141 | }
|
| 142 |
|
| 143 | @Override
|
| 144 | public PacketType<ClientboundMoveEntityPacket.PosRot> type() {
|
| 145 | return GamePacketTypes.CLIENTBOUND_MOVE_ENTITY_POS_ROT;
|
| 146 | }
|
| 147 | }
|
| 148 |
|
| 149 | public static class Rot extends ClientboundMoveEntityPacket {
|
| 150 | public static final StreamCodec<FriendlyByteBuf, ClientboundMoveEntityPacket.Rot> STREAM_CODEC = Packet.codec(
|
| 151 | ClientboundMoveEntityPacket.Rot::write, ClientboundMoveEntityPacket.Rot::read
|
| 152 | );
|
| 153 |
|
| 154 | public Rot(final int id, final byte yRot, final byte xRot, final boolean onGround) {
|
| 155 | super(id, VecDelta.ZERO, yRot, xRot, onGround, true, false);
|
| 156 | }
|
| 157 |
|
| 158 | private static ClientboundMoveEntityPacket.Rot read(final FriendlyByteBuf input) {
|
| 159 | int entityId = input.readVarInt();
|
| 160 | boolean onGround = input.readBoolean();
|
| 161 | byte yRot = input.readByte();
|
| 162 | byte xRot = input.readByte();
|
| 163 | return new ClientboundMoveEntityPacket.Rot(entityId, yRot, xRot, onGround);
|
| 164 | }
|
| 165 |
|
| 166 | private void write(final FriendlyByteBuf output) {
|
| 167 | output.writeVarInt(this.entityId);
|
| 168 | output.writeBoolean(this.onGround);
|
| 169 | output.writeByte(this.yRot);
|
| 170 | output.writeByte(this.xRot);
|
| 171 | }
|
| 172 |
|
| 173 | @Override
|
| 174 | public PacketType<ClientboundMoveEntityPacket.Rot> type() {
|
| 175 | return GamePacketTypes.CLIENTBOUND_MOVE_ENTITY_ROT;
|
| 176 | }
|
| 177 | }
|
| 178 | }
|
| 179 |
|