Removed old reflection code
This commit is contained in:
parent
84ec5838d4
commit
4b41a6198d
@ -1,95 +0,0 @@
|
||||
/*
|
||||
package hu.ditservices.utils.reflection;
|
||||
|
||||
import hu.ditservices.utils.Version;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public final class ClazzContainer {
|
||||
private static Class<?> packet, ChatComponentTextClass, CraftPlayerClass, IChatBaseComponentClass, PacketPlayOutPlayerListHeaderFooterClass;
|
||||
private static Object ChatComponentText, IChatBaseComponent;
|
||||
|
||||
static {
|
||||
try {
|
||||
IChatBaseComponentClass = classByName("net.minecraft.network.chat", "IChatBaseComponent");
|
||||
packet = classByName("net.minecraft.network.protocol", "Packet");
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
public ClazzContainer(){
|
||||
|
||||
}
|
||||
|
||||
public static Class<?> classByName(String newPackageName, String name) throws ClassNotFoundException {
|
||||
if (Version.ServerVersion.isCurrentLower(Version.ServerVersion.v1_17_R1) || newPackageName == null) {
|
||||
newPackageName = "net.minecraft.server." + Version.ServerVersion.getArrayVersion()[3];
|
||||
}
|
||||
|
||||
return Class.forName(newPackageName + "." + name);
|
||||
}
|
||||
|
||||
public static Object buildChatComponentText(String text) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
|
||||
Constructor<?> constructor;
|
||||
if (Version.ServerVersion.isCurrentEqualOrHigher(Version.ServerVersion.v1_17_R1)){
|
||||
if (ChatComponentTextClass == null){
|
||||
ChatComponentTextClass = Reflection.getClass("net.minecraft.network.chat.ChatComponentText");
|
||||
}
|
||||
|
||||
constructor = ChatComponentTextClass.getConstructor(String.class);
|
||||
return constructor.newInstance(text);
|
||||
}else{
|
||||
if (ChatComponentTextClass == null){
|
||||
ChatComponentTextClass = Reflection.getClass("net.minecraft.server.%v.ChatComponentText");
|
||||
}
|
||||
constructor = Reflection.getClass("net.minecraft.server.%v.ChatComponentText").getConstructor(String.class);
|
||||
return constructor.newInstance(text);
|
||||
}
|
||||
}
|
||||
|
||||
public static Object buildPacketPlayOutPlayerListHeaderFooter(Object header, Object footer) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
|
||||
Constructor<?> constructor;
|
||||
Field headerField;
|
||||
Field footerField;
|
||||
Object packet;
|
||||
if (Version.ServerVersion.isCurrentEqualOrHigher(Version.ServerVersion.v1_17_R1)){
|
||||
if (PacketPlayOutPlayerListHeaderFooterClass == null){
|
||||
PacketPlayOutPlayerListHeaderFooterClass = Reflection.getClass("net.minecraft.network.protocol.game.PacketPlayOutPlayerListHeaderFooter");
|
||||
}
|
||||
}else{
|
||||
if (PacketPlayOutPlayerListHeaderFooterClass == null){
|
||||
PacketPlayOutPlayerListHeaderFooterClass = Reflection.getClass("net.minecraft.server.%v.PacketPlayOutPlayerListHeaderFooter");
|
||||
}
|
||||
}
|
||||
if (Version.ServerVersion.isCurrentEqualOrLower(Version.ServerVersion.v1_12_R1)){
|
||||
constructor = PacketPlayOutPlayerListHeaderFooterClass.getConstructor();
|
||||
packet = constructor.newInstance();
|
||||
try {
|
||||
headerField = packet.getClass().getDeclaredField("a");
|
||||
headerField.setAccessible(true);
|
||||
headerField.set(packet, buildChatComponentText((String) header));
|
||||
footerField = packet.getClass().getDeclaredField("b");
|
||||
footerField.setAccessible(true);
|
||||
footerField.set(packet, buildChatComponentText((String) footer));
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}else{
|
||||
constructor = PacketPlayOutPlayerListHeaderFooterClass.getConstructor(IChatBaseComponentClass,IChatBaseComponentClass);
|
||||
packet = constructor.newInstance(header,footer);
|
||||
}
|
||||
return packet;
|
||||
}
|
||||
|
||||
|
||||
public static Class<?> getIChatBaseComponent() {
|
||||
return IChatBaseComponentClass;
|
||||
}
|
||||
public static Class<?> getPacket() {
|
||||
return packet;
|
||||
}
|
||||
}
|
||||
*/
|
@ -1,126 +0,0 @@
|
||||
/*
|
||||
package hu.ditservices.utils.reflection;
|
||||
|
||||
import hu.ditservices.utils.Version;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class Reflection {
|
||||
|
||||
private static Method playerHandleMethod, sendPacketMethod, chatSerializerMethodA,chatSerializerMethodgetString;
|
||||
private static Field playerConnectionField;
|
||||
private static Class<?> chatSerializer;
|
||||
|
||||
private Reflection(){}
|
||||
|
||||
|
||||
public static Class<?> getClass(String name) {
|
||||
try {
|
||||
return Class.forName(name.replace("%v",Version.ServerVersion.getCurrent().toString()));
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Class<?> getCraftClass(String className) throws ClassNotFoundException {
|
||||
return Class.forName("org.bukkit.craftbukkit." + Version.ServerVersion.getArrayVersion()[3] + "." + className);
|
||||
}
|
||||
|
||||
public static Object getPlayerHandle(Player player) throws Exception {
|
||||
if (playerHandleMethod == null) {
|
||||
playerHandleMethod = player.getClass().getDeclaredMethod("getHandle");
|
||||
}
|
||||
|
||||
return playerHandleMethod.invoke(player);
|
||||
}
|
||||
|
||||
public static void sendPacket(Player player, Object packet) {
|
||||
if (player == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
Object playerHandle = getPlayerHandle(player);
|
||||
|
||||
if (playerConnectionField == null) {
|
||||
playerConnectionField = playerHandle.getClass().getDeclaredField(
|
||||
(Version.ServerVersion.isCurrentEqualOrHigher(Version.ServerVersion.v1_17_R1) ? "b" : "playerConnection"));
|
||||
}
|
||||
|
||||
Object playerConnection = playerConnectionField.get(playerHandle);
|
||||
|
||||
if (sendPacketMethod == null) {
|
||||
sendPacketMethod = playerConnection.getClass().getDeclaredMethod(
|
||||
Version.ServerVersion.isCurrentEqualOrHigher(Version.ServerVersion.v1_18_R1) ? "a" : "sendPacket",
|
||||
ClazzContainer.getPacket());
|
||||
}
|
||||
|
||||
sendPacketMethod.invoke(playerConnection, packet);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static String getChatSerializerString(Object serializer) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
||||
if (chatSerializerMethodgetString==null){
|
||||
if (Version.ServerVersion.isCurrentEqualOrLower(Version.ServerVersion.v1_12_R1)){
|
||||
chatSerializerMethodgetString = ClazzContainer.getIChatBaseComponent().getDeclaredMethod("getText");
|
||||
}else{
|
||||
chatSerializerMethodgetString = ClazzContainer.getIChatBaseComponent().getDeclaredMethod("getString");
|
||||
}
|
||||
}
|
||||
return (String) chatSerializerMethodgetString.invoke(serializer);
|
||||
//return (String) ClazzContainer.getIChatBaseComponent().cast(chatSerializerMethodgetString.invoke(serializer));
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static Object asChatSerializer(String json) throws Exception {
|
||||
try {
|
||||
if (Version.ServerVersion.isCurrentEqualOrHigher(Version.ServerVersion.v1_17_R1)){
|
||||
if (chatSerializer==null){
|
||||
chatSerializer = Class.forName("net.minecraft.network.chat.IChatBaseComponent$ChatSerializer");
|
||||
}
|
||||
|
||||
}else{
|
||||
if (chatSerializer==null){
|
||||
chatSerializer = Class.forName("net.minecraft.server." + Version.ServerVersion.getArrayVersion()[3] + ".IChatBaseComponent$ChatSerializer");
|
||||
}
|
||||
}
|
||||
if (chatSerializerMethodA == null) {
|
||||
chatSerializerMethodA = chatSerializer.getMethod("a", String.class);
|
||||
}
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return chatSerializerMethodA.invoke(chatSerializer,json);
|
||||
//return ClazzContainer.getIChatBaseComponent().cast(chatSerializerMethodA.invoke(chatSerializer, json));
|
||||
}
|
||||
|
||||
*/
|
||||
/*public static Class<?> getNMSClassRegex(String nmsClass) {
|
||||
|
||||
String version = null;
|
||||
Pattern pat = Pattern.compile("net\\.minecraft\\.(?:server)?\\.(v(?:\\d_)+R\\d)");
|
||||
for (Package p : Package.getPackages()) {
|
||||
String name = p.getName();
|
||||
Matcher m = pat.matcher(name);
|
||||
if (m.matches()) version = m.group(1);
|
||||
}
|
||||
|
||||
if (version == null) return null;
|
||||
|
||||
try {
|
||||
return Class.forName(String.format(nmsClass, version));
|
||||
} catch (ClassNotFoundException e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}*//*
|
||||
|
||||
}
|
||||
*/
|
Loading…
x
Reference in New Issue
Block a user