Solved Chat Listener? ... Thread safety?

Discussion in 'Plugin Development' started by InflamedSebi, Apr 18, 2013.

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


    Hay i'm trying to listen to everything in the chat and change it if neccesary ...

    first of all: Does the AsyncPlayerChatEvent get called for every message to a player (also messages from plugins / or the server)?

    if yes:

    This is what it should do for now:

    1. // Maps & global Variables
    2. List<MyEntry<String, String>> replacePartsList = new ArrayList<MyEntry<String, String>>();
    3. List<MyEntry<String, String>> replaceMessageList = new ArrayList<MyEntry<String, String>>();
    5. // Events
    6. @EventHandler(priority = EventPriority.NORMAL)
    7. public void onChatMessage(AsyncPlayerChatEvent evt) {
    8. if (!evt.isCancelled())
    9. return;
    11. String message = evt.getMessage();
    12. String player = evt.getPlayer().getDisplayName();
    14. for (MyEntry<String, String> entry : replacePartsList) {
    15. if (message.contains(entry.getKey()))
    16. message.replaceAll(entry.getKey(), entry.getValue());
    17. }
    18. for (MyEntry<String, String> entry : replaceMessageList) {
    19. if (message.contains(entry.getKey()))
    20. message = entry.getValue().replaceAll("%p", player);
    21. }
    22. }

    Is this Thread safe?
    seems like player.getDisplayName(); or the list actions may cause errors ...

    I'm totally noob at async / sync things, so how to do it right?
    - the lists will be loaded at onEnable() and sould not change until server restart ...
  2. Offline


    The async chat event only listens for chat sent by the client to the server.
    And getting the display name of a player is thread safe assuming isn't being changed in the process of the event getting it. If the list aren't being changed it should be fine aswell. you might want to declare them final or switch to a ConcurrentHashMap.
  3. Offline


    Thanks :D Tzeentchful

    Do you know, how to listen to the whole chat?
    because this needs to ...
  4. Offline


    Short of using Protocol Lib to listen for every chat packet sent to the client i don't think it's possible.
Thread Status:
Not open for further replies.

Share This Page