Sending messages to players within a list

Discussion in 'Plugin Development' started by HamOmlet, Jun 14, 2012.

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

    HamOmlet

    I'm trying to allow players to send messages to other players within an ArrayList. However, I don't want the message to be sent "server-wide" (as in all other players). Here's my below code:

    Code:
          @EventHandler(priority=EventPriority.HIGHEST)
          public void onPlayerChat(PlayerChatEvent event) {
              Player p = event.getPlayer();
              String message = event.getMessage();
              if(GameManager.getInstance().isSpectator(p)){
                  int spec = GameManager.getInstance().getPlayerSpectateId(p);
                  for(Player pl : GameManager.getInstance().getGame(spec).getSpectators()) {
                      pl.sendMessage(message);
                  }
              }
           
              if(GameManager.getInstance().isPlayerActive(p)){
                  int play = GameManager.getInstance().getPlayerGameId(p);
                  for(Player pl : GameManager.getInstance().getGame(play).getCurrentPlayers()) {
                      pl.sendMessage(message);
                  }
              }
           
          }    
    The code works, but two messages are sent: one from the for-loop and another from the message being sent "server-wide". How can I prevent this?
     
  2. Offline

    imjake9

    You'll have to cancel the event using event.setCancelled(true). That will prevent the usual action from being taken.
     
  3. Offline

    HamOmlet

    Actually, couldn't I just add the ArrayList to the recipients and send the recipients that message?
     
  4. Offline

    imjake9

    I'm confused about what exactly you're asking here. The reason you have to do event.setCancelled(false) is that if you don't, the player chat will happen anyway. The event will continue to process, and both the standard message and your custom message will be dispatched. You need to cancel the default server behavior to prevent the server-wide message from occurring.
     
  5. Offline

    Njol

    You can set the recipients to the players you want to send the message to, but you have to clear the set first:
    Code:
    Set<Player> recipients = event.getRecipients();
    recipients.clear();
     
    recipients.addAll(spectators);
    //or
    recipients.addAll(players);
    I also suggest to get rid of those "game ids" and only use 'Game' objects instead, and to make all possible methods in GameManager static instead of using GameManager.getInstance():
    Code:
    public void onPlayerChat(PlayerChatEvent event) {
        Set<Player> recipients = event.getRecipients();
        recipients.clear();
        Game game = GameManager.getGame(p);// returns the game the player is playing or spectating
        if (game.isSpectator(player))
            recipients.addAll(game.getSpectators());
        else
            recipients.addAll(game.getPlayers());
    }
     
Thread Status:
Not open for further replies.

Share This Page