1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

How to check items in inventory(?)

Discussion in 'Development' started by Hoyee, Mar 7, 2020.

  1. Hoyee

    Hoyee Baby Zombie

    Messages:
    126
    if ($command == "cc"){
    if($player->getInventory()->getItem(Item::get(Item::STONE)) >= 10){

    $player->getInventory()->removeItem(Item::get(Item::STONE, 0, 10));

    $sender->sendMessage($tag."Success");
    }
    }


    When I do /cc, it crash and says "Argument 1 passed to pocketmine\inventory\BaseInventory::getItem() must be of the type int, object given ~~~

    So how can I solve this problem?

    I want to make it if I have more than 10 stones in my inventory and when i say /cc, remove 10 stones in my inventory.
     
  2. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    Put this function into your plugin: https://forums.pmmp.io/threads/removing-items-from-players-invent-problem.8686/#post-67327
    It allows you to remove a specific amount of items, even if they're spread around in the inventory.

    Then you can use this code:
    PHP:
    $item Item::get(Item::STONE);
    $item->setCount(10);
    $inventory $player->getInventory();
    if (
    $inventory->contains($item)) {
      
    removeItems($item$inventory);
      
    $sender->sendMessage($tag "Success");
    } else {
      
    $sender->sendMessage($tag "You need " $item->getCount() . " x " $item->getName() . "!");
    }
     
    GamakCZ likes this.
  3. Hoyee

    Hoyee Baby Zombie

    Messages:
    126
    I do what you said, but when I /cc, it crash and says "Call to undefined function ga\removeItems()" (EXCEPTION)~~

    Here is the code





    private function removeItems(Item $item, BaseInventory $inventory){
    $checkDamage = !$item->hasAnyDamageValue();
    $checkTags = $item->hasCompoundTag();
    $checkCount = $item->getCount() === null ? false : true;
    $count = $item->getCount();
    foreach($inventory->getContents() as $index => $i){
    if($item->equals($i, $checkDamage, $checkTags)){
    if($checkCount && $i->getCount() > $count) {
    $i->setCount($i->getCount() - $count);
    $inventory->setItem($index, $i);
    return;
    } elseif($checkCount && $i->getCount() < $count) {
    $count -= $i->getCount();
    $inventory->clear($index);
    } else {
    $inventory->clear($index);
    return;
    }
    }
    }
    }




    public function onCommand(CommandSender $sender, Command $command, string $label, array $args): bool {
    $command = $command->getName ();
    $player = $sender->getPlayer ();
    $name = $sender->getName ();
    $tag = "§l§b[ §l§fwow§l§b ] §f";
    $a=array("cc");

    $item = Item::get(Item::STONE);
    $item->setCount(8);
    $inventory = $player->getInventory();
    if ($command==$a[0] && $inventory->contains($item)) {
    removeItems($item, $inventory);
    $sender->sendMessage($tag . "success");
    } else {
    $sender->sendMessage($tag . "You need" . $item->getCount() . " x " . $item->getName() . "!");
    }
    return true;
    }

    }
     
  4. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    Ah, I forgot, it's a class method so you need to do $this->removeItems instead
     
  5. Hoyee

    Hoyee Baby Zombie

    Messages:
    126
    I edit to $this->removeItems() but it says TypeError: "Argument 2 passed to ga\GA::removeItems() must be an instance of ga\BaseInventory, instance of pocketmine\inventory\PlayerInventory given, called in~~~



    private function removeItems(Item $item, BaseInventory $inventory){
    $checkDamage = !$item->hasAnyDamageValue();
    $checkTags = $item->hasCompoundTag();
    $checkCount = $item->getCount() === null ? false : true;
    $count = $item->getCount();
    foreach($inventory->getContents() as $index => $i){
    if($item->equals($i, $checkDamage, $checkTags)){
    if($checkCount && $i->getCount() > $count) {
    $i->setCount($i->getCount() - $count);
    $inventory->setItem($index, $i);
    return;
    } elseif($checkCount && $i->getCount() < $count) {
    $count -= $i->getCount();
    $inventory->clear($index);
    } else {
    $inventory->clear($index);
    return;
    }
    }
    }
    }




    public function onCommand(CommandSender $sender, Command $command, string $label, array $args): bool {
    $command = $command->getName ();
    $player = $sender->getPlayer ();
    $name = $sender->getName ();
    $tag = "§l§b[ §l§fwow§l§b ] §f";
    $a=array("cc");

    $item = Item::get(Item::STONE);
    $item->setCount(8);
    $inventory = $player->getInventory();
    if ($command==$a[0] && $inventory->contains($item)) {
    $this->removeItems($item, $inventory);
    $sender->sendMessage($tag . "Success");
    } else {
    $sender->sendMessage($tag . "You Need " . $item->getCount() . " x " . $item->getName() . "!");
    }
    return true;
    }

    }
     
  6. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    You will need to add
    PHP:
    use pocketmine\inventory\BaseInventory;
    to your other imports.

    And please use php tags for pasting code (use the + button in the text editor to insert one)
     
  7. Hoyee

    Hoyee Baby Zombie

    Messages:
    126
    Wow!!!!!!!!!!!!! thanks!!! I love you
     
    HimbeersaftLP likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.