3333import java .util .Optional ;
3434import java .util .UUID ;
3535import java .util .logging .Level ;
36+ import java .util .regex .Pattern ;
37+
38+ import static net .kyori .adventure .text .serializer .legacy .LegacyComponentSerializer .*;
3639
3740@ Value
3841@ ToString (callSuper = true )
@@ -71,40 +74,29 @@ public PacketAdapter upgradeToNative(ChatMessagePacket packet) {
7174 //todo: lookup linked user
7275 sender = Player .builder ().id (new UUID (0 , 0 )).name (packet .getMessage ().getSenderName ()).build ();
7376 }
74- return new PacketAdapter (packet .getPacketType (),
77+ return new PacketAdapter (packet .getPacketType (). getAurionPacketType (), packet . getPacketType () ,
7578 packet .getSource (),
7679 packet .getRoute (),
7780 new AurionPlayer (sender .getId (), sender .getName (), null , null ),
7881 packet .getChannel (),
7982 mod .getPlayerAdapter ().getPlayer (sender .getId ()).map (Player ::getName ).orElseGet (packet .getMessage ()::getSenderName ),
80- GsonComponentSerializer .gson ().serialize (packet .getMessage ().getFullText ())).setMod (mod );
83+ GsonComponentSerializer .gson ().serialize (packet .getMessage ().getFullText ()), config ).setMod (mod );
8184 }
8285
8386 @ Value
8487 public static class PacketAdapter extends AurionPacket implements ChatMessagePacket {
85- @ Expose PacketType packetType ;
88+ @ Expose PacketType packetType ;
89+ ChatModules .AurionChatProviderConfig config ;
8690 @ Getter (onMethod_ = @ __ (@ JsonIgnore )) @ Setter (onMethod_ = @ __ (@ JsonIgnore )) @ NonFinal ModuleContainer mod ;
8791
88- public PacketAdapter (
89- Type type , String source , List <String > route , @ Nullable AurionPlayer player , @ Nullable String channel ,
90- @ Nullable String displayName , @ NotNull String tellRawData
91- ) {
92- this (type , PacketType .of (type ), source , route , player , channel , displayName , tellRawData );
93- }
94-
95- public PacketAdapter (
96- PacketType packetType , String source , List <String > route , @ Nullable AurionPlayer player , @ Nullable String channel ,
97- @ Nullable String displayName , @ NotNull String tellRawData
98- ) {
99- this (packetType .getAurionPacketType (), packetType , source , route , player , channel , displayName , tellRawData );
100- }
101-
10292 public PacketAdapter (
10393 Type type , PacketType packetType , String source , List <String > route , @ Nullable AurionPlayer player , @ Nullable String channel ,
104- @ Nullable String displayName , @ NotNull String tellRawData
94+ @ Nullable String displayName , @ NotNull String tellRawData ,
95+ ChatModules .AurionChatProviderConfig config
10596 ) {
10697 super (type == null ? AurionPacket .Type .CHAT : type , source , route == null ? new ArrayList <>() : route , player , channel , displayName , tellRawData );
10798 this .packetType = packetType ;
99+ this .config = config ;
108100 }
109101
110102 @ Override
@@ -123,7 +115,15 @@ public ChatMessage getMessage() {
123115 .flatMap (mod .getPlayerAdapter ()::getPlayer )
124116 .or (() -> optional .map (AurionPlayer ::getName ).flatMap (mod .getPlayerAdapter ()::getPlayer ))
125117 .orElseThrow (() -> new NoSuchElementException ("Player not found" ));
126- return new ChatMessage (sender , mod .getPlayerAdapter ().getDisplayName (sender .getId ()), getDisplayString (), (TextComponent ) getComponent ());
118+ TextComponent component ;
119+ if (config .getContentPattern () == null )
120+ component = (TextComponent ) getComponent ();
121+ else {
122+ var txt = legacySection ().serialize (getComponent ());
123+ var matcher = Pattern .compile (config .getContentPattern ()).matcher (txt );
124+ component = legacySection ().deserialize (matcher .find () ? matcher .group (1 ) : txt );
125+ }
126+ return new ChatMessage (sender , mod .getPlayerAdapter ().getDisplayName (sender .getId ()), getDisplayString (), component );
127127 }
128128 }
129129}
0 commit comments