Question AsyncChatEvent chat formatting

themrsung

New member
Jun 9, 2023
1
0
1
Hi. I'm new to making plugins, and want to write some code that formats the chat message when typed. For example,
  • Before: <abc> hello! &6this is gold text!
  • After: [rank] abc: hello! this is gold text!
I have made an event handler for AsyncChatEvent, but I'm stuck here.

Java:
    @EventHandler
    public void onPlayerChatEvent(AsyncChatEvent event) {
       event.message().replaceText(new TextReplacementConfig() {
           @Override
           public @NotNull Pattern matchPattern() {
               return null;
           }

           @Override
           public @NotNull Builder toBuilder() {
               return null;
           }
       })
    }

This is what IntelliJ autocomplete has got me. And honestly I have no idea where to even start.

How should I go about it?
 

Notro

New member
Aug 23, 2023
9
0
1
Hey, if you are trying to color fix a message you could do:

1697638931599.png
1697638977071.png


Please note that I'm using a LegacyComponentSerializer, and not TextReplacementConfig, make sure to learn this.
Glad to help! :)
 
  • Sad
Reactions: 4drian3d

Silal

New member
Feb 13, 2024
4
0
1
Hey, you can also do this:

@EventHandler public void onPlayerChat(AsyncPlayerChatEvent e) { String message = e.getMessage().replace("&", "§"); e.setMessage(message); }

if its easyer.
 

4drian3d

Member
Jan 5, 2022
60
4
8
Perú
Hey, you can also do this:

@EventHandler public void onPlayerChat(AsyncPlayerChatEvent e) { String message = e.getMessage().replace("&", "§"); e.setMessage(message); }

if its easyer.
That event is deprecated and its use is not recommended, there is a possibility that in the future it will be removed.
 

nathanfranke

New member
Feb 22, 2024
1
0
1
This works for legacy components

Java:
    @EventHandler
    public void onAsyncChat(AsyncChatEvent event) {
        if (event.getPlayer().hasPermission("chat_colors")) {
            String rawMessage = LegacyComponentSerializer.legacyAmpersand().serialize(event.message());
            event.message(LegacyComponentSerializer.legacyAmpersand().deserialize(rawMessage));
        }
    }