Players crashing after I run this part of the code

Discussion in 'Plugin Development' started by MGlolenstine, Jul 15, 2017.

Thread Status:
Not open for further replies.
  1. Goal of this code is to change player's nametag.

    As mentioned above, clients get kicked from server when my code runs the following:
    Code:
    public void NPC(String name, String tag) {
    
            MinecraftServer nmsServer = ((CraftServer) Bukkit.getServer()).getServer();
            WorldServer nmsWorld = ((CraftWorld) Bukkit.getWorlds().get(0)).getHandle();
            final Player g = Bukkit.getPlayer(name);
            final EntityPlayer npc = new EntityPlayer(nmsServer, nmsWorld, new GameProfile(g.getUniqueId(), tag), new PlayerInteractManager(nmsWorld));
            new BukkitRunnable() {
                int run = 0;
    
                public void run() {
                    PlayerConnection connection = ((CraftPlayer) g).getHandle().playerConnection;
                    connection.sendPacket(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER, npc));
                    connection.sendPacket(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER, npc));
                    for (Player al : plugin.getServer().getOnlinePlayers()) {
                        if(al != g) {
                            connection = ((CraftPlayer) al).getHandle().playerConnection;
                            connection.sendPacket(new PacketPlayOutEntityDestroy());
                            connection.sendPacket(new PacketPlayOutEntityTeleport(npc));
                        }
                    }
                    // }
                }
            }.runTaskTimer(plugin, 0L, 1L);
        }
    kick message is as following:
    [​IMG]

    Questions:
    1.) Where did I go wrong?
    2.) What can I do to improve this code?
    3.) What is even wrong?
     
  2. @MGlolenstine
    I'm not exactly sure what the error is. Only time I've seen that before is when you've messed with the indicies of a PacketDataSerializer.

    Also, your little code there probably isn't going to work, it's a bit more complex to change player name tags. I'd have a look at my NameTagChanger library:
    https://bukkit.org/threads/change-player-name-tags-once-again.444702/
    (If you don't want to use it, feel free to steal whatever code you find inside it)
     
  3. thanks for answering... I noticed, that players don't crash if line containing "
    connection.sendPacket(new PacketPlayOutEntityDestroy());" is commented out
     
  4. @MGlolenstine
    Ah, I probably see what's wrong. Because you don't supply any entity IDs for which entities to destroy (which you should, otherwise the packet does nothing) it writes an empty array of entity IDs which the client probably isn't designed to handle.
     
  5. How can I supply them?
    All I have there as arguments is "int...ints" and that's it, I don't have any int anywhere
     
  6. I've heard of them before... But the problem is that I don't know which int to supply.

    Sent from my Xperia M4 Aqua using Tapatalk
     
  7. Thanks!

    Sent from my Xperia M4 Aqua using Tapatalk
     
Thread Status:
Not open for further replies.

Share This Page