Spawn EntityPlayer

Discussion in 'Spigot Plugin Development' started by KarimAKL, Jun 7, 2019.

  1. I'm currently trying to spawn an EntityPlayer in the world without using packets.
    I can see the entity when i spawn it with packets but then events won't get called on it, that's why i want to do it without packets.
    I'm using version 1.12.2.
    Here is my current code:
    Code (Java):
    private EntityPlayer spawn(Player player) {
        MinecraftServer server = ((CraftServer) Bukkit.getServer()).getServer();
        WorldServer world = ((CraftWorld) location.getWorld()).getHandle();
        GameProfile profile = new GameProfile(UUID.randomUUID(), name);
        Property textures = ((CraftPlayer) player).getHandle().getProfile().getProperties().get("textures").iterator().next();
        profile.getProperties().put("textures", textures);
        EntityPlayer npc = new EntityPlayer(server, world, profile, new PlayerInteractManager(world));
        npc.setLocation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
        DataWatcher data = npc.getDataWatcher();
        data.set(DataWatcherRegistry.a.a(13), (byte) 127);
        new PlayerConnection(server, new NetworkManager(EnumProtocolDirection.SERVERBOUND), npc);
        world.addEntity(npc, SpawnReason.CUSTOM);
        return npc;
    I get both the "Spawning..." and "Spawned!" message when i use this.
    I don't know why it's not spawning the entity even though i get the messages. :/
    EDIT: Fixed spaces in code.
    #1 KarimAKL, Jun 7, 2019
    Last edited: Jun 9, 2019
  2. Why’s this here?
  3. I read somewhere that it should remove the npc from the tab list, and the npc didn't spawn before i added that line of code.
  4. Bump, anyone?
    EDIT: I think i got it working, i just needed to send the required packets to the online players, after adding the npc to the world.
    Though the npc doesn't seem to be taking knockback, anyone know why?
    #4 KarimAKL, Jun 9, 2019
    Last edited: Jun 9, 2019
  5. Bump. (It does take damage, if in survival mode, but not knockback)