package net.minecraft.network.protocol.game;

import net.minecraft.network.PacketListener;
import io.netty.buffer.ByteBuf;
import java.io.IOException;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.network.protocol.Packet;

public class ServerboundCustomPayloadPacket implements Packet<ServerGamePacketListener> {
    public static final ResourceLocation BRAND;
    private ResourceLocation identifier;
    private FriendlyByteBuf data;
    
    public ServerboundCustomPayloadPacket() {
    }
    
    public ServerboundCustomPayloadPacket(final ResourceLocation sm, final FriendlyByteBuf kv) {
        this.identifier = sm;
        this.data = kv;
    }
    
    @Override
    public void read(final FriendlyByteBuf kv) throws IOException {
        this.identifier = kv.readResourceLocation();
        final int integer3 = kv.readableBytes();
        if (integer3 < 0 || integer3 > 32767) {
            throw new IOException("Payload may not be larger than 32767 bytes");
        }
        this.data = new FriendlyByteBuf(kv.readBytes(integer3));
    }
    
    @Override
    public void write(final FriendlyByteBuf kv) throws IOException {
        kv.writeResourceLocation(this.identifier);
        kv.writeBytes(this.data);
    }
    
    @Override
    public void handle(final ServerGamePacketListener pl) {
        pl.handleCustomPayload(this);
        if (this.data != null) {
            this.data.release();
        }
    }
    
    static {
        BRAND = new ResourceLocation("brand");
    }
}
