hello, How to create a simple npc that does not move? How to detect it at EntityDamageEvent ? thank you in advance
Please don't forget to add a correct skin to the entity Or you will end up like @Kyd https://forums.pmmp.io/threads/server-crash.1825
No. Don't use DataPacketReceiveEvent. If you can use a PocketMine event, use a PocketMine event. Simply use EntityDamageByEntityEvent. DataPacketReceiveEvent is very aggressive, and you should only use it if you can't do what you want to do without it.
Yeah, it's getting called whenever a packet is received but think about how many other API and event calls you'll be saving by intercepting the packet. When you think about it logically, you'll actually be saving CPU time by handling it yourself (assuming you don't call more functions or events than PocketMine would). PocketMine performs a switch on every packet a player sends anyway so I don't see how another, earlier switch could cause your server too much harm performance wise.
Of course it does. If you hit it you hit an entity, and you are a player => entity which means damage by entity which means EntityDamageByEntityEvent. See Slapper as an example.
PHP: public function onClick(\pocketmine\event\entity\EntityDamageEvent $e) { if($e instanceof \pocketmine\event\entity\EntityDamageByEntityEvent) { $p = $e->getDamager(); $entity = $e->getEntity(); if($entity instanceof Player && $p instanceof Player) { } if($entity instanceof Human) { $e->setCancelled(); if($entity->getNameTag() === "yournametag") { $this->getServer()->dispatchCommand($p, "yourcommand"); } } } } Yes you can use with EntityDamageByEntityEvent example for you .
This will not detect the specific entity you created. I recommend storing a flag in the entity NBT that identifies that your plugin created it. You can then check it in the event handler.
This is my code, and apparently @Marabou don't know how to handle it xd PHP: public function onPacketReceived(DataPacketReceiveEvent $event){ $pk = $event->getPacket(); $player = $event->getPlayer(); if($pk instanceof InteractPacket and $pk->action === InteractPacket::ACTION_LEFT_CLICK){ if($pk->target == $IdOfMyNPC){ //code to execute } } }