| 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.world.phys.Vec3;
|
| 8 |
|
| 9 | public abstract class ServerboundMovePlayerPacket implements Packet<ServerGamePacketListener> {
|
| 10 | private static final int FLAG_ON_GROUND = 1;
|
| 11 | private static final int FLAG_HORIZONTAL_COLLISION = 2;
|
| 12 | protected final double x;
|
| 13 | protected final double y;
|
| 14 | protected final double z;
|
| 15 | protected final float yRot;
|
| 16 | protected final float xRot;
|
| 17 | protected final boolean onGround;
|
| 18 | protected final boolean horizontalCollision;
|
| 19 | protected final boolean hasPos;
|
| 20 | protected final boolean hasRot;
|
| 21 |
|
| 22 | private static int packFlags(final boolean onGround, final boolean horizontalCollision) {
|
| 23 | int flags = 0;
|
| 24 | if (onGround) {
|
| 25 | flags |= 1;
|
| 26 | }
|
| 27 |
|
| 28 | if (horizontalCollision) {
|
| 29 | flags |= 2;
|
| 30 | }
|
| 31 |
|
| 32 | return flags;
|
| 33 | }
|
| 34 |
|
| 35 | private static boolean unpackOnGround(final int flags) {
|
| 36 | return (flags & 1) != 0;
|
| 37 | }
|
| 38 |
|
| 39 | private static boolean unpackHorizontalCollision(final int flags) {
|
| 40 | return (flags & 2) != 0;
|
| 41 | }
|
| 42 |
|
| 43 | protected ServerboundMovePlayerPacket(
|
| 44 | final double x,
|
| 45 | final double y,
|
| 46 | final double z,
|
| 47 | final float yRot,
|
| 48 | final float xRot,
|
| 49 | final boolean onGround,
|
| 50 | final boolean horizontalCollision,
|
| 51 | final boolean hasPos,
|
| 52 | final boolean hasRot
|
| 53 | ) {
|
| 54 | this.x = x;
|
| 55 | this.y = y;
|
| 56 | this.z = z;
|
| 57 | this.yRot = yRot;
|
| 58 | this.xRot = xRot;
|
| 59 | this.onGround = onGround;
|
| 60 | this.horizontalCollision = horizontalCollision;
|
| 61 | this.hasPos = hasPos;
|
| 62 | this.hasRot = hasRot;
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | public abstract PacketType<? extends ServerboundMovePlayerPacket> type();
|
| 67 |
|
| 68 | public void handle(final ServerGamePacketListener listener) {
|
| 69 | listener.handleMovePlayer(this);
|
| 70 | }
|
| 71 |
|
| 72 | public double getX(final double fallback) {
|
| 73 | return this.hasPos ? this.x : fallback;
|
| 74 | }
|
| 75 |
|
| 76 | public double getY(final double fallback) {
|
| 77 | return this.hasPos ? this.y : fallback;
|
| 78 | }
|
| 79 |
|
| 80 | public double getZ(final double fallback) {
|
| 81 | return this.hasPos ? this.z : fallback;
|
| 82 | }
|
| 83 |
|
| 84 | public float getYRot(final float fallback) {
|
| 85 | return this.hasRot ? this.yRot : fallback;
|
| 86 | }
|
| 87 |
|
| 88 | public float getXRot(final float fallback) {
|
| 89 | return this.hasRot ? this.xRot : fallback;
|
| 90 | }
|
| 91 |
|
| 92 | public boolean isOnGround() {
|
| 93 | return this.onGround;
|
| 94 | }
|
| 95 |
|
| 96 | public boolean horizontalCollision() {
|
| 97 | return this.horizontalCollision;
|
| 98 | }
|
| 99 |
|
| 100 | public boolean hasPosition() {
|
| 101 | return this.hasPos;
|
| 102 | }
|
| 103 |
|
| 104 | public boolean hasRotation() {
|
| 105 | return this.hasRot;
|
| 106 | }
|
| 107 |
|
| 108 | public static class Pos extends ServerboundMovePlayerPacket {
|
| 109 | public static final StreamCodec<FriendlyByteBuf, ServerboundMovePlayerPacket.Pos> STREAM_CODEC = Packet.codec(
|
| 110 | ServerboundMovePlayerPacket.Pos::write, ServerboundMovePlayerPacket.Pos::read
|
| 111 | );
|
| 112 |
|
| 113 | public Pos(final Vec3 pos, final boolean onGround, final boolean horizontalCollision) {
|
| 114 | super(pos.x, pos.y, pos.z, 0.0F, 0.0F, onGround, horizontalCollision, true, false);
|
| 115 | }
|
| 116 |
|
| 117 | public Pos(final double x, final double y, final double z, final boolean onGround, final boolean horizontalCollision) {
|
| 118 | super(x, y, z, 0.0F, 0.0F, onGround, horizontalCollision, true, false);
|
| 119 | }
|
| 120 |
|
| 121 | private static ServerboundMovePlayerPacket.Pos read(final FriendlyByteBuf input) {
|
| 122 | double x = input.readDouble();
|
| 123 | double y = input.readDouble();
|
| 124 | double z = input.readDouble();
|
| 125 | short flags = input.readUnsignedByte();
|
| 126 | boolean onGround = ServerboundMovePlayerPacket.unpackOnGround(flags);
|
| 127 | boolean horizontalCollision = ServerboundMovePlayerPacket.unpackHorizontalCollision(flags);
|
| 128 | return new ServerboundMovePlayerPacket.Pos(x, y, z, onGround, horizontalCollision);
|
| 129 | }
|
| 130 |
|
| 131 | private void write(final FriendlyByteBuf output) {
|
| 132 | output.writeDouble(this.x);
|
| 133 | output.writeDouble(this.y);
|
| 134 | output.writeDouble(this.z);
|
| 135 | output.writeByte(ServerboundMovePlayerPacket.packFlags(this.onGround, this.horizontalCollision));
|
| 136 | }
|
| 137 |
|
| 138 | @Override
|
| 139 | public PacketType<ServerboundMovePlayerPacket.Pos> type() {
|
| 140 | return GamePacketTypes.SERVERBOUND_MOVE_PLAYER_POS;
|
| 141 | }
|
| 142 | }
|
| 143 |
|
| 144 | public static class PosRot extends ServerboundMovePlayerPacket {
|
| 145 | public static final StreamCodec<FriendlyByteBuf, ServerboundMovePlayerPacket.PosRot> STREAM_CODEC = Packet.codec(
|
| 146 | ServerboundMovePlayerPacket.PosRot::write, ServerboundMovePlayerPacket.PosRot::read
|
| 147 | );
|
| 148 |
|
| 149 | public PosRot(final Vec3 pos, final float yRot, final float xRot, final boolean onGround, final boolean horizontalCollision) {
|
| 150 | super(pos.x, pos.y, pos.z, yRot, xRot, onGround, horizontalCollision, true, true);
|
| 151 | }
|
| 152 |
|
| 153 | public PosRot(final double x, final double y, final double z, final float yRot, final float xRot, final boolean onGround, final boolean horizontalCollision) {
|
| 154 | super(x, y, z, yRot, xRot, onGround, horizontalCollision, true, true);
|
| 155 | }
|
| 156 |
|
| 157 | private static ServerboundMovePlayerPacket.PosRot read(final FriendlyByteBuf input) {
|
| 158 | double x = input.readDouble();
|
| 159 | double y = input.readDouble();
|
| 160 | double z = input.readDouble();
|
| 161 | float yRot = input.readFloat();
|
| 162 | float xRot = input.readFloat();
|
| 163 | short flags = input.readUnsignedByte();
|
| 164 | boolean onGround = ServerboundMovePlayerPacket.unpackOnGround(flags);
|
| 165 | boolean horizontalCollision = ServerboundMovePlayerPacket.unpackHorizontalCollision(flags);
|
| 166 | return new ServerboundMovePlayerPacket.PosRot(x, y, z, yRot, xRot, onGround, horizontalCollision);
|
| 167 | }
|
| 168 |
|
| 169 | private void write(final FriendlyByteBuf output) {
|
| 170 | output.writeDouble(this.x);
|
| 171 | output.writeDouble(this.y);
|
| 172 | output.writeDouble(this.z);
|
| 173 | output.writeFloat(this.yRot);
|
| 174 | output.writeFloat(this.xRot);
|
| 175 | output.writeByte(ServerboundMovePlayerPacket.packFlags(this.onGround, this.horizontalCollision));
|
| 176 | }
|
| 177 |
|
| 178 | @Override
|
| 179 | public PacketType<ServerboundMovePlayerPacket.PosRot> type() {
|
| 180 | return GamePacketTypes.SERVERBOUND_MOVE_PLAYER_POS_ROT;
|
| 181 | }
|
| 182 | }
|
| 183 |
|
| 184 | public static class Rot extends ServerboundMovePlayerPacket {
|
| 185 | public static final StreamCodec<FriendlyByteBuf, ServerboundMovePlayerPacket.Rot> STREAM_CODEC = Packet.codec(
|
| 186 | ServerboundMovePlayerPacket.Rot::write, ServerboundMovePlayerPacket.Rot::read
|
| 187 | );
|
| 188 |
|
| 189 | public Rot(final float yRot, final float xRot, final boolean onGround, final boolean horizontalCollision) {
|
| 190 | super(0.0, 0.0, 0.0, yRot, xRot, onGround, horizontalCollision, false, true);
|
| 191 | }
|
| 192 |
|
| 193 | private static ServerboundMovePlayerPacket.Rot read(final FriendlyByteBuf input) {
|
| 194 | float yRot = input.readFloat();
|
| 195 | float xRot = input.readFloat();
|
| 196 | short flags = input.readUnsignedByte();
|
| 197 | boolean onGround = ServerboundMovePlayerPacket.unpackOnGround(flags);
|
| 198 | boolean horizontalCollision = ServerboundMovePlayerPacket.unpackHorizontalCollision(flags);
|
| 199 | return new ServerboundMovePlayerPacket.Rot(yRot, xRot, onGround, horizontalCollision);
|
| 200 | }
|
| 201 |
|
| 202 | private void write(final FriendlyByteBuf output) {
|
| 203 | output.writeFloat(this.yRot);
|
| 204 | output.writeFloat(this.xRot);
|
| 205 | output.writeByte(ServerboundMovePlayerPacket.packFlags(this.onGround, this.horizontalCollision));
|
| 206 | }
|
| 207 |
|
| 208 | @Override
|
| 209 | public PacketType<ServerboundMovePlayerPacket.Rot> type() {
|
| 210 | return GamePacketTypes.SERVERBOUND_MOVE_PLAYER_ROT;
|
| 211 | }
|
| 212 | }
|
| 213 |
|
| 214 | public static class StatusOnly extends ServerboundMovePlayerPacket {
|
| 215 | public static final StreamCodec<FriendlyByteBuf, ServerboundMovePlayerPacket.StatusOnly> STREAM_CODEC = Packet.codec(
|
| 216 | ServerboundMovePlayerPacket.StatusOnly::write, ServerboundMovePlayerPacket.StatusOnly::read
|
| 217 | );
|
| 218 |
|
| 219 | public StatusOnly(final boolean onGround, final boolean horizontalCollision) {
|
| 220 | super(0.0, 0.0, 0.0, 0.0F, 0.0F, onGround, horizontalCollision, false, false);
|
| 221 | }
|
| 222 |
|
| 223 | private static ServerboundMovePlayerPacket.StatusOnly read(final FriendlyByteBuf input) {
|
| 224 | short flags = input.readUnsignedByte();
|
| 225 | boolean onGround = ServerboundMovePlayerPacket.unpackOnGround(flags);
|
| 226 | boolean horizontalCollision = ServerboundMovePlayerPacket.unpackHorizontalCollision(flags);
|
| 227 | return new ServerboundMovePlayerPacket.StatusOnly(onGround, horizontalCollision);
|
| 228 | }
|
| 229 |
|
| 230 | private void write(final FriendlyByteBuf output) {
|
| 231 | output.writeByte(ServerboundMovePlayerPacket.packFlags(this.onGround, this.horizontalCollision));
|
| 232 | }
|
| 233 |
|
| 234 | @Override
|
| 235 | public PacketType<ServerboundMovePlayerPacket.StatusOnly> type() {
|
| 236 | return GamePacketTypes.SERVERBOUND_MOVE_PLAYER_STATUS_ONLY;
|
| 237 | }
|
| 238 | }
|
| 239 | }
|
| 240 |
|