package top.leisuretimedock.dglabgame.core.capability;

import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.Entity;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityManager;
import net.minecraftforge.common.capabilities.CapabilityToken;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.util.LazyOptional;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class DGLabDataProvider implements ICapabilitySerializable<CompoundTag> {
    public static final Capability<AbstractDGLabData> DG_LAB_DATA_CAPABILITY = CapabilityManager.get(new CapabilityToken<>() {});
    private final AbstractDGLabData instance;
    private final LazyOptional<AbstractDGLabData> optional;

    public DGLabDataProvider(final Entity entity) {
        this.instance = new EntityDGLabData(entity);
        this.optional = LazyOptional.of(() -> this.instance);
    }

    @Override
    public @NotNull <T> LazyOptional<T> getCapability(@NotNull Capability<T> capability, @Nullable Direction direction) {
        return DG_LAB_DATA_CAPABILITY.orEmpty(capability, this.optional);
    }

    @Override
    public CompoundTag serializeNBT() {
        return instance.serializeNBT();
    }

    @Override
    public void deserializeNBT(CompoundTag compoundTag) {
        instance.deserializeNBT(compoundTag);
    }
}
