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 HopperMenu extends AbstractContainerMenu {
    private final Container hopper;
    
    public HopperMenu(final int integer, final Inventory ayi) {
        this(integer, ayi, new SimpleContainer(5));
    }
    
    public HopperMenu(final int integer, final Inventory ayi, final Container aje) {
        super(MenuType.HOPPER, integer);
        AbstractContainerMenu.checkContainerSize(this.hopper = aje, 5);
        aje.startOpen(ayi.player);
        final int integer2 = 51;
        for (int integer3 = 0; integer3 < 5; ++integer3) {
            this.addSlot(new Slot(aje, integer3, 44 + integer3 * 18, 20));
        }
        for (int integer3 = 0; integer3 < 3; ++integer3) {
            for (int integer4 = 0; integer4 < 9; ++integer4) {
                this.addSlot(new Slot(ayi, integer4 + integer3 * 9 + 9, 8 + integer4 * 18, integer3 * 18 + 51));
            }
        }
        for (int integer3 = 0; integer3 < 9; ++integer3) {
            this.addSlot(new Slot(ayi, integer3, 8 + integer3 * 18, 109));
        }
    }
    
    @Override
    public boolean stillValid(final Player ayj) {
        return this.hopper.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.hopper.getContainerSize()) {
                if (!this.moveItemStackTo(ben5, this.hopper.getContainerSize(), this.slots.size(), true)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (!this.moveItemStackTo(ben5, 0, this.hopper.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.hopper.stopOpen(ayj);
    }
}
