Question NMS NPC spawning issue [1.21]

Stormyz

New member
Jul 18, 2024
1
0
1
Hi, I really need help with NMS to spawn an NPC in 1.21.
Here's my code:

Java:
private static void spawnNPC(Player p) { // there's no problem with the player
        CraftPlayer craftP = (CraftPlayer) p;
        ServerPlayer sp = craftP.getHandle();

        MinecraftServer server = sp.getServer();
        ServerLevel level = sp.serverLevel();
        GameProfile gameProfile = new GameProfile(UUID.randomUUID(), "Billy Bob");

        ServerPlayer npc = new ServerPlayer(server, level, gameProfile, new ClientInformation("dqs", 2, ChatVisiblity.HIDDEN, true, 3, HumanoidArm.LEFT, true, true));
        ServerEntity npcServerEntity = new ServerEntity(level, sp, 0, false, packet -> { }, Set.of()); // someone on a forum said to do that ...*1
        npc.setPos(p.getX(), p.getY(), p.getZ());

        ServerGamePacketListenerImpl ps = sp.connection;
        ps.send(new ClientboundAddEntityPacket(npc, npcServerEntity)); // *1... and put it here
    }

But, nothing happens...
And 1.21 is really different from 1.20.1 that's why I can't just do the same thing than in the Kody Simpson tutorial. ( it's ClientboundAddEntityPacket and no ClientboundAddPlayerPacket )
I searched on the web but nothing can really help me ( I think because 1.21 is recent )
Of course I'm using mojang mappings
 

egg82

New member
Jul 23, 2024
1
0
1
Mostly like you're missing the player list / tab menu packet, and/or the entity data packet to be sent.
(thanks kenny and EC)

As an aside, perhaps just using PLib (ProtocolLib) would be better suited to this than NMS?