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

Adding Items To Players Inventory

Comments in 'Plugin Development' started by Calrizer, Jul 5, 2015.

  1. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    Hello people of PocketMine,

    Just a quick one, I'm trying to add an item to all the players inventories in a certain world. I have this so far but its giving me problems.
    PHP:
    foreach($this->getServer()->getLevelByName($world)->getPlayers() as $p){
          
    $p->getInventory()->addItem($datavalue$ammount);
    }
    The variable $world is declared and works as I tried sending a message to everyone and that worked.
    Maybe my syntax is messed up or I'm using the wrong functions.

    Any feedback is appreciated!

    -Calrizer
  2. InusualZ
    Offline

    InusualZ Active Member Plugin Developer

    Joined:
    Aug 23, 2013
    Posts:
    218
    Plugins:
    1
    Minecraft User:
    Inusu4lZ
    Inventory::addItem parameter is a Item Array. Try using this code.
    PHP:
    $items = [
        
    Item::get($id$data$amount),
        
    Item::get($id$data$amount)
    ];

    foreach(
    $this->getServer()->getLevelByName($world)->getPlayers() as $p)
    {
          
    $p->getInventory()->addItem($items);
    }
    For further problem like this you should check out the docs
    Calrizer likes this.
  3. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    What do I use the $data for?
  4. InusualZ
    Offline

    InusualZ Active Member Plugin Developer

    Joined:
    Aug 23, 2013
    Posts:
    218
    Plugins:
    1
    Minecraft User:
    Inusu4lZ
    Things like wool have data to difference their color.
    Calrizer likes this.
  5. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    I've implemented it now but I'm getting some Class not found errors.
    I've used:
    PHP:
    use pocketmine\Player;
    use 
    pocketmine\inventory\Inventory;
    use 
    pocketmine\Server;
    Do I need any others?
  6. InusualZ
    Offline

    InusualZ Active Member Plugin Developer

    Joined:
    Aug 23, 2013
    Posts:
    218
    Plugins:
    1
    Minecraft User:
    Inusu4lZ
    You have to import the Item class

  7. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    Yep all is working perfectly now!
    Thanks for your help!
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    No, it is not an item array. It is a variadic function where you pass as many items as you want.
  9. InusualZ
    Offline

    InusualZ Active Member Plugin Developer

    Joined:
    Aug 23, 2013
    Posts:
    218
    Plugins:
    1
    Minecraft User:
    Inusu4lZ
    You are right. But its going to work as well
  10. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    No it won't. PHP isn't Java. You can't pass arrays as varargs.

Share This Page

Advertisement