Solved Spawn An NPC?

Discussion in 'Spigot Plugin Development' started by Giorgino, Feb 16, 2020.

  1. Hi everyone, by chance someone knows how to spawn an NPC?
     
  2. Define NPC,do you want a player like npc or a custom villager?
     
    • Useful Useful x 1
  3. SteelPhoenix

    Moderator

    Use PacketPlayOutNamedEntitySpawn (if the entity is a player he also needs to be on tab)
     
    • Useful Useful x 1
  4. Like a fake player
     
  5. Ah OK. So I have to use NMS and I have to remove the NPC from the tab, Right?
     
  6. Yes. In order to spawn it, you need to use PacketPlayOutNamedEntitySpawn, which has the parameters: Entity ID (Can be anything IIRC), GameProfile ID (https://github.com/Techcable/Authlib/blob/master/src/main/java/com/mojang/authlib/GameProfile.java source, no docs), Location x, y, z, yaw, pitch
    yaw and pitch needs to be fixed, here's my method since it's fairly simple
    Code (Java):

    public static byte fixYawPitch(float value) {
        return (byte) ((int) (value * 256.0F / 360.0F));
    }
     

    packet code can be set using reflection, for instance, the private field named "a" being the Entity ID(there might be another way to do it but I don't know it)
    a StackOverflow post on reflection https://stackoverflow.com/questions/32716952/set-private-field-value-with-reflection

    you then need to send the packet to the players you want to see the NPC, you can do that using either for (Player a : Bukkit.getOnlinePlayers()) or just using a single player by getting the player's handle, getting the connection from that handle, and then sending the packet with that connection

    Here's a good mapping viewer for viewing deobfuscated NMS values https://minidigger.github.io/MiniMappingViewer/#/ (shoutout MiniDigger)

    and some tutorials I watched for NMS:

     
    • Winner Winner x 2