package com.r3944realms.leashedplayer.content.blocks.entity;

import net.minecraft.core.BlockPos;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.ValueInput;
import net.minecraft.world.level.storage.ValueOutput;
import org.jetbrains.annotations.NotNull;

public class ItemMemoryBlockEntity extends BlockEntity {

    private ItemStack memorizedItem = ItemStack.EMPTY;
    public ItemMemoryBlockEntity(BlockPos pos, BlockState blockState) {
        super(ModBlockEntityRegister.ITEM_MEMORY_BLOCK.get(), pos, blockState);
    }
    // 记忆物品
    public void setMemorizedItem(ItemStack memorizedItem) {
        this.memorizedItem = memorizedItem.copy();
        this.memorizedItem.setCount(1);
        setChanged();
    }
    // 获取记忆的物品
    public ItemStack getMemorizedItem() {
        return memorizedItem.copy();
    }
    //--- 数据持久化（必须实现）---//
    @Override
    protected void saveAdditional(@NotNull ValueOutput valueOutput) {
        super.saveAdditional(valueOutput);
        if (!memorizedItem.isEmpty()) {
            valueOutput.store("memorizedItem", ItemStack.CODEC, memorizedItem);
        }

    }

    @Override
    protected void loadAdditional(@NotNull ValueInput valueInput) {
        super.loadAdditional(valueInput);
        memorizedItem = valueInput.read("memorizedItem", ItemStack.CODEC).orElse(ItemStack.EMPTY);
    }
}
