Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Check if inventory has no items

Comments in 'Plugin Development' started by AndrewBit, Jan 2, 2016.

  1. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    How I can check this? I'm not pretty sure...
  2. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    get the inv array and check it?
  3. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    And how I can check it? :/

    SOLVED, I was checking PocketMine classes and I saw firstEmpty x)
    I suppose that
    PHP:
    $inv->firstEmpty() == 0;
    is enought.
    Thanks!
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    No. What if there's a stack in the second slot?
    You must iterate over every slot and check if the inventory has a slot that isn't air.
    hoyinm14mc likes this.
  5. Lambo
    Offline

    Lambo Notable Member Plugin Developer

    Joined:
    Sep 14, 2013
    Posts:
    431
    Plugins:
    4
    Minecraft User:
    Lambo
    Something like this?
    PHP:
    if(count($inv->getContents()) === 0){

    }
    KubixQ and AndrewBit like this.
  6. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    My life is easier with you.
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Are you sure it doesn't return an array full of air items?
  8. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    In my case, I'm sure. :p I'm using it on a plugin and works fine.
  9. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    Here is your solution:
    PHP:
    public function isInventoryEmpty(Player $player) {
      for(
    $i 0$i $player->getInventory()->getSize(); $i++) {
        if(
    $player->getInventory()->getItem($i)->getId() == 0) {
          return 
    true;
        }
      }
    }
  10. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    Nope, that will return true when it finds the first empty slot
    PHP:
    public function isInventoryEmpty(Player $player) {
      for(
    $i 0$i $player->getInventory()->getSize(); $i++) {
        if(
    $player->getInventory()->getItem($i)->getId() !== 0) {
          return 
    false;
        }
      }
      return 
    true;
    }

Share This Page

Advertisement