1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

How I spawn a simple npc?

Discussion in 'Development' started by Enrico Angelon, Feb 22, 2018.

  1. Enrico Angelon

    Enrico Angelon Spider Jockey

    Messages:
    37
    GitHub:
    herryyt
    Hi everyone, I'm making a server core with multiple classes, doing them I'm learning pmmp API at same time, I know php and a little php OOP, but I need some help because there are things that I don't understand, how do I spawn a simple npc in my lobby with custom pos? I watched other threads but were useless for them. Thx for you help, I appreciate it!
     
  2. xXNiceAssassinlo YT

    xXNiceAssassinlo YT Zombie Pigman

    Messages:
    499
    GitHub:
    xXNiceYT
    Look at Slapper Plugin
     
  3. Enrico Angelon

    Enrico Angelon Spider Jockey

    Messages:
    37
    GitHub:
    herryyt
    It's useless when u want to make only one core ._.
     
  4. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    598
    GitHub:
    GamakCZ
    PHP:
    public function spawnNPC(Player $player)  {
                    
    $nbt = new CompoundTag;
                    
    $nbt->Pos = new ListTag("Pos", [
                        new 
    DoubleTag(""$player->getX()),
                        new 
    DoubleTag(""$player->getY()),
                        new 
    DoubleTag(""$player->getZ())
                    ]);
                    
    $nbt->Motion = new ListTag("Motion", [
                        new 
    DoubleTag(""0),
                        new 
    DoubleTag(""0),
                        new 
    DoubleTag(""0)
                    ]);
                    
    $nbt->Rotation = new ListTag("Rotation", [
                        new 
    FloatTag(""$player->getYaw()),
                        new 
    FloatTag(""$player->getPitch())
                    ]);
                    
    $nbt->Health = new ShortTag("Health"20);
                    
    $nbt->Skin = new CompoundTag("Skin", [
                        
    "Data" => new StringTag("Data"$player->getSkin()->getSkinData()),
                        
    "Name" => new StringTag("Name"$player->getSkin()->getSkinId())]);

                    
    $human = new Human($player->getLevel(), $nbt);
                    
    $human->setNameTag($player->getName());
                    
    $human->setNameTagVisible(false);
                    
    $human->spawnTo($player);
    }
     
  5. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    It's useless to make core when you can use slapper ;)
     
  6. Daniktheboss

    Daniktheboss Baby Zombie

    Messages:
    144
    GitHub:
    daniktheboss
    Irrelevant
     
    artulloss and HyperxXxHound like this.
  7. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    Why? If he don't know to use search bar, it's better for him to use slapper
     
    iiFlamiinBlaze and Thunder33345 like this.
  8. Daniktheboss

    Daniktheboss Baby Zombie

    Messages:
    144
    GitHub:
    daniktheboss
    He asked a question, if you know code then its not hard to post how to spawn a NPC.
     
    ethaniccc, Alemiz, artulloss and 2 others like this.
  9. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    why dont u post how to spawn an NPC then not talk trash to these people
     
  10. Daniktheboss

    Daniktheboss Baby Zombie

    Messages:
    144
    GitHub:
    daniktheboss
    PHP:
    $pk = new AddPlayerPacket();
        
    $pk->entityRuntimeId Entity::$entityCount++;
        
    $pk->uuid UUID::fromRandom();
        
    $pk->username "NPC";
        
    $pk->position $position;
        
    $pk->item ItemFactory::get(Item::AIR00);
        
    self::$plugin->getServer()->broadcastPacket([$player], $pk);
    1. Not a real entity so its less lag
    2. Here. I posted code
     
    NickTehUnicorn likes this.
  11. Enrico Angelon

    Enrico Angelon Spider Jockey

    Messages:
    37
    GitHub:
    herryyt
    Thanks, it seems working, i edited pmmp adding nbt, and fixed few things, it seems working but it gives this error related at the entity skin: InvalidStateException: "Human must have a valid skin set". So i think the $player->getSkin()->getSkinData(); is not working properly, any help? Thanks.
     
  12. Daniktheboss

    Daniktheboss Baby Zombie

    Messages:
    144
    GitHub:
    daniktheboss
    How are you setting the skin
     
  13. Enrico Angelon

    Enrico Angelon Spider Jockey

    Messages:
    37
    GitHub:
    herryyt
    i used GamakCZ method, so should be this:
    $nbt->Skin = new CompoundTag("Skin", [
    "Data" => new StringTag("Data", $player->getSkin()->getSkinData()),
    "Name" => new StringTag("Name", $player->getSkin()->getSkinId())]);
     
  14. xXNiceAssassinlo YT

    xXNiceAssassinlo YT Zombie Pigman

    Messages:
    499
    GitHub:
    xXNiceYT
    Do u have Player $player

    Btw what’s err
     
  15. Enrico Angelon

    Enrico Angelon Spider Jockey

    Messages:
    37
    GitHub:
    herryyt
    [14:13:39] [Server thread/CRITICAL]: Could not pass event 'pocketmine\event\player\PlayerJoinEvent' to 'HyCrystal v1': Human must have a valid skin set on WMCore\Loader
    [14:13:39] [Server thread/CRITICAL]: InvalidStateException: "Human must have a valid skin set" (EXCEPTION) in "src/pocketmine/entity/Human" at line 92
    [14:13:39] [Server thread/DEBUG]: #0 WallMine PE Core/src/WMCore/Loader(72): pocketmine\entity\Human->__construct(pocketmine\level\Level object, pocketmine\nbt\tag\CompoundTag object)
    [14:13:39] [Server thread/DEBUG]: #1 src/pocketmine/plugin/MethodEventExecutor(38): WMCore\Loader->onJoin(pocketmine\event\player\PlayerJoinEvent object)
    [14:13:39] [Server thread/DEBUG]: #2 src/pocketmine/plugin/RegisteredListener(98): pocketmine\plugin\MethodEventExecutor->execute(WMCore\Loader object, pocketmine\event\player\PlayerJoinEvent object)
    [14:13:39] [Server thread/DEBUG]: #3 src/pocketmine/plugin/PluginManager(683): pocketmine\plugin\RegisteredListener->callEvent(pocketmine\event\player\PlayerJoinEvent object)
    [14:13:39] [Server thread/DEBUG]: #4 src/pocketmine/Player(969): pocketmine\plugin\PluginManager->callEvent(pocketmine\event\player\PlayerJoinEvent object)
    [14:13:39] [Server thread/DEBUG]: #5 src/pocketmine/Player(917): pocketmine\Player->doFirstSpawn()
    [14:13:39] [Server thread/DEBUG]: #6 src/pocketmine/level/Level(2550): pocketmine\Player->sendChunk(integer -2, integer 1, pocketmine\network\mcpe\protocol\BatchPacket object)
    [14:13:39] [Server thread/DEBUG]: #7 src/pocketmine/level/Level(2595): pocketmine\level\Level->sendChunkFromCache(integer -2, integer 1)
    [14:13:39] [Server thread/DEBUG]: #8 src/pocketmine/level/format/io/ChunkRequestTask(88): pocketmine\level\Level->chunkRequestCallback(integer -2, integer 1, pocketmine\network\mcpe\protocol\BatchPacket object)
    [14:13:39] [Server thread/DEBUG]: #9 src/pocketmine/scheduler/AsyncPool(162): pocketmine\level\format\io\ChunkRequestTask->onCompletion(pocketmine\Server object)
    [14:13:39] [Server thread/DEBUG]: #10 src/pocketmine/scheduler/ServerScheduler(342): pocketmine\scheduler\AsyncPool->collectTasks()
    [14:13:39] [Server thread/DEBUG]: #11 src/pocketmine/Server(2543): pocketmine\scheduler\ServerScheduler->mainThreadHeartbeat(integer 784)
    [14:13:39] [Server thread/DEBUG]: #12 src/pocketmine/Server(2285): pocketmine\Server->tick()
    [14:13:39] [Server thread/DEBUG]: #13 src/pocketmine/Server(2158): pocketmine\Server->tickProcessor()
    [14:13:39] [Server thread/DEBUG]: #14 src/pocketmine/Server(1744): pocketmine\Server->start()
    [14:13:39] [Server thread/DEBUG]: #15 src/pocketmine/PocketMine(385): pocketmine\Server->__construct(BaseClassLoader object, pocketmine\utils\MainLogger object, string C:\Users\Enrico\Desktop\PocketMine-MP\, string C:\Users\Enrico\Desktop\PocketMine-MP\plugins\)
    [14:13:39] [Server thread/DEBUG]: #16 C:/Users/Enrico/Desktop/PocketMine-MP/PocketMine-MP.phar(1): require(string phar://C:/Users/Enrico/Desktop/PocketMine-MP/PocketMine-MP.phar/src/pocketmine/PocketMine.php)
    and yes, is registered:
    $player = $e->getPlayer();
     
  16. xXNiceAssassinlo YT

    xXNiceAssassinlo YT Zombie Pigman

    Messages:
    499
    GitHub:
    xXNiceYT
    Did u have $player = $ev->getPlayer()
     
  17. Enrico Angelon

    Enrico Angelon Spider Jockey

    Messages:
    37
    GitHub:
    herryyt
    Yes i have It!
     
  18. Daniktheboss

    Daniktheboss Baby Zombie

    Messages:
    144
    GitHub:
    daniktheboss
    with this code, you cant set skin with your method. Use a packet to set skin
     
  19. Enrico Angelon

    Enrico Angelon Spider Jockey

    Messages:
    37
    GitHub:
    herryyt
    thanks, now is working, but if i want to add it a function onTouch() and a custom skin, what should i have to do? i have to store skin or with my method it auto change evry player join
     
  20. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    Code that @Daniktheboss gave you register entity only to the client, you have to register it in server if you want to check touching it etc without DataPacketReceiveEvent.
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.