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/WallShelfBlockEntity.class */
public class WallShelfBlockEntity extends BlockEntity implements Container {
    public static final BlockEntityType<WallShelfBlockEntity> WALL_SHELF = BlockEntityType.Builder.of(WallShelfBlockEntity::wallShelf, new Block[]{(Block) CustomBlocks.BLOCK_ACACIA_WALL_SHELF.get(), (Block) CustomBlocks.BLOCK_BIRCH_WALL_SHELF.get(), (Block) CustomBlocks.BLOCK_CRIMSON_WALL_SHELF.get(), (Block) CustomBlocks.BLOCK_DARK_OAK_WALL_SHELF.get(), (Block) CustomBlocks.BLOCK_JUNGLE_WALL_SHELF.get(), (Block) CustomBlocks.BLOCK_OAK_WALL_SHELF.get(), (Block) CustomBlocks.BLOCK_SPRUCE_WALL_SHELF.get(), (Block) CustomBlocks.BLOCK_WARPED_WALL_SHELF.get()}).build((Type) null);
    public static final BlockEntityType<WallShelfBlockEntity> DOUBLE_WALL_SHELF = BlockEntityType.Builder.of(WallShelfBlockEntity::doubleWallShelf, new Block[]{(Block) CustomBlocks.BLOCK_DOUBLE_ACACIA_WALL_SHELF.get(), (Block) CustomBlocks.BLOCK_DOUBLE_BIRCH_WALL_SHELF.get(), (Block) CustomBlocks.BLOCK_DOUBLE_CRIMSON_WALL_SHELF.get(), (Block) CustomBlocks.BLOCK_DOUBLE_DARK_OAK_WALL_SHELF.get(), (Block) CustomBlocks.BLOCK_DOUBLE_JUNGLE_WALL_SHELF.get(), (Block) CustomBlocks.BLOCK_DOUBLE_OAK_WALL_SHELF.get(), (Block) CustomBlocks.BLOCK_DOUBLE_SPRUCE_WALL_SHELF.get(), (Block) CustomBlocks.BLOCK_DOUBLE_WARPED_WALL_SHELF.get()}).build((Type) null);
    private static final Logger LOGGER = LogManager.getLogger();
    public static final int WALL_SHELF_CONTAINER_SIZE = 3;
    public static final int DOUBLE_WALL_SHELF_CONTAINER_SIZE = 6;
    private NonNullList<ItemStack> items;

    public NonNullList<ItemStack> getItems() {
        return this.items;
    }

    public void initItems() {
        BlockEntityType type;
        if (this.items == null && (type = getType()) != null) {
            if (type == DOUBLE_WALL_SHELF) {
                ModernLifeCommon.LOGGER.debug("init with size = 6");
                this.items = NonNullList.withSize(6, ItemStack.EMPTY);
            } else {
                ModernLifeCommon.LOGGER.debug("init with size = 3");
                this.items = NonNullList.withSize(3, ItemStack.EMPTY);
            }
        }
    }

    public boolean isFull() {
        int count = 0;
        for (int i = 0; i < this.items.size(); i++) {
            if (!((ItemStack) this.items.get(i)).isEmpty()) {
                count++;
            }
        }
        return count == this.items.size();
    }

    public int getNextFreeSlot() {
        for (int i = 0; i < this.items.size(); i++) {
            if (((ItemStack) this.items.get(i)).isEmpty()) {
                return i;
            }
        }
        return -1;
    }

    public int getNextOccupiedSlot() {
        for (int i = this.items.size() - 1; i >= 0; i--) {
            if (!((ItemStack) this.items.get(i)).isEmpty()) {
                return i;
            }
        }
        return -1;
    }

    public void placeItem(ItemStack itemStack) {
        int freeSlot = getNextFreeSlot();
        if (freeSlot != -1) {
            setItem(freeSlot, itemStack.copy());
        }
    }

    public WallShelfBlockEntity(boolean doubleShelf, BlockPos pos, BlockState state) {
        super(doubleShelf ? DOUBLE_WALL_SHELF : WALL_SHELF, pos, state);
        this.items = null;
        initItems();
    }

    public static WallShelfBlockEntity wallShelf(BlockPos pos, BlockState state) {
        return new WallShelfBlockEntity(false, pos, state);
    }

    public static WallShelfBlockEntity doubleWallShelf(BlockPos pos, BlockState state) {
        return new WallShelfBlockEntity(true, pos, state);
    }

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

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

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

    public void onDataPacket(Connection conn, ClientboundBlockEntityDataPacket pkt) {
        CompoundTag tag = pkt.getTag();
        if (tag != null) {
            initItems();
            this.items.clear();
            ContainerHelper.loadAllItems(tag, this.items);
        }
    }

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

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

    public void m_6211_() {
        for (int i = 0; i < this.items.size(); i++) {
            this.items.set(i, ItemStack.EMPTY);
        }
    }

    public int getContainerSize() {
        return this.items.size();
    }

    public boolean isEmpty() {
        for (int i = 0; i < this.items.size(); i++) {
            if (this.items.get(i) != ItemStack.EMPTY) {
                return false;
            }
        }
        return true;
    }

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

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

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

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