Send message to everyone in an ArrayList.

Discussion in 'Spigot Plugin Development' started by Surprisejedi, Jun 5, 2017.

  1. Self Explanitory.
    Code (Java):
    package me.surprisejedi.cac;

    import java.util.ArrayList;

    import org.bukkit.entity.Player;

    public class CAChat {
        ArrayList<String> StaffGroup = new ArrayList<String>();
        public void StaffChat() {
        public void sendStaffAlert(Player p, String msg) {
    I want EVERY player in the ArrayList StaffGroup to be sent a message.

    I know I can find out how to do it myself, I just don't want to spend 30 minutes doing that.
  2. You can use a forEach loop.
    Here is an example:
    Code (Text):

    for(Player players : StaffGroup) {
    players.sendMessage("Your message");
    Also, i would suggest you use a
    Code (Text):

    ArrayList<Player> staffGroup
    instead of
    Code (Text):

    ArrayList<String> staffGroup
  3. Thank you, I knew it was something along that line.
  4. ScarabCoder

    ScarabCoder Retired Resource Staff

    You shouldn't use the usernames, even temporarily. Use player.getUniqueID() UUID instead.
    Also, it's against Java convention to capitalize the first letter of a variable.
  5. Hold on...
    Code (Text):
    Type mismatch: cannot convert from element type String to Player
  6. Its because you have a ArrayList with the type of a String. You have to use a Player ArrayList! Look at my reply.
  7. I know its rude to say this but you really need a good understanding of java and OOP programming before diving into api's. I know I'm being Mr.YouNeedToKnowThis but this is a fairly simple thing to do.
  8. Actually I wouldn't use Player neither. If they log off and on, you will have trouble sending them messages (nullpointers everywhere). Instead just store the <UUID>, and use Bukkit.getPlayer(UUID uuid) to get their Player instance. Do a simple == null check, and send them a message.

    And would not recommend List, but instead a Set in this case. As indexing is pointless, so a Set is faster.

    Code (Text):
    Set<UUID> players = new HashSet<>(); // using set instead of list because indexing is basically pointless, so it's faster

    players.add(Bukkit.getPlayer("Notch").getUniqueId()); // might want to run a null check on Notch's Player object

    // for each loop
    for (UUID uuid : players) {
      Player staff = Bukkit.getPlayer(uuid);

      // simple null check
      if (staff = null) continue; // offline

      staff.sendMessage("You are supposed to see this message");
  9. Even better: use Streams
    Code (Java): (uuid -> {
      Player p = Bukkit.getPlayer(uuid);
      if (please!= null) {
        p.sendMessage ("foo");
    } );
    • Like Like x 1
    • Friendly Friendly x 1
  10. Even better:
    Code (Text):
      .forEach(p -> p.sendMessage("foo"))
    • Agree Agree x 1
  11. Lmao, the past 3 posts are why OOP programming languages and the people that use them are just brilliant.
    • Funny Funny x 1
  12. Please don't
  13. Why?
  14. Just store the player objects
    • Agree Agree x 2
  15. Is that related to my autocorrect, or...?
  16. Just store the damn Player objects. Seriously.. If you need a Player object, store it. Don't store something else like a name or uuid, only to turn around and get a Player object from that name/uuid. It's stupid and a waste of cpu (especially if you are doing it by name, good God).

    Just learn to properly clean up any references you store. Remove them on player quit. Also check if the player is null or not online before you use it. If null or not online then remove it from your collection, it's a double check to make sure you don't run into any issues.

    I seriously hate the "don't store Player objects" mentality mbaxter put out there years ago. He only did it as a quick band aid fix to seeing many kids making plugins with memory leak. He took the easy way out and just said "don't do it" instead of teaching properly how to do it.
    • Agree Agree x 2
    • Funny Funny x 1
  17. I've learned so much from this simple post. Now to make my lambda expressions even neater! Thank you!
    • Like Like x 1