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 beb) {
        this(integer, beb, new SimpleContainer(5));
    }
    
    public HopperMenu(final int integer, final Inventory beb, final Container amz) {
        super(MenuType.HOPPER, integer);
        AbstractContainerMenu.checkContainerSize(this.hopper = amz, 5);
        amz.startOpen(beb.player);
        final int integer2 = 51;
        for (int integer3 = 0; integer3 < 5; ++integer3) {
            this.addSlot(new Slot(amz, integer3, 44 + integer3 * 18, 20));
        }
        for (int integer3 = 0; integer3 < 3; ++integer3) {
            for (int integer4 = 0; integer4 < 9; ++integer4) {
                this.addSlot(new Slot(beb, integer4 + integer3 * 9 + 9, 8 + integer4 * 18, integer3 * 18 + 51));
            }
        }
        for (int integer3 = 0; integer3 < 9; ++integer3) {
            this.addSlot(new Slot(beb, integer3, 8 + integer3 * 18, 109));
        }
    }
    
    @Override
    public boolean stillValid(final Player bec) {
        return this.hopper.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 < this.hopper.getContainerSize()) {
                if (!this.moveItemStackTo(bki5, this.hopper.getContainerSize(), this.slots.size(), true)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (!this.moveItemStackTo(bki5, 0, this.hopper.getContainerSize(), false)) {
                return ItemStack.EMPTY;
            }
            if (bki5.isEmpty()) {
                bhw5.set(ItemStack.EMPTY);
            }
            else {
                bhw5.setChanged();
            }
        }
        return bki4;
    }
    
    @Override
    public void removed(final Player bec) {
        super.removed(bec);
        this.hopper.stopOpen(bec);
    }
}
