Solved Add Items and Stop When Inventory is Full

Discussion in 'Plugin Development' started by Compressions, Apr 6, 2013.

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

    Compressions

    I want to add mushroom soup to a player's inventory until the inventory is full, where I want to stop giving them the mushroom soup. Is there an effective or efficient way to stop the code upon the full inventory check? I believe the check is something like this:
    Code:java
    1. Player player = (Player) sender;
    2. PlayerInventory pi = p.getInventory;
    3. if(pi.firstEmpty() == -1) {
    4. //run code here
    5. }


    I don't know, so help would be greatly appreciated! :D Thanks!

    Code:
    Code:java
    1. } if(cmd.getName().equalsIgnoreCase("refill")) {
    2. Player p = (Player) sender;
    3. PlayerInventory pi = p.getInventory();
    4. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    5. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    6. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    7. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    8. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    9. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    10. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    11. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    12. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    13. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    14. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    15. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    16. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    17. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    18. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    19. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    20. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    21. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    22. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    23. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    24. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    25. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    26. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    27. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    28. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    29. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    30. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    31. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    32. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    33. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    34. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    35. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    36. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    37. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    38. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    39. p.sendMessage(ChatColor.AQUA + "Your " + ChatColor.YELLOW + "mushroom soup " + ChatColor.AQUA + "has been delivered!");
    40. }
     
  2. Offline

    MrSparkzz

    Compressions The best way to do this is to use a while loop
    Code:java
    1.  
    2. Player p = (Player) sender;
    3. PlayerInventory pi = p.getInventory();
    4.  
    5. while (!(pi.firstEmpty() == -1)) {
    6. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    7. }
    8.  
    9. p.sendMessage(ChatColor.AQUA + "Your " + ChatColor.YELLOW + "mushroom soup " + ChatColor.AQUA + "has been delivered!");
    10.  
     
  3. Offline

    Compressions

    MrSparkzz Will the while loop replace all of my addItem lines?
     
  4. Offline

    MrSparkzz

    Compressions Yes, it'll take the place of those lines, it'll simplify your code and make it look more professional.
     
    Compressions likes this.
  5. Offline

    Compressions

    MrSparkzz Thanks! Solved. Just realized that Bukkit cancels inventory overflow, but, as you stated,
    MrSparkzz How would you use a while loop to check if you have less than or equal to a certain Material?

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

    MrSparkzz

    I know you can check for a direct value, but I don't know how you would implement less than or greater than into the pi.contains() method. I'm gonna do some testing and I'll get back to you as soon as possible.
     
  7. Offline

    Compressions

  8. Offline

    MrSparkzz

    Compressions I'm not sure if this is what you were looking for, but this, after every loop, will check to see if the amount of Mushroom Stew is a certain amount. if it is, then it will exit the loop and continue the code.

    Code:java
    1.  
    2. while (!(pi.firstEmpty() == -1)) {
    3. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    4.  
    5. if (pi.contains(Material.MUSHROOM_SOUP, 10)) {
    6. break;
    7. }
    8. }
    9.  
    10. p.sendMessage(ChatColor.AQUA + "Your " + ChatColor.YELLOW + "mushroom soup " + ChatColor.AQUA + "has been delivered!");
    11.  
     
  9. Offline

    Compressions

    MrSparkzz I was using it for another command, where it adds 8 mushroom soup, or it has a while loop that counts until it adds 8 mushroom soup.
     
  10. Offline

    MrSparkzz

    Compressions are you saying, it will add mushroom soup until their inventory has 8 in it? Or?
     
  11. Offline

    Compressions

    MrSparkzz It disregards how many mushroom soup the inventory has. What it needs to check is to add mushroom soup until the check has added 8 mushroom soup.
     
  12. Offline

    MrSparkzz

    Compressions I solved it.
    Code:java
    1.  
    2. int count = 0;
    3. do {
    4. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    5. count++;
    6. if (pi.firstEmpty() == -1) break;
    7. } while (count < 8);
    8.  

    This is a do loop. Every time it runs it will add one to the "count" variable. There's a while check at the end, which will check if "count" is less than 8. If it is then it will exit the loop. There's also a check in side the loop which will check to see if there is any room left. If there isn't then it will break the loop.
     
    Compressions likes this.
  13. Offline

    Compressions

Thread Status:
Not open for further replies.

Share This Page