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.network.chat.MutableComponent;
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.network.chat.FormattedText;
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.world.level.ItemLike;
import net.minecraft.world.item.Items;
import net.minecraft.client.resources.language.I18n;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
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 com.mojang.blaze3d.platform.InputConstants;
import java.util.Objects;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.world.inventory.ContainerListener;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
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.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 bec) {
        super(new ItemPickerMenu(bec), bec.inventory, TextComponent.EMPTY);
        this.visibleTags = Maps.newTreeMap();
        bec.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 bhw, final int integer2, final int integer3, ClickType bgq) {
        if (this.isCreativeSlot(bhw)) {
            this.searchBox.moveCursorToEnd();
            this.searchBox.setHighlightPos(0);
        }
        final boolean boolean6 = bgq == ClickType.QUICK_MOVE;
        bgq = ((integer2 == -999 && bgq == ClickType.PICKUP) ? ClickType.THROW : bgq);
        if (bhw != null || CreativeModeInventoryScreen.selectedTab == CreativeModeTab.TAB_INVENTORY.getId() || bgq == ClickType.QUICK_CRAFT) {
            if (bhw != null && !bhw.mayPickup(this.minecraft.player)) {
                return;
            }
            if (bhw == 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 (bhw == this.destroyItemSlot) {
                    this.minecraft.player.inventory.setCarried(ItemStack.EMPTY);
                }
                else if (bgq == ClickType.THROW && bhw != null && bhw.hasItem()) {
                    final ItemStack bki7 = bhw.remove((integer3 == 0) ? 1 : bhw.getItem().getMaxStackSize());
                    final ItemStack bki8 = bhw.getItem();
                    this.minecraft.player.drop(bki7, true);
                    this.minecraft.gameMode.handleCreativeModeItemDrop(bki7);
                    this.minecraft.gameMode.handleCreativeModeItemAdd(bki8, ((SlotWrapper)bhw).target.index);
                }
                else if (bgq == 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((bhw == null) ? integer2 : ((SlotWrapper)bhw).target.index, integer3, bgq, this.minecraft.player);
                    this.minecraft.player.inventoryMenu.broadcastChanges();
                }
            }
            else if (bgq != ClickType.QUICK_CRAFT && bhw.container == CreativeModeInventoryScreen.CONTAINER) {
                final Inventory beb7 = this.minecraft.player.inventory;
                ItemStack bki8 = beb7.getCarried();
                final ItemStack bki9 = bhw.getItem();
                if (bgq == ClickType.SWAP) {
                    if (!bki9.isEmpty()) {
                        final ItemStack bki10 = bki9.copy();
                        bki10.setCount(bki10.getMaxStackSize());
                        this.minecraft.player.inventory.setItem(integer3, bki10);
                        this.minecraft.player.inventoryMenu.broadcastChanges();
                    }
                    return;
                }
                if (bgq == ClickType.CLONE) {
                    if (beb7.getCarried().isEmpty() && bhw.hasItem()) {
                        final ItemStack bki10 = bhw.getItem().copy();
                        bki10.setCount(bki10.getMaxStackSize());
                        beb7.setCarried(bki10);
                    }
                    return;
                }
                if (bgq == ClickType.THROW) {
                    if (!bki9.isEmpty()) {
                        final ItemStack bki10 = bki9.copy();
                        bki10.setCount((integer3 == 0) ? 1 : bki10.getMaxStackSize());
                        this.minecraft.player.drop(bki10, true);
                        this.minecraft.gameMode.handleCreativeModeItemDrop(bki10);
                    }
                    return;
                }
                if (!bki8.isEmpty() && !bki9.isEmpty() && bki8.sameItem(bki9) && ItemStack.tagMatches(bki8, bki9)) {
                    if (integer3 == 0) {
                        if (boolean6) {
                            bki8.setCount(bki8.getMaxStackSize());
                        }
                        else if (bki8.getCount() < bki8.getMaxStackSize()) {
                            bki8.grow(1);
                        }
                    }
                    else {
                        bki8.shrink(1);
                    }
                }
                else if (bki9.isEmpty() || !bki8.isEmpty()) {
                    if (integer3 == 0) {
                        beb7.setCarried(ItemStack.EMPTY);
                    }
                    else {
                        beb7.getCarried().shrink(1);
                    }
                }
                else {
                    beb7.setCarried(bki9.copy());
                    bki8 = beb7.getCarried();
                    if (boolean6) {
                        bki8.setCount(bki8.getMaxStackSize());
                    }
                }
            }
            else if (this.menu != null) {
                final ItemStack bki7 = (bhw == null) ? ItemStack.EMPTY : ((ItemPickerMenu)this.menu).getSlot(bhw.index).getItem();
                ((ItemPickerMenu)this.menu).clicked((bhw == null) ? integer2 : bhw.index, integer3, bgq, 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 (bhw != null) {
                    final ItemStack bki8 = ((ItemPickerMenu)this.menu).getSlot(bhw.index).getItem();
                    this.minecraft.gameMode.handleCreativeModeItemAdd(bki8, bhw.index - ((ItemPickerMenu)this.menu).slots.size() + 9 + 36);
                    final int integer6 = 45 + integer3;
                    if (bgq == ClickType.SWAP) {
                        this.minecraft.gameMode.handleCreativeModeItemAdd(bki7, integer6 - ((ItemPickerMenu)this.menu).slots.size() + 9 + 36);
                    }
                    else if (bgq == ClickType.THROW && !bki7.isEmpty()) {
                        final ItemStack bki10 = bki7.copy();
                        bki10.setCount((integer3 == 0) ? 1 : bki10.getMaxStackSize());
                        this.minecraft.player.drop(bki10, true);
                        this.minecraft.gameMode.handleCreativeModeItemDrop(bki10);
                    }
                    this.minecraft.player.inventoryMenu.broadcastChanges();
                }
            }
        }
        else {
            final Inventory beb7 = this.minecraft.player.inventory;
            if (!beb7.getCarried().isEmpty() && this.hasClickedOutside) {
                if (integer3 == 0) {
                    this.minecraft.player.drop(beb7.getCarried(), true);
                    this.minecraft.gameMode.handleCreativeModeItemDrop(beb7.getCarried());
                    beb7.setCarried(ItemStack.EMPTY);
                }
                if (integer3 == 1) {
                    final ItemStack bki8 = beb7.getCarried().split(1);
                    this.minecraft.player.drop(bki8, true);
                    this.minecraft.gameMode.handleCreativeModeItemDrop(bki8);
                }
            }
        }
    }
    
    private boolean isCreativeSlot(@Nullable final Slot bhw) {
        return bhw != null && bhw.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, new TranslatableComponent("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 dlx, final int integer2, final int integer3) {
        final String string5 = this.searchBox.getValue();
        this.init(dlx, 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.hasItem();
            final boolean boolean6 = InputConstants.getKey(integer1, integer2).getNumericKeyValue().isPresent();
            if (boolean5 && boolean6 && this.checkHotbarKeyPressed(integer1, integer2)) {
                return this.ignoreTextInput = true;
            }
            final String string7 = this.searchBox.getValue();
            if (this.searchBox.keyPressed(integer1, integer2, integer3)) {
                if (!Objects.equals(string7, 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 bke4 : Registry.ITEM) {
                bke4.fillItemCategory(CreativeModeTab.TAB_SEARCH, ((ItemPickerMenu)this.menu).items);
            }
        }
        else {
            SearchTree<ItemStack> eov3;
            if (string2.startsWith("#")) {
                string2 = string2.substring(1);
                eov3 = this.minecraft.<ItemStack>getSearchTree(SearchRegistry.CREATIVE_TAGS);
                this.updateVisibleTags(string2);
            }
            else {
                eov3 = this.minecraft.<ItemStack>getSearchTree(SearchRegistry.CREATIVE_NAMES);
            }
            ((ItemPickerMenu)this.menu).items.addAll(eov3.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 = (uh -> uh.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 = (uh -> uh.getNamespace().contains(s) && uh.getPath().contains(s2));
        }
        final TagCollection<Item> adg5 = ItemTags.getAllTags();
        final Tag<Item> tag;
        adg5.getAvailableTags().stream().filter(predicate4).forEach(uh -> tag = this.visibleTags.put(uh, adg5.getTag(uh)));
    }
    
    @Override
    protected void renderLabels(final PoseStack dhl, final int integer2, final int integer3) {
        final CreativeModeTab biy5 = CreativeModeTab.TABS[CreativeModeInventoryScreen.selectedTab];
        if (biy5.showTitle()) {
            RenderSystem.disableBlend();
            this.font.draw(dhl, I18n.get(biy5.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 biy14 : CreativeModeTab.TABS) {
                if (this.checkTabClicked(biy14, 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 biy14 : CreativeModeTab.TABS) {
                if (this.checkTabClicked(biy14, double3, double4)) {
                    this.selectTab(biy14);
                    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 biy) {
        final int integer3 = CreativeModeInventoryScreen.selectedTab;
        CreativeModeInventoryScreen.selectedTab = biy.getId();
        this.quickCraftSlots.clear();
        ((ItemPickerMenu)this.menu).items.clear();
        if (biy == CreativeModeTab.TAB_HOTBAR) {
            final HotbarManager dlt4 = this.minecraft.getHotbarManager();
            for (int integer4 = 0; integer4 < 9; ++integer4) {
                final Hotbar ebh6 = dlt4.get(integer4);
                if (ebh6.isEmpty()) {
                    for (int integer5 = 0; integer5 < 9; ++integer5) {
                        if (integer5 == integer4) {
                            final ItemStack bki8 = new ItemStack(Items.PAPER);
                            bki8.getOrCreateTagElement("CustomCreativeLock");
                            final Component mr9 = this.minecraft.options.keyHotbarSlots[integer4].getTranslatedKeyMessage();
                            final Component mr10 = this.minecraft.options.keySaveHotbarActivator.getTranslatedKeyMessage();
                            bki8.setHoverName(new TranslatableComponent("inventory.hotbarInfo", new Object[] { mr10, mr9 }));
                            ((ItemPickerMenu)this.menu).items.add(bki8);
                        }
                        else {
                            ((ItemPickerMenu)this.menu).items.add(ItemStack.EMPTY);
                        }
                    }
                }
                else {
                    ((ItemPickerMenu)this.menu).items.addAll(ebh6);
                }
            }
        }
        else if (biy != CreativeModeTab.TAB_SEARCH) {
            biy.fillItemList(((ItemPickerMenu)this.menu).items);
        }
        if (biy == CreativeModeTab.TAB_INVENTORY) {
            final AbstractContainerMenu bgi4 = 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 < bgi4.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 bhw8 = new SlotWrapper(bgi4.slots.get(integer4), integer4, integer9, integer5);
                ((ItemPickerMenu)this.menu).slots.add(bhw8);
            }
            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 (biy == CreativeModeTab.TAB_SEARCH) {
                this.searchBox.setVisible(true);
                this.searchBox.setCanLoseFocus(false);
                this.searchBox.setFocus(true);
                if (integer3 != biy.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 PoseStack dhl, final int integer2, final int integer3, final float float4) {
        this.renderBackground(dhl);
        super.render(dhl, integer2, integer3, float4);
        for (final CreativeModeTab biy9 : CreativeModeTab.TABS) {
            if (this.checkTabHovering(dhl, biy9, integer2, integer3)) {
                break;
            }
        }
        if (this.destroyItemSlot != null && CreativeModeInventoryScreen.selectedTab == CreativeModeTab.TAB_INVENTORY.getId() && this.isHovering(this.destroyItemSlot.x, this.destroyItemSlot.y, 16, 16, integer2, integer3)) {
            this.renderTooltip(dhl, new TranslatableComponent("inventory.binSlot"), integer2, integer3);
        }
        RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
        this.renderTooltip(dhl, integer2, integer3);
    }
    
    @Override
    protected void renderTooltip(final PoseStack dhl, final ItemStack bki, final int integer3, final int integer4) {
        if (CreativeModeInventoryScreen.selectedTab == CreativeModeTab.TAB_SEARCH.getId()) {
            final List<Component> list6 = bki.getTooltipLines(this.minecraft.player, this.minecraft.options.advancedItemTooltips ? TooltipFlag.Default.ADVANCED : TooltipFlag.Default.NORMAL);
            final List<Component> list7 = Lists.newArrayList(list6);
            final Item bke8 = bki.getItem();
            CreativeModeTab biy9 = bke8.getItemCategory();
            if (biy9 == null && bke8 == Items.ENCHANTED_BOOK) {
                final Map<Enchantment, Integer> map10 = EnchantmentHelper.getEnchantments(bki);
                if (map10.size() == 1) {
                    final Enchantment bnw11 = map10.keySet().iterator().next();
                    for (final CreativeModeTab biy10 : CreativeModeTab.TABS) {
                        if (biy10.hasEnchantmentCategory(bnw11.category)) {
                            biy9 = biy10;
                            break;
                        }
                    }
                }
            }
            final Item object;
            final TextComponent textComponent;
            final List<MutableComponent> list8;
            final int n;
            this.visibleTags.forEach((uh, adf) -> {
                if (adf.contains(object)) {
                    new TextComponent("#" + uh);
                    list8.add(n, textComponent.withStyle(ChatFormatting.DARK_PURPLE));
                }
                return;
            });
            if (biy9 != null) {
                list7.add(1, new TranslatableComponent(biy9.getName()).withStyle(ChatFormatting.BLUE));
            }
            this.renderTooltip(dhl, list7, integer3, integer4);
        }
        else {
            super.renderTooltip(dhl, bki, integer3, integer4);
        }
    }
    
    @Override
    protected void renderBg(final PoseStack dhl, final float float2, final int integer3, final int integer4) {
        RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
        final CreativeModeTab biy6 = CreativeModeTab.TABS[CreativeModeInventoryScreen.selectedTab];
        for (final CreativeModeTab biy7 : CreativeModeTab.TABS) {
            this.minecraft.getTextureManager().bind(CreativeModeInventoryScreen.CREATIVE_TABS_LOCATION);
            if (biy7.getId() != CreativeModeInventoryScreen.selectedTab) {
                this.renderTabButton(dhl, biy7);
            }
        }
        this.minecraft.getTextureManager().bind(new ResourceLocation("textures/gui/container/creative_inventory/tab_" + biy6.getBackgroundSuffix()));
        this.blit(dhl, this.leftPos, this.topPos, 0, 0, this.imageWidth, this.imageHeight);
        this.searchBox.render(dhl, integer3, integer4, float2);
        RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
        final int integer5 = this.leftPos + 175;
        final int integer6 = this.topPos + 18;
        final int integer7 = integer6 + 112;
        this.minecraft.getTextureManager().bind(CreativeModeInventoryScreen.CREATIVE_TABS_LOCATION);
        if (biy6.canScroll()) {
            this.blit(dhl, integer5, integer6 + (int)((integer7 - integer6 - 17) * this.scrollOffs), 232 + (this.canScroll() ? 0 : 12), 0, 12, 15);
        }
        this.renderTabButton(dhl, biy6);
        if (biy6 == CreativeModeTab.TAB_INVENTORY) {
            InventoryScreen.renderEntityInInventory(this.leftPos + 88, this.topPos + 45, 20, (float)(this.leftPos + 88 - integer3), (float)(this.topPos + 45 - 30 - integer4), this.minecraft.player);
        }
    }
    
    protected boolean checkTabClicked(final CreativeModeTab biy, final double double2, final double double3) {
        final int integer7 = biy.getColumn();
        int integer8 = 28 * integer7;
        int integer9 = 0;
        if (biy.isAlignedRight()) {
            integer8 = this.imageWidth - 28 * (6 - integer7) + 2;
        }
        else if (integer7 > 0) {
            integer8 += integer7;
        }
        if (biy.isTopRow()) {
            integer9 -= 32;
        }
        else {
            integer9 += this.imageHeight;
        }
        return double2 >= integer8 && double2 <= integer8 + 28 && double3 >= integer9 && double3 <= integer9 + 32;
    }
    
    protected boolean checkTabHovering(final PoseStack dhl, final CreativeModeTab biy, final int integer3, final int integer4) {
        final int integer5 = biy.getColumn();
        int integer6 = 28 * integer5;
        int integer7 = 0;
        if (biy.isAlignedRight()) {
            integer6 = this.imageWidth - 28 * (6 - integer5) + 2;
        }
        else if (integer5 > 0) {
            integer6 += integer5;
        }
        if (biy.isTopRow()) {
            integer7 -= 32;
        }
        else {
            integer7 += this.imageHeight;
        }
        if (this.isHovering(integer6 + 3, integer7 + 3, 23, 27, integer3, integer4)) {
            this.renderTooltip(dhl, new TranslatableComponent(biy.getName()), integer3, integer4);
            return true;
        }
        return false;
    }
    
    protected void renderTabButton(final PoseStack dhl, final CreativeModeTab biy) {
        final boolean boolean4 = biy.getId() == CreativeModeInventoryScreen.selectedTab;
        final boolean boolean5 = biy.isTopRow();
        final int integer6 = biy.getColumn();
        final int integer7 = integer6 * 28;
        int integer8 = 0;
        int integer9 = this.leftPos + 28 * integer6;
        int integer10 = this.topPos;
        final int integer11 = 32;
        if (boolean4) {
            integer8 += 32;
        }
        if (biy.isAlignedRight()) {
            integer9 = this.leftPos + this.imageWidth - 28 * (6 - integer6);
        }
        else if (integer6 > 0) {
            integer9 += integer6;
        }
        if (boolean5) {
            integer10 -= 28;
        }
        else {
            integer8 += 64;
            integer10 += this.imageHeight - 4;
        }
        this.blit(dhl, integer9, integer10, integer7, integer8, 28, 32);
        this.itemRenderer.blitOffset = 100.0f;
        integer9 += 6;
        integer10 += 8 + (boolean5 ? 1 : -1);
        RenderSystem.enableRescaleNormal();
        final ItemStack bki12 = biy.getIconItem();
        this.itemRenderer.renderAndDecorateItem(bki12, integer9, integer10);
        this.itemRenderer.renderGuiItemDecorations(this.font, bki12, integer9, integer10);
        this.itemRenderer.blitOffset = 0.0f;
    }
    
    public int getSelectedTab() {
        return CreativeModeInventoryScreen.selectedTab;
    }
    
    public static void handleHotbarLoadOrSave(final Minecraft dlx, final int integer, final boolean boolean3, final boolean boolean4) {
        final LocalPlayer ebf5 = dlx.player;
        final HotbarManager dlt6 = dlx.getHotbarManager();
        final Hotbar ebh7 = dlt6.get(integer);
        if (boolean3) {
            for (int integer2 = 0; integer2 < Inventory.getSelectionSize(); ++integer2) {
                final ItemStack bki9 = ebh7.get(integer2).copy();
                ebf5.inventory.setItem(integer2, bki9);
                dlx.gameMode.handleCreativeModeItemAdd(bki9, 36 + integer2);
            }
            ebf5.inventoryMenu.broadcastChanges();
        }
        else if (boolean4) {
            for (int integer2 = 0; integer2 < Inventory.getSelectionSize(); ++integer2) {
                ebh7.set(integer2, ebf5.inventory.getItem(integer2).copy());
            }
            final Component mr8 = dlx.options.keyHotbarSlots[integer].getTranslatedKeyMessage();
            final Component mr9 = dlx.options.keyLoadHotbarActivator.getTranslatedKeyMessage();
            dlx.gui.setOverlayMessage(new TranslatableComponent("inventory.hotbarSaved", new Object[] { mr9, mr8 }), false);
            dlt6.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 bec) {
            super(null, 0);
            this.items = NonNullList.<ItemStack>create();
            final Inventory beb3 = bec.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(beb3, integer4, 9 + integer4 * 18, 112));
            }
            this.scrollTo(0.0f);
        }
        
        @Override
        public boolean stillValid(final Player bec) {
            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 bec, final int integer) {
            if (integer >= this.slots.size() - 9 && integer < this.slots.size()) {
                final Slot bhw4 = this.slots.get(integer);
                if (bhw4 != null && bhw4.hasItem()) {
                    bhw4.set(ItemStack.EMPTY);
                }
            }
            return ItemStack.EMPTY;
        }
        
        @Override
        public boolean canTakeItemForPickAll(final ItemStack bki, final Slot bhw) {
            return bhw.container != CreativeModeInventoryScreen.CONTAINER;
        }
        
        @Override
        public boolean canDragTo(final Slot bhw) {
            return bhw.container != CreativeModeInventoryScreen.CONTAINER;
        }
    }
    
    static class SlotWrapper extends Slot {
        private final Slot target;
        
        public SlotWrapper(final Slot bhw, final int integer2, final int integer3, final int integer4) {
            super(bhw.container, integer2, integer3, integer4);
            this.target = bhw;
        }
        
        @Override
        public ItemStack onTake(final Player bec, final ItemStack bki) {
            return this.target.onTake(bec, bki);
        }
        
        @Override
        public boolean mayPlace(final ItemStack bki) {
            return this.target.mayPlace(bki);
        }
        
        @Override
        public ItemStack getItem() {
            return this.target.getItem();
        }
        
        @Override
        public boolean hasItem() {
            return this.target.hasItem();
        }
        
        @Override
        public void set(final ItemStack bki) {
            this.target.set(bki);
        }
        
        @Override
        public void setChanged() {
            this.target.setChanged();
        }
        
        @Override
        public int getMaxStackSize() {
            return this.target.getMaxStackSize();
        }
        
        @Override
        public int getMaxStackSize(final ItemStack bki) {
            return this.target.getMaxStackSize(bki);
        }
        
        @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 bec) {
            return this.target.mayPickup(bec);
        }
    }
    
    static class CustomCreativeSlot extends Slot {
        public CustomCreativeSlot(final Container amz, final int integer2, final int integer3, final int integer4) {
            super(amz, integer2, integer3, integer4);
        }
        
        @Override
        public boolean mayPickup(final Player bec) {
            if (super.mayPickup(bec) && this.hasItem()) {
                return this.getItem().getTagElement("CustomCreativeLock") == null;
            }
            return !this.hasItem();
        }
    }
}
