package com.dairymoose.modernlife.tileentities;

import com.dairymoose.modernlife.blocks.gui.chess.ChessScreen;
import com.dairymoose.modernlife.core.CustomBlocks;
import com.dairymoose.modernlife.core.ModernLifeCommon;
import com.mojang.datafixers.types.Type;
import net.minecraft.client.Minecraft;
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 net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

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

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

    public ChessBoardBlockEntity(BlockPos pos, BlockState state) {
        super(CHESS_BOARD, pos, state);
        this.chessStack = NonNullList.withSize(1, ItemStack.EMPTY);
    }

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

    public void handleUpdateTag(CompoundTag tag) {
        ContainerHelper.loadAllItems(tag, this.chessStack);
        super.handleUpdateTag(tag);
    }

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

    @OnlyIn(Dist.CLIENT)
    public void onDataPacket(Connection conn, ClientboundBlockEntityDataPacket pkt) {
        CompoundTag tag = pkt.getTag();
        if (tag != null) {
            ContainerHelper.loadAllItems(tag, this.chessStack);
            ModernLifeCommon.LOGGER.debug("onDataPacket update = " + this.chessStack);
            if (Minecraft.getInstance().screen instanceof ChessScreen) {
                Minecraft.getInstance().setScreen(new ChessScreen(getBlockPos()));
            }
        }
    }

    public void load(CompoundTag tag) {
        super.load(tag);
        ContainerHelper.loadAllItems(tag, this.chessStack);
    }

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

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

    public int getContainerSize() {
        return 1;
    }

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

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

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

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

    public void setItem(int var1, ItemStack var2) {
        this.chessStack.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;
    }
}
