How do i make it so when i use an item for certain amount of time that item disappear For example using "Diamond Hoe" to interact with block 5 times, the hoe disappear
Create variable which has value 0, and ++ it while player interacted. Then just check if the variable is bigger than 5.
He asked for a hoe. When using a hoe it adds damage to the item. So when the damage is 5 it will disappear. The problem with your explanation is that he needs to save it somewhere and thats so unnecessary. If he said like: a random item, it would be different but he is especially talking about a TOOL here. Tools get damage every single time you use them.
that wont work because what im doing is if player open chest 5 times the item will disappear, and interaction with chest wont decrease the damage of the tools
Item->getDamage() doesn't return if item is damaged, but returns item meta. If you are giving item in game, you use /give player 1:5 and 5 is the damage (meta). Durability you can get from item nbt https://github.com/pmmp/PocketMine-MP/blob/stable/src/pocketmine/item/Durable.php
PHP: public $uses = 0;public function onInteract(PlayerInteractEvent $event) { if($event->getAction() == $event::RIGHT_CLICK_BLOCK && $event->getItem()->getId() == Item::DIAMOND_HOE) { $this->uses++; // $this->uses = $this->uses + 1 if($this->uses >= 5) { // your code $this->uses = 0; } }}
Thank it works, but whenever i stop the server, the incrementing did not get save and reset back to 0. Is there a way to prevent?
I cant seem to find any method on this forum that other player ask on how to store data in another folder
I don't see any reason to use file_put_contents if we can use simple NBT to do everything. Code that GamakCZ sent you will cause problems if multiple players clicked with a hoe! PHP: function interact(\pocketmine\event\player\PlayerInteractEvent $ev){ $item = $ev->getItem(); if($ev->getAction() == $ev::RIGHT_CLICK_BLOCK && $item->getId() == \pocketmine\item\Item::DIAMOND_HOE){ $nbt = $item->getNamedTag() ?? new \pocketmine\nbt\tag\CompoundTag("", []); if(!isset($item->getNamedTag()->uses)){ $nbt->uses = new \pocketmine\nbt\tag\IntTag("uses", 1); }else if($nbt->uses->getValue() < 5){ $nbt->uses = new \pocketmine\nbt\tag\IntTag("uses", $nbt->uses->getValue() + 1); }else{ $nbt->uses = new \pocketmine\nbt\tag\IntTag("uses", 0); $item->pop(); } }}