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 DispenserMenu extends AbstractContainerMenu {
    private final Container dispenser;
    
    public DispenserMenu(final int integer, final Inventory beb) {
        this(integer, beb, new SimpleContainer(9));
    }
    
    public DispenserMenu(final int integer, final Inventory beb, final Container amz) {
        super(MenuType.GENERIC_3x3, integer);
        AbstractContainerMenu.checkContainerSize(amz, 9);
        (this.dispenser = amz).startOpen(beb.player);
        for (int integer2 = 0; integer2 < 3; ++integer2) {
            for (int integer3 = 0; integer3 < 3; ++integer3) {
                this.addSlot(new Slot(amz, integer3 + integer2 * 3, 62 + integer3 * 18, 17 + integer2 * 18));
            }
        }
        for (int integer2 = 0; integer2 < 3; ++integer2) {
            for (int integer3 = 0; integer3 < 9; ++integer3) {
                this.addSlot(new Slot(beb, integer3 + integer2 * 9 + 9, 8 + integer3 * 18, 84 + integer2 * 18));
            }
        }
        for (int integer2 = 0; integer2 < 9; ++integer2) {
            this.addSlot(new Slot(beb, integer2, 8 + integer2 * 18, 142));
        }
    }
    
    @Override
    public boolean stillValid(final Player bec) {
        return this.dispenser.stillValid(bec);
    }
    
    @Override
    public ItemStack quickMoveStack(final Player bec, final int integer) {
        ItemStack bki4 = ItemStack.EMPTY;
        final Slot bhw5 = this.slots.get(integer);
        if (bhw5 != null && bhw5.hasItem()) {
            final ItemStack bki5 = bhw5.getItem();
            bki4 = bki5.copy();
            if (integer < 9) {
                if (!this.moveItemStackTo(bki5, 9, 45, true)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (!this.moveItemStackTo(bki5, 0, 9, false)) {
                return ItemStack.EMPTY;
            }
            if (bki5.isEmpty()) {
                bhw5.set(ItemStack.EMPTY);
            }
            else {
                bhw5.setChanged();
            }
            if (bki5.getCount() == bki4.getCount()) {
                return ItemStack.EMPTY;
            }
            bhw5.onTake(bec, bki5);
        }
        return bki4;
    }
    
    @Override
    public void removed(final Player bec) {
        super.removed(bec);
        this.dispenser.stopOpen(bec);
    }
}
