Making chat only visible for players in world

Discussion in 'Plugin Development' started by RedstoneForDayz, Sep 23, 2014.

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

    RedstoneForDayz

    I need help making a plugin where a player is in my plotworld, and is using the chat to talk. I want it so the plotworld people can see what the plotworld player is putting in chat, but people in the hub world cant see the chat from the plotworld.
     
  2. Offline

    Onlineids

    RedstoneForDayz
    Code:java
    1. @EventHandler
    2. public void onChat(AsyncPlayerChatEvent e){
    3. List<String> players = new ArrayList<String>();
    4. World w = e.getPlayer().getWorld();
    5. for(Player p : Bukkit.getOnlinePlayers()){
    6. if(!p.getWorld().equals(w)) continue;
    7. players.add(p.getName());
    8. }
    9. for(Player p : e.getRecipients()){
    10. if(!players.contains(p.getName())){
    11. e.getRecipients().remove(p);
    12. }
    13. }
    14. }
     
  3. Offline

    EvilWitchdoctor

    Here's another solution that doesn't involve the inefficiency of multiple loops and objects
    Code:java
    1. @EventHandler
    2. public void onPlayerChat(AsyncPlayerChatEvent evt){
    3. World plotsWorld = evt.getPlayer().getWorld();
    4.  
    5. for(Player player : evt.getRecipients()){
    6. if(player.getWorld().equals(plotsWorld) == false) evt.getRecipients().remove(player);
    7. }
    8. }
     
    Hawktasard likes this.
  4. Offline

    Onlineids

    EvilWitchdoctor No need for the == false its a Boolean so just add a !
     
  5. Offline

    EvilWitchdoctor

    Both methods work. :)
    I personally prefer " == false " for readability reasons, but it's solely the coder's choice on that matter
     
Thread Status:
Not open for further replies.

Share This Page