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->x = $player->getX(); $pk->y = $player->getY(); $pk->z = $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]); }} }
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 }}
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
Well um, it's been quite a while since I opened this, and it works fine now, just made some mistakes, sorry y'all...