Solved Inventory Can Carry Item

Discussion in 'Spigot Plugin Development' started by XZot1K, May 23, 2016.

  1. Hey everyone, i need some help with creating a method that checks if a inventory can carry a item i have this so far but the problem with this is the last slot is ignored no matter what amount i feed it:

    Code (Text):
        public static boolean canCarryItem(Inventory inv, ItemStack item) {
            int needed = item.getAmount();
            for (int i = 0; i < inv.getSize(); i++) {
                if (needed <= 0) {
                    return true;
                }
                ItemStack is = inv.getItem(i);
                if ((is == null) || (is.getType() == Material.AIR)) {
                    needed -= 64;
                } else if ((is.getType() == item.getType()) && (is.getDurability() == item.getDurability())) {
                    needed -= 64 - is.getAmount();
                }
            }
            return false;
        }
     
  2. MiniDigger

    Supporter

    the last slot is ignored because you check if needed is <= 0 at the beginning of you loop. If the last slot caused needed to be <= 0 then you will not return true because your loop has finished.
    just check if needed is <= 0 after the loop and you should be fine.
     
    • Winner Winner x 1
  3. You my friend are a god! I love simple mistakes. :p
     
  4. MiniDigger

    Supporter

    the simple ones are the hardest to fix. if you look at your code you can't see a mistake because its so simple.
    Remember to mark this as solved.