AdvancedPeripherals icon indicating copy to clipboard operation
AdvancedPeripherals copied to clipboard

Inventory Manager does not pull the correct amount of item

Open jajajafeti opened this issue 3 years ago • 2 comments

Descripe

The function inventoryManager.removeItemFromPlayer(string direction, int count[, int slot\, string item]) does not pull the correct amount (count) of items. It seems to ignore 1 amount of every slot, therefore a slot with 1 item does not count at all and a slot with 2 items counts as 1 (see Video below). Furthermore it does not pull items "randomely" as it states in the documentation.

Steps to reproduce

I hooked the Inventory Manager up to the PC and chest everythings fine and then run the one function as seen in the video.

https://user-images.githubusercontent.com/61298096/160904746-1230f2ae-c130-44ad-8578-4cca1ccce21a.mp4

Multiplayer?

Yes

Version

1.18.1-0.7.9r (Latest 1.18)

Minecraft, Forge and maybe other related mods versions

forge 39.0.88 with advancedperipherals-1.18.1-0.7.12b

Screenshots or Videos

No response

Crashlog/log

No response

jajajafeti avatar Mar 30 '22 18:03 jajajafeti

I think I found the problem in this well documented code.

int amount = count;
        int transferableAmount = 0;

        ItemStack rest = ItemStack.EMPTY;
if (invSlot == -1)
            for (int i = 0; i < inventoryFrom.getContainerSize(); i++) {
                
                ... removed code

                if (stack.isEmpty())
                    if (inventoryFrom.getItem(i).getCount() >= amount) {
                        rest = insertItem(inventoryTo, inventoryFrom.removeItem(i, amount));
                        transferableAmount += amount - rest.getCount();
                        break;
                    } else {
                        int subcount = inventoryFrom.getItem(i).getCount();
                        rest = insertItem(inventoryTo, inventoryFrom.removeItem(i, subcount));
                        amount = count - subcount;
                        transferableAmount += subcount - rest.getCount();
                        if (!rest.isEmpty())
                            break;
                    }
            }

count is the amount of items we want to move and does not get changed. amount gets used to check the amount of items which still need to be transfered subcount is obviously the amount of item in the given slot (iterating over all slots from inventoryFrom)

Here comes the problem if inventoryFrom.getItem(i).getCount() >= amount is false we get the else statement, where amount = count - subcount. The amount gets basically reset for every itteration, in which there is not enough items in the curent and the previous slot. Thus it will never stop until the sum of the count of slot(i) and slot(i+1) equals to the count. Replacing the equation with amount -= subcount should do the trick. I just thought about another option, which gets rid of amount, by just replacing if (inventoryFrom.getItem(i).getCount() >= amount) with if (inventoryFrom.getItem(i).getCount() >= count - transferableAmount) becaus transferableAmount is basically the amount of transfered items (if the target inventory is full the loop breaks anyways). (This also needs to be fixed for the removed Code)

jajajafeti avatar Apr 09 '22 22:04 jajajafeti

Such a wonderful explanation and no pr 😉

Just kidding, I`ll see what I can do

SirEndii avatar Apr 12 '22 17:04 SirEndii