Check if item interact with entity

Discussion in 'Development' started by Kyd, Sep 16, 2017.

  1. Kyd

    Kyd Zombie Pigman

    Messages:
    683
    I'm throwing IronSword using this code
    PHP:
    public function throwSword(Player $shooter){
            
    $player $shooter;
            
    $aimPos $player->getDirectionVector();
            
    $nbt = new CompoundTag("", [
                
    "Pos" => new ListTag("Pos", [
                    new 
    DoubleTag(""$player->x),
                    new 
    DoubleTag(""$player->$player->getEyeHeight()),
                    new 
    DoubleTag(""$player->z)
                ]),
                
    "Motion" => new ListTag("Motion", [
                    new 
    DoubleTag(""$aimPos->x),
                    new 
    DoubleTag(""$aimPos->y),
                    new 
    DoubleTag(""$aimPos->z)
                ]),
                
    "Rotation" => new ListTag("Rotation", [
                    new 
    FloatTag(""$player->yaw),
                    new 
    FloatTag(""$player->pitch)
                ]),
                
    "Health" => new ShortTag("Health"5),
                
    "Item" => new CompoundTag("Item", [
                    
    "id" => new ShortTag("id"Item::IRON_SWORD),
                    
    "Damage" => new ShortTag("Damage"0),
                    
    "Count" => new ByteTag("Count"1),
                ]),
                
    "PickupDelay" => new ShortTag("PickupDelay"0x7F),
            ]);
            
    $f 1.5;
            
    $swordEntity::createEntity("Item"$player->getLevel(), $nbt$player);
            
    $sword->setMotion($sword->getMotion()->multiply($f));
            
    $sword->spawnToAll();
        }
    How can I check if sword interact with Entity (Player) ?
     
  2. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    697
    GitHub:
    winterbuild7074
    You can use the EntityDamageByEntity event and get the damager's item in hand:
    PHP:
    public function onDamage(EntityDamageByEntityEvent $event) {
        
    $damager$event->getDamager();
        
    $itemheld $damager->getInventory()->getItemInHand();
        
    $swordids = array(267268272276283); // sword IDs
        
    if($damager instanceof Player) {
            if(
    in_array($itemheld->getId(), $swordids)) { // if damager's item held is a sword
                // do something
            
    }
        }
    }
     
  3. Kyd

    Kyd Zombie Pigman

    Messages:
    683
    Thanks, but your code check when damager damage entity with iron sword in hand and I want to check when entity interact with THROWEN sword , so sword is not in hand
     
  4. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,479
    GitHub:
    jasonwynn10
    Are you attempting to track if the thrown item was picked up or used or what do you mean interacted with? Please specify
     
  5. Kyd

    Kyd Zombie Pigman

    Messages:
    683
    I want to check when throwen item "hit" player
     
  6. Kyd

    Kyd Zombie Pigman

    Messages:
    683
    Any idea ?
     
  7. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    697
    GitHub:
    winterbuild7074
    The InventoryItemPickupEvent does the same. You can use:
    PHP:
    public function onItemPickup(InventoryItemPickupEvent $event) {
        
    $swordids = array(267268272276283); // sword IDs
        
    if(in_array($event->getItem()->getId(), $swordids)) { // if player collects/touches a dropped sword
            // code
        
    }
    }
     
  8. Kyd

    Kyd Zombie Pigman

    Messages:
    683
    It's bad idea because player can pickup item only if it's on ground and if I throw it to player's head, player can go away before it fall on ground
     
  9. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    488
    GitHub:
    GamakCZ
    Try to use

    PHP:
    foreach ($this->getServer()->getOnlinePlayers() as $player) {
                foreach (
    $player->getLevel()->getNearbyEntities($player->getBoundingBox()->grow(1,1,1), $player) as $entity) {
                    if(
    $entity instanceof Item) {
                        if(
    $entity->getItem()->getId() == \pocketmine\item\Item::IRON_SWORD) {
                            
    // code
                        
    }
                    }
                }
            }
     
    jasonwynn10 likes this.
  10. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,479
    GitHub:
    jasonwynn10
    Why not assign an NBT tag to the item that was thrown, then check if the picked up item has the tag?
     
  11. RoccoYT

    RoccoYT Spider

    Messages:
    14
    GitHub:
    RoccoTheDev
    What must i write as use pocketmine\grow or what becorse my console said pocketmine\math\AxisAlignedBB::grow() is not definied pls help
     
  12. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    488
    GitHub:
    GamakCZ

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.