26
26
*
27
27
* @author Rayzr
28
28
*/
29
+ @ SuppressWarnings ({"WeakerAccess" , "unused" })
29
30
public class JSONMessage {
30
31
31
32
private static final BiMap <ChatColor , String > stylesToNames ;
@@ -55,7 +56,7 @@ public class JSONMessage {
55
56
stylesToNames = builder .build ();
56
57
}
57
58
58
- private List <MessagePart > parts = new ArrayList <>();
59
+ private final List <MessagePart > parts = new ArrayList <>();
59
60
60
61
/**
61
62
* Creates a new {@link JSONMessage} object
@@ -70,13 +71,16 @@ private JSONMessage(String text) {
70
71
* Creates a new {@link JSONMessage} object
71
72
*
72
73
* @param text The text to start with
74
+ * @return A new {@link JSONMessage} object
73
75
*/
74
76
public static JSONMessage create (String text ) {
75
77
return new JSONMessage (text );
76
78
}
77
79
78
80
/**
79
81
* Creates a new {@link JSONMessage} object
82
+ *
83
+ * @return A new {@link JSONMessage} object
80
84
*/
81
85
public static JSONMessage create () {
82
86
return create ("" );
@@ -85,6 +89,7 @@ public static JSONMessage create() {
85
89
/**
86
90
* Sends an action bar message
87
91
*
92
+ * @param message The message to send
88
93
* @param players The players you want to send it to
89
94
*/
90
95
public static void actionbar (String message , Player ... players ) {
@@ -132,7 +137,7 @@ public String toString() {
132
137
133
138
/**
134
139
* Converts this {@link JSONMessage} object to the legacy formatting system, which
135
- * uses formatting codes (like &6, &l, &4, etc.)
140
+ * uses formatting codes (like & 6, & l, & 4, etc.)
136
141
*
137
142
* @return This {@link JSONMessage} instance {@link JSONMessage} in legacy format
138
143
*/
@@ -483,88 +488,77 @@ public static MessageEvent showAchievement(String id) {
483
488
484
489
private static class ReflectionHelper {
485
490
491
+ private static final String version ;
486
492
private static Class <?> craftPlayer ;
487
-
488
493
private static Constructor <?> chatComponentText ;
489
494
private static Class <?> packetPlayOutChat ;
490
495
private static Class <?> packetPlayOutTitle ;
491
496
private static Class <?> iChatBaseComponent ;
492
497
private static Class <?> titleAction ;
493
-
494
498
private static Field connection ;
495
499
private static MethodHandle GET_HANDLE ;
496
500
private static MethodHandle SEND_PACKET ;
497
501
private static MethodHandle STRING_TO_CHAT ;
498
-
499
502
private static Object enumActionTitle ;
500
503
private static Object enumActionSubtitle ;
501
-
502
504
private static Object enumChatMessage ;
503
505
private static Object enumActionbarMessage ;
504
-
505
- private static String version ;
506
-
507
- private static boolean SETUP = false ;
506
+ private static boolean SETUP ;
508
507
private static int MAJOR_VER = -1 ;
509
508
510
509
static {
510
+ String [] split = Bukkit .getServer ().getClass ().getPackage ().getName ().split ("\\ ." );
511
+ version = split [split .length - 1 ];
511
512
512
- if (!SETUP ) {
513
-
514
- String [] split = Bukkit .getServer ().getClass ().getPackage ().getName ().split ("\\ ." );
515
- version = split [split .length - 1 ];
516
-
517
- try {
518
- SETUP = true ;
519
-
520
- MAJOR_VER = getVersion ();
513
+ try {
514
+ SETUP = true ;
521
515
522
- craftPlayer = getClass ("{obc}.entity.CraftPlayer" );
523
- Method getHandle = craftPlayer .getMethod ("getHandle" );
524
- connection = getHandle .getReturnType ().getField ("playerConnection" );
525
- Method sendPacket = connection .getType ().getMethod ("sendPacket" , getClass ("{nms}.Packet" ));
516
+ MAJOR_VER = getVersion ();
526
517
527
- chatComponentText = getClass ("{nms}.ChatComponentText" ).getConstructor (String .class );
518
+ craftPlayer = getClass ("{obc}.entity.CraftPlayer" );
519
+ Method getHandle = craftPlayer .getMethod ("getHandle" );
520
+ connection = getHandle .getReturnType ().getField ("playerConnection" );
521
+ Method sendPacket = connection .getType ().getMethod ("sendPacket" , getClass ("{nms}.Packet" ));
528
522
529
- iChatBaseComponent = getClass ("{nms}.IChatBaseComponent" );
523
+ chatComponentText = getClass ("{nms}.ChatComponentText" ). getConstructor ( String . class );
530
524
531
- Method stringToChat ;
525
+ iChatBaseComponent = getClass ( "{nms}.IChatBaseComponent" ) ;
532
526
533
- if (MAJOR_VER < 8 ) {
534
- stringToChat = getClass ("{nms}.ChatSerializer" ).getMethod ("a" , String .class );
535
- } else {
536
- stringToChat = getClass ("{nms}.IChatBaseComponent$ChatSerializer" ).getMethod ("a" , String .class );
537
- }
527
+ Method stringToChat ;
538
528
539
- GET_HANDLE = MethodHandles .lookup ().unreflect (getHandle );
540
- SEND_PACKET = MethodHandles .lookup ().unreflect (sendPacket );
541
- STRING_TO_CHAT = MethodHandles .lookup ().unreflect (stringToChat );
529
+ if (MAJOR_VER < 8 ) {
530
+ stringToChat = getClass ("{nms}.ChatSerializer" ).getMethod ("a" , String .class );
531
+ } else {
532
+ stringToChat = getClass ("{nms}.IChatBaseComponent$ChatSerializer" ).getMethod ("a" , String .class );
533
+ }
542
534
543
- packetPlayOutChat = getClass ("{nms}.PacketPlayOutChat" );
544
- packetPlayOutTitle = getClass ("{nms}.PacketPlayOutTitle" );
535
+ GET_HANDLE = MethodHandles .lookup ().unreflect (getHandle );
536
+ SEND_PACKET = MethodHandles .lookup ().unreflect (sendPacket );
537
+ STRING_TO_CHAT = MethodHandles .lookup ().unreflect (stringToChat );
545
538
546
- titleAction = getClass ("{nms}.PacketPlayOutTitle$EnumTitleAction" );
539
+ packetPlayOutChat = getClass ("{nms}.PacketPlayOutChat" );
540
+ packetPlayOutTitle = getClass ("{nms}.PacketPlayOutTitle" );
547
541
548
- enumActionTitle = titleAction .getField ("TITLE" ).get (null );
549
- enumActionSubtitle = titleAction .getField ("SUBTITLE" ).get (null );
542
+ titleAction = getClass ("{nms}.PacketPlayOutTitle$EnumTitleAction" );
550
543
551
- if ( MAJOR_VER >= 12 ) {
552
- Method getChatMessageType = getClass ( "{nms}.ChatMessageType " ).getMethod ( "a" , byte . class );
544
+ enumActionTitle = titleAction . getField ( "TITLE" ). get ( null );
545
+ enumActionSubtitle = titleAction . getField ( "SUBTITLE " ).get ( null );
553
546
554
- enumChatMessage = getChatMessageType .invoke (null , (byte ) 1 );
555
- enumActionbarMessage = getChatMessageType .invoke (null , (byte ) 2 );
556
- }
547
+ if (MAJOR_VER >= 12 ) {
548
+ Method getChatMessageType = getClass ("{nms}.ChatMessageType" ).getMethod ("a" , byte .class );
557
549
558
- } catch (Exception e ) {
559
- e .printStackTrace ();
560
- SETUP = false ;
550
+ enumChatMessage = getChatMessageType .invoke (null , (byte ) 1 );
551
+ enumActionbarMessage = getChatMessageType .invoke (null , (byte ) 2 );
561
552
}
562
553
554
+ } catch (Exception e ) {
555
+ e .printStackTrace ();
556
+ SETUP = false ;
563
557
}
564
558
565
559
}
566
560
567
- public static void sendPacket (Object packet , Player ... players ) {
561
+ static void sendPacket (Object packet , Player ... players ) {
568
562
if (!SETUP ) {
569
563
throw new IllegalStateException ("ReflectionHelper is not set up!" );
570
564
}
@@ -585,19 +579,22 @@ public static void sendPacket(Object packet, Player... players) {
585
579
586
580
private static void setType (Object object , byte type ) {
587
581
if (MAJOR_VER >= 12 ) {
588
- if (type == 1 ) {
589
- set ("b" , object , enumChatMessage );
590
- } else if (type == 2 ) {
591
- set ("b" , object , enumActionbarMessage );
592
- } else {
593
- throw new IllegalArgumentException ("type must be 1 or 2" );
582
+ switch (type ) {
583
+ case 1 :
584
+ set ("b" , object , enumChatMessage );
585
+ break ;
586
+ case 2 :
587
+ set ("b" , object , enumActionbarMessage );
588
+ break ;
589
+ default :
590
+ throw new IllegalArgumentException ("type must be 1 or 2" );
594
591
}
595
592
} else {
596
593
set ("b" , object , type );
597
594
}
598
595
}
599
596
600
- public static Object createActionbarPacket (String message ) {
597
+ static Object createActionbarPacket (String message ) {
601
598
if (!SETUP ) {
602
599
throw new IllegalStateException ("ReflectionHelper is not set up!" );
603
600
}
@@ -606,7 +603,7 @@ public static Object createActionbarPacket(String message) {
606
603
return packet ;
607
604
}
608
605
609
- public static Object createTextPacket (String message ) {
606
+ static Object createTextPacket (String message ) {
610
607
if (!SETUP ) {
611
608
throw new IllegalStateException ("ReflectionHelper is not set up!" );
612
609
}
@@ -622,7 +619,7 @@ public static Object createTextPacket(String message) {
622
619
623
620
}
624
621
625
- public static Object createTitlePacket (String message ) {
622
+ static Object createTitlePacket (String message ) {
626
623
if (!SETUP ) {
627
624
throw new IllegalStateException ("ReflectionHelper is not set up!" );
628
625
}
@@ -635,7 +632,7 @@ public static Object createTitlePacket(String message) {
635
632
636
633
}
637
634
638
- public static Object createSubtitlePacket (String message ) {
635
+ static Object createSubtitlePacket (String message ) {
639
636
if (!SETUP ) {
640
637
throw new IllegalStateException ("ReflectionHelper is not set up!" );
641
638
}
@@ -648,7 +645,7 @@ public static Object createSubtitlePacket(String message) {
648
645
649
646
}
650
647
651
- public static Object createTitleTimesPacket (int fadeIn , int stay , int fadeOut ) {
648
+ static Object createTitleTimesPacket (int fadeIn , int stay , int fadeOut ) {
652
649
if (!SETUP ) {
653
650
throw new IllegalStateException ("ReflectionHelper is not set up!" );
654
651
}
@@ -667,7 +664,7 @@ public static Object createTitleTimesPacket(int fadeIn, int stay, int fadeOut) {
667
664
* @param message The text to convert to a chat component
668
665
* @return The chat component
669
666
*/
670
- public static Object componentText (String message ) {
667
+ static Object componentText (String message ) {
671
668
if (!SETUP ) {
672
669
throw new IllegalStateException ("ReflectionHelper is not set up!" );
673
670
}
@@ -686,7 +683,7 @@ public static Object componentText(String message) {
686
683
* @param json The JSON to attempt to parse
687
684
* @return The object representing the text in JSON form, or <code>null</code> if something went wrong converting the String to JSON data
688
685
*/
689
- public static Object fromJson (String json ) {
686
+ static Object fromJson (String json ) {
690
687
if (!SETUP ) {
691
688
throw new IllegalStateException ("ReflectionHelper is not set up!" );
692
689
}
@@ -717,7 +714,7 @@ public static Object fromJson(String json) {
717
714
* @return The class
718
715
* @throws ClassNotFoundException If the class was not found
719
716
*/
720
- public static Class <?> getClass (String path ) throws ClassNotFoundException {
717
+ static Class <?> getClass (String path ) throws ClassNotFoundException {
721
718
if (!SETUP ) {
722
719
throw new IllegalStateException ("ReflectionHelper is not set up!" );
723
720
}
@@ -731,7 +728,7 @@ public static Class<?> getClass(String path) throws ClassNotFoundException {
731
728
* @param obj The object to change the field of
732
729
* @param value The new value to set
733
730
*/
734
- public static void set (String field , Object obj , Object value ) {
731
+ static void set (String field , Object obj , Object value ) {
735
732
try {
736
733
Field f = obj .getClass ().getDeclaredField (field );
737
734
f .setAccessible (true );
@@ -741,7 +738,7 @@ public static void set(String field, Object obj, Object value) {
741
738
}
742
739
}
743
740
744
- public static int getVersion () {
741
+ static int getVersion () {
745
742
if (!SETUP ) {
746
743
throw new IllegalStateException ("ReflectionHelper is not set up!" );
747
744
}
@@ -766,9 +763,9 @@ public static int getVersion() {
766
763
*/
767
764
public class MessagePart {
768
765
766
+ private final List <ChatColor > styles = new ArrayList <>();
769
767
private MessageEvent onClick ;
770
768
private MessageEvent onHover ;
771
- private List <ChatColor > styles = new ArrayList <>();
772
769
private ChatColor color ;
773
770
private String text ;
774
771
0 commit comments