package com.dairymoose.modernlife.tileentities;

import com.dairymoose.modernlife.core.CustomBlocks;
import com.dairymoose.modernlife.core.ModernLifeCommon;
import com.mojang.datafixers.types.Type;
import net.minecraft.core.BlockPos;
import net.minecraft.core.NonNullList;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.Connection;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.Container;
import net.minecraft.world.ContainerHelper;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/* loaded from: outputsrg.jar:com/dairymoose/modernlife/tileentities/EaselBlockEntity.class */
public class EaselBlockEntity extends BlockEntity implements Container {
    public static final BlockEntityType<EaselBlockEntity> EASEL = BlockEntityType.Builder.of(EaselBlockEntity::new, new Block[]{(Block) CustomBlocks.BLOCK_EASEL.get()}).build((Type) null);
    private static final Logger LOGGER = LogManager.getLogger();
    public static final int EASEL_CONTAINER_SIZE = 1;
    private NonNullList<ItemStack> canvasStack;

    public NonNullList<ItemStack> getCanvasStack() {
        return this.canvasStack;
    }

    public EaselBlockEntity(BlockPos pos, BlockState state) {
        super(EASEL, pos, state);
        this.canvasStack = NonNullList.withSize(1, ItemStack.EMPTY);
    }

    public CompoundTag getUpdateTag() {
        CompoundTag nbt = super.getUpdateTag();
        ContainerHelper.saveAllItems(nbt, this.canvasStack);
        return nbt;
    }

    public void handleUpdateTag(CompoundTag tag) {
        ContainerHelper.loadAllItems(tag, this.canvasStack);
        ModernLifeCommon.LOGGER.debug("tag update = " + this.canvasStack);
        super.handleUpdateTag(tag);
    }

    /* renamed from: getUpdatePacket */
    public ClientboundBlockEntityDataPacket m223getUpdatePacket() {
        return ClientboundBlockEntityDataPacket.create(this);
    }

    public void onDataPacket(Connection conn, ClientboundBlockEntityDataPacket pkt) {
        CompoundTag tag = pkt.getTag();
        if (tag != null) {
            ContainerHelper.loadAllItems(tag, this.canvasStack);
            ModernLifeCommon.LOGGER.debug("onDataPacket update = " + this.canvasStack);
        }
    }

    public void load(CompoundTag p_230337_2_) {
        super.load(p_230337_2_);
        ContainerHelper.loadAllItems(p_230337_2_, this.canvasStack);
    }

    protected void saveAdditional(CompoundTag p_189515_1_) {
        ContainerHelper.saveAllItems(p_189515_1_, this.canvasStack);
    }

    public void m_6211_() {
        this.canvasStack.set(0, ItemStack.EMPTY);
    }

    public int getContainerSize() {
        return 1;
    }

    public boolean isEmpty() {
        return ((ItemStack) this.canvasStack.get(0)).isEmpty();
    }

    public ItemStack getItem(int var1) {
        return (ItemStack) this.canvasStack.get(var1);
    }

    public ItemStack removeItem(int var1, int var2) {
        return ContainerHelper.removeItem(this.canvasStack, var1, var2);
    }

    public ItemStack removeItemNoUpdate(int var1) {
        return ContainerHelper.takeItem(this.canvasStack, var1);
    }

    public void setItem(int var1, ItemStack var2) {
        this.canvasStack.set(var1, var2);
    }

    public boolean stillValid(Player p_70300_1_) {
        return this.level.getBlockEntity(this.worldPosition) == this && p_70300_1_.distanceToSqr(((double) this.worldPosition.getX()) + 0.5d, ((double) this.worldPosition.getY()) + 0.5d, ((double) this.worldPosition.getZ()) + 0.5d) <= 64.0d;
    }
}
