| 1 | package net.minecraft.network.protocol.game;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
| 4 | import java.util.List;
|
| 5 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 6 | import net.minecraft.network.codec.StreamCodec;
|
| 7 | import net.minecraft.network.protocol.Packet;
|
| 8 | import net.minecraft.network.protocol.PacketType;
|
| 9 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 10 |
|
| 11 | public record ClientboundSetEntityDataPacket(int id, List<SynchedEntityData.DataValue<?>> packedItems) implements Packet<ClientGamePacketListener> {
|
| 12 | public static final StreamCodec<RegistryFriendlyByteBuf, ClientboundSetEntityDataPacket> STREAM_CODEC = Packet.codec(
|
| 13 | ClientboundSetEntityDataPacket::write, ClientboundSetEntityDataPacket::new
|
| 14 | );
|
| 15 | public static final int EOF_MARKER = 255;
|
| 16 |
|
| 17 | private ClientboundSetEntityDataPacket(final RegistryFriendlyByteBuf input) {
|
| 18 | this(input.readVarInt(), unpack(input));
|
| 19 | }
|
| 20 |
|
| 21 | private static void pack(final List<SynchedEntityData.DataValue<?>> items, final RegistryFriendlyByteBuf output) {
|
| 22 | for (SynchedEntityData.DataValue<?> item : items) {
|
| 23 | item.write(output);
|
| 24 | }
|
| 25 |
|
| 26 | output.writeByte(255);
|
| 27 | }
|
| 28 |
|
| 29 | private static List<SynchedEntityData.DataValue<?>> unpack(final RegistryFriendlyByteBuf input) {
|
| 30 | List<SynchedEntityData.DataValue<?>> result = new ArrayList<>();
|
| 31 |
|
| 32 | int id;
|
| 33 | while ((id = input.readUnsignedByte()) != 255) {
|
| 34 | result.add(SynchedEntityData.DataValue.read(input, id));
|
| 35 | }
|
| 36 |
|
| 37 | return result;
|
| 38 | }
|
| 39 |
|
| 40 | private void write(final RegistryFriendlyByteBuf output) {
|
| 41 | output.writeVarInt(this.id);
|
| 42 | pack(this.packedItems, output);
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | public PacketType<ClientboundSetEntityDataPacket> type() {
|
| 47 | return GamePacketTypes.CLIENTBOUND_SET_ENTITY_DATA;
|
| 48 | }
|
| 49 |
|
| 50 | public void handle(final ClientGamePacketListener listener) {
|
| 51 | listener.handleSetEntityData(this);
|
| 52 | }
|
| 53 | }
|
| 54 |
|