|
| 1 | +package by.milansky.protocol.vanilla.standard; |
| 2 | + |
| 3 | +import by.milansky.protocol.api.packet.Packet; |
| 4 | +import by.milansky.protocol.api.version.ProtocolVersion; |
| 5 | +import by.milansky.protocol.vanilla.utility.ProtocolUtility; |
| 6 | +import by.milansky.protocol.vanilla.version.VanillaProtocolVersion; |
| 7 | +import io.netty.buffer.ByteBuf; |
| 8 | +import lombok.AccessLevel; |
| 9 | +import lombok.AllArgsConstructor; |
| 10 | +import lombok.Data; |
| 11 | +import lombok.NoArgsConstructor; |
| 12 | +import lombok.experimental.Accessors; |
| 13 | +import lombok.experimental.ExtensionMethod; |
| 14 | +import lombok.experimental.FieldDefaults; |
| 15 | +import org.jetbrains.annotations.NotNull; |
| 16 | + |
| 17 | +import java.util.UUID; |
| 18 | + |
| 19 | +/** |
| 20 | + * @author milansky |
| 21 | + */ |
| 22 | +@Data |
| 23 | +@NoArgsConstructor |
| 24 | +@AllArgsConstructor |
| 25 | +@Accessors(fluent = true, chain = true) |
| 26 | +@ExtensionMethod({ProtocolUtility.class}) |
| 27 | +@FieldDefaults(level = AccessLevel.PRIVATE) |
| 28 | +public final class ClientboundSpawnEntityLiving implements Packet { |
| 29 | + private static final double POSITION_FACTOR = 32.0; |
| 30 | + private static final float ROTATION_FACTOR = 256.0F / 360.0F; |
| 31 | + private static final double VELOCITY_FACTOR = 8000.0; |
| 32 | + |
| 33 | + int entityID; |
| 34 | + UUID entityUUID; |
| 35 | + int entityTypeID; |
| 36 | + double positionX, positionY, positionZ; |
| 37 | + float yaw, pitch, headPitch; |
| 38 | + double velocityX, velocityY, velocityZ; |
| 39 | + |
| 40 | + @Override |
| 41 | + public void encode(final @NotNull ByteBuf byteBuf, final @NotNull ProtocolVersion version) { |
| 42 | + byteBuf.writeVarInt(entityID); |
| 43 | + if (version.lower(VanillaProtocolVersion.MINECRAFT_1_9)) { |
| 44 | + byteBuf.writeByte(entityTypeID & 255); |
| 45 | + byteBuf.writeInt((int) Math.floor(positionX * POSITION_FACTOR)); |
| 46 | + byteBuf.writeInt((int) Math.floor(positionY * POSITION_FACTOR)); |
| 47 | + byteBuf.writeInt((int) Math.floor(positionZ * POSITION_FACTOR)); |
| 48 | + } else { |
| 49 | + byteBuf.writeUuid(entityUUID); |
| 50 | + |
| 51 | + if (version.greaterEqual(VanillaProtocolVersion.MINECRAFT_1_11)) { |
| 52 | + byteBuf.writeVarInt(entityTypeID); |
| 53 | + } else { |
| 54 | + byteBuf.writeByte(entityTypeID & 255); |
| 55 | + } |
| 56 | + |
| 57 | + byteBuf.writeDouble(positionX); |
| 58 | + byteBuf.writeDouble(positionY); |
| 59 | + byteBuf.writeDouble(positionZ); |
| 60 | + } |
| 61 | + |
| 62 | + byteBuf.writeByte((int) (yaw * ROTATION_FACTOR)); |
| 63 | + byteBuf.writeByte((int) (pitch * ROTATION_FACTOR)); |
| 64 | + byteBuf.writeByte((int) (headPitch * ROTATION_FACTOR)); |
| 65 | + |
| 66 | + byteBuf.writeShort((int) (velocityX * VELOCITY_FACTOR)); |
| 67 | + byteBuf.writeShort((int) (velocityY * VELOCITY_FACTOR)); |
| 68 | + byteBuf.writeShort((int) (velocityZ * VELOCITY_FACTOR)); |
| 69 | + |
| 70 | + if (version.lower(VanillaProtocolVersion.MINECRAFT_1_15)) |
| 71 | + byteBuf.writeEmptyEntityMetadata(version); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void decode(final @NotNull ByteBuf byteBuf, final @NotNull ProtocolVersion version) { |
| 76 | + this.entityID = byteBuf.readVarInt(); |
| 77 | + if (version.lower(VanillaProtocolVersion.MINECRAFT_1_9)) { |
| 78 | + this.entityUUID = new UUID(0L, 0L); |
| 79 | + this.entityTypeID = byteBuf.readByte() & 255; |
| 80 | + |
| 81 | + this.positionX = byteBuf.readInt() / POSITION_FACTOR; |
| 82 | + this.positionY = byteBuf.readInt() / POSITION_FACTOR; |
| 83 | + this.positionZ = byteBuf.readInt() / POSITION_FACTOR; |
| 84 | + } else { |
| 85 | + this.entityUUID = byteBuf.readUuid(); |
| 86 | + if (version.greaterEqual(VanillaProtocolVersion.MINECRAFT_1_11)) { |
| 87 | + this.entityTypeID = byteBuf.readVarInt(); |
| 88 | + } else { |
| 89 | + this.entityTypeID = byteBuf.readUnsignedByte(); |
| 90 | + } |
| 91 | + |
| 92 | + this.positionX = byteBuf.readDouble(); |
| 93 | + this.positionY = byteBuf.readDouble(); |
| 94 | + this.positionZ = byteBuf.readDouble(); |
| 95 | + } |
| 96 | + this.yaw = byteBuf.readByte() / ROTATION_FACTOR; |
| 97 | + this.pitch = byteBuf.readByte() / ROTATION_FACTOR; |
| 98 | + this.headPitch = byteBuf.readByte() / ROTATION_FACTOR; |
| 99 | + |
| 100 | + this.velocityX = byteBuf.readShort() / VELOCITY_FACTOR; |
| 101 | + this.velocityY = byteBuf.readShort() / VELOCITY_FACTOR; |
| 102 | + this.velocityZ = byteBuf.readShort() / VELOCITY_FACTOR; |
| 103 | + |
| 104 | + if (version.lower(VanillaProtocolVersion.MINECRAFT_1_15)) { |
| 105 | + byteBuf.skipBytes(byteBuf.readableBytes()); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments