package net.minecraft.client.gui.screens.inventory;

import net.minecraft.world.entity.player.Player;
import com.mojang.blaze3d.platform.InputConstants;
import net.minecraft.client.Minecraft;
import net.minecraft.world.inventory.ClickType;
import java.util.Iterator;
import com.mojang.datafixers.util.Pair;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.client.gui.GuiComponent;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.network.chat.FormattedText;
import net.minecraft.Util;
import net.minecraft.ChatFormatting;
import net.minecraft.util.Mth;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import com.google.common.collect.Sets;
import net.minecraft.network.chat.Component;
import java.util.Set;
import net.minecraft.world.item.ItemStack;
import javax.annotation.Nullable;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.world.inventory.AbstractContainerMenu;

public abstract class AbstractContainerScreen<T extends AbstractContainerMenu> extends Screen implements MenuAccess<T> {
    public static final ResourceLocation INVENTORY_LOCATION;
    protected int imageWidth;
    protected int imageHeight;
    protected int titleLabelX;
    protected int titleLabelY;
    protected int inventoryLabelX;
    protected int inventoryLabelY;
    protected final T menu;
    protected final Inventory inventory;
    @Nullable
    protected Slot hoveredSlot;
    @Nullable
    private Slot clickedSlot;
    @Nullable
    private Slot snapbackEnd;
    @Nullable
    private Slot quickdropSlot;
    @Nullable
    private Slot lastClickSlot;
    protected int leftPos;
    protected int topPos;
    private boolean isSplittingStack;
    private ItemStack draggingItem;
    private int snapbackStartX;
    private int snapbackStartY;
    private long snapbackTime;
    private ItemStack snapbackItem;
    private long quickdropTime;
    protected final Set<Slot> quickCraftSlots;
    protected boolean isQuickCrafting;
    private int quickCraftingType;
    private int quickCraftingButton;
    private boolean skipNextRelease;
    private int quickCraftingRemainder;
    private long lastClickTime;
    private int lastClickButton;
    private boolean doubleclick;
    private ItemStack lastQuickMoved;
    
    public AbstractContainerScreen(final T bgi, final Inventory beb, final Component mr) {
        super(mr);
        this.imageWidth = 176;
        this.imageHeight = 166;
        this.draggingItem = ItemStack.EMPTY;
        this.snapbackItem = ItemStack.EMPTY;
        this.quickCraftSlots = Sets.newHashSet();
        this.lastQuickMoved = ItemStack.EMPTY;
        this.menu = bgi;
        this.inventory = beb;
        this.skipNextRelease = true;
        this.titleLabelX = 8;
        this.titleLabelY = 6;
        this.inventoryLabelX = 8;
        this.inventoryLabelY = this.imageHeight - 94;
    }
    
    @Override
    protected void init() {
        super.init();
        this.leftPos = (this.width - this.imageWidth) / 2;
        this.topPos = (this.height - this.imageHeight) / 2;
    }
    
    @Override
    public void render(final PoseStack dhl, final int integer2, final int integer3, final float float4) {
        final int integer4 = this.leftPos;
        final int integer5 = this.topPos;
        this.renderBg(dhl, float4, integer2, integer3);
        RenderSystem.disableRescaleNormal();
        RenderSystem.disableDepthTest();
        super.render(dhl, integer2, integer3, float4);
        RenderSystem.pushMatrix();
        RenderSystem.translatef((float)integer4, (float)integer5, 0.0f);
        RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
        RenderSystem.enableRescaleNormal();
        this.hoveredSlot = null;
        final int integer6 = 240;
        final int integer7 = 240;
        RenderSystem.glMultiTexCoord2f(33986, 240.0f, 240.0f);
        RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
        for (int integer8 = 0; integer8 < this.menu.slots.size(); ++integer8) {
            final Slot bhw11 = this.menu.slots.get(integer8);
            if (bhw11.isActive()) {
                this.renderSlot(dhl, bhw11);
            }
            if (this.isHovering(bhw11, integer2, integer3) && bhw11.isActive()) {
                this.hoveredSlot = bhw11;
                RenderSystem.disableDepthTest();
                final int integer9 = bhw11.x;
                final int integer10 = bhw11.y;
                RenderSystem.colorMask(true, true, true, false);
                this.fillGradient(dhl, integer9, integer10, integer9 + 16, integer10 + 16, -2130706433, -2130706433);
                RenderSystem.colorMask(true, true, true, true);
                RenderSystem.enableDepthTest();
            }
        }
        this.renderLabels(dhl, integer2, integer3);
        final Inventory beb10 = this.minecraft.player.inventory;
        ItemStack bki11 = this.draggingItem.isEmpty() ? beb10.getCarried() : this.draggingItem;
        if (!bki11.isEmpty()) {
            final int integer9 = 8;
            final int integer10 = this.draggingItem.isEmpty() ? 8 : 16;
            String string14 = null;
            if (!this.draggingItem.isEmpty() && this.isSplittingStack) {
                bki11 = bki11.copy();
                bki11.setCount(Mth.ceil(bki11.getCount() / 2.0f));
            }
            else if (this.isQuickCrafting && this.quickCraftSlots.size() > 1) {
                bki11 = bki11.copy();
                bki11.setCount(this.quickCraftingRemainder);
                if (bki11.isEmpty()) {
                    string14 = "" + ChatFormatting.YELLOW + "0";
                }
            }
            this.renderFloatingItem(bki11, integer2 - integer4 - 8, integer3 - integer5 - integer10, string14);
        }
        if (!this.snapbackItem.isEmpty()) {
            float float5 = (Util.getMillis() - this.snapbackTime) / 100.0f;
            if (float5 >= 1.0f) {
                float5 = 1.0f;
                this.snapbackItem = ItemStack.EMPTY;
            }
            final int integer10 = this.snapbackEnd.x - this.snapbackStartX;
            final int integer11 = this.snapbackEnd.y - this.snapbackStartY;
            final int integer12 = this.snapbackStartX + (int)(integer10 * float5);
            final int integer13 = this.snapbackStartY + (int)(integer11 * float5);
            this.renderFloatingItem(this.snapbackItem, integer12, integer13, null);
        }
        RenderSystem.popMatrix();
        RenderSystem.enableDepthTest();
    }
    
    protected void renderTooltip(final PoseStack dhl, final int integer2, final int integer3) {
        if (this.minecraft.player.inventory.getCarried().isEmpty() && this.hoveredSlot != null && this.hoveredSlot.hasItem()) {
            this.renderTooltip(dhl, this.hoveredSlot.getItem(), integer2, integer3);
        }
    }
    
    private void renderFloatingItem(final ItemStack bki, final int integer2, final int integer3, final String string) {
        RenderSystem.translatef(0.0f, 0.0f, 32.0f);
        this.setBlitOffset(200);
        this.itemRenderer.blitOffset = 200.0f;
        this.itemRenderer.renderAndDecorateItem(bki, integer2, integer3);
        this.itemRenderer.renderGuiItemDecorations(this.font, bki, integer2, integer3 - (this.draggingItem.isEmpty() ? 0 : 8), string);
        this.setBlitOffset(0);
        this.itemRenderer.blitOffset = 0.0f;
    }
    
    protected void renderLabels(final PoseStack dhl, final int integer2, final int integer3) {
        this.font.draw(dhl, this.title, (float)this.titleLabelX, (float)this.titleLabelY, 4210752);
        this.font.draw(dhl, this.inventory.getDisplayName(), (float)this.inventoryLabelX, (float)this.inventoryLabelY, 4210752);
    }
    
    protected abstract void renderBg(final PoseStack dhl, final float float2, final int integer3, final int integer4);
    
    private void renderSlot(final PoseStack dhl, final Slot bhw) {
        final int integer4 = bhw.x;
        final int integer5 = bhw.y;
        ItemStack bki6 = bhw.getItem();
        boolean boolean7 = false;
        boolean boolean8 = bhw == this.clickedSlot && !this.draggingItem.isEmpty() && !this.isSplittingStack;
        final ItemStack bki7 = this.minecraft.player.inventory.getCarried();
        String string10 = null;
        if (bhw == this.clickedSlot && !this.draggingItem.isEmpty() && this.isSplittingStack && !bki6.isEmpty()) {
            bki6 = bki6.copy();
            bki6.setCount(bki6.getCount() / 2);
        }
        else if (this.isQuickCrafting && this.quickCraftSlots.contains(bhw) && !bki7.isEmpty()) {
            if (this.quickCraftSlots.size() == 1) {
                return;
            }
            if (AbstractContainerMenu.canItemQuickReplace(bhw, bki7, true) && this.menu.canDragTo(bhw)) {
                bki6 = bki7.copy();
                boolean7 = true;
                AbstractContainerMenu.getQuickCraftSlotCount(this.quickCraftSlots, this.quickCraftingType, bki6, bhw.getItem().isEmpty() ? 0 : bhw.getItem().getCount());
                final int integer6 = Math.min(bki6.getMaxStackSize(), bhw.getMaxStackSize(bki6));
                if (bki6.getCount() > integer6) {
                    string10 = ChatFormatting.YELLOW.toString() + integer6;
                    bki6.setCount(integer6);
                }
            }
            else {
                this.quickCraftSlots.remove(bhw);
                this.recalculateQuickCraftRemaining();
            }
        }
        this.setBlitOffset(100);
        this.itemRenderer.blitOffset = 100.0f;
        if (bki6.isEmpty() && bhw.isActive()) {
            final Pair<ResourceLocation, ResourceLocation> pair11 = bhw.getNoItemIcon();
            if (pair11 != null) {
                final TextureAtlasSprite elv12 = this.minecraft.getTextureAtlas((ResourceLocation)pair11.getFirst()).apply((ResourceLocation)pair11.getSecond());
                this.minecraft.getTextureManager().bind(elv12.atlas().location());
                GuiComponent.blit(dhl, integer4, integer5, this.getBlitOffset(), 16, 16, elv12);
                boolean8 = true;
            }
        }
        if (!boolean8) {
            if (boolean7) {
                GuiComponent.fill(dhl, integer4, integer5, integer4 + 16, integer5 + 16, -2130706433);
            }
            RenderSystem.enableDepthTest();
            this.itemRenderer.renderAndDecorateItem(this.minecraft.player, bki6, integer4, integer5);
            this.itemRenderer.renderGuiItemDecorations(this.font, bki6, integer4, integer5, string10);
        }
        this.itemRenderer.blitOffset = 0.0f;
        this.setBlitOffset(0);
    }
    
    private void recalculateQuickCraftRemaining() {
        final ItemStack bki2 = this.minecraft.player.inventory.getCarried();
        if (bki2.isEmpty() || !this.isQuickCrafting) {
            return;
        }
        if (this.quickCraftingType == 2) {
            this.quickCraftingRemainder = bki2.getMaxStackSize();
            return;
        }
        this.quickCraftingRemainder = bki2.getCount();
        for (final Slot bhw4 : this.quickCraftSlots) {
            final ItemStack bki3 = bki2.copy();
            final ItemStack bki4 = bhw4.getItem();
            final int integer7 = bki4.isEmpty() ? 0 : bki4.getCount();
            AbstractContainerMenu.getQuickCraftSlotCount(this.quickCraftSlots, this.quickCraftingType, bki3, integer7);
            final int integer8 = Math.min(bki3.getMaxStackSize(), bhw4.getMaxStackSize(bki3));
            if (bki3.getCount() > integer8) {
                bki3.setCount(integer8);
            }
            this.quickCraftingRemainder -= bki3.getCount() - integer7;
        }
    }
    
    @Nullable
    private Slot findSlot(final double double1, final double double2) {
        for (int integer6 = 0; integer6 < this.menu.slots.size(); ++integer6) {
            final Slot bhw7 = this.menu.slots.get(integer6);
            if (this.isHovering(bhw7, double1, double2) && bhw7.isActive()) {
                return bhw7;
            }
        }
        return null;
    }
    
    @Override
    public boolean mouseClicked(final double double1, final double double2, final int integer) {
        if (super.mouseClicked(double1, double2, integer)) {
            return true;
        }
        final boolean boolean7 = this.minecraft.options.keyPickItem.matchesMouse(integer);
        final Slot bhw8 = this.findSlot(double1, double2);
        final long long9 = Util.getMillis();
        this.doubleclick = (this.lastClickSlot == bhw8 && long9 - this.lastClickTime < 250L && this.lastClickButton == integer);
        this.skipNextRelease = false;
        if (integer == 0 || integer == 1 || boolean7) {
            final int integer2 = this.leftPos;
            final int integer3 = this.topPos;
            final boolean boolean8 = this.hasClickedOutside(double1, double2, integer2, integer3, integer);
            int integer4 = -1;
            if (bhw8 != null) {
                integer4 = bhw8.index;
            }
            if (boolean8) {
                integer4 = -999;
            }
            if (this.minecraft.options.touchscreen && boolean8 && this.minecraft.player.inventory.getCarried().isEmpty()) {
                this.minecraft.setScreen(null);
                return true;
            }
            if (integer4 != -1) {
                if (this.minecraft.options.touchscreen) {
                    if (bhw8 != null && bhw8.hasItem()) {
                        this.clickedSlot = bhw8;
                        this.draggingItem = ItemStack.EMPTY;
                        this.isSplittingStack = (integer == 1);
                    }
                    else {
                        this.clickedSlot = null;
                    }
                }
                else if (!this.isQuickCrafting) {
                    if (this.minecraft.player.inventory.getCarried().isEmpty()) {
                        if (this.minecraft.options.keyPickItem.matchesMouse(integer)) {
                            this.slotClicked(bhw8, integer4, integer, ClickType.CLONE);
                        }
                        else {
                            final boolean boolean9 = integer4 != -999 && (InputConstants.isKeyDown(Minecraft.getInstance().getWindow().getWindow(), 340) || InputConstants.isKeyDown(Minecraft.getInstance().getWindow().getWindow(), 344));
                            ClickType bgq16 = ClickType.PICKUP;
                            if (boolean9) {
                                this.lastQuickMoved = ((bhw8 != null && bhw8.hasItem()) ? bhw8.getItem().copy() : ItemStack.EMPTY);
                                bgq16 = ClickType.QUICK_MOVE;
                            }
                            else if (integer4 == -999) {
                                bgq16 = ClickType.THROW;
                            }
                            this.slotClicked(bhw8, integer4, integer, bgq16);
                        }
                        this.skipNextRelease = true;
                    }
                    else {
                        this.isQuickCrafting = true;
                        this.quickCraftingButton = integer;
                        this.quickCraftSlots.clear();
                        if (integer == 0) {
                            this.quickCraftingType = 0;
                        }
                        else if (integer == 1) {
                            this.quickCraftingType = 1;
                        }
                        else if (this.minecraft.options.keyPickItem.matchesMouse(integer)) {
                            this.quickCraftingType = 2;
                        }
                    }
                }
            }
        }
        else {
            this.checkHotbarMouseClicked(integer);
        }
        this.lastClickSlot = bhw8;
        this.lastClickTime = long9;
        this.lastClickButton = integer;
        return true;
    }
    
    private void checkHotbarMouseClicked(final int integer) {
        if (this.hoveredSlot != null && this.minecraft.player.inventory.getCarried().isEmpty()) {
            if (this.minecraft.options.keySwapOffhand.matchesMouse(integer)) {
                this.slotClicked(this.hoveredSlot, this.hoveredSlot.index, 40, ClickType.SWAP);
                return;
            }
            for (int integer2 = 0; integer2 < 9; ++integer2) {
                if (this.minecraft.options.keyHotbarSlots[integer2].matchesMouse(integer)) {
                    this.slotClicked(this.hoveredSlot, this.hoveredSlot.index, integer2, ClickType.SWAP);
                }
            }
        }
    }
    
    protected boolean hasClickedOutside(final double double1, final double double2, final int integer3, final int integer4, final int integer5) {
        return double1 < integer3 || double2 < integer4 || double1 >= integer3 + this.imageWidth || double2 >= integer4 + this.imageHeight;
    }
    
    @Override
    public boolean mouseDragged(final double double1, final double double2, final int integer, final double double4, final double double5) {
        final Slot bhw11 = this.findSlot(double1, double2);
        final ItemStack bki12 = this.minecraft.player.inventory.getCarried();
        if (this.clickedSlot != null && this.minecraft.options.touchscreen) {
            if (integer == 0 || integer == 1) {
                if (this.draggingItem.isEmpty()) {
                    if (bhw11 != this.clickedSlot && !this.clickedSlot.getItem().isEmpty()) {
                        this.draggingItem = this.clickedSlot.getItem().copy();
                    }
                }
                else if (this.draggingItem.getCount() > 1 && bhw11 != null && AbstractContainerMenu.canItemQuickReplace(bhw11, this.draggingItem, false)) {
                    final long long13 = Util.getMillis();
                    if (this.quickdropSlot == bhw11) {
                        if (long13 - this.quickdropTime > 500L) {
                            this.slotClicked(this.clickedSlot, this.clickedSlot.index, 0, ClickType.PICKUP);
                            this.slotClicked(bhw11, bhw11.index, 1, ClickType.PICKUP);
                            this.slotClicked(this.clickedSlot, this.clickedSlot.index, 0, ClickType.PICKUP);
                            this.quickdropTime = long13 + 750L;
                            this.draggingItem.shrink(1);
                        }
                    }
                    else {
                        this.quickdropSlot = bhw11;
                        this.quickdropTime = long13;
                    }
                }
            }
        }
        else if (this.isQuickCrafting && bhw11 != null && !bki12.isEmpty() && (bki12.getCount() > this.quickCraftSlots.size() || this.quickCraftingType == 2) && AbstractContainerMenu.canItemQuickReplace(bhw11, bki12, true) && bhw11.mayPlace(bki12) && this.menu.canDragTo(bhw11)) {
            this.quickCraftSlots.add(bhw11);
            this.recalculateQuickCraftRemaining();
        }
        return true;
    }
    
    @Override
    public boolean mouseReleased(final double double1, final double double2, final int integer) {
        final Slot bhw7 = this.findSlot(double1, double2);
        final int integer2 = this.leftPos;
        final int integer3 = this.topPos;
        final boolean boolean10 = this.hasClickedOutside(double1, double2, integer2, integer3, integer);
        int integer4 = -1;
        if (bhw7 != null) {
            integer4 = bhw7.index;
        }
        if (boolean10) {
            integer4 = -999;
        }
        if (this.doubleclick && bhw7 != null && integer == 0 && this.menu.canTakeItemForPickAll(ItemStack.EMPTY, bhw7)) {
            if (hasShiftDown()) {
                if (!this.lastQuickMoved.isEmpty()) {
                    for (final Slot bhw8 : this.menu.slots) {
                        if (bhw8 != null && bhw8.mayPickup(this.minecraft.player) && bhw8.hasItem() && bhw8.container == bhw7.container && AbstractContainerMenu.canItemQuickReplace(bhw8, this.lastQuickMoved, true)) {
                            this.slotClicked(bhw8, bhw8.index, integer, ClickType.QUICK_MOVE);
                        }
                    }
                }
            }
            else {
                this.slotClicked(bhw7, integer4, integer, ClickType.PICKUP_ALL);
            }
            this.doubleclick = false;
            this.lastClickTime = 0L;
        }
        else {
            if (this.isQuickCrafting && this.quickCraftingButton != integer) {
                this.isQuickCrafting = false;
                this.quickCraftSlots.clear();
                return this.skipNextRelease = true;
            }
            if (this.skipNextRelease) {
                this.skipNextRelease = false;
                return true;
            }
            if (this.clickedSlot != null && this.minecraft.options.touchscreen) {
                if (integer == 0 || integer == 1) {
                    if (this.draggingItem.isEmpty() && bhw7 != this.clickedSlot) {
                        this.draggingItem = this.clickedSlot.getItem();
                    }
                    final boolean boolean11 = AbstractContainerMenu.canItemQuickReplace(bhw7, this.draggingItem, false);
                    if (integer4 != -1 && !this.draggingItem.isEmpty() && boolean11) {
                        this.slotClicked(this.clickedSlot, this.clickedSlot.index, integer, ClickType.PICKUP);
                        this.slotClicked(bhw7, integer4, 0, ClickType.PICKUP);
                        if (this.minecraft.player.inventory.getCarried().isEmpty()) {
                            this.snapbackItem = ItemStack.EMPTY;
                        }
                        else {
                            this.slotClicked(this.clickedSlot, this.clickedSlot.index, integer, ClickType.PICKUP);
                            this.snapbackStartX = Mth.floor(double1 - integer2);
                            this.snapbackStartY = Mth.floor(double2 - integer3);
                            this.snapbackEnd = this.clickedSlot;
                            this.snapbackItem = this.draggingItem;
                            this.snapbackTime = Util.getMillis();
                        }
                    }
                    else if (!this.draggingItem.isEmpty()) {
                        this.snapbackStartX = Mth.floor(double1 - integer2);
                        this.snapbackStartY = Mth.floor(double2 - integer3);
                        this.snapbackEnd = this.clickedSlot;
                        this.snapbackItem = this.draggingItem;
                        this.snapbackTime = Util.getMillis();
                    }
                    this.draggingItem = ItemStack.EMPTY;
                    this.clickedSlot = null;
                }
            }
            else if (this.isQuickCrafting && !this.quickCraftSlots.isEmpty()) {
                this.slotClicked(null, -999, AbstractContainerMenu.getQuickcraftMask(0, this.quickCraftingType), ClickType.QUICK_CRAFT);
                for (final Slot bhw8 : this.quickCraftSlots) {
                    this.slotClicked(bhw8, bhw8.index, AbstractContainerMenu.getQuickcraftMask(1, this.quickCraftingType), ClickType.QUICK_CRAFT);
                }
                this.slotClicked(null, -999, AbstractContainerMenu.getQuickcraftMask(2, this.quickCraftingType), ClickType.QUICK_CRAFT);
            }
            else if (!this.minecraft.player.inventory.getCarried().isEmpty()) {
                if (this.minecraft.options.keyPickItem.matchesMouse(integer)) {
                    this.slotClicked(bhw7, integer4, integer, ClickType.CLONE);
                }
                else {
                    final boolean boolean11 = integer4 != -999 && (InputConstants.isKeyDown(Minecraft.getInstance().getWindow().getWindow(), 340) || InputConstants.isKeyDown(Minecraft.getInstance().getWindow().getWindow(), 344));
                    if (boolean11) {
                        this.lastQuickMoved = ((bhw7 != null && bhw7.hasItem()) ? bhw7.getItem().copy() : ItemStack.EMPTY);
                    }
                    this.slotClicked(bhw7, integer4, integer, boolean11 ? ClickType.QUICK_MOVE : ClickType.PICKUP);
                }
            }
        }
        if (this.minecraft.player.inventory.getCarried().isEmpty()) {
            this.lastClickTime = 0L;
        }
        this.isQuickCrafting = false;
        return true;
    }
    
    private boolean isHovering(final Slot bhw, final double double2, final double double3) {
        return this.isHovering(bhw.x, bhw.y, 16, 16, double2, double3);
    }
    
    protected boolean isHovering(final int integer1, final int integer2, final int integer3, final int integer4, double double5, double double6) {
        final int integer5 = this.leftPos;
        final int integer6 = this.topPos;
        double5 -= integer5;
        double6 -= integer6;
        return double5 >= integer1 - 1 && double5 < integer1 + integer3 + 1 && double6 >= integer2 - 1 && double6 < integer2 + integer4 + 1;
    }
    
    protected void slotClicked(final Slot bhw, int integer2, final int integer3, final ClickType bgq) {
        if (bhw != null) {
            integer2 = bhw.index;
        }
        this.minecraft.gameMode.handleInventoryMouseClick(this.menu.containerId, integer2, integer3, bgq, this.minecraft.player);
    }
    
    @Override
    public boolean shouldCloseOnEsc() {
        return false;
    }
    
    @Override
    public boolean keyPressed(final int integer1, final int integer2, final int integer3) {
        if (super.keyPressed(integer1, integer2, integer3)) {
            return true;
        }
        if (integer1 == 256 || this.minecraft.options.keyInventory.matches(integer1, integer2)) {
            this.minecraft.player.closeContainer();
            return true;
        }
        this.checkHotbarKeyPressed(integer1, integer2);
        if (this.hoveredSlot != null && this.hoveredSlot.hasItem()) {
            if (this.minecraft.options.keyPickItem.matches(integer1, integer2)) {
                this.slotClicked(this.hoveredSlot, this.hoveredSlot.index, 0, ClickType.CLONE);
            }
            else if (this.minecraft.options.keyDrop.matches(integer1, integer2)) {
                this.slotClicked(this.hoveredSlot, this.hoveredSlot.index, Screen.hasControlDown() ? 1 : 0, ClickType.THROW);
            }
        }
        return true;
    }
    
    protected boolean checkHotbarKeyPressed(final int integer1, final int integer2) {
        if (this.minecraft.player.inventory.getCarried().isEmpty() && this.hoveredSlot != null) {
            if (this.minecraft.options.keySwapOffhand.matches(integer1, integer2)) {
                this.slotClicked(this.hoveredSlot, this.hoveredSlot.index, 40, ClickType.SWAP);
                return true;
            }
            for (int integer3 = 0; integer3 < 9; ++integer3) {
                if (this.minecraft.options.keyHotbarSlots[integer3].matches(integer1, integer2)) {
                    this.slotClicked(this.hoveredSlot, this.hoveredSlot.index, integer3, ClickType.SWAP);
                    return true;
                }
            }
        }
        return false;
    }
    
    @Override
    public void removed() {
        if (this.minecraft.player == null) {
            return;
        }
        this.menu.removed(this.minecraft.player);
    }
    
    @Override
    public boolean isPauseScreen() {
        return false;
    }
    
    @Override
    public void tick() {
        super.tick();
        if (!this.minecraft.player.isAlive() || this.minecraft.player.removed) {
            this.minecraft.player.closeContainer();
        }
    }
    
    @Override
    public T getMenu() {
        return this.menu;
    }
    
    static {
        INVENTORY_LOCATION = new ResourceLocation("textures/gui/container/inventory.png");
    }
}
