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

import com.mojang.datafixers.util.Pair;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.core.NonNullList;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.ChatFormatting;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import com.google.common.collect.Lists;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.util.Mth;
import net.minecraft.client.player.inventory.Hotbar;
import net.minecraft.client.HotbarManager;
import net.minecraft.world.Container;
import com.google.common.collect.ImmutableList;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.item.Items;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.tags.TagCollection;
import java.util.function.Predicate;
import net.minecraft.tags.ItemTags;
import net.minecraft.client.searchtree.SearchTree;
import java.util.Iterator;
import java.util.Collection;
import java.util.Locale;
import net.minecraft.client.searchtree.SearchRegistry;
import net.minecraft.core.Registry;
import java.util.Objects;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.world.inventory.ContainerListener;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.inventory.ClickType;
import net.minecraft.client.gui.screens.Screen;
import com.google.common.collect.Maps;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.tags.Tag;
import java.util.Map;
import javax.annotation.Nullable;
import net.minecraft.world.inventory.Slot;
import java.util.List;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.world.SimpleContainer;
import net.minecraft.resources.ResourceLocation;

public class CreativeModeInventoryScreen extends EffectRenderingInventoryScreen<ItemPickerMenu> {
    private static final ResourceLocation CREATIVE_TABS_LOCATION;
    private static final SimpleContainer CONTAINER;
    private static int selectedTab;
    private float scrollOffs;
    private boolean scrolling;
    private EditBox searchBox;
    @Nullable
    private List<Slot> originalSlots;
    @Nullable
    private Slot destroyItemSlot;
    private CreativeInventoryListener listener;
    private boolean ignoreTextInput;
    private boolean hasClickedOutside;
    private final Map<ResourceLocation, Tag<Item>> visibleTags;
    
    public CreativeModeInventoryScreen(final Player ayj) {
        super(new ItemPickerMenu(ayj), ayj.inventory, new TextComponent(""));
        this.visibleTags = Maps.newTreeMap();
        ayj.containerMenu = this.menu;
        this.passEvents = true;
        this.imageHeight = 136;
        this.imageWidth = 195;
    }
    
    @Override
    public void tick() {
        if (!this.minecraft.gameMode.hasInfiniteItems()) {
            this.minecraft.setScreen(new InventoryScreen(this.minecraft.player));
        }
        else if (this.searchBox != null) {
            this.searchBox.tick();
        }
    }
    
    @Override
    protected void slotClicked(@Nullable final Slot bca, final int integer2, final int integer3, ClickType bav) {
        if (this.isCreativeSlot(bca)) {
            this.searchBox.moveCursorToEnd();
            this.searchBox.setHighlightPos(0);
        }
        final boolean boolean6 = bav == ClickType.QUICK_MOVE;
        bav = ((integer2 == -999 && bav == ClickType.PICKUP) ? ClickType.THROW : bav);
        if (bca != null || CreativeModeInventoryScreen.selectedTab == CreativeModeTab.TAB_INVENTORY.getId() || bav == ClickType.QUICK_CRAFT) {
            if (bca != null && !bca.mayPickup(this.minecraft.player)) {
                return;
            }
            if (bca == this.destroyItemSlot && boolean6) {
                for (int integer4 = 0; integer4 < this.minecraft.player.inventoryMenu.getItems().size(); ++integer4) {
                    this.minecraft.gameMode.handleCreativeModeItemAdd(ItemStack.EMPTY, integer4);
                }
            }
            else if (CreativeModeInventoryScreen.selectedTab == CreativeModeTab.TAB_INVENTORY.getId()) {
                if (bca == this.destroyItemSlot) {
                    this.minecraft.player.inventory.setCarried(ItemStack.EMPTY);
                }
                else if (bav == ClickType.THROW && bca != null && bca.hasItem()) {
                    final ItemStack ben7 = bca.remove((integer3 == 0) ? 1 : bca.getItem().getMaxStackSize());
                    final ItemStack ben8 = bca.getItem();
                    this.minecraft.player.drop(ben7, true);
                    this.minecraft.gameMode.handleCreativeModeItemDrop(ben7);
                    this.minecraft.gameMode.handleCreativeModeItemAdd(ben8, ((SlotWrapper)bca).target.index);
                }
                else if (bav == ClickType.THROW && !this.minecraft.player.inventory.getCarried().isEmpty()) {
                    this.minecraft.player.drop(this.minecraft.player.inventory.getCarried(), true);
                    this.minecraft.gameMode.handleCreativeModeItemDrop(this.minecraft.player.inventory.getCarried());
                    this.minecraft.player.inventory.setCarried(ItemStack.EMPTY);
                }
                else {
                    this.minecraft.player.inventoryMenu.clicked((bca == null) ? integer2 : ((SlotWrapper)bca).target.index, integer3, bav, this.minecraft.player);
                    this.minecraft.player.inventoryMenu.broadcastChanges();
                }
            }
            else if (bav != ClickType.QUICK_CRAFT && bca.container == CreativeModeInventoryScreen.CONTAINER) {
                final Inventory ayi7 = this.minecraft.player.inventory;
                ItemStack ben8 = ayi7.getCarried();
                final ItemStack ben9 = bca.getItem();
                if (bav == ClickType.SWAP) {
                    if (!ben9.isEmpty() && integer3 >= 0 && integer3 < 9) {
                        final ItemStack ben10 = ben9.copy();
                        ben10.setCount(ben10.getMaxStackSize());
                        this.minecraft.player.inventory.setItem(integer3, ben10);
                        this.minecraft.player.inventoryMenu.broadcastChanges();
                    }
                    return;
                }
                if (bav == ClickType.CLONE) {
                    if (ayi7.getCarried().isEmpty() && bca.hasItem()) {
                        final ItemStack ben10 = bca.getItem().copy();
                        ben10.setCount(ben10.getMaxStackSize());
                        ayi7.setCarried(ben10);
                    }
                    return;
                }
                if (bav == ClickType.THROW) {
                    if (!ben9.isEmpty()) {
                        final ItemStack ben10 = ben9.copy();
                        ben10.setCount((integer3 == 0) ? 1 : ben10.getMaxStackSize());
                        this.minecraft.player.drop(ben10, true);
                        this.minecraft.gameMode.handleCreativeModeItemDrop(ben10);
                    }
                    return;
                }
                if (!ben8.isEmpty() && !ben9.isEmpty() && ben8.sameItem(ben9) && ItemStack.tagMatches(ben8, ben9)) {
                    if (integer3 == 0) {
                        if (boolean6) {
                            ben8.setCount(ben8.getMaxStackSize());
                        }
                        else if (ben8.getCount() < ben8.getMaxStackSize()) {
                            ben8.grow(1);
                        }
                    }
                    else {
                        ben8.shrink(1);
                    }
                }
                else if (ben9.isEmpty() || !ben8.isEmpty()) {
                    if (integer3 == 0) {
                        ayi7.setCarried(ItemStack.EMPTY);
                    }
                    else {
                        ayi7.getCarried().shrink(1);
                    }
                }
                else {
                    ayi7.setCarried(ben9.copy());
                    ben8 = ayi7.getCarried();
                    if (boolean6) {
                        ben8.setCount(ben8.getMaxStackSize());
                    }
                }
            }
            else if (this.menu != null) {
                final ItemStack ben7 = (bca == null) ? ItemStack.EMPTY : ((ItemPickerMenu)this.menu).getSlot(bca.index).getItem();
                ((ItemPickerMenu)this.menu).clicked((bca == null) ? integer2 : bca.index, integer3, bav, this.minecraft.player);
                if (AbstractContainerMenu.getQuickcraftHeader(integer3) == 2) {
                    for (int integer5 = 0; integer5 < 9; ++integer5) {
                        this.minecraft.gameMode.handleCreativeModeItemAdd(((ItemPickerMenu)this.menu).getSlot(45 + integer5).getItem(), 36 + integer5);
                    }
                }
                else if (bca != null) {
                    final ItemStack ben8 = ((ItemPickerMenu)this.menu).getSlot(bca.index).getItem();
                    this.minecraft.gameMode.handleCreativeModeItemAdd(ben8, bca.index - ((ItemPickerMenu)this.menu).slots.size() + 9 + 36);
                    final int integer6 = 45 + integer3;
                    if (bav == ClickType.SWAP) {
                        this.minecraft.gameMode.handleCreativeModeItemAdd(ben7, integer6 - ((ItemPickerMenu)this.menu).slots.size() + 9 + 36);
                    }
                    else if (bav == ClickType.THROW && !ben7.isEmpty()) {
                        final ItemStack ben10 = ben7.copy();
                        ben10.setCount((integer3 == 0) ? 1 : ben10.getMaxStackSize());
                        this.minecraft.player.drop(ben10, true);
                        this.minecraft.gameMode.handleCreativeModeItemDrop(ben10);
                    }
                    this.minecraft.player.inventoryMenu.broadcastChanges();
                }
            }
        }
        else {
            final Inventory ayi7 = this.minecraft.player.inventory;
            if (!ayi7.getCarried().isEmpty() && this.hasClickedOutside) {
                if (integer3 == 0) {
                    this.minecraft.player.drop(ayi7.getCarried(), true);
                    this.minecraft.gameMode.handleCreativeModeItemDrop(ayi7.getCarried());
                    ayi7.setCarried(ItemStack.EMPTY);
                }
                if (integer3 == 1) {
                    final ItemStack ben8 = ayi7.getCarried().split(1);
                    this.minecraft.player.drop(ben8, true);
                    this.minecraft.gameMode.handleCreativeModeItemDrop(ben8);
                }
            }
        }
    }
    
    private boolean isCreativeSlot(@Nullable final Slot bca) {
        return bca != null && bca.container == CreativeModeInventoryScreen.CONTAINER;
    }
    
    @Override
    protected void checkEffectRendering() {
        final int integer2 = this.leftPos;
        super.checkEffectRendering();
        if (this.searchBox != null && this.leftPos != integer2) {
            this.searchBox.setX(this.leftPos + 82);
        }
    }
    
    @Override
    protected void init() {
        if (this.minecraft.gameMode.hasInfiniteItems()) {
            super.init();
            this.minecraft.keyboardHandler.setSendRepeatsToGui(true);
            final Font font = this.font;
            final int integer3 = this.leftPos + 82;
            final int integer4 = this.topPos + 6;
            final int integer5 = 80;
            this.font.getClass();
            (this.searchBox = new EditBox(font, integer3, integer4, integer5, 9, I18n.get("itemGroup.search"))).setMaxLength(50);
            this.searchBox.setBordered(false);
            this.searchBox.setVisible(false);
            this.searchBox.setTextColor(16777215);
            this.children.add(this.searchBox);
            final int integer2 = CreativeModeInventoryScreen.selectedTab;
            CreativeModeInventoryScreen.selectedTab = -1;
            this.selectTab(CreativeModeTab.TABS[integer2]);
            this.minecraft.player.inventoryMenu.removeSlotListener(this.listener);
            this.listener = new CreativeInventoryListener(this.minecraft);
            this.minecraft.player.inventoryMenu.addSlotListener(this.listener);
        }
        else {
            this.minecraft.setScreen(new InventoryScreen(this.minecraft.player));
        }
    }
    
    @Override
    public void resize(final Minecraft dbn, final int integer2, final int integer3) {
        final String string5 = this.searchBox.getValue();
        this.init(dbn, integer2, integer3);
        this.searchBox.setValue(string5);
        if (!this.searchBox.getValue().isEmpty()) {
            this.refreshSearchResults();
        }
    }
    
    @Override
    public void removed() {
        super.removed();
        if (this.minecraft.player != null && this.minecraft.player.inventory != null) {
            this.minecraft.player.inventoryMenu.removeSlotListener(this.listener);
        }
        this.minecraft.keyboardHandler.setSendRepeatsToGui(false);
    }
    
    @Override
    public boolean charTyped(final char character, final int integer) {
        if (this.ignoreTextInput) {
            return false;
        }
        if (CreativeModeInventoryScreen.selectedTab != CreativeModeTab.TAB_SEARCH.getId()) {
            return false;
        }
        final String string4 = this.searchBox.getValue();
        if (this.searchBox.charTyped(character, integer)) {
            if (!Objects.equals(string4, this.searchBox.getValue())) {
                this.refreshSearchResults();
            }
            return true;
        }
        return false;
    }
    
    @Override
    public boolean keyPressed(final int integer1, final int integer2, final int integer3) {
        this.ignoreTextInput = false;
        if (CreativeModeInventoryScreen.selectedTab != CreativeModeTab.TAB_SEARCH.getId()) {
            if (this.minecraft.options.keyChat.matches(integer1, integer2)) {
                this.ignoreTextInput = true;
                this.selectTab(CreativeModeTab.TAB_SEARCH);
                return true;
            }
            return super.keyPressed(integer1, integer2, integer3);
        }
        else {
            final boolean boolean5 = !this.isCreativeSlot(this.hoveredSlot) || (this.hoveredSlot != null && this.hoveredSlot.hasItem());
            if (boolean5 && this.checkNumkeyPressed(integer1, integer2)) {
                return this.ignoreTextInput = true;
            }
            final String string6 = this.searchBox.getValue();
            if (this.searchBox.keyPressed(integer1, integer2, integer3)) {
                if (!Objects.equals(string6, this.searchBox.getValue())) {
                    this.refreshSearchResults();
                }
                return true;
            }
            return (this.searchBox.isFocused() && this.searchBox.isVisible() && integer1 != 256) || super.keyPressed(integer1, integer2, integer3);
        }
    }
    
    @Override
    public boolean keyReleased(final int integer1, final int integer2, final int integer3) {
        this.ignoreTextInput = false;
        return super.keyReleased(integer1, integer2, integer3);
    }
    
    private void refreshSearchResults() {
        ((ItemPickerMenu)this.menu).items.clear();
        this.visibleTags.clear();
        String string2 = this.searchBox.getValue();
        if (string2.isEmpty()) {
            for (final Item bei4 : Registry.ITEM) {
                bei4.fillItemCategory(CreativeModeTab.TAB_SEARCH, ((ItemPickerMenu)this.menu).items);
            }
        }
        else {
            SearchTree<ItemStack> edi3;
            if (string2.startsWith("#")) {
                string2 = string2.substring(1);
                edi3 = this.minecraft.<ItemStack>getSearchTree(SearchRegistry.CREATIVE_TAGS);
                this.updateVisibleTags(string2);
            }
            else {
                edi3 = this.minecraft.<ItemStack>getSearchTree(SearchRegistry.CREATIVE_NAMES);
            }
            ((ItemPickerMenu)this.menu).items.addAll(edi3.search(string2.toLowerCase(Locale.ROOT)));
        }
        this.scrollOffs = 0.0f;
        ((ItemPickerMenu)this.menu).scrollTo(0.0f);
    }
    
    private void updateVisibleTags(final String string) {
        final int integer3 = string.indexOf(58);
        Predicate<ResourceLocation> predicate4;
        if (integer3 == -1) {
            predicate4 = (sm -> sm.getPath().contains(string));
        }
        else {
            final String string2 = string.substring(0, integer3).trim();
            final String string3 = string.substring(integer3 + 1).trim();
            final CharSequence s;
            final CharSequence s2;
            predicate4 = (sm -> sm.getNamespace().contains(s) && sm.getPath().contains(s2));
        }
        final TagCollection<Item> aba5 = ItemTags.getAllTags();
        final Tag<Item> tag;
        aba5.getAvailableTags().stream().filter(predicate4).forEach(sm -> tag = this.visibleTags.put(sm, aba5.getTag(sm)));
    }
    
    @Override
    protected void renderLabels(final int integer1, final int integer2) {
        final CreativeModeTab bdd4 = CreativeModeTab.TABS[CreativeModeInventoryScreen.selectedTab];
        if (bdd4.showTitle()) {
            RenderSystem.disableBlend();
            this.font.draw(I18n.get(bdd4.getName()), 8.0f, 6.0f, 4210752);
        }
    }
    
    @Override
    public boolean mouseClicked(final double double1, final double double2, final int integer) {
        if (integer == 0) {
            final double double3 = double1 - this.leftPos;
            final double double4 = double2 - this.topPos;
            for (final CreativeModeTab bdd14 : CreativeModeTab.TABS) {
                if (this.checkTabClicked(bdd14, double3, double4)) {
                    return true;
                }
            }
            if (CreativeModeInventoryScreen.selectedTab != CreativeModeTab.TAB_INVENTORY.getId() && this.insideScrollbar(double1, double2)) {
                this.scrolling = this.canScroll();
                return true;
            }
        }
        return super.mouseClicked(double1, double2, integer);
    }
    
    @Override
    public boolean mouseReleased(final double double1, final double double2, final int integer) {
        if (integer == 0) {
            final double double3 = double1 - this.leftPos;
            final double double4 = double2 - this.topPos;
            this.scrolling = false;
            for (final CreativeModeTab bdd14 : CreativeModeTab.TABS) {
                if (this.checkTabClicked(bdd14, double3, double4)) {
                    this.selectTab(bdd14);
                    return true;
                }
            }
        }
        return super.mouseReleased(double1, double2, integer);
    }
    
    private boolean canScroll() {
        return CreativeModeInventoryScreen.selectedTab != CreativeModeTab.TAB_INVENTORY.getId() && CreativeModeTab.TABS[CreativeModeInventoryScreen.selectedTab].canScroll() && ((ItemPickerMenu)this.menu).canScroll();
    }
    
    private void selectTab(final CreativeModeTab bdd) {
        final int integer3 = CreativeModeInventoryScreen.selectedTab;
        CreativeModeInventoryScreen.selectedTab = bdd.getId();
        this.quickCraftSlots.clear();
        ((ItemPickerMenu)this.menu).items.clear();
        if (bdd == CreativeModeTab.TAB_HOTBAR) {
            final HotbarManager dbj4 = this.minecraft.getHotbarManager();
            for (int integer4 = 0; integer4 < 9; ++integer4) {
                final Hotbar dqd6 = dbj4.get(integer4);
                if (dqd6.isEmpty()) {
                    for (int integer5 = 0; integer5 < 9; ++integer5) {
                        if (integer5 == integer4) {
                            final ItemStack ben8 = new ItemStack(Items.PAPER);
                            ben8.getOrCreateTagElement("CustomCreativeLock");
                            final String string9 = this.minecraft.options.keyHotbarSlots[integer4].getTranslatedKeyMessage();
                            final String string10 = this.minecraft.options.keySaveHotbarActivator.getTranslatedKeyMessage();
                            ben8.setHoverName(new TranslatableComponent("inventory.hotbarInfo", new Object[] { string10, string9 }));
                            ((ItemPickerMenu)this.menu).items.add(ben8);
                        }
                        else {
                            ((ItemPickerMenu)this.menu).items.add(ItemStack.EMPTY);
                        }
                    }
                }
                else {
                    ((ItemPickerMenu)this.menu).items.addAll(dqd6);
                }
            }
        }
        else if (bdd != CreativeModeTab.TAB_SEARCH) {
            bdd.fillItemList(((ItemPickerMenu)this.menu).items);
        }
        if (bdd == CreativeModeTab.TAB_INVENTORY) {
            final AbstractContainerMenu ban4 = this.minecraft.player.inventoryMenu;
            if (this.originalSlots == null) {
                this.originalSlots = ImmutableList.copyOf(((ItemPickerMenu)this.menu).slots);
            }
            ((ItemPickerMenu)this.menu).slots.clear();
            for (int integer4 = 0; integer4 < ban4.slots.size(); ++integer4) {
                int integer5;
                int integer9;
                if (integer4 >= 5 && integer4 < 9) {
                    final int integer6 = integer4 - 5;
                    final int integer7 = integer6 / 2;
                    final int integer8 = integer6 % 2;
                    integer9 = 54 + integer7 * 54;
                    integer5 = 6 + integer8 * 27;
                }
                else if (integer4 >= 0 && integer4 < 5) {
                    integer9 = -2000;
                    integer5 = -2000;
                }
                else if (integer4 == 45) {
                    integer9 = 35;
                    integer5 = 20;
                }
                else {
                    final int integer6 = integer4 - 9;
                    final int integer7 = integer6 % 9;
                    final int integer8 = integer6 / 9;
                    integer9 = 9 + integer7 * 18;
                    if (integer4 >= 36) {
                        integer5 = 112;
                    }
                    else {
                        integer5 = 54 + integer8 * 18;
                    }
                }
                final Slot bca8 = new SlotWrapper(ban4.slots.get(integer4), integer4, integer9, integer5);
                ((ItemPickerMenu)this.menu).slots.add(bca8);
            }
            this.destroyItemSlot = new Slot(CreativeModeInventoryScreen.CONTAINER, 0, 173, 112);
            ((ItemPickerMenu)this.menu).slots.add(this.destroyItemSlot);
        }
        else if (integer3 == CreativeModeTab.TAB_INVENTORY.getId()) {
            ((ItemPickerMenu)this.menu).slots.clear();
            ((ItemPickerMenu)this.menu).slots.addAll(this.originalSlots);
            this.originalSlots = null;
        }
        if (this.searchBox != null) {
            if (bdd == CreativeModeTab.TAB_SEARCH) {
                this.searchBox.setVisible(true);
                this.searchBox.setCanLoseFocus(false);
                this.searchBox.setFocus(true);
                if (integer3 != bdd.getId()) {
                    this.searchBox.setValue("");
                }
                this.refreshSearchResults();
            }
            else {
                this.searchBox.setVisible(false);
                this.searchBox.setCanLoseFocus(true);
                this.searchBox.setFocus(false);
                this.searchBox.setValue("");
            }
        }
        this.scrollOffs = 0.0f;
        ((ItemPickerMenu)this.menu).scrollTo(0.0f);
    }
    
    @Override
    public boolean mouseScrolled(final double double1, final double double2, final double double3) {
        if (!this.canScroll()) {
            return false;
        }
        final int integer8 = (((ItemPickerMenu)this.menu).items.size() + 9 - 1) / 9 - 5;
        this.scrollOffs -= (float)(double3 / integer8);
        this.scrollOffs = Mth.clamp(this.scrollOffs, 0.0f, 1.0f);
        ((ItemPickerMenu)this.menu).scrollTo(this.scrollOffs);
        return true;
    }
    
    @Override
    protected boolean hasClickedOutside(final double double1, final double double2, final int integer3, final int integer4, final int integer5) {
        final boolean boolean9 = double1 < integer3 || double2 < integer4 || double1 >= integer3 + this.imageWidth || double2 >= integer4 + this.imageHeight;
        return this.hasClickedOutside = (boolean9 && !this.checkTabClicked(CreativeModeTab.TABS[CreativeModeInventoryScreen.selectedTab], double1, double2));
    }
    
    protected boolean insideScrollbar(final double double1, final double double2) {
        final int integer6 = this.leftPos;
        final int integer7 = this.topPos;
        final int integer8 = integer6 + 175;
        final int integer9 = integer7 + 18;
        final int integer10 = integer8 + 14;
        final int integer11 = integer9 + 112;
        return double1 >= integer8 && double2 >= integer9 && double1 < integer10 && double2 < integer11;
    }
    
    @Override
    public boolean mouseDragged(final double double1, final double double2, final int integer, final double double4, final double double5) {
        if (this.scrolling) {
            final int integer2 = this.topPos + 18;
            final int integer3 = integer2 + 112;
            this.scrollOffs = ((float)double2 - integer2 - 7.5f) / (integer3 - integer2 - 15.0f);
            this.scrollOffs = Mth.clamp(this.scrollOffs, 0.0f, 1.0f);
            ((ItemPickerMenu)this.menu).scrollTo(this.scrollOffs);
            return true;
        }
        return super.mouseDragged(double1, double2, integer, double4, double5);
    }
    
    @Override
    public void render(final int integer1, final int integer2, final float float3) {
        this.renderBackground();
        super.render(integer1, integer2, float3);
        for (final CreativeModeTab bdd8 : CreativeModeTab.TABS) {
            if (this.checkTabHovering(bdd8, integer1, integer2)) {
                break;
            }
        }
        if (this.destroyItemSlot != null && CreativeModeInventoryScreen.selectedTab == CreativeModeTab.TAB_INVENTORY.getId() && this.isHovering(this.destroyItemSlot.x, this.destroyItemSlot.y, 16, 16, integer1, integer2)) {
            this.renderTooltip(I18n.get("inventory.binSlot"), integer1, integer2);
        }
        RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
        this.renderTooltip(integer1, integer2);
    }
    
    @Override
    protected void renderTooltip(final ItemStack ben, final int integer2, final int integer3) {
        if (CreativeModeInventoryScreen.selectedTab == CreativeModeTab.TAB_SEARCH.getId()) {
            final List<Component> list5 = ben.getTooltipLines(this.minecraft.player, this.minecraft.options.advancedItemTooltips ? TooltipFlag.Default.ADVANCED : TooltipFlag.Default.NORMAL);
            final List<String> list6 = Lists.newArrayListWithCapacity(list5.size());
            for (final Component lf8 : list5) {
                list6.add(lf8.getColoredString());
            }
            final Item bei7 = ben.getItem();
            CreativeModeTab bdd8 = bei7.getItemCategory();
            if (bdd8 == null && bei7 == Items.ENCHANTED_BOOK) {
                final Map<Enchantment, Integer> map9 = EnchantmentHelper.getEnchantments(ben);
                if (map9.size() == 1) {
                    final Enchantment bhx10 = map9.keySet().iterator().next();
                    for (final CreativeModeTab bdd9 : CreativeModeTab.TABS) {
                        if (bdd9.hasEnchantmentCategory(bhx10.category)) {
                            bdd8 = bdd9;
                            break;
                        }
                    }
                }
            }
            final Item object;
            final List<String> list7;
            this.visibleTags.forEach((sm, aaz) -> {
                if (aaz.contains(object)) {
                    list7.add(1, "" + ChatFormatting.BOLD + ChatFormatting.DARK_PURPLE + "#" + sm);
                }
                return;
            });
            if (bdd8 != null) {
                list6.add(1, "" + ChatFormatting.BOLD + ChatFormatting.BLUE + I18n.get(bdd8.getName()));
            }
            for (int integer4 = 0; integer4 < list6.size(); ++integer4) {
                if (integer4 == 0) {
                    list6.set(integer4, ben.getRarity().color + list6.get(integer4));
                }
                else {
                    list6.set(integer4, ChatFormatting.GRAY + list6.get(integer4));
                }
            }
            this.renderTooltip(list6, integer2, integer3);
        }
        else {
            super.renderTooltip(ben, integer2, integer3);
        }
    }
    
    @Override
    protected void renderBg(final float float1, final int integer2, final int integer3) {
        RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
        final CreativeModeTab bdd5 = CreativeModeTab.TABS[CreativeModeInventoryScreen.selectedTab];
        for (final CreativeModeTab bdd6 : CreativeModeTab.TABS) {
            this.minecraft.getTextureManager().bind(CreativeModeInventoryScreen.CREATIVE_TABS_LOCATION);
            if (bdd6.getId() != CreativeModeInventoryScreen.selectedTab) {
                this.renderTabButton(bdd6);
            }
        }
        this.minecraft.getTextureManager().bind(new ResourceLocation("textures/gui/container/creative_inventory/tab_" + bdd5.getBackgroundSuffix()));
        this.blit(this.leftPos, this.topPos, 0, 0, this.imageWidth, this.imageHeight);
        this.searchBox.render(integer2, integer3, float1);
        RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
        final int integer4 = this.leftPos + 175;
        final int integer5 = this.topPos + 18;
        final int integer6 = integer5 + 112;
        this.minecraft.getTextureManager().bind(CreativeModeInventoryScreen.CREATIVE_TABS_LOCATION);
        if (bdd5.canScroll()) {
            this.blit(integer4, integer5 + (int)((integer6 - integer5 - 17) * this.scrollOffs), 232 + (this.canScroll() ? 0 : 12), 0, 12, 15);
        }
        this.renderTabButton(bdd5);
        if (bdd5 == CreativeModeTab.TAB_INVENTORY) {
            InventoryScreen.renderEntityInInventory(this.leftPos + 88, this.topPos + 45, 20, (float)(this.leftPos + 88 - integer2), (float)(this.topPos + 45 - 30 - integer3), this.minecraft.player);
        }
    }
    
    protected boolean checkTabClicked(final CreativeModeTab bdd, final double double2, final double double3) {
        final int integer7 = bdd.getColumn();
        int integer8 = 28 * integer7;
        int integer9 = 0;
        if (bdd.isAlignedRight()) {
            integer8 = this.imageWidth - 28 * (6 - integer7) + 2;
        }
        else if (integer7 > 0) {
            integer8 += integer7;
        }
        if (bdd.isTopRow()) {
            integer9 -= 32;
        }
        else {
            integer9 += this.imageHeight;
        }
        return double2 >= integer8 && double2 <= integer8 + 28 && double3 >= integer9 && double3 <= integer9 + 32;
    }
    
    protected boolean checkTabHovering(final CreativeModeTab bdd, final int integer2, final int integer3) {
        final int integer4 = bdd.getColumn();
        int integer5 = 28 * integer4;
        int integer6 = 0;
        if (bdd.isAlignedRight()) {
            integer5 = this.imageWidth - 28 * (6 - integer4) + 2;
        }
        else if (integer4 > 0) {
            integer5 += integer4;
        }
        if (bdd.isTopRow()) {
            integer6 -= 32;
        }
        else {
            integer6 += this.imageHeight;
        }
        if (this.isHovering(integer5 + 3, integer6 + 3, 23, 27, integer2, integer3)) {
            this.renderTooltip(I18n.get(bdd.getName()), integer2, integer3);
            return true;
        }
        return false;
    }
    
    protected void renderTabButton(final CreativeModeTab bdd) {
        final boolean boolean3 = bdd.getId() == CreativeModeInventoryScreen.selectedTab;
        final boolean boolean4 = bdd.isTopRow();
        final int integer5 = bdd.getColumn();
        final int integer6 = integer5 * 28;
        int integer7 = 0;
        int integer8 = this.leftPos + 28 * integer5;
        int integer9 = this.topPos;
        final int integer10 = 32;
        if (boolean3) {
            integer7 += 32;
        }
        if (bdd.isAlignedRight()) {
            integer8 = this.leftPos + this.imageWidth - 28 * (6 - integer5);
        }
        else if (integer5 > 0) {
            integer8 += integer5;
        }
        if (boolean4) {
            integer9 -= 28;
        }
        else {
            integer7 += 64;
            integer9 += this.imageHeight - 4;
        }
        this.blit(integer8, integer9, integer6, integer7, 28, 32);
        this.setBlitOffset(100);
        this.itemRenderer.blitOffset = 100.0f;
        integer8 += 6;
        integer9 += 8 + (boolean4 ? 1 : -1);
        RenderSystem.enableRescaleNormal();
        final ItemStack ben11 = bdd.getIconItem();
        this.itemRenderer.renderAndDecorateItem(ben11, integer8, integer9);
        this.itemRenderer.renderGuiItemDecorations(this.font, ben11, integer8, integer9);
        this.itemRenderer.blitOffset = 0.0f;
        this.setBlitOffset(0);
    }
    
    public int getSelectedTab() {
        return CreativeModeInventoryScreen.selectedTab;
    }
    
    public static void handleHotbarLoadOrSave(final Minecraft dbn, final int integer, final boolean boolean3, final boolean boolean4) {
        final LocalPlayer dqb5 = dbn.player;
        final HotbarManager dbj6 = dbn.getHotbarManager();
        final Hotbar dqd7 = dbj6.get(integer);
        if (boolean3) {
            for (int integer2 = 0; integer2 < Inventory.getSelectionSize(); ++integer2) {
                final ItemStack ben9 = dqd7.get(integer2).copy();
                dqb5.inventory.setItem(integer2, ben9);
                dbn.gameMode.handleCreativeModeItemAdd(ben9, 36 + integer2);
            }
            dqb5.inventoryMenu.broadcastChanges();
        }
        else if (boolean4) {
            for (int integer2 = 0; integer2 < Inventory.getSelectionSize(); ++integer2) {
                dqd7.set(integer2, dqb5.inventory.getItem(integer2).copy());
            }
            final String string8 = dbn.options.keyHotbarSlots[integer].getTranslatedKeyMessage();
            final String string9 = dbn.options.keyLoadHotbarActivator.getTranslatedKeyMessage();
            dbn.gui.setOverlayMessage(new TranslatableComponent("inventory.hotbarSaved", new Object[] { string9, string8 }), false);
            dbj6.save();
        }
    }
    
    static {
        CREATIVE_TABS_LOCATION = new ResourceLocation("textures/gui/container/creative_inventory/tabs.png");
        CONTAINER = new SimpleContainer(45);
        CreativeModeInventoryScreen.selectedTab = CreativeModeTab.TAB_BUILDING_BLOCKS.getId();
    }
    
    public static class ItemPickerMenu extends AbstractContainerMenu {
        public final NonNullList<ItemStack> items;
        
        public ItemPickerMenu(final Player ayj) {
            super(null, 0);
            this.items = NonNullList.<ItemStack>create();
            final Inventory ayi3 = ayj.inventory;
            for (int integer4 = 0; integer4 < 5; ++integer4) {
                for (int integer5 = 0; integer5 < 9; ++integer5) {
                    this.addSlot(new CustomCreativeSlot(CreativeModeInventoryScreen.CONTAINER, integer4 * 9 + integer5, 9 + integer5 * 18, 18 + integer4 * 18));
                }
            }
            for (int integer4 = 0; integer4 < 9; ++integer4) {
                this.addSlot(new Slot(ayi3, integer4, 9 + integer4 * 18, 112));
            }
            this.scrollTo(0.0f);
        }
        
        @Override
        public boolean stillValid(final Player ayj) {
            return true;
        }
        
        public void scrollTo(final float float1) {
            final int integer3 = (this.items.size() + 9 - 1) / 9 - 5;
            int integer4 = (int)(float1 * integer3 + 0.5);
            if (integer4 < 0) {
                integer4 = 0;
            }
            for (int integer5 = 0; integer5 < 5; ++integer5) {
                for (int integer6 = 0; integer6 < 9; ++integer6) {
                    final int integer7 = integer6 + (integer5 + integer4) * 9;
                    if (integer7 >= 0 && integer7 < this.items.size()) {
                        CreativeModeInventoryScreen.CONTAINER.setItem(integer6 + integer5 * 9, this.items.get(integer7));
                    }
                    else {
                        CreativeModeInventoryScreen.CONTAINER.setItem(integer6 + integer5 * 9, ItemStack.EMPTY);
                    }
                }
            }
        }
        
        public boolean canScroll() {
            return this.items.size() > 45;
        }
        
        @Override
        public ItemStack quickMoveStack(final Player ayj, final int integer) {
            if (integer >= this.slots.size() - 9 && integer < this.slots.size()) {
                final Slot bca4 = this.slots.get(integer);
                if (bca4 != null && bca4.hasItem()) {
                    bca4.set(ItemStack.EMPTY);
                }
            }
            return ItemStack.EMPTY;
        }
        
        @Override
        public boolean canTakeItemForPickAll(final ItemStack ben, final Slot bca) {
            return bca.container != CreativeModeInventoryScreen.CONTAINER;
        }
        
        @Override
        public boolean canDragTo(final Slot bca) {
            return bca.container != CreativeModeInventoryScreen.CONTAINER;
        }
    }
    
    static class SlotWrapper extends Slot {
        private final Slot target;
        
        public SlotWrapper(final Slot bca, final int integer2, final int integer3, final int integer4) {
            super(bca.container, integer2, integer3, integer4);
            this.target = bca;
        }
        
        @Override
        public ItemStack onTake(final Player ayj, final ItemStack ben) {
            return this.target.onTake(ayj, ben);
        }
        
        @Override
        public boolean mayPlace(final ItemStack ben) {
            return this.target.mayPlace(ben);
        }
        
        @Override
        public ItemStack getItem() {
            return this.target.getItem();
        }
        
        @Override
        public boolean hasItem() {
            return this.target.hasItem();
        }
        
        @Override
        public void set(final ItemStack ben) {
            this.target.set(ben);
        }
        
        @Override
        public void setChanged() {
            this.target.setChanged();
        }
        
        @Override
        public int getMaxStackSize() {
            return this.target.getMaxStackSize();
        }
        
        @Override
        public int getMaxStackSize(final ItemStack ben) {
            return this.target.getMaxStackSize(ben);
        }
        
        @Nullable
        @Override
        public Pair<ResourceLocation, ResourceLocation> getNoItemIcon() {
            return this.target.getNoItemIcon();
        }
        
        @Override
        public ItemStack remove(final int integer) {
            return this.target.remove(integer);
        }
        
        @Override
        public boolean isActive() {
            return this.target.isActive();
        }
        
        @Override
        public boolean mayPickup(final Player ayj) {
            return this.target.mayPickup(ayj);
        }
    }
    
    static class CustomCreativeSlot extends Slot {
        public CustomCreativeSlot(final Container aje, final int integer2, final int integer3, final int integer4) {
            super(aje, integer2, integer3, integer4);
        }
        
        @Override
        public boolean mayPickup(final Player ayj) {
            if (super.mayPickup(ayj) && this.hasItem()) {
                return this.getItem().getTagElement("CustomCreativeLock") == null;
            }
            return !this.hasItem();
        }
    }
}
