Chat Plugin help

Discussion in 'Spigot Plugin Development' started by MadGunner, May 26, 2017.

  1. Hello, I have developed a chat plugin that is simply just used for local chat within a block radius, and everything worked fine. Recently however, I updated it so that it allows you to ignore any non local chat. The code i use to do this works, as it works on both my test server AND it worked for a while on another server with more players. recently however, it doesn't seem to work. Any clues as to why would be appreciated.
    Code (Text):
    @EventHandler
        public void onChat(AsyncPlayerChatEvent e){
                 for( Player p : e.getRecipients()){
                    if (noGeneral.contains(p)){
                        e.getRecipients().remove(p);
                    }
                }
        }
     
  2. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    You should never remove something from a list while looping through it. You can use a CopyOnWriteArrayList instead, something along the lines of:
    Code (Text):
    List<Player> players = new CopyOnWriteArrayList<Player>(e.getRecipients());
    for(Player p : players){
        //Do your stuff
    }
    e.setRecipients(players);
     
  3. WAS

    WAS

    There is no AsyncPlayerChatEvent#setRecipients() method.

    Code (Java):
    Iterator<Player> it = e.getRecipients().iterator();
    while (it.hasNext()) {
          if (noGeneral.contains(it.next()))
                it.remove();
    }
    Or further simplified with lambda.

    Code (Java):
    e.getRecipients().removeIf( p -> noGeneral.contains(p) );
    All untested so be sure to check the code to fit your instance.
     
    #3 WAS, May 26, 2017
    Last edited: May 26, 2017
    • Agree Agree x 1
  4. Thank you for the clarification on basic coding etiquette, but that doesn't change the fact that the method seems to work fine when there is little activity on the server, but not when there is strenuous activity.
     
  5. I was under the impression that a for each loops was essentially just like using an iterator, is it not?
     
  6. My first guess would be that it is asynchronous, so server lag may mess up the timing of the two threads. there is no "SyncPlayerChatEvent" tho as far as I know.
     
  7. WAS

    WAS

    A for loop is not a iterator, and you cannot remove an active elm from it while looping it.
     
  8. Alright, I'll fix that. why does it work MOST of the time though, and only doesn't work when there is a lot of players on?
     
  9. WAS

    WAS

    The error helps you out with this: ConcurrentModificationException

    Which means you are concurrently trying to modify (remove) a element which was being accessed elsewhere.
     
  10. That's not answering the question. He's asking why this works fine if there are few people, but not when there are many.
     
  11. Mas

    Mas

    There is just 'PlayerChatEvent' (deprecated).
     
  12. WAS

    WAS

    Which is answered by the exception, and why it's triggered, and followup text. Lol Are you explaining it? Sure doesn't look like it. Look's like you you in fact have contributed nothing but complaint.
     
  13. Yeah, cause all you ever contribute to any thread is help. Like that kotlin thread, you definitely weren't just flaming for no reason. Nope

    Sent from my Nexus 5 using Tapatalk
     
  14. WAS

    WAS

    It was a topic about using Kotlin and subsequent opinions obviously, actually.

    Staying on topic though; the reason is very much covered, and you were out of line. The error alone is reason enough (and fully covers it), the laymen summary should further that.