NPC - cancel receiving damage on NPC by player

Discussion in 'Development' started by FirzMob, Sep 8, 2019.

  1. FirzMob

    FirzMob Creeper

    Messages:
    3
    I need the NPC to take no damage. Where and how to write it?
    PHP:
    <?php
    namespace npc;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\entity\EntityDamageEvent;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\Player;
    use 
    pocketmine\entity\Human;
    use 
    pocketmine\nbt\NBT;
    use 
    pocketmine\math\Vector3;
    use 
    pocketmine\nbt\tag\CompoundTag;
    use 
    pocketmine\nbt\tag\ListTag;
    use 
    pocketmine\nbt\tag\DoubleTag;
    use 
    pocketmine\nbt\tag\FloatTag;
    use 
    pocketmine\nbt\tag\StringTag;


    class 
    npcmain extends PluginBase implements Listener{
        public function 
    onCommand(CommandSender $playerCommand $commandstring $label, array $args,) : bool{
            if(
    $command->getName() == "npc"){
                if(
    $player instanceof Player){
                       if(isset(
    $args[0])){
                        if(
    $args[0] == "create"){
                            
    $this->createNpc($player);
                        }
                    }else {
                        
    $p->sendMessage("/npc [CREATE]");
                    }
                }
            } return 
    true;
        }
        public function 
    createNpc(Player $player){
            
    $nbt = new CompoundTag();

            
    $nbt->setTag(new ListTag("Pos", [
                new 
    DoubleTag(""$player->getX()),
                new 
    DoubleTag(""$player->getY()),
                new 
    DoubleTag(""$player->getZ())
            ]));
            
    $nbt->setTag(new ListTag("Motion", [
                new 
    DoubleTag(""0),
                new 
    DoubleTag(""0),
                new 
    DoubleTag(""0)
            ]));
            
    $nbt->setTag(new ListTag("Rotation", [
                new 
    FloatTag(""$player->getYaw()),
                new 
    FloatTag(""$player->getPitch())
            ]));
            
    $nbt->setTag(new CompoundTag("Skin", [
                
    "Data" => new StringTag("Data"$player->getSkin()->getSkinData()),
                
    "Name" => new StringTag("Name"$player->getSkin()->getSkinId())
            ]));

            
    $human = new Human($player->getLevel(), $nbt);
            
    $human->spawnToAll();
            
    $human->setNameTagVisible(false);
            
    $player->getLevel()->addEntity($human);
            
    $player->sendMessage("§6{†DINOHARD†} §2NPC успешно создан.");
            return 
    true;
        }
    }
     
  2. xZeroMCPE

    xZeroMCPE Witch

    Messages:
    68
    GitHub:
    xZeroMCPE
    Listen on
    Code:
    EntityDamageEvent
    and check if the entity if yours, by checking if it's a Human, nametag, or even instance of your class if you extended Human.
     
  3. FirzMob

    FirzMob Creeper

    Messages:
    3
    And how to do it in the new public. And so that it was used
     

Share This Page

  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.