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

Get multiple armor content at the same time

Discussion in 'Development' started by CupidonSauce173, Apr 20, 2019.

  1. CupidonSauce173

    CupidonSauce173 Zombie

    Messages:
    298
    GitHub:
    cupidonsauce173
    Hi, I am making a plugin that needs to check if a player has example a full diamond armor.

    PHP:
            if($player instanceof Player) {
                if (
    $armorCheck->getHelmet()->getCustomName() === "Helmet Of The Warrior" && $armorCheck->getChestplate()->getCustomName() === "Chestplate Of The Warrior" && $armorCheck->getLeggings()->getCustomName() === "Leggings Of The Warrior" && $armorCheck->getBoots()->getCustomName() === "Boots Of The Warrior") {
                    
    //code
                
    }
            }
    But there is no event, I just want it to check when the player puts armor on it so I thought it would be in the EntityArmorChangeEvent so I wrote these lines in it and it doesn't work. probably because the second line was check if the new item had 4 different names at the same time (I was using the getNewItem() thingy.

    But I am wondering how to avoid the repeating task to know if a player has the "combo" of armor and check it directly when he wear armor.
    Thank you.
     
  2. UnknownOre

    UnknownOre Silverfish

    Messages:
    21
    GitHub:
    UnknownOre
    you can use InventoryTransactionEvent
    example
    PHP:
    <?php

    namespace Author/Plugin;

    use 
    pocketmine\event\Listener;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\player\Player;
    use 
    pocketmine\event\inventory\InventoryTransactionEvent;
    use 
    pocketmine\inventory\ArmorInventory;

    class 
    EventListener implements Listener{
        private 
    $plugin;
     
        public function 
    __construct(PluginMain $plugin){
            
    $this->plugin $plugin;
        }
     
        public function 
    onTransaction(InventoryTransactionEvent $event){
            if(!
    $event->getInventory() instanceof ArmorInventory) return;
            if(!
    $event->getInventory()->getHolder() instanceof Player) return;
            
    $boots $event->getInventory()->getBoots();
            if(
    $boots->getName() == 'CustomName'){
                
    //Do smth
            
    }
         
        }
    }
    ?>
    ?>
     
    Last edited: Apr 20, 2019
  3. CupidonSauce173

    CupidonSauce173 Zombie

    Messages:
    298
    GitHub:
    cupidonsauce173
    Let me jump from my bed and try, I didn't even know about that one!
     
  4. CupidonSauce173

    CupidonSauce173 Zombie

    Messages:
    298
    GitHub:
    cupidonsauce173
    Call to undefined method pocketmine\inventory\transaction\InventoryTransaction::getInventory();

    if I try with getInventories, it tells me that it tries to getBoots() on array.
     
  5. UnknownOre

    UnknownOre Silverfish

    Messages:
    21
    GitHub:
    UnknownOre
    yes because getInventories return array
     
  6. CupidonSauce173

    CupidonSauce173 Zombie

    Messages:
    298
    GitHub:
    cupidonsauce173
    But getInventory still a undefined mrthid for that event (I even just copedc pasted your code and it doesn't work
     
  7. UnknownOre

    UnknownOre Silverfish

    Messages:
    21
    GitHub:
    UnknownOre
    use this
    PHP:
    <?php

    namespace Author/Plugin;

    use 
    pocketmine\event\Listener;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\player\Player;
    use 
    pocketmine\event\inventory\InventoryTransactionEvent;
    use 
    pocketmine\inventory\ArmorInventory;
    use 
    pocketmine\inventory\transaction\InventoryTransaction;

    class 
    EventListener implements Listener{
        private 
    $plugin;
     
        public function 
    __construct(PluginMain $plugin){
            
    $this->plugin $plugin;
        }
     
        public function 
    onTransaction(InventoryTransactionEvent $event){
            foreach(
    $event->getTransactions()->getInventories() as $inventory){
                if(
    $inventory instanceof ArmorInventory){
                    
    $boots $inventory->getInventory()->getBoots();
                    if(
    $boots->getCustomName() == 'CustomName'){
                        
    //do smth
                    
    }
                }
            }
        }
    }
    ?>
    And you can use getActions and check if the inventory is instance of ArmorInventory
    anyway why are you trying to get Armor content?
     
    Last edited: Apr 20, 2019
  8. CupidonSauce173

    CupidonSauce173 Zombie

    Messages:
    298
    GitHub:
    cupidonsauce173
    Error: "Call to undefined method pocketmine\event\inventory\InventoryTransactionEvent::getTransactions()"

    I try to get it so if a player wear a certain armor (like if the player has TestHelmet, TestChesplate, TestLeggings and TestBoots, it gives him/her effects or just do something.
     
  9. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    I guess using InventoryTransactionEvent for this case is a low-level move. It works but there's a lot you'd need to do to get to what EntityArmorChangeEvent already does.

    You are facing this issue because cancellable events are usually called before a change takes place (as they're supposed to). So, at the time the event gets called, the player still isn't wearing the new armor item.
    PHP:
    /** @var EntityArmorChangeEvent $event */
    $player $event->getEntity();
    if(
    $player instanceof Player){
        
    $inventory $player->getArmorInventory();
        
    // $inventory contains the old armor
    }
    The event has $event->getSlot() and $event->getNewItem() that you can utilize to solve your issue.
    PHP:
    $inventory $player->getArmorInventory();

    $new_armor $inventory->getContents();
    $new_armor[$event->getSlot()] = $event->getNewItem();

    if(
    count($new_armor) === 4){
        [
    $helmet$chestplate$leggings$boots] = $new_armor;
        if(
    $helmet->getCustomName() === ...){
            
    // ...
        
    }
    }
     
    CupidonSauce173 likes this.
  10. CupidonSauce173

    CupidonSauce173 Zombie

    Messages:
    298
    GitHub:
    cupidonsauce173
    So basically by using that if the player wear $helmet, $chestplate, $leggings and $boots, the code will run ?
     
  11. CupidonSauce173

    CupidonSauce173 Zombie

    Messages:
    298
    GitHub:
    cupidonsauce173
    I love you lol.
    Ok for real thank you
     
  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.