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

Solved Prevent NPC from despawning

Discussion in 'Development' started by AppleDevelops, Jul 26, 2017.

  1. Alright, so I'm working on an npc based plugin for my server hub, but whenever I use the entity for transferring servers, it despawns. EDIT: After transferring to another server, and trying to spawn any entity, the entity remains invisible. Here's my code:

    PHP:
    public function attack(EntityDamageEvent $source){
            if(
    $source instanceof EntityDamageByEntityEvent){
                
    $damager $source->getDamager();
                if(
    $damager instanceof Player){
                    
    $pk = new TransferPacket();
                    
    $pk->address "this.is.fake";
                    
    $pk->port 19132;
                    
    $damager->directDataPacket($pk);
                }
            }

    public function 
    spawnTo(Player $player){
        if(
    CoreLoader::getInstance()->hasSpawned === false){
            
    CoreLoader::getInstance()->hasSpawned true;
            
    $pk = new AddPlayerPacket();
            
    $pk->username "";
            
    $pk->uuid $this->getUniqueId();
            
    $pk->item Item::get(Item::BOW);
            
    $pk->pitch $this->pitch;
            
    $pk->yaw $this->yaw;
            
    $pk->$player->getX();
            
    $pk->$player->getY();
            
    $pk->$player->getZ();
            
    $pk->speedX 0;
            
    $pk->speedY 0;
            
    $pk->speedZ 0;
            
    $pk->entityRuntimeId $this->getId();
            
    $pk->metadata $this->dataProperties;

            
    $player->dataPacket($pk);

            
    $player->server->updatePlayerListData($this->getUniqueId(), $this->getId(), "OITQ"$this->skinId$this->skin, [$player]);
        }
    }
        }
     
    Last edited by a moderator: Jul 26, 2017
  2. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    try debugging
     
  3. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    Is it invisible on the server before transferring or on the new server after?
     
  4. chansol

    chansol Spider Jockey

    Messages:
    32
    GitHub:
    ParkChanSol
    use PlayerListPacket();
     
  5. After the player transfers, it won't show up until I restart the server
     
  6. Andrés Komet

    Andrés Komet Creeper

    Messages:
    3
    GitHub:
    GiantQuartz
    Probably it doesn't despawn, it simply die. You must cancel the event EntityDamageEvent or the NPC will get damage.
    Also, you must check if the entity being hurt is a NPC, if not, when you hit any entity you'll be transferred, even if it's a chicken, a common player... any kind of entity.

    I have all my NPCs implementig a interface called Statue to do this. Example:
    PHP:
    public function onEntityDamage(EntityDamageEvent $event) {
            
    $entity $event->getEntity();
            if(
    $entity instanceof Statue) {
                
    $event->setCancelled();
                if(!
    $event instanceof EntityDamageByEntityEvent) {
                    return;
                }
                
    $damager $event->getDamager();
                if(!
    $damager instanceof Player) {
                    return;
                }
                
    // Here you can transfer the player
            
    }
    }
     
    YourName404 likes this.
  7. Andrés Komet

    Andrés Komet Creeper

    Messages:
    3
    GitHub:
    GiantQuartz
    Well, If something doesn't work and I don't know how to fix it, I would rather remove it than have a corrupt server :p
     
    Last edited: Jul 30, 2017
  8. Well um, it's been quite a while since I opened this, and it works fine now, just made some mistakes, sorry y'all...
     
    Sandertv likes 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.