[1.10] How Spawn NPC (Player) ??

Discussion in 'Spigot Plugin Development' started by fanor, May 16, 2017.

  1. Hi, first sorry for my english, i'm french, and i'm very very bad in english.

    secondly i want to spawn a NPC tpye of Player but when i try i've got this error :


    Code (Text):
    java.lang.IllegalArgumentException: Cannot spawn an entity for org.bukkit.entity.Player
    i have try to find my probleme on the web but nothing, all the solutions i have found are in older version of minecraft ..
    BUT

    when i try to spawn another entity like cow or pig, my code works ..

    any idéa ?

    this is my part of code :

    Code (Text):

            World w = getServer().getWorlds().get(0);
            Entity e = w.spawnEntity(new Location(w, 0, 140, 0), EntityType.PLAYER);
            e.setCustomName("Fanor79");
            e.setCustomNameVisible(true);
            e.setInvulnerable(true);

     
    Thanks.
     
    • Friendly Friendly x 2
  2. You Cannot spawn players anymore, Now it is much more advanced
    (It was removed in a older version)
     
  3. This works in 1.11, not sure about 1.10 but you can try:

    Code (Text):
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            if (!(sender instanceof Player)) {
                return true;
            }
           
            Player player = (Player) sender;
           
            PacketPlayOutNamedEntitySpawn npc = new PacketPlayOutNamedEntitySpawn(((CraftPlayer) player).getHandle());
            try {
                Field field = npc.getClass().getDeclaredField("a");
                field.setAccessible(true);
               
                field.setInt(npc, 123);
                field.setAccessible(!field.isAccessible());
               
            } catch(Exception x) {
                x.printStackTrace();
            }
           
            ((CraftPlayer) player).getHandle().playerConnection.sendPacket(npc);
           
           
            return true;
        }
    when you do the command it will spawn you into the world as a npc

    Code (Text):
            Player player = (Player) sender;
           
            PacketPlayOutNamedEntitySpawn npc = new PacketPlayOutNamedEntitySpawn(((CraftPlayer) player).getHandle());
            try {
                Field field = npc.getClass().getDeclaredField("a");
                field.setAccessible(true);
               
                field.setInt(npc, 123);
                field.setAccessible(!field.isAccessible());
               
            } catch(Exception x) {
                x.printStackTrace();
            }
           
            ((CraftPlayer) player).getHandle().playerConnection.sendPacket(npc);
     
  4. Can't work, i havn't class named "PacketPlayOutNamedEntitySpawn" :/

    Edit :
    I just noticed that I have no class "Packet" I use Maven, I think I have forgotten two three things with spigot import: /

    Edit2:
    When i import Spigot with Build path, i have the class named "PacketPlayOutNamedEntitySpawn" ..
    it's strange ..
     
    #4 fanor, May 17, 2017
    Last edited: May 17, 2017
  5. OK it's fine now

    but how spanw other people ? and when i execute the cmd my older npc despawn for my new npc :/