Reducing Usage

Discussion in 'Development' started by JarguarLoveMC, Jan 31, 2020.

  1. JarguarLoveMC

    JarguarLoveMC Spider Jockey

    Messages:
    49
    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
     
  2. DataLion

    DataLion Spider

    Messages:
    11
    GitHub:
    dataliontje
    PHP:
    if($item->getDamage() => 5){
          
    //REMOVE ITEM HERE
    }
     
  3. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    560
    GitHub:
    GamakCZ
    This seems as code to crash plugin
     
  4. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    560
    GitHub:
    GamakCZ
    Create variable which has value 0, and ++ it while player interacted. Then just check if the variable is bigger than 5.
     
  5. DataLion

    DataLion Spider

    Messages:
    11
    GitHub:
    dataliontje
    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.
     
  6. JarguarLoveMC

    JarguarLoveMC Spider Jockey

    Messages:
    49
    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
     
  7. JarguarLoveMC

    JarguarLoveMC Spider Jockey

    Messages:
    49
    May i ask how to create variable? I only know how to increment it
     
  8. JarguarLoveMC

    JarguarLoveMC Spider Jockey

    Messages:
    49
    wait is it $item = 0, then $item++?
     
  9. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    560
    GitHub:
    GamakCZ
    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
     
    xXNiceAssassinlo YT likes this.
  10. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    560
    GitHub:
    GamakCZ
    It will crash because of this
    PHP:
    => 
    operator.
     
    xXNiceAssassinlo YT likes this.
  11. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    560
    GitHub:
    GamakCZ
    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;
          }
       }
    }
     
  12. JarguarLoveMC

    JarguarLoveMC Spider Jockey

    Messages:
    49
    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?
     
  13. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    560
    GitHub:
    GamakCZ
    You can save it to file when server stops.
     
  14. JarguarLoveMC

    JarguarLoveMC Spider Jockey

    Messages:
    49
    I cant seem to find any method on this forum that other player ask on how to store data in another folder
     
  15. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    560
    GitHub:
    GamakCZ
  16. Kyd

    Kyd Zombie Pigman

    Messages:
    673
    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();
            }
      }
    }
     
    GamakCZ likes this.
  17. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    560
    GitHub:
    GamakCZ
    It's better to use $nbt->setInt($key, $value);
     

Share This Page

  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.