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

It's suddenly not working(removeItems)

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

  1. Hoyee

    Hoyee Baby Zombie

    Messages:
    126
    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("d");
    
          $rand = mt_rand(1,100);
    
          $item = Item::get(Item::DIRT);
    $item->setCount(8);
    $inventory = $player->getInventory();
    if ($command==$a[0] or $a[1] && $inventory->contains($item)) {
      $this->removeItems($item, $inventory);
    
    if($rand<=10){
     $player->getInventory()->addItem(Item::get(1,0,1));
    } else if($rand<=30){
    $player->getInventory()->addItem(Item::get(2,0,1));
    } else if($rand<=60){
    $player->getInventory()->addItem(Item::get(3,0,1));
    } else if($rand<=100){
    $player->getInventory()->addItem(Item::get(4,0,1));
    }
    
    
      $sender->sendMessage($tag . "Success");
    } else {
      $sender->sendMessage($tag . "You need " . $item->getCount() . "Dirts!");
    }
      return true;
    }


    It was working well, but suddenly if I do /$a[0]or$a[1], To start, I should have at least 8 dirts in my inventory, but it just ignore and give mt_rand items. what's wrong????
     
  2. Invy55

    Invy55 Witch

    Messages:
    52
    GitHub:
    invy55
    Try using
    PHP:
    if (($command==$a[0] or $command==$a[1]) && $inventory->contains($item)) {
     
  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.