2626 *
2727 * @author Rayzr
2828 */
29+ @ SuppressWarnings ({"WeakerAccess" , "unused" })
2930public class JSONMessage {
3031
3132 private static final BiMap <ChatColor , String > stylesToNames ;
@@ -55,7 +56,7 @@ public class JSONMessage {
5556 stylesToNames = builder .build ();
5657 }
5758
58- private List <MessagePart > parts = new ArrayList <>();
59+ private final List <MessagePart > parts = new ArrayList <>();
5960
6061 /**
6162 * Creates a new {@link JSONMessage} object
@@ -70,13 +71,16 @@ private JSONMessage(String text) {
7071 * Creates a new {@link JSONMessage} object
7172 *
7273 * @param text The text to start with
74+ * @return A new {@link JSONMessage} object
7375 */
7476 public static JSONMessage create (String text ) {
7577 return new JSONMessage (text );
7678 }
7779
7880 /**
7981 * Creates a new {@link JSONMessage} object
82+ *
83+ * @return A new {@link JSONMessage} object
8084 */
8185 public static JSONMessage create () {
8286 return create ("" );
@@ -85,6 +89,7 @@ public static JSONMessage create() {
8589 /**
8690 * Sends an action bar message
8791 *
92+ * @param message The message to send
8893 * @param players The players you want to send it to
8994 */
9095 public static void actionbar (String message , Player ... players ) {
@@ -132,7 +137,7 @@ public String toString() {
132137
133138 /**
134139 * 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.)
136141 *
137142 * @return This {@link JSONMessage} instance {@link JSONMessage} in legacy format
138143 */
@@ -483,88 +488,77 @@ public static MessageEvent showAchievement(String id) {
483488
484489 private static class ReflectionHelper {
485490
491+ private static final String version ;
486492 private static Class <?> craftPlayer ;
487-
488493 private static Constructor <?> chatComponentText ;
489494 private static Class <?> packetPlayOutChat ;
490495 private static Class <?> packetPlayOutTitle ;
491496 private static Class <?> iChatBaseComponent ;
492497 private static Class <?> titleAction ;
493-
494498 private static Field connection ;
495499 private static MethodHandle GET_HANDLE ;
496500 private static MethodHandle SEND_PACKET ;
497501 private static MethodHandle STRING_TO_CHAT ;
498-
499502 private static Object enumActionTitle ;
500503 private static Object enumActionSubtitle ;
501-
502504 private static Object enumChatMessage ;
503505 private static Object enumActionbarMessage ;
504-
505- private static String version ;
506-
507- private static boolean SETUP = false ;
506+ private static boolean SETUP ;
508507 private static int MAJOR_VER = -1 ;
509508
510509 static {
510+ String [] split = Bukkit .getServer ().getClass ().getPackage ().getName ().split ("\\ ." );
511+ version = split [split .length - 1 ];
511512
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 ;
521515
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 ();
526517
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" ));
528522
529- iChatBaseComponent = getClass ("{nms}.IChatBaseComponent" );
523+ chatComponentText = getClass ("{nms}.ChatComponentText" ). getConstructor ( String . class );
530524
531- Method stringToChat ;
525+ iChatBaseComponent = getClass ( "{nms}.IChatBaseComponent" ) ;
532526
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 ;
538528
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+ }
542534
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 );
545538
546- titleAction = getClass ("{nms}.PacketPlayOutTitle$EnumTitleAction" );
539+ packetPlayOutChat = getClass ("{nms}.PacketPlayOutChat" );
540+ packetPlayOutTitle = getClass ("{nms}.PacketPlayOutTitle" );
547541
548- enumActionTitle = titleAction .getField ("TITLE" ).get (null );
549- enumActionSubtitle = titleAction .getField ("SUBTITLE" ).get (null );
542+ titleAction = getClass ("{nms}.PacketPlayOutTitle$EnumTitleAction" );
550543
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 );
553546
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 );
557549
558- } catch (Exception e ) {
559- e .printStackTrace ();
560- SETUP = false ;
550+ enumChatMessage = getChatMessageType .invoke (null , (byte ) 1 );
551+ enumActionbarMessage = getChatMessageType .invoke (null , (byte ) 2 );
561552 }
562553
554+ } catch (Exception e ) {
555+ e .printStackTrace ();
556+ SETUP = false ;
563557 }
564558
565559 }
566560
567- public static void sendPacket (Object packet , Player ... players ) {
561+ static void sendPacket (Object packet , Player ... players ) {
568562 if (!SETUP ) {
569563 throw new IllegalStateException ("ReflectionHelper is not set up!" );
570564 }
@@ -585,19 +579,22 @@ public static void sendPacket(Object packet, Player... players) {
585579
586580 private static void setType (Object object , byte type ) {
587581 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" );
594591 }
595592 } else {
596593 set ("b" , object , type );
597594 }
598595 }
599596
600- public static Object createActionbarPacket (String message ) {
597+ static Object createActionbarPacket (String message ) {
601598 if (!SETUP ) {
602599 throw new IllegalStateException ("ReflectionHelper is not set up!" );
603600 }
@@ -606,7 +603,7 @@ public static Object createActionbarPacket(String message) {
606603 return packet ;
607604 }
608605
609- public static Object createTextPacket (String message ) {
606+ static Object createTextPacket (String message ) {
610607 if (!SETUP ) {
611608 throw new IllegalStateException ("ReflectionHelper is not set up!" );
612609 }
@@ -622,7 +619,7 @@ public static Object createTextPacket(String message) {
622619
623620 }
624621
625- public static Object createTitlePacket (String message ) {
622+ static Object createTitlePacket (String message ) {
626623 if (!SETUP ) {
627624 throw new IllegalStateException ("ReflectionHelper is not set up!" );
628625 }
@@ -635,7 +632,7 @@ public static Object createTitlePacket(String message) {
635632
636633 }
637634
638- public static Object createSubtitlePacket (String message ) {
635+ static Object createSubtitlePacket (String message ) {
639636 if (!SETUP ) {
640637 throw new IllegalStateException ("ReflectionHelper is not set up!" );
641638 }
@@ -648,7 +645,7 @@ public static Object createSubtitlePacket(String message) {
648645
649646 }
650647
651- public static Object createTitleTimesPacket (int fadeIn , int stay , int fadeOut ) {
648+ static Object createTitleTimesPacket (int fadeIn , int stay , int fadeOut ) {
652649 if (!SETUP ) {
653650 throw new IllegalStateException ("ReflectionHelper is not set up!" );
654651 }
@@ -667,7 +664,7 @@ public static Object createTitleTimesPacket(int fadeIn, int stay, int fadeOut) {
667664 * @param message The text to convert to a chat component
668665 * @return The chat component
669666 */
670- public static Object componentText (String message ) {
667+ static Object componentText (String message ) {
671668 if (!SETUP ) {
672669 throw new IllegalStateException ("ReflectionHelper is not set up!" );
673670 }
@@ -686,7 +683,7 @@ public static Object componentText(String message) {
686683 * @param json The JSON to attempt to parse
687684 * @return The object representing the text in JSON form, or <code>null</code> if something went wrong converting the String to JSON data
688685 */
689- public static Object fromJson (String json ) {
686+ static Object fromJson (String json ) {
690687 if (!SETUP ) {
691688 throw new IllegalStateException ("ReflectionHelper is not set up!" );
692689 }
@@ -717,7 +714,7 @@ public static Object fromJson(String json) {
717714 * @return The class
718715 * @throws ClassNotFoundException If the class was not found
719716 */
720- public static Class <?> getClass (String path ) throws ClassNotFoundException {
717+ static Class <?> getClass (String path ) throws ClassNotFoundException {
721718 if (!SETUP ) {
722719 throw new IllegalStateException ("ReflectionHelper is not set up!" );
723720 }
@@ -731,7 +728,7 @@ public static Class<?> getClass(String path) throws ClassNotFoundException {
731728 * @param obj The object to change the field of
732729 * @param value The new value to set
733730 */
734- public static void set (String field , Object obj , Object value ) {
731+ static void set (String field , Object obj , Object value ) {
735732 try {
736733 Field f = obj .getClass ().getDeclaredField (field );
737734 f .setAccessible (true );
@@ -741,7 +738,7 @@ public static void set(String field, Object obj, Object value) {
741738 }
742739 }
743740
744- public static int getVersion () {
741+ static int getVersion () {
745742 if (!SETUP ) {
746743 throw new IllegalStateException ("ReflectionHelper is not set up!" );
747744 }
@@ -766,9 +763,9 @@ public static int getVersion() {
766763 */
767764 public class MessagePart {
768765
766+ private final List <ChatColor > styles = new ArrayList <>();
769767 private MessageEvent onClick ;
770768 private MessageEvent onHover ;
771- private List <ChatColor > styles = new ArrayList <>();
772769 private ChatColor color ;
773770 private String text ;
774771
0 commit comments