I'm throwing IronSword using this code Spoiler 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->y + $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; $sword= Entity::createEntity("Item", $player->getLevel(), $nbt, $player); $sword->setMotion($sword->getMotion()->multiply($f)); $sword->spawnToAll(); } How can I check if sword interact with Entity (Player) ?
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(267, 268, 272, 276, 283); // sword IDs if($damager instanceof Player) { if(in_array($itemheld->getId(), $swordids)) { // if damager's item held is a sword // do something } }}
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
Are you attempting to track if the thrown item was picked up or used or what do you mean interacted with? Please specify
The InventoryItemPickupEvent does the same. You can use: PHP: public function onItemPickup(InventoryItemPickupEvent $event) { $swordids = array(267, 268, 272, 276, 283); // sword IDs if(in_array($event->getItem()->getId(), $swordids)) { // if player collects/touches a dropped sword // code }}
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
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 } } } }
What must i write as use pocketmine\grow or what becorse my console said pocketmine\math\AxisAlignedBB::grow() is not definied pls help