Skip to content
This repository has been archived by the owner on Feb 15, 2022. It is now read-only.

Commit

Permalink
Fix Bukkit to be compatible with 1.13+ properly since they changed th…
Browse files Browse the repository at this point in the history
…e PluginMessage system and also change output jar name.
  • Loading branch information
mastergberry committed May 20, 2019
1 parent 5e6ba49 commit 140ed1b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
16 changes: 15 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>net.badlion</groupId>
<artifactId>timer-api</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>

<packaging>jar</packaging>

Expand All @@ -27,6 +27,20 @@
</dependencies>

<build>
<defaultGoal>clean install</defaultGoal>
<finalName>badlionclienttimerapi</finalName>
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
<resource>
<targetPath>.</targetPath>
<filtering>true</filtering>
<directory>src/main/resources/</directory>
<includes>
<include>*.yml</include>
</includes>
</resource>
</resources>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
33 changes: 29 additions & 4 deletions src/main/java/net/badlion/timers/impl/NmsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public class NmsManager {
private static Class<?> packetDataSerializerClass;
private static Constructor<?> packetDataSerializerConstructor;

// Bukkit 1.13+ support
private static Class<?> minecraftKeyClass;
private static Constructor<?> minecraftKeyConstructor;

private static Method wrappedBufferMethod;

public static void init(TimerPlugin plugin) {
Expand Down Expand Up @@ -100,7 +104,22 @@ public static void init(TimerPlugin plugin) {
// If we made it this far in theory we are on at least 1.8
NmsManager.packetPlayOutCustomPayloadConstructor = NmsManager.getConstructor(packetPlayOutCustomPayloadClass, String.class, NmsManager.packetDataSerializerClass);
if (NmsManager.packetPlayOutCustomPayloadConstructor == null) {
throw new RuntimeException("Failed to find PacketPlayOutCustomPayload constructor 2x");
// Ok we are in 1.13 or higher now...
NmsManager.minecraftKeyClass = NmsManager.getClass("net.minecraft.server." + NmsManager.versionSuffix + ".MinecraftKey");
if (NmsManager.minecraftKeyClass == null) {
throw new RuntimeException("Failed to find PacketPlayOutCustomPayload constructor or MinecraftKey class");
}

NmsManager.minecraftKeyConstructor = NmsManager.getConstructor(NmsManager.minecraftKeyClass, String.class, String.class);
if (NmsManager.minecraftKeyConstructor == null) {
throw new RuntimeException("Failed to find PacketPlayOutCustomPayload constructor or MinecraftKey constructor");
}

// If we still can't find this...unknown version
NmsManager.packetPlayOutCustomPayloadConstructor = NmsManager.getConstructor(packetPlayOutCustomPayloadClass, NmsManager.minecraftKeyClass, NmsManager.packetDataSerializerClass);
if (NmsManager.packetPlayOutCustomPayloadConstructor == null) {
throw new RuntimeException("Failed to find PacketPlayOutCustomPayload constructor");
}
}
}

Expand All @@ -124,13 +143,19 @@ public static void sendPluginMessage(Player player, String channel, byte[] messa
try {
Object packet;

// Newer MC version, setup ByteBuf object
if (NmsManager.packetDataSerializerClass != null) {
// 1.13+
if (NmsManager.minecraftKeyClass != null) {
Object minecraftKey = NmsManager.minecraftKeyConstructor.newInstance("badlion", "timers");
Object byteBuf = NmsManager.wrappedBufferMethod.invoke(null, (Object) message);
Object packetDataSerializer = NmsManager.packetDataSerializerConstructor.newInstance(byteBuf);

packet = NmsManager.packetPlayOutCustomPayloadConstructor.newInstance(minecraftKey, packetDataSerializer);
} else if (NmsManager.packetDataSerializerClass != null) { // 1.8+
Object byteBuf = NmsManager.wrappedBufferMethod.invoke(null, (Object) message);
Object packetDataSerializer = NmsManager.packetDataSerializerConstructor.newInstance(byteBuf);

packet = NmsManager.packetPlayOutCustomPayloadConstructor.newInstance(channel, packetDataSerializer);
} else {
} else { // 1.7
// Work our magic to make the packet
packet = NmsManager.packetPlayOutCustomPayloadConstructor.newInstance(channel, message);
}
Expand Down

0 comments on commit 140ed1b

Please sign in to comment.