NMS | help with packets

Discussion in 'Plugin Development' started by Remigio07_, Oct 18, 2019.

Thread Status:
Not open for further replies.
  1. Offline

    Remigio07_

    hi everyone,
    I'm building a little plugin for my server and in particular I'm trying to create a custom animated tablist.
    I'm new in packets so I don't know exactly how they work, but after some tries the plugin worked well until another player joined the server. Basically, when there's more than 1 player online and I try to get the player's name using a for loop, to set it as the header or as the footer, it swaps the players' names.

    PHP:
    public static void idk() {
            
    PacketPlayOutPlayerListHeaderFooter pack = new PacketPlayOutPlayerListHeaderFooter();
        
            for (
    Player p Bukkit.getOnlinePlayers()) {
                
    CraftPlayer cp = (CraftPlayerp;
                
    PlayerConnection conn cp.getHandle().playerConnection;
            
                try {
                    
    pack.getClass().getField("header").set(pack, new ChatComponentText(p.getName()));
                    
    pack.getClass().getField("footer").set(pack, new ChatComponentText(p.getName()));
                } catch (
    IllegalArgumentException IllegalAccessException NoSuchFieldException SecurityException e) {
                    
    e.printStackTrace();
                }
            
                try {
                    
    conn.getClass().getMethod("sendPacket"getNMSClass("Packet")).invoke(connpack);
                } catch (
    IllegalAccessException IllegalArgumentException InvocationTargetException
                        
    NoSuchMethodException SecurityException e) {
                    
    e.printStackTrace();
                }
            }
        }
    you can note that I send the same instance of PacketPlayOutPlayerListHeaderFooter to every player, but every time the loop gets another player the header and the footer are set to the new player's name, so every time they should change, but they don't.
    I also know that if I create an instance for every player I could solve the problem, but I'd just like to understand why it's not working.
     
    Last edited: Oct 18, 2019
  2. Offline

    Kars

    By the looks of it you are sending the packet for every player that is connected. I don't fully understand what you mean. You want the header/footer to be set to the last players name?
    With your code, you set the header/footer to the first player, send the packet, and then repeat this process for the second player. I don't know how this "packet" thing works, but it might not update because it receives them in rapid sucession.
     
  3. Offline

    Remigio07_

    I'd want to set the header and the footer to the actual player's name. the loop gets every player and does this operation for everyone.
    the problem is that the header and the footer are set after the packet is sent, so I also think that it's because I'm sending it too quickly, but how can I avoid this without creating multiple instances of the packet?
     
    Last edited: Oct 19, 2019
  4. Offline

    Kars

  5. Offline

    Remigio07_

    no, the for loop gets every player and should do the operation for every player online.
     
  6. Offline

    Remigio07_

    any idea?
     
  7. Offline

    Kars

    Like i said i don't know how any of this works but you are also re-using the same packet.
     
Thread Status:
Not open for further replies.

Share This Page