Help, inventory remover bug

Discussion in 'Plugin Development' started by SirHeadLessNick, Jan 29, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    SirHeadLessNick

    When I run the command "/take cobblestone" with more than 1 stack of cobblestone in my inventory, it will take all of the stacks of cobblestone when it should only take one stack.

    Here's the code:
    Code:java
    1. for (int i = 0; i < inventory.getSize(); i++) {
    2. ItemStack item = inventory.getItem(i);
    3. if (item != null && item.getType() == Material.COBBLESTONE) {
    4. ItemStack newItem;
    5. if (item.getAmount() <= 64) {
    6. newItem = null;
    7. } else {
    8. newItem = item.clone();
    9. newItem.setAmount(newItem.getAmount() - 64);
    10. }
    11. inventory.setItem(i, newItem);
    12. }
    13. }


    Thanks in advance!
     
  2. Offline

    AoH_Ruthless

    SirHeadLessNick
    That's because you are looping through all the inventory slots, regardless of not whether cobblestone has already been removed. You have to figure out of a way to break the for loop if cobblestone has already been removed :)
     
  3. Offline

    SirHeadLessNick

    How?

    ^

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  4. Offline

    Th3Br1x

    You can 'name' a loop as the following:
    Code:java
    1. YourLoopNameHere: for(...){
    2.  
    3. }


    then, from inside of the for-loop, you can break it with:
    Code:java
    1. break YourLoopNameHere;


    I hope this helps you ;)
     
  5. Offline

    SirHeadLessNick

    Cool, cool it works!

    But I have a new bug now. If I have a stack of cobblestone layered out in my inventory like in the picture below
    it will remove 16 cobblestone blocks not the 4 stacks of 16 which make a stack. Why is this? And how do I fix it?
    [​IMG]
    Code:java
    1.  
    2. Take: for(int i = 0; i < 64; i ++) {
    3. ItemStack item = inventory.getItem(i);
    4. if (item != null && item.getType() == Material.COBBLESTONE) {
    5. ItemStack newItem;
    6. if (item.getAmount() <= 64) {
    7. newItem = null;
    8. } else {
    9. newItem = item.clone();
    10. newItem.setAmount(newItem.getAmount() - 64);
    11. }
    12. inventory.setItem(i, newItem);
    13. break Take;
    14. }
    15. }


    ^If anyone knows how to fix it, feel free to tell.

    Thanks in advance!!

    ^

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  6. Offline

    Nghtmr9999

    If I am understanding your code correctly, it looks like you are breaking out of your for loop after you remove one stack of cobblestone, which is why only one stack is removed.

    What exactly are you trying to do? Remove one stack of cobblestone if there is a stack of 64, and all others that aren't?
     
  7. Offline

    SirHeadLessNick


    I do that because if I don't all stacks of cobblestone inside the inventory will be affected

    .

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
Thread Status:
Not open for further replies.

Share This Page