Skip to content

Commit

Permalink
Fixed some bugs and redeployed
Browse files Browse the repository at this point in the history
  • Loading branch information
foxfirecodes committed Jan 6, 2018
1 parent 686fa0c commit 288b60e
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 69 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A modern version of Fanciful

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.

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!
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!

## Usage

Expand Down Expand Up @@ -91,7 +91,7 @@ JSONMessage.create("A wild subtitle has appeared!")
.subtitle(player);
```

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

> Note: If you don't have the following features then please make sure to download the latest version of the class file
Expand Down Expand Up @@ -141,7 +141,7 @@ Method | Description
`bar()` | Creates a horizontal divider bar 53 characters long. This is perfect for the default chat window width
`newline()` | Inserts a newline. It really isn't necessary, you can just use `\n` if you want
`toJSON()` | Converts the JSONMessage to a `JsonObject` (Google's Gson library, comes with Bukkit)
`toString()` | Converts the JSONMessage to a String, useable in things like `/tellraw`. This is an alias of `toJSON().toString()`
`toString()` | Converts the JSONMessage to a String, usable in things like `/tellraw`. This is an alias of `toJSON().toString()`
`send(Player...)` | Sends the JSONMessage to one or many players
`title(int, int, int, Player...)` | Sends the JSONMessage as a title to one or many players. Int parameters are `fadeIn`, `stay`, and `fadeOut`
`subtitle(Player...)` | Sends the JSONMessage as a subtitle to one or many players
Expand Down
47 changes: 46 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,57 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<version>3.7.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>deploy</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>attach-javadocs</id>
<phase>deploy</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<executions>
<execution>
<id>deploy</id>
<phase>deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
<!--<configuration>-->
<!--<javadoc>${project.build.directory}/${project.build.finalName}-javadoc.jar</javadoc>-->
<!--</configuration>-->
</plugin>
</plugins>
</build>
</project>
127 changes: 62 additions & 65 deletions src/main/java/me/rayzr522/jsonmessage/JSONMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*
* @author Rayzr
*/
@SuppressWarnings({"WeakerAccess", "unused"})
public class JSONMessage {

private static final BiMap<ChatColor, String> stylesToNames;
Expand Down Expand Up @@ -55,7 +56,7 @@ public class JSONMessage {
stylesToNames = builder.build();
}

private List<MessagePart> parts = new ArrayList<>();
private final List<MessagePart> parts = new ArrayList<>();

/**
* Creates a new {@link JSONMessage} object
Expand All @@ -70,13 +71,16 @@ private JSONMessage(String text) {
* Creates a new {@link JSONMessage} object
*
* @param text The text to start with
* @return A new {@link JSONMessage} object
*/
public static JSONMessage create(String text) {
return new JSONMessage(text);
}

/**
* Creates a new {@link JSONMessage} object
*
* @return A new {@link JSONMessage} object
*/
public static JSONMessage create() {
return create("");
Expand All @@ -85,6 +89,7 @@ public static JSONMessage create() {
/**
* Sends an action bar message
*
* @param message The message to send
* @param players The players you want to send it to
*/
public static void actionbar(String message, Player... players) {
Expand Down Expand Up @@ -132,7 +137,7 @@ public String toString() {

/**
* Converts this {@link JSONMessage} object to the legacy formatting system, which
* uses formatting codes (like &6, &l, &4, etc.)
* uses formatting codes (like &amp;6, &amp;l, &amp;4, etc.)
*
* @return This {@link JSONMessage} instance {@link JSONMessage} in legacy format
*/
Expand Down Expand Up @@ -483,88 +488,77 @@ public static MessageEvent showAchievement(String id) {

private static class ReflectionHelper {

private static final String version;
private static Class<?> craftPlayer;

private static Constructor<?> chatComponentText;
private static Class<?> packetPlayOutChat;
private static Class<?> packetPlayOutTitle;
private static Class<?> iChatBaseComponent;
private static Class<?> titleAction;

private static Field connection;
private static MethodHandle GET_HANDLE;
private static MethodHandle SEND_PACKET;
private static MethodHandle STRING_TO_CHAT;

private static Object enumActionTitle;
private static Object enumActionSubtitle;

private static Object enumChatMessage;
private static Object enumActionbarMessage;

private static String version;

private static boolean SETUP = false;
private static boolean SETUP;
private static int MAJOR_VER = -1;

static {
String[] split = Bukkit.getServer().getClass().getPackage().getName().split("\\.");
version = split[split.length - 1];

if (!SETUP) {

String[] split = Bukkit.getServer().getClass().getPackage().getName().split("\\.");
version = split[split.length - 1];

try {
SETUP = true;

MAJOR_VER = getVersion();
try {
SETUP = true;

craftPlayer = getClass("{obc}.entity.CraftPlayer");
Method getHandle = craftPlayer.getMethod("getHandle");
connection = getHandle.getReturnType().getField("playerConnection");
Method sendPacket = connection.getType().getMethod("sendPacket", getClass("{nms}.Packet"));
MAJOR_VER = getVersion();

chatComponentText = getClass("{nms}.ChatComponentText").getConstructor(String.class);
craftPlayer = getClass("{obc}.entity.CraftPlayer");
Method getHandle = craftPlayer.getMethod("getHandle");
connection = getHandle.getReturnType().getField("playerConnection");
Method sendPacket = connection.getType().getMethod("sendPacket", getClass("{nms}.Packet"));

iChatBaseComponent = getClass("{nms}.IChatBaseComponent");
chatComponentText = getClass("{nms}.ChatComponentText").getConstructor(String.class);

Method stringToChat;
iChatBaseComponent = getClass("{nms}.IChatBaseComponent");

if (MAJOR_VER < 8) {
stringToChat = getClass("{nms}.ChatSerializer").getMethod("a", String.class);
} else {
stringToChat = getClass("{nms}.IChatBaseComponent$ChatSerializer").getMethod("a", String.class);
}
Method stringToChat;

GET_HANDLE = MethodHandles.lookup().unreflect(getHandle);
SEND_PACKET = MethodHandles.lookup().unreflect(sendPacket);
STRING_TO_CHAT = MethodHandles.lookup().unreflect(stringToChat);
if (MAJOR_VER < 8) {
stringToChat = getClass("{nms}.ChatSerializer").getMethod("a", String.class);
} else {
stringToChat = getClass("{nms}.IChatBaseComponent$ChatSerializer").getMethod("a", String.class);
}

packetPlayOutChat = getClass("{nms}.PacketPlayOutChat");
packetPlayOutTitle = getClass("{nms}.PacketPlayOutTitle");
GET_HANDLE = MethodHandles.lookup().unreflect(getHandle);
SEND_PACKET = MethodHandles.lookup().unreflect(sendPacket);
STRING_TO_CHAT = MethodHandles.lookup().unreflect(stringToChat);

titleAction = getClass("{nms}.PacketPlayOutTitle$EnumTitleAction");
packetPlayOutChat = getClass("{nms}.PacketPlayOutChat");
packetPlayOutTitle = getClass("{nms}.PacketPlayOutTitle");

enumActionTitle = titleAction.getField("TITLE").get(null);
enumActionSubtitle = titleAction.getField("SUBTITLE").get(null);
titleAction = getClass("{nms}.PacketPlayOutTitle$EnumTitleAction");

if (MAJOR_VER >= 12) {
Method getChatMessageType = getClass("{nms}.ChatMessageType").getMethod("a", byte.class);
enumActionTitle = titleAction.getField("TITLE").get(null);
enumActionSubtitle = titleAction.getField("SUBTITLE").get(null);

enumChatMessage = getChatMessageType.invoke(null, (byte) 1);
enumActionbarMessage = getChatMessageType.invoke(null, (byte) 2);
}
if (MAJOR_VER >= 12) {
Method getChatMessageType = getClass("{nms}.ChatMessageType").getMethod("a", byte.class);

} catch (Exception e) {
e.printStackTrace();
SETUP = false;
enumChatMessage = getChatMessageType.invoke(null, (byte) 1);
enumActionbarMessage = getChatMessageType.invoke(null, (byte) 2);
}

} catch (Exception e) {
e.printStackTrace();
SETUP = false;
}

}

public static void sendPacket(Object packet, Player... players) {
static void sendPacket(Object packet, Player... players) {
if (!SETUP) {
throw new IllegalStateException("ReflectionHelper is not set up!");
}
Expand All @@ -585,19 +579,22 @@ public static void sendPacket(Object packet, Player... players) {

private static void setType(Object object, byte type) {
if (MAJOR_VER >= 12) {
if (type == 1) {
set("b", object, enumChatMessage);
} else if (type == 2) {
set("b", object, enumActionbarMessage);
} else {
throw new IllegalArgumentException("type must be 1 or 2");
switch (type) {
case 1:
set("b", object, enumChatMessage);
break;
case 2:
set("b", object, enumActionbarMessage);
break;
default:
throw new IllegalArgumentException("type must be 1 or 2");
}
} else {
set("b", object, type);
}
}

public static Object createActionbarPacket(String message) {
static Object createActionbarPacket(String message) {
if (!SETUP) {
throw new IllegalStateException("ReflectionHelper is not set up!");
}
Expand All @@ -606,7 +603,7 @@ public static Object createActionbarPacket(String message) {
return packet;
}

public static Object createTextPacket(String message) {
static Object createTextPacket(String message) {
if (!SETUP) {
throw new IllegalStateException("ReflectionHelper is not set up!");
}
Expand All @@ -622,7 +619,7 @@ public static Object createTextPacket(String message) {

}

public static Object createTitlePacket(String message) {
static Object createTitlePacket(String message) {
if (!SETUP) {
throw new IllegalStateException("ReflectionHelper is not set up!");
}
Expand All @@ -635,7 +632,7 @@ public static Object createTitlePacket(String message) {

}

public static Object createSubtitlePacket(String message) {
static Object createSubtitlePacket(String message) {
if (!SETUP) {
throw new IllegalStateException("ReflectionHelper is not set up!");
}
Expand All @@ -648,7 +645,7 @@ public static Object createSubtitlePacket(String message) {

}

public static Object createTitleTimesPacket(int fadeIn, int stay, int fadeOut) {
static Object createTitleTimesPacket(int fadeIn, int stay, int fadeOut) {
if (!SETUP) {
throw new IllegalStateException("ReflectionHelper is not set up!");
}
Expand All @@ -667,7 +664,7 @@ public static Object createTitleTimesPacket(int fadeIn, int stay, int fadeOut) {
* @param message The text to convert to a chat component
* @return The chat component
*/
public static Object componentText(String message) {
static Object componentText(String message) {
if (!SETUP) {
throw new IllegalStateException("ReflectionHelper is not set up!");
}
Expand All @@ -686,7 +683,7 @@ public static Object componentText(String message) {
* @param json The JSON to attempt to parse
* @return The object representing the text in JSON form, or <code>null</code> if something went wrong converting the String to JSON data
*/
public static Object fromJson(String json) {
static Object fromJson(String json) {
if (!SETUP) {
throw new IllegalStateException("ReflectionHelper is not set up!");
}
Expand Down Expand Up @@ -717,7 +714,7 @@ public static Object fromJson(String json) {
* @return The class
* @throws ClassNotFoundException If the class was not found
*/
public static Class<?> getClass(String path) throws ClassNotFoundException {
static Class<?> getClass(String path) throws ClassNotFoundException {
if (!SETUP) {
throw new IllegalStateException("ReflectionHelper is not set up!");
}
Expand All @@ -731,7 +728,7 @@ public static Class<?> getClass(String path) throws ClassNotFoundException {
* @param obj The object to change the field of
* @param value The new value to set
*/
public static void set(String field, Object obj, Object value) {
static void set(String field, Object obj, Object value) {
try {
Field f = obj.getClass().getDeclaredField(field);
f.setAccessible(true);
Expand All @@ -741,7 +738,7 @@ public static void set(String field, Object obj, Object value) {
}
}

public static int getVersion() {
static int getVersion() {
if (!SETUP) {
throw new IllegalStateException("ReflectionHelper is not set up!");
}
Expand All @@ -766,9 +763,9 @@ public static int getVersion() {
*/
public class MessagePart {

private final List<ChatColor> styles = new ArrayList<>();
private MessageEvent onClick;
private MessageEvent onHover;
private List<ChatColor> styles = new ArrayList<>();
private ChatColor color;
private String text;

Expand Down

0 comments on commit 288b60e

Please sign in to comment.