Hi I have AsyncPlayerChatEvent, end I checking if player is in arraylist, like that: ArrayList: Code (Text): public ArrayList<String> red = new ArrayList<>(); Check in Event: Code (Text): for(String string : red) { if(Bukkit.getOnlinePlayers().contains(string)) { Bukkit.getPlayer(string).sendMessage("§c" + p.getName() + "§8: §7" + e.getMessage()); } } and I cancelling event, but message cannot send.
Go through all online players, check if they are in the ArrayList and if they are send them a message.
Loop thru all strings in your array and check if the player isn't equal to null then send the message.
Do something like this: Code (Java): for (Player player : Bukkit.getOnlinePlayers()) { if (red.contains(player.getName()) { // Do stuff } // Not in the list }
Why don't you just store the Player object opposed to their name? That way you can just do: Code (Java): red.foreach(player -> player.sendMessage("Hello"));
This showing error: Code (Java): for(Player pl : Bukkit.getOnlinePlayers()) { red.forEach(pl -> pl.sendMessage(msgstring)); } Error: "Lambda expression's parameter pl cannot redeclare another local variable defined in an enclosing scope. "
No reason to loop thru all players online if you want to send a message to every player in an arraylist? Code (Java): for (String str : array){ if (Bukkit.getPlayer(str) != null) // Check if the player is online Bukkit.getPlayer(str).sendMessage("My message"); }
I agree with this idea^ just one thing, though. Instead of doing Bukkit.getPlayer() twice, store the variable. The getPlayer method loops through all the players so that's doing uneccessary extra work. Code (Java): for (String str : array){ Player p = Bukkit.getPlayer(str); if (p != null) // Check if the player is online p.sendMessage("My message"); } Also, remember to remove the player names from the list when they leave. Memory leaks are bad
Code (Java): private Collection<Player> teamRed = new HashSet<>(); private Collection<Player> teamBlue = new HashSet<>(); @EventHandler public void onChat(AsyncPlayerChatEvent event) { Player sender = event.getPlayer(); // sender is on team red. only send message to red team members if (teamRed.contains(sender)) { event.getRecipients().clear(); event.getRecipients().addAll(teamRed); } // sender if on team blue. only send message to blue team members else if(teamBlue.contains(sender)) { event.getRecipients().clear(); event.getRecipients().addAll(teamBlue); } } Never ever ever cancel the chat event if anyone is receiving a chat message! Instead, just modify the recipients list. Too many plugins do this wrong (Factions, for example) and it breaks compatibility with other plugins that listen to chat (like DiscordSRV).
Thank you, it's works. But I have one another question for all: How can I make randomly join to team with this collections, when game starting and start count is on 1 or 0?
You can use ArrayList instead of Collections. Listen for JoinEvent and use a Random object for have a number in range 0-1. If number is 0, add to red; if number is 1, add to blue. Check before if red or blue team is full and add players only to the other team
Can you send me please a example code? But I don't want to call this in JoinEvent, but in the GameStart Method.
Instead of Code (Java): private Collection<Player> teamRed = new HashSet<>(); private Collection<Player> teamBlue = new HashSet<>(); Use Code (Java): private List<Player> teamRed = Lists.newArrayList(); private List<Player> teamBlue = Lists.newArrayList();
Why use an ArrayList when a HashSet has waay less overhead. Lists should only be necessary when order is a requirement. From the sounds of it, he doesnt care about the ordering here.