Jump to content

Accounting for Slot stack limit in Container.mergeItemStack()


Eastonium

Recommended Posts

I recently added a slot to a container I had, and overrided the getSlotStackLimit() function.

However, when I shift-clicked items from my inventory, the value I set in getSlotStackLimit() was completely ignored.

 

I ended up having to override the mergeItemStack() function in my container class to account for the slot stack limit.

I think this would be a good thing to change for all instances however, and given forge already changes the mergeItemStack() function to make sure that the item (to put in) is valid for the slot, I don't see why this shouldn't be added.

 

Here's the new version of the function that I came up with:

//Changed to be sensitive to Slot stack limits
@Override
protected boolean mergeItemStack(ItemStack stack, int startIndex, int endIndex, boolean reverseDirection){
    boolean flag = false;
    int i = startIndex;
    if (reverseDirection) i = endIndex - 1;
    
    if (stack.isStackable()){
        while (stack.stackSize > 0 && (!reverseDirection && i < endIndex || reverseDirection && i >= startIndex)){
            Slot slot = (Slot)this.inventorySlots.get(i);
            ItemStack itemstack = slot.getStack();
            int maxLimit = Math.min(stack.getMaxStackSize(), slot.getSlotStackLimit());
            
            if (itemstack != null && areItemStacksEqual(stack, itemstack)){
                int j = itemstack.stackSize + stack.stackSize;
                if (j <= maxLimit){
                    stack.stackSize = 0;
                    itemstack.stackSize = j;
                    slot.onSlotChanged();
                    flag = true;
                    
                }else if (itemstack.stackSize < maxLimit){
                    stack.stackSize -= maxLimit - itemstack.stackSize;
                    itemstack.stackSize = maxLimit;
                    slot.onSlotChanged();
                    flag = true;
                }
            }
            if (reverseDirection){ 
            	--i;
            }else ++i;
        }
    }
    if (stack.stackSize > 0){
        if (reverseDirection){
            i = endIndex - 1;
        }else i = startIndex;

        while (!reverseDirection && i < endIndex || reverseDirection && i >= startIndex){
            Slot slot1 = (Slot)this.inventorySlots.get(i);
            ItemStack itemstack1 = slot1.getStack();

            if (itemstack1 == null && slot1.isItemValid(stack)){ // Forge: Make sure to respect isItemValid in the slot.
            	if(stack.stackSize <= slot1.getSlotStackLimit()){
            	    slot1.putStack(stack.copy());
                    slot1.onSlotChanged();
                    stack.stackSize = 0;
                    flag = true;
                    break;
            	}else{
            	    itemstack1 = stack.copy();
            	    stack.stackSize -= slot1.getSlotStackLimit();
                    itemstack1.stackSize = slot1.getSlotStackLimit();
                    slot1.putStack(itemstack1);
                    slot1.onSlotChanged();
                    flag = true;
            	}                    
            }
            if (reverseDirection){
                --i;
            }else ++i;
        }
    }
    return flag;
}

I'm not currently using 4 spaces or the other formatting stuff, but I can change that if need be.

Link to comment
Share on other sites

This would be really nice.  I was able to work around the issue in 1.7, as things generally accounted for it except in the TransferStack method, which everyone always supplied a copy-paste from the wiki because It Worked.

 

Now though, I can't.  Even the method above isn't sufficient to fix the issues I have trying to make a hopper with a slot stack limit of 16.

 

One problem is that shift-clicking results in an infinite loop:

at net.minecraft.inventory.Container.retrySlotClick(Container.java:549)

at net.minecraft.inventory.Container.slotClick(Container.java:284)

at net.minecraft.inventory.Container.retrySlotClick(Container.java:549)

at net.minecraft.inventory.Container.slotClick(Container.java:284)

 

"If I failed, retry with the same parameters, maybe it'll work then."  Yeesh.

 

The other is that hopper also doesn't respect is own inventory limits when pulling from other containers.  It pulls the item, puts it into its inventory, then checks the stack against the size limits and deletes the extra.

 

"Click, click" works properly (only 16 are put into the slot) as does hopper-push.

But shift-clicking and hopper-pull is broken.

 

:\

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Finally, someone replied!

 

When are you getting the infinite loop? (hopper-pull, shift-click...)

Also, I made my modifications to the mergeItemStack() function when I came across problems while shift-clicking, and I tested it and it worked fine for me. How is shift-clicking not working for you?

 

I'm determined to get this added to forge (hopefully) now that I know others wish this were a thing.

 

Link to comment
Share on other sites

  • 3 weeks later...

When are you getting the infinite loop? (hopper-pull, shift-click...)

 

Shift-click.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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

    • Baba  Serege [[+27-73 590 8989]] has experience of 27 years in helping and guiding many people from all over the world. His psychic abilities may help you answer and resolve many unanswered questions. He specialize in helping women and men from all walks of life.. 1) – Bring back lost lover. even if lost for a long time. 2) – My lover is abusing alcohol, partying and cheating on me I urgently need help” 3) – Divorce or court issues. 4) – Is your love falling apart? 5) – Do you want your love to grow stronger? 6) – Is your partner losing interest in you? 7) – Do you want to catch your partner cheating on you? – We help to keep your partner faithful and loyal to you. 9) – We recover love and happiness when relationship breaks down. 10) – Making your partner loves you alone. 11) – We create loyalty and everlasting love between couples. 12) – Get a divorce settlement quickly from your ex-partner. 13) – We create everlasting love between couples. 14) – We help you look for the best suitable partner. 15) – We bring back lost lover even if lost for a long time. 16) – We strengthen bonds in all love relationship and marriages 17) – Are you an herbalist who wants to get more powers? 18) – Buy a house or car of your dream. 19) – Unfinished jobs by other doctors come to me. 20) – I help those seeking employment. 21) – Pensioners free treatment. 22) – Win business tenders and contracts. 23) – Do you need to recover your lost property? 24) – Promotion at work and better pay. 25) – Do you want to be protected from bad spirits and nightmares? 26) – Financial problems. 27) – Why you can’t keep money or lovers? 28) – Why you have a lot of enemies? 29) – Why you are fired regularly on jobs? 30) – Speed up money claim spell, delayed payments, pension and accident funds 31) – I help students pass their exams/interviews. 33) – Removal of bad luck and debts. 34) – Are struggling to sleep because of a spiritual wife or husband. 35- ) Recover stolen property
    • OLXTOTO adalah situs bandar togel online resmi terbesar dan terpercaya di Indonesia. Bergabunglah dengan OLXTOTO dan nikmati pengalaman bermain togel yang aman dan terjamin. Koleksi toto 4D dan togel toto terlengkap di OLXTOTO membuat para member memiliki pilihan taruhan yang lebih banyak. Sebagai situs togel terpercaya, OLXTOTO menjaga keamanan dan kenyamanan para membernya dengan sistem keamanan terbaik dan enkripsi data. Transaksi yang cepat, aman, dan terpercaya merupakan jaminan dari OLXTOTO. Nikmati layanan situs toto terbaik dari OLXTOTO dengan tampilan yang user-friendly dan mudah digunakan. Layanan pelanggan tersedia 24/7 untuk membantu para member. Bergabunglah dengan OLXTOTO sekarang untuk merasakan pengalaman bermain togel yang menyenangkan dan menguntungkan.
    • Baba  Serege [[+27-73 590 8989]] has experience of 27 years in helping and guiding many people from all over the world. His psychic abilities may help you answer and resolve many unanswered questions. He specialize in helping women and men from all walks of life.. 1) – Bring back lost lover. even if lost for a long time. 2) – My lover is abusing alcohol, partying and cheating on me I urgently need help” 3) – Divorce or court issues. 4) – Is your love falling apart? 5) – Do you want your love to grow stronger? 6) – Is your partner losing interest in you? 7) – Do you want to catch your partner cheating on you? – We help to keep your partner faithful and loyal to you. 9) – We recover love and happiness when relationship breaks down. 10) – Making your partner loves you alone. 11) – We create loyalty and everlasting love between couples. 12) – Get a divorce settlement quickly from your ex-partner. 13) – We create everlasting love between couples. 14) – We help you look for the best suitable partner. 15) – We bring back lost lover even if lost for a long time. 16) – We strengthen bonds in all love relationship and marriages 17) – Are you an herbalist who wants to get more powers? 18) – Buy a house or car of your dream. 19) – Unfinished jobs by other doctors come to me. 20) – I help those seeking employment. 21) – Pensioners free treatment. 22) – Win business tenders and contracts. 23) – Do you need to recover your lost property? 24) – Promotion at work and better pay. 25) – Do you want to be protected from bad spirits and nightmares? 26) – Financial problems. 27) – Why you can’t keep money or lovers? 28) – Why you have a lot of enemies? 29) – Why you are fired regularly on jobs? 30) – Speed up money claim spell, delayed payments, pension and accident funds 31) – I help students pass their exams/interviews. 33) – Removal of bad luck and debts. 34) – Are struggling to sleep because of a spiritual wife or husband. 35- ) Recover stolen property
  • Topics

×
×
  • Create New...

Important Information

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