I want to have it so when an entity like a zombie for example is hit by a player the entity walks towards the player and attacks them back. I spawn the entity like so PHP: $nbt = new CompoundTag('', [ 'Pos' => new ListTag('Pos', [ new DoubleTag('', 253), new DoubleTag('', 4), new DoubleTag('', 253) ]), 'Motion' => new ListTag('Motion', [ new DoubleTag('', 0), new DoubleTag('', 0), new DoubleTag('', 0) ]), 'Rotation' => new ListTag('Rotation', [ new FloatTag('', lcg_value() * 360), new FloatTag('', 0) ]),]); $entity = Entity::createEntity(Entity::ZOMBIE, $this->getServer()->getDefaultLevel(), $nbt); $entity->spawnToAll(); Can anyone teach me how to achieve this behavior? Any help at all is appreciated
Try this: Code: $nbt = $this->makeNBT("Human", $player); $human = Entity::createEntity("Human", $player->getLevel(), $nbt); $human->setMaxHealth(20); $human->setHealth(20); $human->setScale(1); $human->setNameTag($player->getName()); $human->setNameTagVisible(true); $human->spawnToAll(); makeNBT function: Code: private function makeNBT($type, Player $player) { $nbt = new CompoundTag; $nbt->Pos = new ListTag("Pos", [ new DoubleTag(0, $player->getX()), new DoubleTag(1, $player->getY() + 2), new DoubleTag(2, $player->getZ()) ]); $nbt->Motion = new ListTag("Motion", [ new DoubleTag(0, 0), new DoubleTag(1, 0), new DoubleTag(2, 0) ]); $nbt->Rotation = new ListTag("Rotation", [ new FloatTag(0, $player->getYaw()), new FloatTag(1, $player->getPitch()) ]); if($type === "Human") { $player->saveNBT(); $nbt->Inventory = clone $player->namedtag->Inventory; $nbt->Skin = new CompoundTag("Skin", ["Data" => new StringTag("Data", $player->getSkinData()), "Name" => new StringTag("Name", $player->getSkinId())]); } return $nbt; } NOTE: change Human if you want
Thats slapper PHP: $nbt = Entity::createBaseNBT($player, null, 2, 2);$npc = new Zombie($player->getLevel(), $nbt);$npc->spawnToAll();
You two both don't seem to understand what i'm asking for, I know how to spawn a entity of my choice but what I would like to know is how do I get this entity to attack a player back once they've aggravated it.
PHP: /** @var Entity $damager *//** @var Entity $entity */$damage = 1.2;$ev = new EntityDamageByEntityEvent($damager, $entity, EntityDamageEvent::CAUSE_ENTITY_ATTACK, $damage);$entity->attack($ev);if(!$ev->isCancelled()){ //no plugins cancelled the EntityDamageEvent}
Hmm i've tried this method earlier like this PHP: public function entity(EntityDamageEvent $event){ if($event instanceof EntityDamageByEntityEvent){ $damager = $event->getDamager(); $entity = $event->getEntity(); if($damager instanceof Player){ $entity->attack(new EntityDamageByEntityEvent($damager, $entity, EntityDamageEvent::CAUSE_ENTITY_ATTACK, 1)); } } } And I get this error PHP: 28.05 22:28:21 [Server] Server thread/DEBUG #2 Crates/src/Ryan/Crates/Main(57): pocketmine\entity\Living->attack(pocketmine\event\entity\EntityDamageByEntityEvent object) 28.05 22:28:21 [Server] Server thread/DEBUG #1 src/pocketmine/entity/Living(524): pocketmine\entity\Entity->attack(pocketmine\event\entity\EntityDamageByEntityEvent object) 28.05 22:28:21 [Server] Server thread/DEBUG #0 src/pocketmine/entity/Entity(896): pocketmine\plugin\PluginManager->callEvent(pocketmine\event\entity\EntityDamageByEntityEvent object) 28.05 22:28:21 [Server] Server thread/CRITICAL RuntimeException: "Recursive event call detected (reached max depth of 50 calls)" (EXCEPTION) in "src/pocketmine/plugin/PluginManager" at line 685 28.05 22:28:21 [Server] Server thread/CRITICAL Could not pass event 'pocketmine\event\entity\EntityDamageByEntityEvent' to 'Crates v3.0.0-1': Recursive event call detected (reached max depth of 50 calls) on Ryan\Crates\Main Is it still possible to make the zombie attack the player using this method or is there some other route I need to take?
Try: PHP: $entity->attack(new EntityDamageEvent($damager, EntityDamageEvent::CAUSE_ENTITY_ATTACK, 1)); because the function Entity::attack() is instead EntityDamageEvent and not EntityDamageByEntityEvent. as you can see here: https://github.com/pmmp/PocketMine-...3b5cacc/src/pocketmine/entity/Entity.php#L895
ohh I see. I've made that change and it did get rid of all the errors I showed earlier, but the zombie I spawned and hit doesn't hit me back and there's no errors on console about it... Any idea's anyone?
Yeah no problem PHP: <?phpnamespace Ryan;use pocketmine\event\Listener;use pocketmine\plugin\PluginBase;use pocketmine\nbt\tag\CompoundTag;use pocketmine\nbt\tag\DoubleTag;use pocketmine\nbt\tag\FloatTag;use pocketmine\nbt\tag\ListTag;use pocketmine\Player;use pocketmine\event\entity\EntityDamageEvent;use pocketmine\entity\Entity;use pocketmine\event\entity\EntityDamageByEntityEvent;class Main extends PluginBase implements Listener { public function onEnable() { $this->getServer()->getPluginManager()->registerEvents($this, $this); $nbt = new CompoundTag('', [ 'Pos' => new ListTag('Pos', [ new DoubleTag('', 253), new DoubleTag('', 4), new DoubleTag('', 253) ]), 'Motion' => new ListTag('Motion', [ new DoubleTag('', 0), new DoubleTag('', 0), new DoubleTag('', 0) ]), 'Rotation' => new ListTag('Rotation', [ new FloatTag('', lcg_value() * 360), new FloatTag('', 0) ]),]); $entity = Entity::createEntity(Entity::ZOMBIE, $this->getServer()->getDefaultLevel(), $nbt); $entity->setHealth(20); $entity->spawnToAll(); } public function entity(EntityDamageEvent $event){ if($event instanceof EntityDamageByEntityEvent){ $damager = $event->getDamager(); $entity = $event->getEntity(); if($damager instanceof Player){ $entity->attack(new EntityDamageEvent($damager, EntityDamageEvent::CAUSE_ENTITY_ATTACK, 1)); } } }}
Hmmm Try: PHP: $damager->attack(new EntityDamageEvent($entity, EntityDamageEvent::CAUSE_ENTITY_ATTACK, 1)); It is the damager who attacks and not the entity I am wrong, it should work now