Try this (I don't know if works) PHP: public function onEntityDamageByEntityEvent(EntityDamageByEntityEvent $ev){ $player = $ev->getDamager(); $sheep = $ev->getEntity(); if(!($player instanceof Player)) return; if(!($sheep instanceof Sheep)) return; $color = $this->getSheepColor($sheep); $this->setSheepColor($sheep, $color); $color++; if($color > 15) $color = 0; $ev->setCancelled(true); //Prevent killing of sheep}private function getSheepColor(Sheep $sheep){ return $sheep->namedtag["color"];}private function setSheepColor(Sheep $sheep, int $color){ $sheep->namedtag->Color = new IntTag("color", $color);}
Yes but no, with EntityDamageEvent you can't check who is the attacker, with EntityDamageByEntityEvent you can do this.
I recommend using DataPacketReceiveEvent with InteractPacket. You can use it to distinguish between left-clicks (click) and right-clicks (long-tap) (like shears), using the InteractPacket::$action field and comparing it against the constants in the InteractPacket class.
You can check the attacker with PHP: $damager = $event->getEntity()->getLastDamageCause()->getDamager(); if($damager instanceof Player){$event->getEntity()->sendMessage("A player hits you");}
Just use DataPacketReceiveEvent... PHP: function ev_pkRecv(\pocketmine\network\protocol\DataPacketReceiveEvent $ev){ $pk = $ev->getPacket(); if($pk->pid() === \pocketmine\network\protocol\Info::INTERACT_PACKET){ $type = $pk->action; if($type === \pocketmine\network\protocol\InteractPacket::ACTION_LEFT_CLICK){ $eid = $pk->target; $entity = $ev->getPlayer()->getLevel()->getEntity($eid); if($entity instanceof \pocketmine\entity\Sheep){ $color = $entity->namedTag["Color"]; if($color === YOUR_TARGET_COLOR){ // ref: http://minecraft.gamepedia.com/Data_values#Wool.2C_Stained_Clay.2C_Stained_Glass_and_Carpet executeTarget(); } } } }}
We mean you cannot directly handle EntityDamageByEntityEvent. You have to do this: PHP: public function onDamage(EntityDamageEvent $event){ if($event instanceof EntityDamageByEntityEvent){ //Stuff }}
And why not use DataPacketReceiveEvent... It is much better than trying to detect an entity harming another entity, which can be of myriad reasons... Even an arrow shooting that entity is an EntityDamageByEntityEvent... And any players who damaged another player through a plugin-defined function can make it impossible to check whether it is called by clicking a ship...