Question Multiple questions about NPCs with ServerPlayer 1.21.5

rec

New member
May 12, 2025
4
0
1
1. Is ServerEntity better then ServerPlayer
I have found a post, comments there say that the ServerPlayer 'logic' is unnecessary and a ServerEntity instance should be created with the ServerPlayer. Why is that? And should ServerLevel.addNewPlayer be used or ServerLevel.addFreshEntity?​
2. Does the server send any packets on its own?
... or do all the packets have to be sent manually?​
3. How to make the npc persist on the client
How to make sure that players always see the npc? So it must appear when they rejoin or load chunks with the npc. How can that be handled?​
4. ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER doesn't add the player to the tab list
Tutorials say that you have to remove it from the tab list, but I doesn't appear there in the first place.
Kotlin:
Java:
    val server: MinecraftServer = (Bukkit.getServer() as CraftServer).server;
     val world: ServerLevel = (Bukkit.getWorlds()[0] as CraftWorld).handle;

     // Create game profile
    val uuid: UUID = UUID.randomUUID();
    val clientInfo = ClientInformation("en",1, ChatVisiblity.HIDDEN,false,0, HumanoidArm.LEFT, false, true, ParticleStatus.MINIMAL);
    val profile: GameProfile = GameProfile(uuid, "npc");
    // Create entity
    val body = ServerPlayer(server, world, profile, clientInfo);
    val cookie = CommonListenerCookie(profile, 0, clientInfo, false);
     body.connection = ServerGamePacketListenerImpl(server, Connection(PacketFlow.CLIENTBOUND), body, cookie);
//        world.addFreshEntity(body);

    for (onlinePlayer in Bukkit.getOnlinePlayers()) {
        val connection = (onlinePlayer as CraftPlayer).handle.connection
        connection.send(ClientboundPlayerInfoUpdatePacket(ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER, body));
        connection.send(ClientboundAddEntityPacket(147, uuid, l.x, l.y, l.z, 0.0f, 0.0f, EntityType.PLAYER, 0, Vec3(0.0,0.0,0.0), 0.0))
    }
    world.addNewPlayer(body);