CheckInventory issue.

Discussion in 'Plugin Development' started by TotallyNotWalshy, Sep 19, 2014.

Thread Status:
Not open for further replies.
  1. When I mine and get the blocks even though there is still space it sends a message saying its full, and when it is full it also still sends it.

    Current code:
    Code:java
    1. public static boolean inventoryCheck(Player player) {
    2. boolean isEmpty = true;
    3. for (ItemStack item : player.getInventory().getContents()) {
    4. if(item != null) {
    5. isEmpty = false;
    6. }
    7. }
    8. if(isEmpty == false) {
    9. player.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Your inventory is full!!");
    10. }
    11. return isEmpty;
    12. }
    13. }
    14.  


    What happens:
    [​IMG]
    http://prntscr.com/4odmk6
     
  2. Offline

    augustas656

    Your code isn't checking wether your inventory is full it's checking wether it has atleast a single item in it, eventhough there may be space left, if you have any items in your inventory your code's going to fire and say Your inventory is full! Change the variable isEmpty to isFull and make it true. Then when you loop, change if statement to if(item == null) { isFull = false; }

    Don't check wether you have items in your inventory, but check if you have any space, because if you have items in your inventory it doesn't mean it's full, it can though, but if you check if there's space, and there's no space then that means your inventory IS full. It's just a matter of logic.

    To tidy up:

    Code:java
    1. public static boolean inventoryCheck(Player player) {
    2. boolean isFull = true;
    3. for (ItemStack item : player.getInventory().getContents()) {
    4. if(item == null) {
    5. isFull = false;
    6. }
    7. }
    8. if(isFull == true) {
    9. player.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Your inventory is full!!");
    10. }
    11. return isEmpty;
    12. }
    13. }



    Regards
    augustas656
     
  3. augustas656 Checking it. I will edit this post with the results.

    EDIT: Ok works great, thanks!
     
  4. Offline

    augustas656

    I've had a quick search on google and if you want to find out wether there's any possible items to add to an itemstack eventhough the inventory is full check: this bukkit forums thread, so basically let's say your inventory is full of dirt but there's one stack that has 32 items, so half a stack and some more can fit, that thread may help you with this. It's better to have a quick search on the forums or google if you haven't on a problem before asking, unless you're sure.

    Regards
    augustas656
     
  5. Offline

    augustas656

    I'll reply to your problem in your other thread.
     
Thread Status:
Not open for further replies.

Share This Page