# Remove count of things from inventory

Comments in 'Plugin Development' started by AppPhil, Oct 11, 2015.

1. Offline

### AppPhilActive Member

Joined:
Oct 3, 2015
Posts:
54
Hey there,

I want to remove 10 bricks from a player inventory.
I got this code:
PHP:
if($player->getInventory()->contains(new \pocketmine\item\Brick(010))) {$player->getInventory()->remove(new \pocketmine\item\Brick(010));

// more...
}
It checks if the player has 10 bricks or more and that works fine. If the player has 10 or more it runs the next line, but there I got a problem. The plugin removes every brick of the stack and not only 10. How can I only remove 10 bricks from the inventory? Thanks in advance.
2. Offline

### LamboNotable MemberPlugin Developer

Joined:
Sep 14, 2013
Posts:
431
Plugins:
4
Minecraft User:
PHP:
if($player->getInventory()->contains(new \pocketmine\item\Brick(0,10)){ for($i=0;$i<36;$i++){

$item$player->getInventory()->getItem($i); if($item instanceof \pocketmine\item\Brick){
if(
$item->getCount() >= 10){$item->setCount($item->getCount() - 10);$player->getInventory()->setItem($i$item);

$player->getInventory()->sendContents($player);
break;
}
}
}
}
I wrote this on my phone so sorry for syntax errors.
Last edited: Oct 11, 2015
AppPhil and Taha_The_Hacker like this.
3. Offline

### AppPhilActive Member

Joined:
Oct 3, 2015
Posts:
54
I only had to change the $inventory to$player->getInventory() and it works! Thanks!
4. Offline

### LamboNotable MemberPlugin Developer

Joined:
Sep 14, 2013
Posts:
431
Plugins:
4
Minecraft User:
Oh. Sorry. That wasn't a syntax error, just me focusing more on my French homework instead of code
AppPhil likes this.
5. Offline

### LamboNotable MemberPlugin Developer

Joined:
Sep 14, 2013
Posts:
431
Plugins:
4
Minecraft User:
Actually, you should try to use this. Not sure if it will work though.
PHP:
if($player->getInventory()->contains(new \pocketmine\item\Brick(0,10)){$removed 0;
for(
$i=0;$i<36;$i++){$item $player->getInventory()->getItem($i);
if(
$item instanceof \pocketmine\item\Brick){ if($item->getCount() >= 10){

$item->setCount($item->getCount() - 10);

$player->getInventory()->setItem($i$item);$player->getInventory()->sendContents($player); break; }else{ if($item->getCount() + $removed >= 10){$item->setCount($item->getCount() - (10$removed));

$player->getInventory()->setItem($i$item);$player->getInventory()->sendContents($player); break; }else{$removed += $item->getCount();$item->setCount(0);

$player->getInventory()->setItem($i\$item);
}
}
}
}
}
AppPhil likes this.
6. Offline

### AppPhilActive Member

Joined:
Oct 3, 2015
Posts:
54
I will try it later! Thanks!
7. Offline

### LamboNotable MemberPlugin Developer

Joined:
Sep 14, 2013
Posts:
431
Plugins:
4
Minecraft User:
This one should remove the blocks if you have eg. 5 bricks in one slot and 5 in another
AppPhil likes this.