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

Check if n slots are available in the player inventory

Comments in 'Plugin Development' started by EschieEsh, Jul 9, 2016.

  1. EschieEsh
    Offline

    EschieEsh Active Member

    Joined:
    Jun 28, 2014
    Posts:
    69
    Minecraft User:
    Escherion
    PHP:
    $filled_slots 0;
    for(
    $i 0$i $this->player->getInventory()->getSize(); $i++) {
          if(
    $this->player->getInventory()->getItem($i)->getId() !== Item::AIR) {
               
    $filled_slots $filled_slots 1;
          }
    }
    if(!(
    <= $this->player->getInventory()->getSize() - $filled_slots)){
         
    $this->player->sendMessage("Your inventory must have at least n empty slots");
    }

    Where is the problem in this code? I don't get it, everything seems correct
  2. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    Is the constant n already defined or is it just a missing $? And getSize() also returns the armor slots, so do -4.
  3. EschieEsh
    Offline

    EschieEsh Active Member

    Joined:
    Jun 28, 2014
    Posts:
    69
    Minecraft User:
    Escherion
    n is an example. It appears that I have forgotten the armor slots then
    Last edited: Jul 11, 2016
  4. EschieEsh
    Offline

    EschieEsh Active Member

    Joined:
    Jun 28, 2014
    Posts:
    69
    Minecraft User:
    Escherion
    Strange. I did as you said and it didn't work. :) but I did this $this->player->getInventory()->getSize() - 9 and it worked perfectly. Number 9 is a magical number, isn't it? Heh


    $this->player->getInventory()->getSize() returns 45 slots.
    There are only 36 in the inventory. 40 if we include the armor slots. Why is it 45 by default?

    By the way doesn't getSize() automatically take care of armor spots?

    public function getSize(){
    return parent::getSize() - 4;
    }
    Last edited: Jul 11, 2016
  5. minebuilder0110
    Offline

    minebuilder0110 Notable Member

    Joined:
    Dec 8, 2014
    Posts:
    485
    Minecraft User:
    Apparitional
    PHP:
    //$i is a player inventory
    if($i->firstEmpty() === -1){
        
    //things to do if the player's inventory is full
    }
  6. EschieEsh
    Offline

    EschieEsh Active Member

    Joined:
    Jun 28, 2014
    Posts:
    69
    Minecraft User:
    Escherion
    I know this already minebuilder. I know it. And I don't want to check if the inventory is full. I want to check if multiple slots are empty. I just want to know why $this->player->getInventory()->getSize() returns 45
  7. Extreme_Heat
    Offline

    Extreme_Heat Active Member

    Joined:
    Apr 19, 2016
    Posts:
    76
    Minecraft User:
    Extreme_Heat
    EschieEsh likes this.
  8. EschieEsh
    Offline

    EschieEsh Active Member

    Joined:
    Jun 28, 2014
    Posts:
    69
    Minecraft User:
    Escherion
    Oh yeah with Inventory::getContents(). I will try this
  9. EschieEsh
    Offline

    EschieEsh Active Member

    Joined:
    Jun 28, 2014
    Posts:
    69
    Minecraft User:
    Escherion
    Yeah that works. Thank you for the advice.
  10. minebuilder0110
    Offline

    minebuilder0110 Notable Member

    Joined:
    Dec 8, 2014
    Posts:
    485
    Minecraft User:
    Apparitional
    Oh right, didn't see that n there...

Share This Page

Advertisement