add nms reflection util, add release info to serverversion

This commit is contained in:
jascotty2 2019-08-27 08:28:57 -05:00
parent 98ce075114
commit c784f7b252
2 changed files with 106 additions and 12 deletions

View File

@ -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();
}
}
}

View File

@ -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();
}
}
}