NPC not spawning

Discussion in 'Plugin Development' started by guitargun, Aug 15, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    guitargun

    So I made my own npc spawning stuff just to get rid of some dependencies. I encountered a problem with spawning/despawning/respawning the npc's.
    does anyone know how to correctly spawn it?
    the class extends EntityPlayer

    Code:
    @Override
        public void spawn(Location location, NPCSpawnReason reason, NPCEntity npc) {
            World world = location.getWorld();
            sendPacketsTo(Bukkit.getOnlinePlayers(), new PacketPlayOutPlayerInfo(
                    EnumPlayerInfoAction.ADD_PLAYER, npc));
            WorldServer worldServer = ((CraftWorld) world).getHandle();
            setPositionRotation(location.getX(), location.getY(), location.getZ(),
                    location.getYaw(), location.getPitch());
    
            worldServer.addEntity(npc, SpawnReason.CUSTOM);
    
            this.spawned = true;
            Bukkit.getPluginManager().callEvent(
                    new NPCSpawnEvent(npc, location, reason));
            plugin.questers.resetHear(getUniqueId());
        }
    
        @Override
        public void despawn(NPCSpawnReason reason) {
            this.spawned = false;
            plugin.questers.canceltasks(getUniqueId());
            sendPacketsTo(Bukkit.getOnlinePlayers(), new PacketPlayOutPlayerInfo(
                    EnumPlayerInfoAction.REMOVE_PLAYER, this));
            if (reason == NPCSpawnReason.DESPAWN) {
                Bukkit.getPluginManager().callEvent(new NPCDespawnEvent(this));
            }
            this.getWorld().removeEntity(this);
        }
        @Override
        public void respawn() {
            Location respawnloc = getCurrentloc();
            this.spawned = false;
            NPCEntity npc = this;
            despawn(NPCSpawnReason.RESPAWN);
            spawn(respawnloc, NPCSpawnReason.RESPAWN, npc);
        }
    
    
    and the packet send methods if you are interested.
    Code:
        private void sendPacketsTo(Iterable<? extends Player> recipients,
                Packet... packets) {
            Iterable<EntityPlayer> nmsRecipients = Iterables.transform(recipients,
                    new Function<Player, EntityPlayer>() {
                        public EntityPlayer apply(Player input) {
                            return (EntityPlayer) getHandle(input);
                        }
                    });
            for (EntityPlayer recipient : nmsRecipients) {
                sendPacketsTo(recipient, packets);
            }
        }
      
        private void sendPacketsTo(EntityPlayer player, Packet... packets) {
            if (player != null) {
                for (Packet p : packets) {
                    if (p == null) {
                        continue;
    
                    }
                    player.playerConnection.sendPacket(p);
                }
            }
        }
     
Thread Status:
Not open for further replies.

Share This Page