package net.minecraft.network.protocol.common;

import io.netty.buffer.ByteBuf;
import java.util.Optional;
import net.minecraft.nbt.NbtAccounter;
import net.minecraft.nbt.Tag;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.PacketType;
import net.minecraft.resources.ResourceLocation;

public record ServerboundCustomClickActionPacket(ResourceLocation id, Optional<Tag> payload) implements Packet<ServerCommonPacketListener> {
    private static final StreamCodec<ByteBuf, Optional<Tag>> UNTRUSTED_TAG_CODEC = ByteBufCodecs.optionalTagCodec(() -> new NbtAccounter(32768L, 16))
        .apply(ByteBufCodecs.lengthPrefixed(65536));
    public static final StreamCodec<ByteBuf, ServerboundCustomClickActionPacket> STREAM_CODEC = StreamCodec.composite(
        ResourceLocation.STREAM_CODEC,
        ServerboundCustomClickActionPacket::id,
        UNTRUSTED_TAG_CODEC,
        ServerboundCustomClickActionPacket::payload,
        ServerboundCustomClickActionPacket::new
    );

    @Override
    public PacketType<ServerboundCustomClickActionPacket> type() {
        return CommonPacketTypes.SERVERBOUND_CUSTOM_CLICK_ACTION;
    }

    @Override
    public void handle(ServerCommonPacketListener handler) {
        handler.handleCustomClickAction(this);
    }
}
