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

Error in spawning npc

Discussion in 'Development' started by kaliiks, Nov 27, 2016.

  1. kaliiks

    kaliiks Zombie

    Messages:
    250
    Hello, I created simple plugin to spawn NPC ,but it do not spawn npc and block sender moving
    code here:
    PHP:
    <?php
    namespace 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->cos($player->yaw M_PI 180);
                
    $block->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();
            }
    }
     
    Last edited: Nov 27, 2016
    CreeperFace likes this.
  2. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    firstly why are you using
     
  3. kaliiks

    kaliiks Zombie

    Messages:
    250
    It is bad?
     
  4. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    yes, you are suppose to register a command listener
     
  5. kaliiks

    kaliiks Zombie

    Messages:
    250
    and it fix my problem??
     
    Last edited: Nov 27, 2016
  6. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    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
     
  7. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    As @Thunder33345 said, it's bad practice to use PlayerCommandPrePlayerEvent try using command map. Debug the code using, var_dump or echo.
     
    Last edited: Nov 28, 2016
  8. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Actually PocketMine has poor support for unregistering commands. Either way is bad practice :D
     
  9. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    command listener means something that listen to command not exactally anything particulare
     
  10. imYannic

    imYannic Baby Zombie

    Messages:
    113
    You need to add $player->getX/Y/Z() to $block->x and $block->z, otherwise it doesnt spawn to you
     
  11. kaliiks

    kaliiks Zombie

    Messages:
    250
    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->cos($player->yaw M_PI 180);
                    
    $block->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
     
  12. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    What do you mean by random position? How did you find it then?
     
  13. kaliiks

    kaliiks Zombie

    Messages:
    250
    It spawning humans to random position near my lobby
     
  14. imYannic

    imYannic Baby Zombie

    Messages:
    113
    Yeah why not just ignore my post and still ask for help althought I said why it spawns at random positions.
     
  15. imYannic

    imYannic Baby Zombie

    Messages:
    113
    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.
     
  16. imYannic

    imYannic Baby Zombie

    Messages:
    113
    Lol, I meant $block->x= <the formual> + $block->getX(),
     
  17. kaliiks

    kaliiks Zombie

    Messages:
    250
    thx, now it works.
    Can i set godmode to npc?
     
  18. Harviy11

    Harviy11 Baby Zombie

    Messages:
    100
    use
    PHP:
    $npc->setMaxHealth(9999);
    $npc->setHealth(9999);
    It block giving npc damage
     
  19. kaliiks

    kaliiks Zombie

    Messages:
    250
    It do not block giving npc damage, it only make npc unkillable because lots of hp, but thanks. :)
     
    Harviy11 likes this.
  20. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    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.
     
    Thunder33345 and HimbeersaftLP like this.
  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.