package me.km.networking;

import java.util.function.Supplier;
import me.km.Server;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fml.network.NetworkEvent;

public class FunctionKey {
    private final byte key;

    public FunctionKey() {
        key = -1;
    }

    public FunctionKey(int key) {
        this.key = (byte) key;
    }

    public static void writeBytes(FunctionKey fk, PacketBuffer buf) {
        buf.writeByte(fk.key);
    }

    public static FunctionKey fromBytes(PacketBuffer buf) {
        return new FunctionKey(buf.readByte());
    }

    public static void handle(FunctionKey fk, Supplier<NetworkEvent.Context> context) {
        context.get().enqueueWork(() -> {
            Server.scriptEvents.onFunctionKey(context.get().getSender(), fk.key);
        });
        context.get().setPacketHandled(true);
    }
}
