package top.leisuretimedock.dglabgame.core.network.toServer;

import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.network.NetworkEvent;
import org.jetbrains.annotations.NotNull;
import top.leisuretimedock.dglabgame.core.capability.DGLabDataProvider;

import java.util.function.Supplier;

public record AcceptUserAgreementPacket() {
    public void encode(FriendlyByteBuf packet) {}
    public static AcceptUserAgreementPacket decode(FriendlyByteBuf packet) {
        return new AcceptUserAgreementPacket();
    }
    public void handle(@NotNull Supplier<NetworkEvent.Context> contextSupplier) {
        NetworkEvent.Context context = contextSupplier.get();
        context.enqueueWork(() -> {
            ServerPlayer sender = context.getSender();
            if (sender != null) {
                sender.getCapability(DGLabDataProvider.DG_LAB_DATA_CAPABILITY)
                        .ifPresent(dgLabData -> {
                            dgLabData.setAcceptUseAgreements(true);
                        });
            }
            context.setPacketHandled(true);
        });
    }
}
