Team Chat

Discussion in 'Spigot Plugin Development' started by SmaxoS, Jun 17, 2018.

  1. 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.
     
  2. use Player not string and after that make a for(Player player : red) ....
     
    • Agree Agree x 1
  3. Doesn't work. Message send all players and players from another team have same color.
     
  4. Go through all online players, check if they are in the ArrayList and if they are send them a message.
     
    • Agree Agree x 1
  5. Loop thru all strings in your array and check if the player isn't equal to null then send the message.
     
  6. How can I loop all strings?
     
  7. Do something like this:

    Code (Java):
    for (Player player : Bukkit.getOnlinePlayers()) {
       if (red.contains(player.getName()) {
        // Do stuff
      } // Not in the list
    }
     
    • Agree Agree x 1
  8. No, that's same error: "Message send all players and players from another team have same color.".
     
  9. 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"));
     
  10. 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. "
     
  11. 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");
    }
     
     
    • Agree Agree x 1
  12. 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 :p
     
    • Agree Agree x 1
  13. 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).
     
    • Winner Winner x 2
    • Agree Agree x 1
    • Informative Informative x 1
  14. 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?
     
  15. 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
     
  16. Can you send me please a example code?

    But I don't want to call this in JoinEvent, but in the GameStart Method.
     
  17. I learn something new everyday
     
  18. 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();
     
  19. 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.
     
    • Agree Agree x 1
  20. Yes, but what now?