From c784f7b25223f13a5e955e94f4c6725a2a79af0f Mon Sep 17 00:00:00 2001 From: jascotty2 Date: Tue, 27 Aug 2019 08:28:57 -0500 Subject: [PATCH] add nms reflection util, add release info to serverversion --- .../core/compatibility/ServerVersion.java | 33 ++++--- .../java/com/songoda/core/utils/NMSUtil.java | 85 +++++++++++++++++++ 2 files changed, 106 insertions(+), 12 deletions(-) create mode 100644 src/main/java/com/songoda/core/utils/NMSUtil.java diff --git a/src/main/java/com/songoda/core/compatibility/ServerVersion.java b/src/main/java/com/songoda/core/compatibility/ServerVersion.java index c68ef722..d98bef4b 100644 --- a/src/main/java/com/songoda/core/compatibility/ServerVersion.java +++ b/src/main/java/com/songoda/core/compatibility/ServerVersion.java @@ -10,23 +10,32 @@ public enum ServerVersion { private final static String serverPackagePath = Bukkit.getServer().getClass().getPackage().getName(); private final static String serverPackageVersion = serverPackagePath.substring(serverPackagePath.lastIndexOf('.') + 1).toUpperCase(); private static ServerVersion serverVersion = UNKNOWN; + private static String serverReleaseVersion; + static { - for (ServerVersion version : values()) - if (serverPackageVersion.startsWith(version.name())) + for (ServerVersion version : values()) { + if (serverPackageVersion.startsWith(version.name())) { serverVersion = version; + serverReleaseVersion = serverPackageVersion.substring(version.name().length() + 2); + } + } } - public boolean isLessThan(ServerVersion other) { - return this.ordinal() < other.ordinal(); - } + public boolean isLessThan(ServerVersion other) { + return this.ordinal() < other.ordinal(); + } - public boolean isGreaterThan(ServerVersion other) { - return this.ordinal() > other.ordinal(); - } + public boolean isGreaterThan(ServerVersion other) { + return this.ordinal() > other.ordinal(); + } - public static String getServerVersionString() { - return serverPackageVersion; - } + public static String getServerVersionString() { + return serverPackageVersion; + } + + public static String getVersionReleaseNumber() { + return serverReleaseVersion; + } public static ServerVersion getServerVersion() { return serverVersion; @@ -47,4 +56,4 @@ public enum ServerVersion { public static boolean isServerVersionBelow(ServerVersion version) { return serverVersion.ordinal() < version.ordinal(); } -} \ No newline at end of file +} diff --git a/src/main/java/com/songoda/core/utils/NMSUtil.java b/src/main/java/com/songoda/core/utils/NMSUtil.java new file mode 100644 index 00000000..7075848b --- /dev/null +++ b/src/main/java/com/songoda/core/utils/NMSUtil.java @@ -0,0 +1,85 @@ +package com.songoda.core.utils; + +import com.songoda.core.compatibility.ServerVersion; +import org.bukkit.entity.Player; + +import java.lang.reflect.Field; + +public class NMSUtil { + + public static Class getNMSClass(String className) { + try { + String fullName = "net.minecraft.server." + ServerVersion.getServerVersionString() + "." + className; + Class clazz = Class.forName(fullName); + return clazz; + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + public static Class getCraftClass(String className) { + try { + String fullName = "org.bukkit.craftbukkit." + ServerVersion.getServerVersionString() + "." + className; + Class clazz = Class.forName(fullName); + return clazz; + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + public static Field getField(Class clazz, String name, boolean declared) { + try { + Field field; + + if (declared) { + field = clazz.getDeclaredField(name); + } else { + field = clazz.getField(name); + } + + field.setAccessible(true); + return field; + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + public static Object getFieldObject(Object object, Field field) { + try { + return field.get(object); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + public static void setField(Object object, String fieldName, Object fieldValue, boolean declared) { + try { + Field field; + + if (declared) { + field = object.getClass().getDeclaredField(fieldName); + } else { + field = object.getClass().getField(fieldName); + } + + field.setAccessible(true); + field.set(object, fieldValue); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void sendPacket(Player player, Object packet) { + try { + Object handle = player.getClass().getMethod("getHandle").invoke(player); + Object playerConnection = handle.getClass().getField("playerConnection").get(handle); + playerConnection.getClass().getMethod("sendPacket", getNMSClass("Packet")).invoke(playerConnection, packet); + } catch (Exception e) { + e.printStackTrace(); + } + } +}