How to make a player-like npc spawn in world?

Discussion in 'Plugin Development' started by DreamTerminator, Mar 19, 2021.

  1. Offline

    DreamTerminator

    An example of it:

    Version: newest version

    First step, I don't know how to spawn a fake player by coding the plugin.

    I just want to fix the first step.
     
  2. Offline

    davidclue

    Spawns an NPC with a players skin at a players location
    Code:
    @SuppressWarnings("deprecation")
        public void spawnFakePlayer(Player player, String displayname, World tempW) {
            MinecraftServer server = ((CraftServer) Bukkit.getServer()).getServer();
            WorldServer world = ((CraftWorld) tempW).getHandle();
    
            Player target = Bukkit.getServer().getPlayer(displayname);
            EntityPlayer npc;
            if (target != null) {
                npc = new EntityPlayer(server, world, new GameProfile(target.getUniqueId(), target.getName()), new PlayerInteractManager(world));
            } else {
                OfflinePlayer op = Bukkit.getServer().getOfflinePlayer(displayname);
                npc = new EntityPlayer(server, world, new GameProfile(op.getUniqueId(), ChatColor.translateAlternateColorCodes('&', " ")), new PlayerInteractManager(world));
            }
            Location loc = player.getLocation();
            npc.setLocation(loc.getBlockX()+0.5, loc.getBlockY(), loc.getBlockZ()+0.5, loc.getYaw(), loc.getPitch());
            PacketPlayOutEntityHeadRotation rotation = new PacketPlayOutEntityHeadRotation(npc, (byte) ((loc.getYaw() * 256.0F) / 360.0F));
           
            for (Player all : Bukkit.getOnlinePlayers()) {
                PlayerConnection connection = ((CraftPlayer)all).getHandle().playerConnection;
                connection.sendPacket(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER, npc));
                connection.sendPacket(new PacketPlayOutNamedEntitySpawn(npc));
                connection.sendPacket(rotation);
            }
        }
    I would like to know actually how to make NPC's move like how they did in the video, I'm assuming with custom entity pathfinding but how did they do all of that with the placing, attacking, breaking, etc.? If anybody knows could you link me to some resources? I am sure the OP would probably want to know about this too.
     
  3. Offline

    DreamTerminator


    Hi, I copy the code and search for class like "CraftServer" "WorldServer" for a long time. But got nothing. Should I create a class called "CraftServer" on my own? QQ截图20210320213148.png
     
  4. Offline

    KarimAKL

    No. You need to include CraftBukkit, not just the Bukkit API.
     
  5. Offline

    DreamTerminator

    Thx.:)
     

Share This Page