Plugin Help e.getRecipients().remove() not working properly! Urgent!

Discussion in 'Plugin Help/Development/Requests' started by HCMatt, Apr 13, 2015.

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

    HCMatt

    I have different radius'/Arraylists that control what players see in chat, its really simple to understand when you see the code:
    Code:
    @EventHandler
        public void onChat(AsyncPlayerChatEvent e) {
           
     Player p = e.getPlayer();
    
                e.setCancelled(true);
                Location pLocation = p.getLocation();
    
                for (Player op : Bukkit.getOnlinePlayers()) {
                    if (wh.contains(p)) {
                        if (op.getLocation().distance(pLocation) <= 5) {
                            if (getConfig().get("PlayerCard." + p.getName() + ".Name") != null) {
                                e.getRecipients().remove(op);
                                e.getRecipients().remove(p);
                                op.sendMessage(ChatColor.GRAY + "[" + ChatColor.DARK_BLUE + "Whisper" + ChatColor.GRAY + "] " + ChatColor.RESET + getConfig().get("PlayerCard." + p.getName() + ".Name")
                                        + ChatColor.DARK_GRAY + " - " + ChatColor.WHITE + e.getMessage());
                            } else {
                                e.getRecipients().remove(op);
                                e.getRecipients().remove(p);
                                op.sendMessage(ChatColor.GRAY + "[" + ChatColor.DARK_BLUE + "Whisper" + ChatColor.GRAY + "] " + ChatColor.RESET + p.getName() + ChatColor.DARK_GRAY + " - "
                                        + ChatColor.WHITE + e.getMessage());
                            }
                        } 
    
                    } else if (s.contains(p)) {
                        if (op.getLocation().distance(pLocation) <= 24) {
                            if (getConfig().get("PlayerCard." + p.getName() + ".Name") != null) {
                                e.getRecipients().remove(op);
                                e.getRecipients().remove(p);
                                op.sendMessage(ChatColor.GRAY + "[" + ChatColor.DARK_RED + "Shout" + ChatColor.GRAY + "] " + ChatColor.RESET + getConfig().get("PlayerCard." + p.getName() + ".Name")
                                        + ChatColor.DARK_GRAY + " - " + ChatColor.WHITE + e.getMessage());
                            } else {
                                e.getRecipients().remove(op);
                                e.getRecipients().remove(p);
                                op.sendMessage(ChatColor.GRAY + "[" + ChatColor.DARK_RED + "Shout" + ChatColor.GRAY + "] " + ChatColor.RESET + p.getName() + ChatColor.DARK_GRAY + " - " + ChatColor.WHITE
                                        + e.getMessage());
                            }
                        }
                    } else {
                        if (op.getLocation().distance(pLocation) <= 16) {
                            if (getConfig().get("PlayerCard." + p.getName() + ".Name") != null) {
                                e.getRecipients().remove(op);
                                e.getRecipients().remove(p);
                                op.sendMessage("" + getConfig().get("PlayerCard." + p.getName() + ".Name") + ChatColor.DARK_GRAY + " - " + ChatColor.RESET + e.getMessage());
                            } else {
                                e.getRecipients().remove(op);
                                e.getRecipients().remove(p);
                                op.sendMessage(p.getName() + ChatColor.DARK_GRAY + " - " + ChatColor.RESET + e.getMessage());
                                return;
                            }
                        }
                    }
                }
            }
        }
    
    First it checks the array list that they are in, then the location of the players around them....

    I exported and tested on a test server and this only works for 1 player that is online, whenever i try and type with another player, it just comes up as it would any other time '<PLAYERNAME> message'.
    Please help!
    Needs to be fixed as soon as possible!

    Thanks

    Matty!
     
    Last edited: Apr 15, 2015
  2. Offline

    BizarrePlatinum

    @HCMatt are you canceling the original event? If not, that would be the reason for the normal chat message I believe. Also, shouldn't you be sending the message to both players?(you may already be doing this)

    --I haven't used AsyncChat yet, so, I am not really sure, I figured you would need to cancel the original event from Bukkit, then send your message. If I am wrong, I apologize.
     
    Last edited: Apr 13, 2015
  3. Offline

    I Al Istannen

    @HCMatt Yes, you are sending the message yourself, but dont cancel the event. Do both messages show up? The edited and the normal?
     
  4. Offline

    HCMatt

    @I Al Istannen I didn't cancel the even at first, but that was more messed up then now. It works perfectly for 1 person, if the other people online type something, no matter if they are in wh or s or none, the normal chat comes up on their screen, but it comes up as it should on the person's screen who it was working for.
     
  5. Offline

    I Al Istannen

    @HCMatt What are these things?
    Code:
    s.contains(p)
    wh.contains(p)
    What is "wh"? What is "s"? And is "p" the player who sent the message?
     
  6. Offline

    HCMatt

    @I Al Istannen
    s is Shout, wh is whisper and p is e.getPlayer().

    i have commands to put the player in each array list and they work fine, i just need the chat to work properly.
     
  7. Offline

    I Al Istannen

    @HCMatt May i ask you why you need this ArrayLists?
    And if you check the distance: go from the smaller to the bigger checks. <=5, <=16, <=24. Why don't you just check the distance (Location.distanceSquared() is faster because it doesn't calculate the square root) and then react based on this and not on the ArrayLists?
     
  8. Offline

    HCMatt

    The array list is so that it knows weather they are shouting or whispering...
     
  9. Offline

    I Al Istannen

    @HCMatt Can they coose? Like say /whisper hi or will it be made automatic, like "you are near ==> whisper"

    Okay, I finally realized why it was sent only to one player. You added a "return" after sending the message to the first player ==> The for loop wont continue!
     
    Last edited: Apr 14, 2015
  10. Offline

    HCMatt

    @I Al Istannen Okay so, i removed the returns and removed some of the code for each of the distances. It works, but here is my problem now:

    Player 1
    Player 2

    Say Player 1 logged on first, then Player 2... And player 1 typed in chat without being in s or wh and was within 16 blocks of Player 2, the message would come up on both players screens. Same would happen for player 2 if he logged in first.

    However, if Player 1 logged on first and player 2 typed, without being in s or wh, and was in 16 blocks oh Player 1, it would show up on Player 1's screen, but doesn't for Player 2. If player 2 moves away from player 1, making himself more than 16 blocks away, only then would it show up on Player 2's screen.

    Please Help! (Ive updated my question with the new code)
     
  11. Offline

    I Al Istannen

    @HCMatt I doubt that e.getRecipients.remove() does anything, especially when you cancelled the event before. And i wouldnt add a player to the ArrayList, but the UUID of a player. So use if wh.contains(op.getUniqueId()) and so on. And you still have an return message in your last if - else. Line 50.
     
Thread Status:
Not open for further replies.

Share This Page