1.17.1 How to prevent a chat EventHandler that calls a new chat event from listening to itself?

Discussion in 'Spigot Plugin Development' started by Kenajcrap, Jun 16, 2021.

  1. Hi all,
    I have a chat listener that modifies the player's message a bit and its recipients, and then actually calls another chat message, with different recipients. (that means that one chat message turns into two, with different text and different recipients, but with the same player)

    The problem is obvious: the listener will trigger once the second event gets sent and a loop will begin. I only need the original message to be split into two so, is there a way to edit the event so the listener will not pay attention to it? Here is the relevant part of the code:
    Code (Java):
    public class ChatEvents implements Listener {

        public void onChat(AsyncPlayerChatEvent event) {
            if (event.getPlayer().hasPermission("proximity.bypass.speak")) {
            // Clear recipients and add recipients that are in the same group
            Set<Player> recipients = event.getRecipients();
            double radius = Proximity.getPlugin().getConfig().getDouble("chat_range");
            for (Player ply : Bukkit.getOnlinePlayers()) {
                //check if the player is in the group of the sender
            // Add sender

            // Get all players that are not in the same group as the sender
            Set<Player> ousiders = new HashSet<>();

            // Send them a second, jumbled, message
            AsyncPlayerChatEvent gibberish = new AsyncPlayerChatEvent(true,event.getPlayer(),event.getMessage().makeGibberish(),ousiders);

    I thought of using HandlerList.unregister() but I think that would unregister the whole listener..
    I can't see any identifiers inside AsyncPlayerChatEvent that would help me make a condition inside onChat to handle this.

    My final objective here is to emulate a WoW-like language barrier.

    Please, don't hesitate to comment on something else I might be doing wrong, as this is one of my first projects.

    Thank you for your time
  2. Create an event similar to the chat event and add parameters like Event(boolean firstCall)
  3. Thanks for the response!

    Wouldn't that also prevent chat eventhandlers from other plugins from triggering on this message?
    Is there any other way to do this that would keep the plug-in somewhat compatible with others that mess with chat?
  4. Why are u doing this? You want to show a message to a group of players? Just use sendMessage
  5. Tau


    create a new event called MyAsyncPlayerChatEvent that extends AsyncPlayerChatEvent and make sure the handler list methods return copies of the super's handler list data. (this is to make sure the event is treated as the same and thus invoke listeners for it's super)

    Then when you call your event call MyAsyncPlayerChatEvent and on your listener check the class.
    for more info check out: https://gitlab.com/TauCu/kaboom/-/b.../taucu/kaboom/events/MyBlockExplodeEvent.java
    and: https://gitlab.com/TauCu/kaboom/-/blob/master/src/main/java/me/taucu/kaboom/KaBoom.java (public void onBlockExplode(BlockExplodeEvent e))
    • Agree Agree x 1