package net.minecraft.network.protocol.game; import java.util.List; import net.minecraft.core.NonNullList; import net.minecraft.network.RegistryFriendlyByteBuf; import net.minecraft.network.codec.StreamCodec; import net.minecraft.network.protocol.Packet; import net.minecraft.network.protocol.PacketType; import net.minecraft.world.item.ItemStack; public class ClientboundContainerSetContentPacket implements Packet { public static final StreamCodec STREAM_CODEC = Packet.codec( ClientboundContainerSetContentPacket::write, ClientboundContainerSetContentPacket::new ); private final int containerId; private final int stateId; private final List items; private final ItemStack carriedItem; public ClientboundContainerSetContentPacket(int containerId, int stateId, NonNullList items, ItemStack carriedItem) { this.containerId = containerId; this.stateId = stateId; this.items = NonNullList.withSize(items.size(), ItemStack.EMPTY); for (int i = 0; i < items.size(); i++) { this.items.set(i, items.get(i).copy()); } this.carriedItem = carriedItem.copy(); } private ClientboundContainerSetContentPacket(RegistryFriendlyByteBuf buffer) { this.containerId = buffer.readUnsignedByte(); this.stateId = buffer.readVarInt(); this.items = ItemStack.OPTIONAL_LIST_STREAM_CODEC.decode(buffer); this.carriedItem = ItemStack.OPTIONAL_STREAM_CODEC.decode(buffer); } private void write(RegistryFriendlyByteBuf buffer) { buffer.writeByte(this.containerId); buffer.writeVarInt(this.stateId); ItemStack.OPTIONAL_LIST_STREAM_CODEC.encode(buffer, this.items); ItemStack.OPTIONAL_STREAM_CODEC.encode(buffer, this.carriedItem); } @Override public PacketType type() { return GamePacketTypes.CLIENTBOUND_CONTAINER_SET_CONTENT; } /** * Passes this Packet on to the NetHandler for processing. */ public void handle(ClientGamePacketListener handler) { handler.handleContainerContent(this); } public int getContainerId() { return this.containerId; } public List getItems() { return this.items; } public ItemStack getCarriedItem() { return this.carriedItem; } public int getStateId() { return this.stateId; } }