Skip to content

Commit 288b60e

Browse files
committed
Fixed some bugs and redeployed
1 parent 686fa0c commit 288b60e

File tree

3 files changed

+111
-69
lines changed

3 files changed

+111
-69
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ A modern version of Fanciful
44

55
This library aims to completely replace [Fanciful](https://bukkit.org/threads/lib-fanciful-pleasant-chat-message-formatting.195148/), which sadly has stopped being updated. I figured I'd take a stab at re-creating it, and even making it better.
66

7-
The best thing about this library? It's only a [single class file](https://github.com/Rayzr522/JSONMessage/blob/master/src/main/java/com/rayzr522/jsonmessage/JSONMessage.java), so you can just drag and drop it into your project!
7+
The best thing about this library? It's only a [single class file](https://github.com/Rayzr522/JSONMessage/blob/master/src/main/java/me/rayzr522/jsonmessage/JSONMessage.java), so you can just drag and drop it into your project!
88

99
## Usage
1010

@@ -91,7 +91,7 @@ JSONMessage.create("A wild subtitle has appeared!")
9191
.subtitle(player);
9292
```
9393

94-
The only thing to note is that for subtitles you don't pass in times, as that's completely handled by the orginial title.
94+
The only thing to note is that for subtitles you don't pass in times, as that's completely handled by the original title.
9595

9696
> Note: If you don't have the following features then please make sure to download the latest version of the class file
9797
@@ -141,7 +141,7 @@ Method | Description
141141
`bar()` | Creates a horizontal divider bar 53 characters long. This is perfect for the default chat window width
142142
`newline()` | Inserts a newline. It really isn't necessary, you can just use `\n` if you want
143143
`toJSON()` | Converts the JSONMessage to a `JsonObject` (Google's Gson library, comes with Bukkit)
144-
`toString()` | Converts the JSONMessage to a String, useable in things like `/tellraw`. This is an alias of `toJSON().toString()`
144+
`toString()` | Converts the JSONMessage to a String, usable in things like `/tellraw`. This is an alias of `toJSON().toString()`
145145
`send(Player...)` | Sends the JSONMessage to one or many players
146146
`title(int, int, int, Player...)` | Sends the JSONMessage as a title to one or many players. Int parameters are `fadeIn`, `stay`, and `fadeOut`
147147
`subtitle(Player...)` | Sends the JSONMessage as a subtitle to one or many players

pom.xml

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,57 @@
4242
<plugin>
4343
<groupId>org.apache.maven.plugins</groupId>
4444
<artifactId>maven-compiler-plugin</artifactId>
45-
<version>3.6.1</version>
45+
<version>3.7.0</version>
4646
<configuration>
4747
<source>${java.version}</source>
4848
<target>${java.version}</target>
4949
</configuration>
5050
</plugin>
51+
<plugin>
52+
<groupId>org.apache.maven.plugins</groupId>
53+
<artifactId>maven-source-plugin</artifactId>
54+
<version>3.0.1</version>
55+
<executions>
56+
<execution>
57+
<id>attach-sources</id>
58+
<phase>deploy</phase>
59+
<goals>
60+
<goal>jar-no-fork</goal>
61+
</goals>
62+
</execution>
63+
</executions>
64+
</plugin>
65+
<plugin>
66+
<groupId>org.apache.maven.plugins</groupId>
67+
<artifactId>maven-javadoc-plugin</artifactId>
68+
<version>3.0.0</version>
69+
<executions>
70+
<execution>
71+
<id>attach-javadocs</id>
72+
<phase>deploy</phase>
73+
<goals>
74+
<goal>jar</goal>
75+
</goals>
76+
</execution>
77+
</executions>
78+
</plugin>
79+
<plugin>
80+
<groupId>org.apache.maven.plugins</groupId>
81+
<artifactId>maven-deploy-plugin</artifactId>
82+
<version>2.8.2</version>
83+
<executions>
84+
<execution>
85+
<id>deploy</id>
86+
<phase>deploy</phase>
87+
<goals>
88+
<goal>deploy</goal>
89+
</goals>
90+
</execution>
91+
</executions>
92+
<!--<configuration>-->
93+
<!--<javadoc>${project.build.directory}/${project.build.finalName}-javadoc.jar</javadoc>-->
94+
<!--</configuration>-->
95+
</plugin>
5196
</plugins>
5297
</build>
5398
</project>

src/main/java/me/rayzr522/jsonmessage/JSONMessage.java

Lines changed: 62 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*
2727
* @author Rayzr
2828
*/
29+
@SuppressWarnings({"WeakerAccess", "unused"})
2930
public 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 &amp;6, &amp;l, &amp;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

Comments
 (0)