| 1 | package net.minecraft.network.protocol.game;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.datafixers.util.Pair;
|
| 5 | import java.util.List;
|
| 6 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 7 | import net.minecraft.network.codec.StreamCodec;
|
| 8 | import net.minecraft.network.protocol.Packet;
|
| 9 | import net.minecraft.network.protocol.PacketType;
|
| 10 | import net.minecraft.world.entity.EquipmentSlot;
|
| 11 | import net.minecraft.world.item.ItemStack;
|
| 12 |
|
| 13 | public class ClientboundSetEquipmentPacket implements Packet<ClientGamePacketListener> {
|
| 14 | public static final StreamCodec<RegistryFriendlyByteBuf, ClientboundSetEquipmentPacket> STREAM_CODEC = Packet.codec(
|
| 15 | ClientboundSetEquipmentPacket::write, ClientboundSetEquipmentPacket::new
|
| 16 | );
|
| 17 | private static final byte CONTINUE_MASK = -128;
|
| 18 | private final int entity;
|
| 19 | private final List<Pair<EquipmentSlot, ItemStack>> slots;
|
| 20 |
|
| 21 | public ClientboundSetEquipmentPacket(final int entity, final List<Pair<EquipmentSlot, ItemStack>> slots) {
|
| 22 | this.entity = entity;
|
| 23 | this.slots = slots;
|
| 24 | }
|
| 25 |
|
| 26 | private ClientboundSetEquipmentPacket(final RegistryFriendlyByteBuf input) {
|
| 27 | this.entity = input.readVarInt();
|
| 28 | this.slots = Lists.newArrayList();
|
| 29 |
|
| 30 | int slotId;
|
| 31 | do {
|
| 32 | slotId = input.readByte();
|
| 33 | EquipmentSlot slot = EquipmentSlot.VALUES.get(slotId & 127);
|
| 34 | ItemStack itemStack = ItemStack.OPTIONAL_STREAM_CODEC.decode(input);
|
| 35 | this.slots.add(Pair.of(slot, itemStack));
|
| 36 | } while ((slotId & -128) != 0);
|
| 37 | }
|
| 38 |
|
| 39 | private void write(final RegistryFriendlyByteBuf output) {
|
| 40 | output.writeVarInt(this.entity);
|
| 41 | int size = this.slots.size();
|
| 42 |
|
| 43 | for (int i = 0; i < size; i++) {
|
| 44 | Pair<EquipmentSlot, ItemStack> e = this.slots.get(i);
|
| 45 | EquipmentSlot slotType = e.getFirst();
|
| 46 | boolean shouldContinue = i != size - 1;
|
| 47 | int slotId = slotType.ordinal();
|
| 48 | output.writeByte(shouldContinue ? slotId | -128 : slotId);
|
| 49 | ItemStack.OPTIONAL_STREAM_CODEC.encode(output, e.getSecond());
|
| 50 | }
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public PacketType<ClientboundSetEquipmentPacket> type() {
|
| 55 | return GamePacketTypes.CLIENTBOUND_SET_EQUIPMENT;
|
| 56 | }
|
| 57 |
|
| 58 | public void handle(final ClientGamePacketListener listener) {
|
| 59 | listener.handleSetEquipment(this);
|
| 60 | }
|
| 61 |
|
| 62 | public int getEntity() {
|
| 63 | return this.entity;
|
| 64 | }
|
| 65 |
|
| 66 | public List<Pair<EquipmentSlot, ItemStack>> getSlots() {
|
| 67 | return this.slots;
|
| 68 | }
|
| 69 | }
|
| 70 |
|