package com.r3944realms.leashedplayer.network.client;

import com.r3944realms.leashedplayer.LeashedPlayer;
import com.r3944realms.leashedplayer.content.gamerules.Gamerules;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.neoforge.network.handling.IPayloadContext;
import org.jetbrains.annotations.NotNull;

public record BooleanGameRuleValueChangeData(String gamerule_id, boolean value) implements IGameRuleValueChangeData {
    public static CustomPacketPayload.Type<BooleanGameRuleValueChangeData> TYPE = new CustomPacketPayload.Type<>(ResourceLocation.fromNamespaceAndPath(LeashedPlayer.MOD_ID,"bool_gamerule_value_change"));
    public static final StreamCodec<FriendlyByteBuf, BooleanGameRuleValueChangeData> STREAM_CODEC =
            StreamCodec.composite(
                    ByteBufCodecs.STRING_UTF8, BooleanGameRuleValueChangeData::gamerule_id,
                    ByteBufCodecs.BOOL, BooleanGameRuleValueChangeData::value,
                    BooleanGameRuleValueChangeData::new
            );

    public void handle(IPayloadContext context) {
        context.enqueueWork(() -> {
            Gamerules.gamerulesBooleanValuesClient.put(gamerule_id, value);
        });
    }
    @Override
    public @NotNull Type<? extends CustomPacketPayload> type() {
        return TYPE;
    }
}
