package net.minecraft.world.inventory;

import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.Container;

public class ShulkerBoxMenu extends AbstractContainerMenu {
    private final Container container;
    
    public ShulkerBoxMenu(final int integer, final Inventory ayi) {
        this(integer, ayi, new SimpleContainer(27));
    }
    
    public ShulkerBoxMenu(final int integer, final Inventory ayi, final Container aje) {
        super(MenuType.SHULKER_BOX, integer);
        AbstractContainerMenu.checkContainerSize(aje, 27);
        (this.container = aje).startOpen(ayi.player);
        final int integer2 = 3;
        final int integer3 = 9;
        for (int integer4 = 0; integer4 < 3; ++integer4) {
            for (int integer5 = 0; integer5 < 9; ++integer5) {
                this.addSlot(new ShulkerBoxSlot(aje, integer5 + integer4 * 9, 8 + integer5 * 18, 18 + integer4 * 18));
            }
        }
        for (int integer4 = 0; integer4 < 3; ++integer4) {
            for (int integer5 = 0; integer5 < 9; ++integer5) {
                this.addSlot(new Slot(ayi, integer5 + integer4 * 9 + 9, 8 + integer5 * 18, 84 + integer4 * 18));
            }
        }
        for (int integer4 = 0; integer4 < 9; ++integer4) {
            this.addSlot(new Slot(ayi, integer4, 8 + integer4 * 18, 142));
        }
    }
    
    @Override
    public boolean stillValid(final Player ayj) {
        return this.container.stillValid(ayj);
    }
    
    @Override
    public ItemStack quickMoveStack(final Player ayj, final int integer) {
        ItemStack ben4 = ItemStack.EMPTY;
        final Slot bca5 = this.slots.get(integer);
        if (bca5 != null && bca5.hasItem()) {
            final ItemStack ben5 = bca5.getItem();
            ben4 = ben5.copy();
            if (integer < this.container.getContainerSize()) {
                if (!this.moveItemStackTo(ben5, this.container.getContainerSize(), this.slots.size(), true)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (!this.moveItemStackTo(ben5, 0, this.container.getContainerSize(), false)) {
                return ItemStack.EMPTY;
            }
            if (ben5.isEmpty()) {
                bca5.set(ItemStack.EMPTY);
            }
            else {
                bca5.setChanged();
            }
        }
        return ben4;
    }
    
    @Override
    public void removed(final Player ayj) {
        super.removed(ayj);
        this.container.stopOpen(ayj);
    }
}
