3232import com .google .common .io .ByteStreams ;
3333import com .loohp .interactivechat .Bungee .Metrics .Charts ;
3434import com .loohp .interactivechat .Bungee .Metrics .Metrics ;
35+ import com .loohp .interactivechat .ObjectHolders .CustomPlaceholder ;
36+ import com .loohp .interactivechat .ObjectHolders .CustomPlaceholder .CustomPlaceholderClickEvent ;
37+ import com .loohp .interactivechat .ObjectHolders .CustomPlaceholder .CustomPlaceholderHoverEvent ;
38+ import com .loohp .interactivechat .ObjectHolders .CustomPlaceholder .CustomPlaceholderReplaceText ;
39+ import com .loohp .interactivechat .ObjectHolders .CustomPlaceholder .ParsePlayer ;
40+ import com .loohp .interactivechat .ObjectHolders .ICPlaceholder ;
3541import com .loohp .interactivechat .Utils .CompressionUtils ;
3642import com .loohp .interactivechat .Utils .CustomArrayUtils ;
3743import com .loohp .interactivechat .Utils .DataTypeIO ;
44+ import com .loohp .interactivechat .Utils .MessageUtils ;
3845
3946import io .netty .channel .ChannelDuplexHandler ;
4047import io .netty .channel .ChannelHandlerContext ;
4653import net .md_5 .bungee .api .ChatColor ;
4754import net .md_5 .bungee .api .ProxyServer ;
4855import net .md_5 .bungee .api .ServerPing ;
56+ import net .md_5 .bungee .api .chat .ClickEvent ;
4957import net .md_5 .bungee .api .chat .TextComponent ;
5058import net .md_5 .bungee .api .config .ServerInfo ;
5159import net .md_5 .bungee .api .connection .ProxiedPlayer ;
60+ import net .md_5 .bungee .api .connection .Server ;
5261import net .md_5 .bungee .api .event .ChatEvent ;
5362import net .md_5 .bungee .api .event .PlayerDisconnectEvent ;
5463import net .md_5 .bungee .api .event .PluginMessageEvent ;
@@ -81,6 +90,9 @@ public class InteractiveChatBungee extends Plugin implements Listener {
8190
8291 public static List <String > parseCommands = new ArrayList <>();
8392
93+ public static Map <String , Map <String , String >> aliasesMapping = new HashMap <>();
94+ public static Map <String , List <ICPlaceholder >> placeholderList = new HashMap <>();
95+
8496 public static int delay = 200 ;
8597
8698 @ Override
@@ -159,7 +171,7 @@ public void onReceive(PluginMessageEvent event) {
159171 int packetNumber = in .readInt ();
160172 int packetId = in .readShort ();
161173
162- if (packetId == 0x08 || packetId == 0x09 ) {
174+ if (packetId == 0x08 || packetId == 0x09 || packetId == 0x10 || packetId == 0x11 ) {
163175 boolean isEnding = in .readBoolean ();
164176 byte [] data = new byte [packet .length - 7 ];
165177 in .readFully (data );
@@ -208,6 +220,49 @@ public void onReceive(PluginMessageEvent event) {
208220 case 0x09 :
209221 loadConfig ();
210222 break ;
223+ case 0x10 :
224+ int size = input .readInt ();
225+ Map <String , String > map = new HashMap <>();
226+ for (int i = 0 ; i < size ; i ++) {
227+ String key = DataTypeIO .readString (input , StandardCharsets .UTF_8 );
228+ String value = DataTypeIO .readString (input , StandardCharsets .UTF_8 );
229+ map .put (key , value );
230+ }
231+ aliasesMapping .put (((Server ) event .getSender ()).getInfo ().getName (), map );
232+ break ;
233+ case 0x11 :
234+ int size1 = input .readInt ();
235+ List <ICPlaceholder > list = new ArrayList <>(size1 );
236+ for (int i = 0 ; i < size1 ; i ++) {
237+ boolean isBulitIn = input .readBoolean ();
238+ if (isBulitIn ) {
239+ list .add (new ICPlaceholder (DataTypeIO .readString (input , StandardCharsets .UTF_8 ), input .readBoolean ()));
240+ } else {
241+ int customNo = input .readInt ();
242+ ParsePlayer parseplayer = ParsePlayer .fromOrder (input .readByte ());
243+ String placeholder = DataTypeIO .readString (input , StandardCharsets .UTF_8 );
244+ List <String > aliases = new ArrayList <>();
245+ int aliasSize = input .readInt ();
246+ for (int u = 0 ; u < aliasSize ; u ++) {
247+ aliases .add (DataTypeIO .readString (input , StandardCharsets .UTF_8 ));
248+ }
249+ boolean parseKeyword = input .readBoolean ();
250+ boolean casesensitive = input .readBoolean ();
251+ long cooldown = input .readLong ();
252+ boolean hoverEnabled = input .readBoolean ();
253+ String hoverText = DataTypeIO .readString (input , StandardCharsets .UTF_8 );
254+ boolean clickEnabled = input .readBoolean ();
255+ String clickAction = DataTypeIO .readString (input , StandardCharsets .UTF_8 );
256+ String clickValue = DataTypeIO .readString (input , StandardCharsets .UTF_8 );
257+ boolean replaceEnabled = input .readBoolean ();
258+ String replaceText = DataTypeIO .readString (input , StandardCharsets .UTF_8 );
259+
260+ list .add (new CustomPlaceholder (customNo , parseplayer , placeholder , aliases , parseKeyword , casesensitive , cooldown , new CustomPlaceholderHoverEvent (hoverEnabled , hoverText ), new CustomPlaceholderClickEvent (clickEnabled , clickEnabled ? ClickEvent .Action .valueOf (clickAction ) : null , clickValue ), new CustomPlaceholderReplaceText (replaceEnabled , replaceText )));
261+ }
262+ }
263+ placeholderList .put (((Server ) event .getSender ()).getInfo ().getName (), list );
264+ forwardPlaceholderList (list , ((Server ) event .getSender ()).getInfo ());
265+ break ;
211266 }
212267 } catch (IOException | DataFormatException e ) {
213268 e .printStackTrace ();
@@ -222,16 +277,98 @@ public void onReceive(PluginMessageEvent event) {
222277 }
223278 }
224279
280+ private void forwardPlaceholderList (List <ICPlaceholder > serverPlaceholderList , ServerInfo serverFrom ) throws IOException {
281+ ByteArrayDataOutput output = ByteStreams .newDataOutput ();
282+
283+ DataTypeIO .writeString (output , serverFrom .getName (), StandardCharsets .UTF_8 );
284+ output .writeInt (serverPlaceholderList .size ());
285+ for (ICPlaceholder placeholder : serverPlaceholderList ) {
286+ boolean isBuiltIn = placeholder .isBuildIn ();
287+ output .writeBoolean (isBuiltIn );
288+ if (isBuiltIn ) {
289+ DataTypeIO .writeString (output , placeholder .getKeyword (), StandardCharsets .UTF_8 );
290+ output .writeBoolean (placeholder .isCaseSensitive ());
291+ } else {
292+ CustomPlaceholder customPlaceholder = placeholder .getCustomPlaceholder ().get ();
293+ output .writeInt (customPlaceholder .getPosition ());
294+ output .writeByte (customPlaceholder .getParsePlayer ().getOrder ());
295+ DataTypeIO .writeString (output , customPlaceholder .getKeyword (), StandardCharsets .UTF_8 );
296+ output .writeInt (customPlaceholder .getAliases ().size ());
297+ for (String each : customPlaceholder .getAliases ()) {
298+ DataTypeIO .writeString (output , each , StandardCharsets .UTF_8 );
299+ }
300+ output .writeBoolean (customPlaceholder .getParseKeyword ());
301+ output .writeBoolean (customPlaceholder .isCaseSensitive ());
302+ output .writeLong (customPlaceholder .getCooldown ());
303+
304+ CustomPlaceholderHoverEvent hover = customPlaceholder .getHover ();
305+ output .writeBoolean (hover .isEnabled ());
306+ DataTypeIO .writeString (output , hover .getText (), StandardCharsets .UTF_8 );
307+
308+ CustomPlaceholderClickEvent click = customPlaceholder .getClick ();
309+ output .writeBoolean (click .isEnabled ());
310+ DataTypeIO .writeString (output , click .getAction () == null ? "" : click .getAction ().name (), StandardCharsets .UTF_8 );
311+ DataTypeIO .writeString (output , click .getValue (), StandardCharsets .UTF_8 );
312+
313+ CustomPlaceholderReplaceText replace = customPlaceholder .getReplace ();
314+ output .writeBoolean (replace .isEnabled ());
315+ DataTypeIO .writeString (output , replace .getReplaceText (), StandardCharsets .UTF_8 );
316+ }
317+ }
318+
319+ int packetNumber = random .nextInt ();
320+ int packetId = 0x09 ;
321+ byte [] data = output .toByteArray ();
322+
323+ byte [][] dataArray = CustomArrayUtils .divideArray (CompressionUtils .compress (data ), 32700 );
324+
325+ for (int i = 0 ; i < dataArray .length ; i ++) {
326+ byte [] chunk = dataArray [i ];
327+
328+ ByteArrayDataOutput out = ByteStreams .newDataOutput ();
329+ out .writeInt (packetNumber );
330+
331+ out .writeShort (packetId );
332+ out .writeBoolean (i == (dataArray .length - 1 ));
333+
334+ out .write (chunk );
335+
336+ for (ServerInfo server : getProxy ().getServers ().values ()) {
337+ if (!server .getSocketAddress ().equals (serverFrom .getSocketAddress ())) {
338+ server .sendData ("interchat:main" , out .toByteArray ());
339+ pluginMessagesCounter .incrementAndGet ();
340+ }
341+ }
342+ }
343+ }
344+
225345 @ EventHandler
226346 public void onBungeeChat (ChatEvent event ) {
227- UUID uuid = ((ProxiedPlayer ) event .getSender ()).getUniqueId ();
347+ ProxiedPlayer player = (ProxiedPlayer ) event .getSender ();
348+ UUID uuid = player .getUniqueId ();
228349 String message = event .getMessage ();
229350
230- if (message .startsWith ("/" )) {
351+ Map <String , String > serverAliasesMapping = aliasesMapping .get (player .getServer ().getInfo ().getName ());
352+ List <ICPlaceholder > serverPlaceholderList = placeholderList .get (player .getServer ().getInfo ().getName ());
353+ System .out .println ((serverAliasesMapping != null ) + " " + (serverPlaceholderList != null ));
354+ if (serverAliasesMapping != null && serverPlaceholderList != null ) {
355+ if (message .startsWith ("/" )) {
356+ if (InteractiveChatBungee .parseCommands .stream ().anyMatch (each -> event .getMessage ().matches (each ))) {
357+ message = MessageUtils .preprocessMessage (message , serverPlaceholderList , serverAliasesMapping );
358+ }
359+ } else {
360+ message = MessageUtils .preprocessMessage (message , serverPlaceholderList , serverAliasesMapping );
361+ }
362+ event .setMessage (message );
363+ }
364+
365+ String newMessage = event .getMessage ();
366+
367+ if (newMessage .startsWith ("/" )) {
231368 for (String parsecommand : InteractiveChatBungee .parseCommands ) {
232369 //getProxy().getConsole().sendMessage(new TextComponent(parsecommand));
233- if (message .matches (parsecommand )) {
234- String command = message .trim ();
370+ if (newMessage .matches (parsecommand )) {
371+ String command = newMessage .trim ();
235372 String uuidmatch = "<" + UUID .randomUUID ().toString () + ">" ;
236373 command += uuidmatch ;
237374 event .setMessage (command );
@@ -248,9 +385,9 @@ public void onBungeeChat(ChatEvent event) {
248385 @ Override
249386 public void run () {
250387 List <String > messages = forwardedMessages .get (uuid );
251- if (!messages .remove (message )) {
388+ if (!messages .remove (newMessage )) {
252389 try {
253- sendMessagePair (uuid , message );
390+ sendMessagePair (uuid , newMessage );
254391 } catch (IOException e ) {
255392 e .printStackTrace ();
256393 }
@@ -441,6 +578,26 @@ private void requestMessageProcess(ProxiedPlayer player, String component) throw
441578
442579 @ EventHandler
443580 public void onSwitch (ServerSwitchEvent event ) {
581+ ServerInfo to = event .getPlayer ().getServer ().getInfo ();
582+ if (!placeholderList .containsKey (to .getName ())) {
583+ try {
584+ requestPlaceholderList (to );
585+ } catch (IOException e ) {
586+ e .printStackTrace ();
587+ }
588+ }
589+ if (!aliasesMapping .containsKey (to .getName ())) {
590+ try {
591+ requestAliasesMapping (to );
592+ } catch (IOException e ) {
593+ e .printStackTrace ();
594+ }
595+ }
596+ try {
597+ sendPlayerListData ();
598+ } catch (IOException e ) {
599+ e .printStackTrace ();
600+ }
444601 new Timer ().schedule (new TimerTask () {
445602 @ Override
446603 public void run () {
@@ -450,13 +607,54 @@ public void run() {
450607 }
451608 }, 200 );
452609 }
610+
611+ private void requestPlaceholderList (ServerInfo server ) throws IOException {
612+ ByteArrayDataOutput output = ByteStreams .newDataOutput ();
453613
454- @ EventHandler
455- public void onJoin (PostLoginEvent event ) {
456- try {
457- sendPlayerListData ();
458- } catch (IOException e ) {
459- e .printStackTrace ();
614+ int packetNumber = random .nextInt ();
615+ int packetId = 0x10 ;
616+ byte [] data = output .toByteArray ();
617+
618+ byte [][] dataArray = CustomArrayUtils .divideArray (CompressionUtils .compress (data ), 32700 );
619+
620+ for (int i = 0 ; i < dataArray .length ; i ++) {
621+ byte [] chunk = dataArray [i ];
622+
623+ ByteArrayDataOutput out = ByteStreams .newDataOutput ();
624+ out .writeInt (packetNumber );
625+
626+ out .writeShort (packetId );
627+ out .writeBoolean (i == (dataArray .length - 1 ));
628+
629+ out .write (chunk );
630+
631+ server .sendData ("interchat:main" , out .toByteArray ());
632+ pluginMessagesCounter .incrementAndGet ();
633+ }
634+ }
635+
636+ private void requestAliasesMapping (ServerInfo server ) throws IOException {
637+ ByteArrayDataOutput output = ByteStreams .newDataOutput ();
638+
639+ int packetNumber = random .nextInt ();
640+ int packetId = 0x11 ;
641+ byte [] data = output .toByteArray ();
642+
643+ byte [][] dataArray = CustomArrayUtils .divideArray (CompressionUtils .compress (data ), 32700 );
644+
645+ for (int i = 0 ; i < dataArray .length ; i ++) {
646+ byte [] chunk = dataArray [i ];
647+
648+ ByteArrayDataOutput out = ByteStreams .newDataOutput ();
649+ out .writeInt (packetNumber );
650+
651+ out .writeShort (packetId );
652+ out .writeBoolean (i == (dataArray .length - 1 ));
653+
654+ out .write (chunk );
655+
656+ server .sendData ("interchat:main" , out .toByteArray ());
657+ pluginMessagesCounter .incrementAndGet ();
460658 }
461659 }
462660
@@ -480,6 +678,7 @@ private void sendPlayerListData() throws IOException {
480678 Collection <ProxiedPlayer > players = ProxyServer .getInstance ().getPlayers ();
481679 output .writeInt (players .size ());
482680 for (ProxiedPlayer player : players ) {
681+ DataTypeIO .writeString (output , player .getServer ().getInfo ().getName (), StandardCharsets .UTF_8 );
483682 DataTypeIO .writeUUID (output , player .getUniqueId ());
484683 DataTypeIO .writeString (output , player .getDisplayName (), StandardCharsets .UTF_8 );
485684 }
0 commit comments