NPC plugin with NMS and paper

Discussion in 'Plugin Development' started by KOOBBIII, Oct 25, 2024.

  1. Offline

    KOOBBIII

    Code:java
    1. package de.npc.npcproject;
    2.  
    3. import com.mojang.authlib.GameProfile;
    4. import net.minecraft.network.Connection;
    5. import net.minecraft.network.protocol.login.ClientboundHelloPacket;
    6. import net.minecraft.resources.ResourceKey;
    7. import net.minecraft.server.MinecraftServer;
    8. import net.minecraft.server.level.ClientInformation;
    9. import net.minecraft.server.level.ServerLevel;
    10. import net.minecraft.server.level.ServerPlayer;
    11. import net.minecraft.server.network.ServerGamePacketListenerImpl;
    12. import net.minecraft.world.entity.HumanoidArm;
    13. import net.minecraft.world.entity.player.ChatVisiblity;
    14. import net.minecraft.world.level.Level;
    15. import net.minecraft.world.phys.Vec3;
    16. import org.bukkit.Bukkit;
    17. import org.bukkit.craftbukkit.entity.CraftPlayer;
    18. import org.bukkit.entity.Player;
    19. import net.minecraft.network.protocol.game.*;
    20.  
    21. import java.util.*;
    22.  
    23. public class NPC2 {
    24.  
    25. private static final List<ServerPlayer> NPC_LIST = new ArrayList<>();
    26.  
    27. public NPC2(Player player) {
    28. CraftPlayer craftPlayer = (CraftPlayer) player;
    29. ServerPlayer serverPlayer = craftPlayer.getHandle();
    30.  
    31. MinecraftServer minecraftServer = serverPlayer.getServer();
    32. ServerLevel serverLevel = serverPlayer.serverLevel();
    33. GameProfile gameProfile = new GameProfile(UUID.randomUUID(), "Billy Bob");
    34.  
    35. // ClientInformation setup (Language, Chat Visibility, etc.)
    36. ClientInformation clientInfo = new ClientInformation(
    37. "en_us",
    38. 10,
    39. ChatVisiblity.FULL,
    40. true,
    41. 0,
    42. HumanoidArm.RIGHT,
    43. false,
    44. true
    45. );
    46.  
    47. // Create NPC ServerPlayer with new ClientInformation
    48. ServerPlayer npc = new ServerPlayer(minecraftServer, serverLevel, gameProfile, clientInfo);
    49.  
    50. // Set a dummy connection for the NPC
    51. Connection connection = new Connection(null);
    52. npc.connection = new DummyConnection(minecraftServer, connection, npc);
    53.  
    54. // Set NPC location based on the player's location
    55. npc.moveTo(
    56. player.getLocation().getX(),
    57. player.getLocation().getY(),
    58. player.getLocation().getZ(),
    59. player.getLocation().getYaw(),
    60. player.getLocation().getPitch()
    61. );
    62.  
    63. // Add NPC to the world
    64. serverLevel.addNewPlayer(npc);
    65.  
    66. // Send packets to all players to make the NPC visible
    67. addNPCPacket(npc);
    68.  
    69. // Add the NPC to the list
    70. NPC_LIST.add(npc);
    71. }
    72.  
    73. public static void addNPCPacket(ServerPlayer npc) {
    74. for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
    75. CraftPlayer craftPlayer = (CraftPlayer) onlinePlayer;
    76. ServerPlayer serverPlayer = craftPlayer.getHandle();
    77. ServerGamePacketListenerImpl connection = serverPlayer.connection;
    78.  
    79. if (connection != null && serverPlayer != npc) {
    80. // Send player info packets to make the NPC visible
    81. connection.send(new ClientboundPlayerInfoUpdatePacket(ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER, npc));
    82. connection.send(new ClientboundAnimatePacket(npc, 1)); // Simulate animation
    83. connection.send(new ClientboundRotateHeadPacket(npc, (byte) (npc.getYHeadRot() * 256 / 360)));
    84. connection.send(new ClientboundSetEntityDataPacket(npc.getId(), npc.getEntityData().getNonDefaultValues()));
    85.  
    86. // Define last NPC position for movement updates
    87. double lastX = npc.getX();
    88. double lastY = npc.getY();
    89. double lastZ = npc.getZ();
    90.  
    91. // Calculate delta values for the next movement
    92. short deltaX = (short) ((npc.getX() - lastX) * 32);
    93. short deltaY = (short) ((npc.getY() - lastY) * 32);
    94. short deltaZ = (short) ((npc.getZ() - lastZ) * 32);
    95.  
    96. byte yaw = (byte) ((npc.getYRot() * 256.0F) / 360.0F);
    97. byte pitch = (byte) ((npc.getXRot() * 256.0F) / 360.0F);
    98.  
    99. boolean onGround = npc.getY() % 1 == 0; // Check if NPC stands on a full block height
    100.  
    101. // Send movement and teleport packets
    102. connection.send(new ClientboundMoveEntityPacket.PosRot(npc.getId(), deltaX, deltaY, deltaZ, yaw, pitch, onGround));
    103. connection.send(new ClientboundTeleportEntityPacket(npc));
    104.  
    105. // Add NPC to the server (alternative method for Minecraft 1.21)
    106. }
    107. }
    108. }


    Hi, I need help creating an NPC using NMS paper, and the Minecraft version is 1.21.1. The NPC spawns, but it is invisible.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 25, 2024

Share This Page