Trying to check if player has available slots in inventory with given material

Discussion in 'Plugin Development' started by makkusan, Sep 25, 2020.

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

    makkusan

    Hello, i've been trying to create a function to check if the player has available slots in the inventory if yes return true and if not then return false. Also a condition would be if the player has a specific material in the inventory. if yes get the amount and check if we can add a given amount to the itemstack. The only problem is, that the function returns true when the player has for example 33 wood and the given amount is 32 so 33 + 32 would be 65 which isnt one stack.

    Code:
    public static boolean hasAvailableSlots(Player p, Material mt, int amount) {
            ItemStack itemToAdd = new ItemStack(mt, amount);
    
            for(ItemStack is : p.getInventory().getContents()) {
                    if(is != null) {
                        System.out.println(">>> 1");
                        if(is.getType() == itemToAdd.getType()) {
                            System.out.println(">>> 2");
                            for(int i = 0; i < 36; i++) {
                                System.out.println(">>> 3");
                                int maxedStackSize = is.getMaxStackSize();
                                int amountOfItem = is.getAmount();
                                int addItemAmount = itemToAdd.getAmount();
                                int restOfItem = maxedStackSize - amountOfItem;
    
                                if(addItemAmount + amountOfItem <= maxedStackSize) {
                                    System.out.println(">>> 5");
                                    i = 32;
                                    return true;
                                } else if(p.getInventory().firstEmpty() == -1) {
                                    System.out.println(">>> 6");
                                    continue;
                                } else {
                                    System.out.println(">>> 7");
                                    i = 32;
                                    return true;
                                }
                            }
                        }
                    } else {
                        System.out.println(">>> 8");
                        return p.getInventory().firstEmpty() != -1;
                    }
            }
            return false;
        }
    
     
  2. @makkusan
    this would be my take

    Code:
    public boolean hasAvailableSlot(Inventory inv, Material m, int amount) {
            if(inv.firstEmpty() == -1) {
                for(ItemStack i : inv.getContents()) {
                    if(i.getType() == m) {
                        int itemAmount = i.getAmount();
                        if((itemAmount + amount) > 64)
                            continue;
                        return true;
                    }
                }
            }
            else {
                return true;
            }
            return false;
        }
     
Thread Status:
Not open for further replies.

Share This Page