Skip to content

Commit

Permalink
Fix issue #9 (Internal representation change in 1.16.4) (#10)
Browse files Browse the repository at this point in the history
* Bump version

* Fix missing javadoc return

* Fix issue #9

* Fix compilation on 1.9+ java versions

* Handle null page as an empty page
  • Loading branch information
elgbar authored Jan 9, 2021
1 parent bd90e4b commit 56aaf8a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ To include the API in your project put this into our pom.xml (maven)
<dependency>
<groupId>xyz.upperlevel.spigot.book</groupId>
<artifactId>spigot-book-api</artifactId>
<version>1.3</version>
<version>1.4</version>
</dependency>
</dependencies>
```
Expand Down
11 changes: 5 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

<groupId>xyz.upperlevel.spigot.book</groupId>
<artifactId>spigot-book-api</artifactId>
<version>1.3</version>
<version>1.4</version>

<name>BookApi</name>
<description>A low-level API for book control on spigot</description>
<url>https://github.com/upperlevel/spigot-book-api</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<mc-version>1.12-pre6-SNAPSHOT</mc-version>
Expand Down Expand Up @@ -44,7 +45,7 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.16</version>
<version>1.18.16</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down Expand Up @@ -85,7 +86,6 @@
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<descriptor>src/assembly/dep.xml</descriptor>
</configuration>
Expand All @@ -99,7 +99,6 @@
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
Expand Down Expand Up @@ -127,7 +126,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.4</version>
<version>3.2.0</version>
<executions>
<execution>
<id>attach-javadocs</id>
Expand All @@ -140,7 +139,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<version>1.6</version>
<executions>
<execution>
<id>sign-artifacts</id>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/xyz/upperlevel/spigot/book/BookUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public static void openPlayer(Player p, ItemStack book) {

/**
* Creates a BookBuilder instance with a written book as the Itemstack's type
* @return
*
* @return A book builder with the material of {@link Material#WRITTEN_BOOK}
*/
public static BookBuilder writtenBook() {
return new BookBuilder(new ItemStack(Material.WRITTEN_BOOK));
Expand Down
53 changes: 40 additions & 13 deletions src/main/java/xyz/upperlevel/spigot/book/NmsBookHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.chat.ComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.block.banner.PatternType;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
Expand All @@ -22,13 +21,17 @@
* The NMS helper for all the Book-API
*/
public final class NmsBookHelper {

private static final String version;
private static final boolean doubleHands;

private static final Class<?> craftMetaBookClass;
private static final Field craftMetaBookField;
private static final Method chatSerializerA;

//nullable
private static final Method craftMetaBookInternalAddPageMethod;

private static final Method craftPlayerGetHandle;
// This method takes an enum that represents the player's hand only in versions
// >= 1.9
Expand All @@ -41,7 +44,7 @@ public final class NmsBookHelper {
/*
* private static final Field entityHumanPlayerConnection; private static final
* Method playerConnectionSendPacket;
*
*
* private static final Constructor<?> packetPlayOutCustomPayloadConstructor;
* private static final Constructor<?> packetDataSerializerConstructor;
*/
Expand All @@ -66,8 +69,22 @@ public final class NmsBookHelper {
doubleHands = major <= 1 && minor >= 9;
try {
craftMetaBookClass = getCraftClass("inventory.CraftMetaBook");

craftMetaBookField = craftMetaBookClass.getDeclaredField("pages");
craftMetaBookField.setAccessible(true);

Method cmbInternalAddMethod = null;
try {
//method is protected
cmbInternalAddMethod = craftMetaBookClass.getDeclaredMethod("internalAddPage", String.class);
cmbInternalAddMethod.setAccessible(true);
} catch (NoSuchMethodException e) {
//Internal data change in 1.16.4
//To detect if the server is using the new internal format we check if the internalAddPageMethod exists
//see https://hub.spigotmc.org/stash/projects/SPIGOT/repos/craftbukkit/commits/560b65c4f8a15619aaa4a1737c7040f21e725cce
}
craftMetaBookInternalAddPageMethod = cmbInternalAddMethod;

Class<?> chatSerializer = getNmsClass("IChatBaseComponent$ChatSerializer", false);
if (chatSerializer == null) {
chatSerializer = getNmsClass("ChatSerializer");
Expand Down Expand Up @@ -106,7 +123,7 @@ public final class NmsBookHelper {
* final Class<?> playerConnectionClass = getNmsClass("PlayerConnection");
* playerConnectionSendPacket = playerConnectionClass.getMethod("sendPacket",
* getNmsClass("Packet"));
*
*
* final Class<?> packetDataSerializerClasss =
* getNmsClass("PacketDataSerializer"); packetPlayOutCustomPayloadConstructor =
* getNmsClass("PacketPlayOutCustomPayload").getConstructor(String.class,
Expand All @@ -128,18 +145,28 @@ public final class NmsBookHelper {

/**
* Sets the pages of the book to the components json equivalent
*
*
* @param meta the book meta to change
* @param components the pages of the book
*/
@SuppressWarnings("unchecked") // reflections = unchecked warnings
public static void setPages(BookMeta meta, BaseComponent[][] components) {
try {
List<Object> pages = (List<Object>) craftMetaBookField.get(meta);
pages.clear();
if (pages != null) {
pages.clear();
}
for (BaseComponent[] c : components) {
final String json = ComponentSerializer.toString(c);
pages.add(chatSerializerA.invoke(null, json));
final String json;
if (craftMetaBookInternalAddPageMethod != null) {
json = c != null ? ComponentSerializer.toString(c) : "";
craftMetaBookInternalAddPageMethod.invoke(meta, json);
}
else {
BaseComponent[] nonNullC = c != null ? c : jsonToComponents("");
json = ComponentSerializer.toString(nonNullC);
pages.add(chatSerializerA.invoke(null, json));
}
}
} catch (Exception e) {
throw new UnsupportedVersionException(e);
Expand All @@ -149,7 +176,7 @@ public static void setPages(BookMeta meta, BaseComponent[][] components) {
/**
* Opens the book to a player (the player needs to have the book in one of his
* hands)
*
*
* @param player the player
* @param book the book to open
* @param offHand false if the book is in the right hand, true otherwise
Expand Down Expand Up @@ -184,7 +211,7 @@ public static void openBook(Player player, ItemStack book, boolean offHand) {

/**
* Translates an ItemStack to his Chat-Component equivalent
*
*
* @param item the item to be converted
* @return a Chat-Component equivalent of the parameter
*/
Expand All @@ -194,7 +221,7 @@ public static BaseComponent[] itemToComponents(ItemStack item) {

/**
* Translates a json string to his Chat-Component equivalent
*
*
* @param json the json string to be converted
* @return a Chat-Component equivalent of the parameter
*/
Expand All @@ -204,7 +231,7 @@ public static BaseComponent[] jsonToComponents(String json) {

/**
* Translates an ItemStack to his json equivalent
*
*
* @param item the item to be converted
* @return a json equivalent of the parameter
*/
Expand Down Expand Up @@ -243,7 +270,7 @@ public UnsupportedVersionException(Exception e) {

/**
* Gets the EntityPlayer handled by the argument
*
*
* @param player the Player handler
* @return the handled class
* @throws InvocationTargetException when some problems are found with the
Expand All @@ -257,7 +284,7 @@ public static Object toNms(Player player) throws InvocationTargetException, Ille

/**
* Creates a NMS copy of the parameter
*
*
* @param item the ItemStack to be nms-copied
* @return a NMS-ItemStack that is the equivalent of the one passed as argument
* @throws InvocationTargetException when some problems are found with the
Expand Down

0 comments on commit 56aaf8a

Please sign in to comment.