Hello, I created simple plugin to spawn NPC ,but it do not spawn npc and block sender moving code here: PHP: <?phpnamespace NPC;use pocketmine\event\Listener;use pocketmine\entity\Entity;use pocketmine\nbt\tag\IntTag;use pocketmine\nbt\tag\ByteTag;use pocketmine\nbt\tag\StringTag;use pocketmine\nbt\tag\DoubleTag;use pocketmine\nbt\tag\CompoundTag;use pocketmine\nbt\tag\ListTag;use pocketmine\nbt\tag\FloatTag;use pocketmine\entity\Human;use pocketmine\Player;use pocketmine\event\player\PlayerCommandPreprocessEvent;class EventListener implements Listener{ public function Commands(PlayerCommandPreprocessEvent $event) { $cmd = explode(" ", strtolower($event->getMessage())); $player = $event->getPlayer(); if ($cmd[0] === "/npc") { $block = $player; $block->x = cos($player->yaw * M_PI / 180); $block->z = sin($player->yaw * M_PI / 180); $npc = new Human($player->chunk, new CompoundTag("", [ "Pos" => new ListTag("Pos", [ new DoubleTag("", $block->getX()), new DoubleTag("", $block->getY()), new DoubleTag("", $block->getZ()) ]), "Motion" => new ListTag("Motion", [ new DoubleTag("", 0), new DoubleTag("", 0), new DoubleTag("", 0) ]), "Rotation" => new ListTag("Rotation", [ new FloatTag("", 90), new FloatTag("", 0) ]), "Skin" => new CompoundTag("Skin", [ "Data" => new StringTag("Data", $player->getSkinData()) ]) ] )); $npc->spawnToAll(); $npc->setNameTagVisible(); $npc->setNameTagAlwaysVisible(); $npc->setNameTag("TEST"); } $event->setCancelled(); }}
There is no such thing as a command listener, you probably meant command map. For example this is a command map command: https://github.com/InfinityGamers/H...erGames/src/hungergames/command/HGCommand.php You execute your code inside the function execute(): https://github.com/InfinityGamers/H...mes/src/hungergames/command/HGCommand.php#L26 This is how you register a command map: https://github.com/InfinityGamers/H...-1/HungerGames/src/hungergames/Loader.php#L45 See required parameters here: https://github.com/pmmp/PocketMine-MP/blob/master/src/pocketmine/command/CommandMap.php#L38
As @Thunder33345 said, it's bad practice to use PlayerCommandPrePlayerEvent try using command map. Debug the code using, var_dump or echo.
Do not work PHP: $cmd = explode(" ", strtolower($event->getMessage())); $player = $event->getPlayer(); if ($cmd[0] === "/npc") { $block = $player; $block->getX(); $block->getY(); $block->getZ(); $block->x = cos($player->yaw * M_PI / 180); $block->z = sin($player->yaw * M_PI / 180); $npc = new Human($player->chunk, new CompoundTag("", [ "Pos" => new ListTag("Pos", [ new DoubleTag("", $block->getX()), new DoubleTag("", $block->getY()), new DoubleTag("", $block->getZ()) ]), "Motion" => new ListTag("Motion", [ new DoubleTag("", 0), new DoubleTag("", 0), new DoubleTag("", 0) ]), "Rotation" => new ListTag("Rotation", [ new FloatTag("", 90), new FloatTag("", 0) ]), "Skin" => new CompoundTag("Skin", [ "Data" => new StringTag("Data", $player->getSkinData()) ]) ] )); $npc->spawnToAll(); $npc->setNameTagVisible(); $npc->setNameTagAlwaysVisible(); $npc->setNameTag("TEST"); //$npc->setDataProperty(Entity::DATA_NAMETAG, Entity::DATA_TYPE_STRING, "YOUR ENTITY NAME"); //$npc->setDataProperty(Entity::DATA_SHOW_NAMETAG, Entity::DATA_TYPE_BYTE, 1);*/ } $event->setCancelled(false); } }} It spawn npc... But on random position
Yeah why not just ignore my post and still ask for help althought I said why it spawns at random positions.
It spawns at X (0 -1) and Z (0 - 1), because he uses that formula you can see there in $block->x and $block->z.
It doesn't block damage and that is a bad practise, the NPC health will be saved and after a lot of hits they will eventually die. Please do it properly through canncelling entity events.