Jump to content

Prevent StackOverflow in Container (code included)


Exsaliver

Recommended Posts

The method slotClick in the Container class is recursive (call path is slockClick -> retryClick -> slotClick) this result in a stack overflow when moving a large amount of items when shift-clicking. It will never happen with Vanilla Minecraft but it currently happens with EE3 (https://github.com/pahimar/Equivalent-Exchange-3/issues/901).

 

I changed the functionality to work as loop, the code have many comments to show that it does not change any of the current functionality.

Note that the current code uses reflection since I don't have access to Minecraft's private variables.

 

Code: (http://pastebin.com/RT74SBF5)

/* Credit Mr-J:
     *      https://github.com/Mr-J/AdvancedBackpackMod/blob/master/unrelated/slotClick%2BComments%2BRename%2BHelpers.java.txt
     *
     * values for flag:
     * 0 = standard single click
     * 1 = single click + shift modifier
     * 2 = hotbar key is pressed (keys 0-9)
     * 3 = click with the middle button
     * 4 = click outside of the current gui window
     * 5 = button pressed & hold with the cursor holding an itemstack
     * 6 = double left click
     */

    @Override
    public ItemStack slotClick(int slotId, int button, int flags, EntityPlayer player)
    {
        ItemStack result = null;
        ItemStack movedItemStack;
        InventoryPlayer playerInventory = player.inventory;
        int sizeOrID;

        if (flags == 5) // DRAGGED DISTRIBUTION
        {
            int l = this.getDistributeState();
            this.setDistributeState(func_94532_c(button));

            if ((l != 1 || this.getDistributeState() != 2) && l != this.getDistributeState())
            {
                this.func_94533_d();
            } else if (playerInventory.getItemStack() == null)
            {
                this.func_94533_d();
            } else if (this.getDistributeState() == 0)
            {
                this.setPressedKeyInRange(func_94529_b(button));

                if (func_94528_d(this.getPressedKeyInRange()))
                {
                    this.setDistributeState(1);
                    this.getDistributeSlotSet().clear();
                } else
                {
                    this.func_94533_d();
                }
            } else if (this.getDistributeState() == 1)
            {
                Slot slot = (Slot) this.inventorySlots.get(slotId);

                if (slot != null && func_94527_a(slot, playerInventory.getItemStack(), true) &&
                        slot.isItemValid(playerInventory.getItemStack()) &&
                        playerInventory.getItemStack().stackSize > this.getDistributeSlotSet().size() &&
                        this.canDragIntoSlot(slot))
                {
                    this.getDistributeSlotSet().add(slot);
                }
            } else if (this.getDistributeState() == 2)
            {
                if (!this.getDistributeSlotSet().isEmpty())
                {
                    movedItemStack = playerInventory.getItemStack().copy();
                    sizeOrID = playerInventory.getItemStack().stackSize;
                    Iterator iterator = this.getDistributeSlotSet().iterator();

                    while (iterator.hasNext())
                    {
                        Slot slot1 = (Slot) iterator.next();

                        if (slot1 != null && func_94527_a(slot1, playerInventory.getItemStack(), true) &&
                                slot1.isItemValid(playerInventory.getItemStack()) &&
                                playerInventory.getItemStack().stackSize >= this.getDistributeSlotSet().size() &&
                                this.canDragIntoSlot(slot1))
                        {
                            ItemStack itemstack1 = movedItemStack.copy();
                            int j1 = slot1.getHasStack() ? slot1.getStack().stackSize : 0;
                            func_94525_a(this.getDistributeSlotSet(), this.getPressedKeyInRange(), itemstack1, j1);

                            if (itemstack1.stackSize > itemstack1.getMaxStackSize())
                            {
                                itemstack1.stackSize = itemstack1.getMaxStackSize();
                            }

                            if (itemstack1.stackSize > slot1.getSlotStackLimit())
                            {
                                itemstack1.stackSize = slot1.getSlotStackLimit();
                            }

                            sizeOrID -= itemstack1.stackSize - j1;
                            slot1.putStack(itemstack1);
                        }
                    }

                    movedItemStack.stackSize = sizeOrID;

                    if (movedItemStack.stackSize <= 0)
                    {
                        movedItemStack = null;
                    }

                    playerInventory.setItemStack(movedItemStack);
                }

                this.func_94533_d();
            } else
            {
                this.func_94533_d();
            }
        } else if (this.getDistributeState() != 0)
        {
            this.func_94533_d();
        } else // NORMAL SLOTCLICK
        {
            Slot targetSlotCopy;
            int l1;
            ItemStack itemstack5;

            if ((flags == 0 || flags == 1) && (button == 0 || button == 1))
            {
                // Not valid stack
                if (slotId == -999)
                {
                        /*
                         * on leftclick drop the complete itemstack from the inventory
                         * on rightclick drop a single item from the itemstack
                         */
                    if (playerInventory.getItemStack() != null && slotId == -999)
                    {
                        if (button == 0)
                        {
                            player.dropPlayerItemWithRandomChoice(playerInventory.getItemStack(), true);
                            playerInventory.setItemStack((ItemStack) null);
                        }

                        if (button == 1)
                        {
                            player.dropPlayerItemWithRandomChoice(playerInventory.getItemStack().splitStack(1), true);

                            if (playerInventory.getItemStack().stackSize == 0)
                            {
                                playerInventory.setItemStack((ItemStack) null);
                            }
                        }
                    }
                } else if (flags == 1) // SHIFT-CLICK
                {
                    while (true)
                    {

                        if (slotId < 0) // Invalid Slot
                        {
                            return null;
                        }

                        targetSlotCopy = (Slot) this.inventorySlots.get(slotId);

                        //if targetSlotCopy is not null and the stack inside the slot can be moved
                        if (targetSlotCopy != null && targetSlotCopy.canTakeStack(player))
                        {
                            //transfer the picked up stack to targetSlotID in the player inverntory
                            movedItemStack = this.transferStackInSlot(player, slotId);

                            //if the movedItemStack was not transferred completely
                            if (movedItemStack != null)
                            {
                                Item item = movedItemStack.getItem();

                                //set the return value to the rest
                                result = movedItemStack.copy();

                                if (targetSlotCopy.getStack() != null && targetSlotCopy.getStack().getItem() == item)
                                {
                                    // Original recursive bugged MC code
                                    // this.retrySlotClick(targetSlotID, mouseButtonPressed, true, player);
                                    // variables are:
                                    //      * [0]:: targetSlotID -> same as entry
                                    //      * [1]:: mouseButtonPressed -> same as entry
                                    //      * [2]:: true -> const -> 1
                                    //              if (flags == 1) -> same as entry
                                    //      * [3]:: player -> same as entry
                                    //
                                    // Final call: slotClick(slotId, button, 1, player)

                                    /* Execution path summery:
                                     *  [entry]->
                                     *      {{
                                     *          [set only] result -> null
                                     *          [set first] movedItemStack -> null
                                     *          [const] playerInventory -> player.inventory
                                     *          [unused] sizeOrID -> 0
                                     *      }}
                                     *      [if (1 == 5) -> false]
                                     *      [else if (this.getDistributeState() != 0)] -> false]
                                     *      [else -> true] ->
                                     *          {{
                                     *              [set first] targetSlotCopy -> null;
                                     *              [unused] l1 -> 0;
                                     *              [unused] itemstack5 -> null;
                                     *          }}
                                     *          [if ((flags == 0 || flags == 1) && (button == 0 || button == 1)) -> true] ->
                                     *              {{}}
                                     *              [if (slotId == -999) -> false]
                                     *              [else if (flags == 1) -> true]
                                     *                  {{loop body}}
                                     *
                                    */
                                    continue; //retry with the shift-click
                                }
                            }
                        }

                        break; //Keep current route
                    }
                } else //if a click with NO shift modifier is performed
                {
                    if (slotId < 0)
                    {
                        return null;
                    }

                    targetSlotCopy = (Slot) this.inventorySlots.get(slotId);

                    if (targetSlotCopy != null)
                    {
                        movedItemStack = targetSlotCopy.getStack();
                        ItemStack itemstack4 = playerInventory.getItemStack();

                        if (movedItemStack != null)
                        {
                            result = movedItemStack.copy();
                        }

                        if (movedItemStack == null)
                        {
                            if (itemstack4 != null && targetSlotCopy.isItemValid(itemstack4))
                            {
                                l1 = button == 0 ? itemstack4.stackSize : 1;

                                if (l1 > targetSlotCopy.getSlotStackLimit())
                                {
                                    l1 = targetSlotCopy.getSlotStackLimit();
                                }

                                if (itemstack4.stackSize >= l1)
                                {
                                    targetSlotCopy.putStack(itemstack4.splitStack(l1));
                                }

                                if (itemstack4.stackSize == 0)
                                {
                                    playerInventory.setItemStack((ItemStack) null);
                                }
                            }
                        } else if (targetSlotCopy.canTakeStack(player))
                        {
                            if (itemstack4 == null)
                            {
                                l1 = button == 0 ? movedItemStack.stackSize : (movedItemStack.stackSize + 1) / 2;
                                itemstack5 = targetSlotCopy.decrStackSize(l1);
                                playerInventory.setItemStack(itemstack5);

                                if (movedItemStack.stackSize == 0)
                                {
                                    targetSlotCopy.putStack((ItemStack) null);
                                }

                                targetSlotCopy.onPickupFromSlot(player, playerInventory.getItemStack());
                            } else if (targetSlotCopy.isItemValid(itemstack4))
                            {
                                if (movedItemStack.getItem() == itemstack4.getItem() && movedItemStack.getItemDamage() == itemstack4.getItemDamage() && ItemStack.areItemStackTagsEqual(movedItemStack, itemstack4))
                                {
                                    l1 = button == 0 ? itemstack4.stackSize : 1;

                                    if (l1 > targetSlotCopy.getSlotStackLimit() - movedItemStack.stackSize)
                                    {
                                        l1 = targetSlotCopy.getSlotStackLimit() - movedItemStack.stackSize;
                                    }

                                    if (l1 > itemstack4.getMaxStackSize() - movedItemStack.stackSize)
                                    {
                                        l1 = itemstack4.getMaxStackSize() - movedItemStack.stackSize;
                                    }

                                    itemstack4.splitStack(l1);

                                    if (itemstack4.stackSize == 0)
                                    {
                                        playerInventory.setItemStack((ItemStack) null);
                                    }

                                    movedItemStack.stackSize += l1;
                                } else if (itemstack4.stackSize <= targetSlotCopy.getSlotStackLimit())
                                {
                                    targetSlotCopy.putStack(itemstack4);
                                    playerInventory.setItemStack(movedItemStack);
                                }
                            } else if (movedItemStack.getItem() == itemstack4.getItem() && itemstack4.getMaxStackSize() > 1 && (!movedItemStack.getHasSubtypes() || movedItemStack.getItemDamage() == itemstack4.getItemDamage()) && ItemStack.areItemStackTagsEqual(movedItemStack, itemstack4))
                            {
                                l1 = movedItemStack.stackSize;

                                if (l1 > 0 && l1 + itemstack4.stackSize <= itemstack4.getMaxStackSize())
                                {
                                    itemstack4.stackSize += l1;
                                    movedItemStack = targetSlotCopy.decrStackSize(l1);

                                    if (movedItemStack.stackSize == 0)
                                    {
                                        targetSlotCopy.putStack((ItemStack) null);
                                    }

                                    targetSlotCopy.onPickupFromSlot(player, playerInventory.getItemStack());
                                }
                            }
                        }

                        targetSlotCopy.onSlotChanged();
                    }
                }
            }
            // If a hotbar key is pressed (flag == 2)
            else if (flags == 2 && button >= 0 && button < 9)
            {
                targetSlotCopy = (Slot) this.inventorySlots.get(slotId);

                if (targetSlotCopy.canTakeStack(player))
                {
                    movedItemStack = playerInventory.getStackInSlot(button);
                    boolean flag = movedItemStack == null || targetSlotCopy.inventory == playerInventory && targetSlotCopy.isItemValid(movedItemStack);
                    l1 = -1;

                    if (!flag)
                    {
                        l1 = playerInventory.getFirstEmptyStack();
                        flag |= l1 > -1;
                    }

                    if (targetSlotCopy.getHasStack() && flag)
                    {
                        itemstack5 = targetSlotCopy.getStack();
                        playerInventory.setInventorySlotContents(button, itemstack5.copy());

                        if ((targetSlotCopy.inventory != playerInventory || !targetSlotCopy.isItemValid(movedItemStack)) && movedItemStack != null)
                        {
                            if (l1 > -1)
                            {
                                playerInventory.addItemStackToInventory(movedItemStack);
                                targetSlotCopy.decrStackSize(itemstack5.stackSize);
                                targetSlotCopy.putStack((ItemStack) null);
                                targetSlotCopy.onPickupFromSlot(player, itemstack5);
                            }
                        } else
                        {
                            targetSlotCopy.decrStackSize(itemstack5.stackSize);
                            targetSlotCopy.putStack(movedItemStack);
                            targetSlotCopy.onPickupFromSlot(player, itemstack5);
                        }
                    } else if (!targetSlotCopy.getHasStack() && movedItemStack != null && targetSlotCopy.isItemValid(movedItemStack))
                    {
                        playerInventory.setInventorySlotContents(button, (ItemStack) null);
                        targetSlotCopy.putStack(movedItemStack);
                    }
                }
            } else if (flags == 3 && player.capabilities.isCreativeMode && playerInventory.getItemStack() == null && slotId >= 0)
            {
                targetSlotCopy = (Slot) this.inventorySlots.get(slotId);

                if (targetSlotCopy != null && targetSlotCopy.getHasStack())
                {
                    movedItemStack = targetSlotCopy.getStack().copy();
                    movedItemStack.stackSize = movedItemStack.getMaxStackSize();
                    playerInventory.setItemStack(movedItemStack);
                }
            } else if (flags == 4 && playerInventory.getItemStack() == null && slotId >= 0)
            {
                targetSlotCopy = (Slot) this.inventorySlots.get(slotId);

                if (targetSlotCopy != null && targetSlotCopy.getHasStack() && targetSlotCopy.canTakeStack(player))
                {
                    movedItemStack = targetSlotCopy.decrStackSize(button == 0 ? 1 : targetSlotCopy.getStack().stackSize);
                    targetSlotCopy.onPickupFromSlot(player, movedItemStack);
                    player.dropPlayerItemWithRandomChoice(movedItemStack, true);
                }
            } else if (flags == 6 && slotId >= 0)
            {
                targetSlotCopy = (Slot) this.inventorySlots.get(slotId);
                movedItemStack = playerInventory.getItemStack();

                if (movedItemStack != null && (targetSlotCopy == null || !targetSlotCopy.getHasStack() || !targetSlotCopy.canTakeStack(player)))
                {
                    sizeOrID = button == 0 ? 0 : this.inventorySlots.size() - 1;
                    l1 = button == 0 ? 1 : -1;

                    for (int i2 = 0; i2 < 2; ++i2)
                    {
                        for (int j2 = sizeOrID; j2 >= 0 && j2 < this.inventorySlots.size() && movedItemStack.stackSize < movedItemStack.getMaxStackSize(); j2 += l1)
                        {
                            Slot slot3 = (Slot) this.inventorySlots.get(j2);

                            if (slot3.getHasStack() && func_94527_a(slot3, movedItemStack, true) && slot3.canTakeStack(player) && this.func_94530_a(movedItemStack, slot3) && (i2 != 0 || slot3.getStack().stackSize != slot3.getStack().getMaxStackSize()))
                            {
                                int k1 = Math.min(movedItemStack.getMaxStackSize() - movedItemStack.stackSize, slot3.getStack().stackSize);
                                ItemStack itemstack2 = slot3.decrStackSize(k1);
                                movedItemStack.stackSize += k1;

                                if (itemstack2.stackSize <= 0)
                                {
                                    slot3.putStack((ItemStack) null);
                                }

                                slot3.onPickupFromSlot(player, itemstack2);
                            }
                        }
                    }
                }

                this.detectAndSendChanges();
            }
        }

        return result;
    }

Link to comment
Share on other sites

Wall of code really doesn't help cases like this, patch files are much more useful.

Quick glance at it shows that it should be sane enough for any normal usage.

How exactly are you getting a stack overflow?

I'll be able to take a more detailed look at it when im back in town, but it shouldn't recurse that much unless you're feeding it extreamly bad information.

I do Forge for free, however the servers to run it arn't free, so anything is appreciated.
Consider supporting the team on Patreon

Link to comment
Share on other sites

Yeah sorry about the code, never really worked with patch files so didn't think about that, though it's easy to find what I've changed, there's a large block off comments around it.

Anyway, you're right, it should work in the majority of cases when our does fail is when you try to dump a lot of items into an inventory. According to the issue on EE3 repository it happens when trying to move about 27 slots worth of items the is a bit more than 1,728 recursive calls.

 

If you really need patch files I can add them when I get home.

Link to comment
Share on other sites

How exactly are you getting a stack overflow?

It is because of the
transferStackInSlot

method. If you don't override that method, it will result in a stack overflow. So I think Pahimar is not overriding the

transferStackInSlot

correctly, and thus creating a stack overflow. This has been so since a long time, and every modder using containers should know that, so I think this is on EE3's side on stuff.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

Yes i can tell what function is recurring. Cuz you know, stack trace.

However, it shouldn't be that big of a deal. How does it expand to 1728 function calls, that seems ridiculous.

Will need to look into it when I get back home.

But yes patch files are far better then hunks of code with comments. Much easier to actually see the difference.

I do Forge for free, however the servers to run it arn't free, so anything is appreciated.
Consider supporting the team on Patreon

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • BRI4D adalah pilihan tepat bagi Anda yang menginginkan pengalaman bermain slot dengan RTP tinggi dan transaksi yang akurat melalui Bank BRI. Berikut adalah beberapa alasan mengapa Anda harus memilih BRI4D: Tingkat Pengembalian (RTP) Tertinggi Kami bangga menjadi salah satu agen situs slot dengan RTP tertinggi, mencapai 99%! Ini berarti Anda memiliki peluang lebih besar untuk meraih kemenangan dalam setiap putaran permainan. Transaksi Melalui Bank BRI yang Akurat Proses deposit dan penarikan dana di BRI4D cepat, mudah, dan akurat. Kami menyediakan layanan transaksi melalui Bank BRI untuk kenyamanan Anda. Dengan begitu, Anda dapat melakukan transaksi dengan lancar dan tanpa khawatir. Beragam Pilihan Permainan BRI4D menyajikan koleksi permainan slot yang beragam dan menarik dari berbagai provider terkemuka. Mulai dari tema klasik hingga yang paling modern, Anda akan menemukan banyak pilihan permainan yang sesuai dengan selera dan preferensi Anda.  
    • SPARTA88 adalah pilihan tepat bagi Anda yang menginginkan agen situs slot terbaik dengan RTP tinggi dan transaksi yang mudah melalui Bank BNI. Berikut adalah beberapa alasan mengapa Anda harus memilih SPARTA88: Tingkat Pengembalian (RTP) Tinggi Kami bangga menjadi salah satu agen situs slot dengan RTP tertinggi, mencapai 98%! Ini berarti Anda memiliki peluang lebih besar untuk meraih kemenangan dalam setiap putaran permainan. Beragam Pilihan Permainan SPARTA88 menyajikan koleksi permainan slot yang beragam dan menarik dari berbagai provider terkemuka. Mulai dari tema klasik hingga yang paling modern, Anda akan menemukan banyak pilihan permainan yang sesuai dengan selera dan preferensi Anda. Kemudahan Bertransaksi Melalui Bank BNI Proses deposit dan penarikan dana di SPARTA88 cepat, mudah, dan aman. Kami menyediakan layanan transaksi melalui Bank BNI untuk kenyamanan Anda. Dengan begitu, Anda dapat melakukan transaksi dengan lancar tanpa perlu khawatir.
    • Slot Bank ALADIN atau Daftar slot Bank ALADIN bisa anda lakukan pada situs WINNING303 kapanpun dan dimanapun, Bermodalkan Hp saja anda bisa mengakses chat ke agen kami selama 24 jam full. keuntungan bergabung bersama kami di WINNING303 adalah anda akan mendapatkan bonus 100% khusus member baru yang bergabung dan deposit. Tidak perlu banyak, 5 ribu rupiah saja anda sudah bisa bermain bersama kami di WINNING303 . Tunggu apa lagi ? Segera Klik DAFTAR dan anda akan jadi Jutawan dalam semalam.
    • Slot Bank PAPUA atau Daftar slot Bank PAPUA bisa anda lakukan pada situs WINNING303 kapanpun dan dimanapun, Bermodalkan Hp saja anda bisa mengakses chat ke agen kami selama 24 jam full. keuntungan bergabung bersama kami di WINNING303 adalah anda akan mendapatkan bonus 100% khusus member baru yang bergabung dan deposit. Tidak perlu banyak, 5 ribu rupiah saja anda sudah bisa bermain bersama kami di WINNING303 . Tunggu apa lagi ? Segera Klik DAFTAR dan anda akan jadi Jutawan dalam semalam.  
    • SLOT MAHJONG WAYS 3 SCATTER HITAM : POLA SLOT MAHJONG 3 SCATTER HITAM HARI INI - TRIK POLA SLOT GACOR x500 SCATTER HITAM KLIK DISINI DAFTAR DISINI SLOT VVIP << KLIK DISINI DAFTAR DISINI SLOT VVIP << KLIK DISINI DAFTAR DISINI SLOT VVIP << KLIK DISINI DAFTAR DISINI SLOT VVIP << SITUS SLOT GACOR 88 MAXWIN X500 HARI INI TERBAIK DAN TERPERCAYA GAMPANG MENANG Dunia Game gacor terus bertambah besar seiring berjalannya waktu, dan sudah tentu dunia itu terus berkembang serta merta bersamaan dengan berkembangnya SLOT GACOR sebagai website number #1 yang pernah ada dan tidak pernah mengecewakan sekalipun. Dengan banyaknya member yang sudah mempercayakan untuk terus menghasilkan uang bersama dengan SLOT GACOR pastinya mereka sudah percaya untuk bermain Game online bersama dengan kami dengan banyaknya testimoni yang sudah membuktikan betapa seringnya member mendapatkan jackpot besar yang bisa mencapai ratusan juta rupiah. Best online Game website that give you more money everyday, itu lah slogan yang tepat untuk bermain bersama SLOT GACOR yang sudah pasti menang setiap harinya dan bisa menjadikan bandar ini sebagai patokan untuk mendapatkan penghasilan tambahan yang efisien dan juga sesuatu hal yang fix setiap hari nya. Kami juga mendapatkan julukan sebagai Number #1 website bocor yang berarti terus memberikan member uang asli dan jackpot setiap hari nya, tidak lupa bocor itu juga bisa diartikan dalam bentuk berbagi promosi untuk para official member yang terus setia bermain bersama dengan kami. Berbagai provider Game terus bertambah banyak setiap harinya dan terus melakukan support untuk membuat para official member terus bisa menang dan terus maxwin dalam bentuk apapun maka itu langsung untuk feel free to try yourself, play with SLOT GACOR now or never !
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.