Solved How to send packet to everyone in a world?

Discussion in 'Spigot Plugin Development' started by Orangemaster123, May 28, 2017.

  1. I am currently making particle trails using packets, but the packet is only sent to the player who has the trail. How can i send the packet to everyone in the same world as the player, so everyone can see the trail.
    Here's is how im sending it now.
    Code (Text):

    ((CraftPlayer)e.getPlayer()).getHandle().playerConnection.sendPacket(peffect);
     
     
  2. BananaPuncher714

    Supporter

    Run a for loop that iterates through everyone on the server.
    Code (Java):
    for ( Player p : Bukkit.getOnlinePlayers() ) {
        ((CraftPlayer)p).getHandle().playerConnection.sendPacket(peffect);
    }
     
  3. I think this is for java 7, do you know how to do this for java 8?
     
  4. BananaPuncher714

    Supporter

    No, im pretty sure that's for java 8 as well. That is standard java syntax, and bukkit 1.11.2
     
  5. you can use that on java 8 as well. doesn't really matter, however, you could use the forEach implementation in java 8, or stream API.

    Code (Java):

    World world = particlePlayer.getWorld();
    Bukkit.getOnlinePlayers().stream().filter(player -> player.getWorld().equals(world)).forEach(player -> ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet));
     
    or to just everyone:
    Code (Java):

    Bukkit.getOnlinePlayers().forEach(player -> ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet));
     
  6. Oh, ok thankyou :)
     
  7. Thank you!
     
  8. You could replace this
    Code (Java):
    Bukkit.getOnlinePlayers().stream().filter(player -> player.getWorld().equals(world)).forEach(...)
    with this
    Code (Java):
    world.getPlayers().forEach(...)