ReflectionRegistry

Added a reflection registry
Added some methods in ReflectionUtils
This commit is contained in:
NichtStudioCode 2021-01-27 18:21:14 +01:00
parent 6521e655a4
commit 793635a9dd
4 changed files with 108 additions and 22 deletions

@ -5,7 +5,8 @@ import com.mojang.authlib.properties.Property;
import com.mojang.authlib.properties.PropertyMap; import com.mojang.authlib.properties.PropertyMap;
import de.studiocode.invgui.util.MojangApiUtils; import de.studiocode.invgui.util.MojangApiUtils;
import de.studiocode.invgui.util.Pair; import de.studiocode.invgui.util.Pair;
import de.studiocode.invgui.util.ReflectionUtils; import de.studiocode.invgui.util.reflection.ReflectionRegistry;
import de.studiocode.invgui.util.reflection.ReflectionUtils;
import de.studiocode.invgui.window.impl.BaseWindow; import de.studiocode.invgui.window.impl.BaseWindow;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
@ -54,7 +55,7 @@ public class ItemBuilder implements Cloneable {
if (itemMeta instanceof Damageable) ((Damageable) itemMeta).setDamage(damage); if (itemMeta instanceof Damageable) ((Damageable) itemMeta).setDamage(damage);
if (customModelData != -1) itemMeta.setCustomModelData(customModelData); if (customModelData != -1) itemMeta.setCustomModelData(customModelData);
if (displayName != null) itemMeta.setDisplayName(displayName); if (displayName != null) itemMeta.setDisplayName(displayName);
if (gameProfile != null) ReflectionUtils.setValueOfDeclaredField(itemMeta, "profile", gameProfile); if (gameProfile != null) ReflectionUtils.setFieldValue(ReflectionRegistry.CB_CRAFT_META_SKULL_PROFILE_FIELD, itemMeta, gameProfile);
enchantments.forEach((enchantment, pair) -> itemMeta.addEnchant(enchantment, pair.getFirst(), pair.getSecond())); enchantments.forEach((enchantment, pair) -> itemMeta.addEnchant(enchantment, pair.getFirst(), pair.getSecond()));
itemMeta.addItemFlags(itemFlags.toArray(new ItemFlag[0])); itemMeta.addItemFlags(itemFlags.toArray(new ItemFlag[0]));
itemMeta.setLore(lore); itemMeta.setLore(lore);

@ -1,20 +0,0 @@
package de.studiocode.invgui.util;
import java.lang.reflect.Field;
public class ReflectionUtils {
public static void setValueOfDeclaredField(Object obj, String fieldName, Object value) {
try {
Class<?> clazz = obj.getClass();
Field field = clazz.getDeclaredField(fieldName);
boolean accessible = field.isAccessible();
field.setAccessible(true);
field.set(obj, value);
field.setAccessible(accessible);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
}

@ -0,0 +1,19 @@
package de.studiocode.invgui.util.reflection;
import java.lang.reflect.Field;
import static de.studiocode.invgui.util.reflection.ReflectionUtils.getCBClass;
import static de.studiocode.invgui.util.reflection.ReflectionUtils.getField;
public class ReflectionRegistry {
public static final String NET_MINECRAFT_SERVER_PACKAGE_PATH = ReflectionUtils.getNMS();
public static final String CRAFT_BUKKIT_PACKAGE_PATH = ReflectionUtils.getCB();
// Classes
public static final Class<?> CB_CRAFT_META_SKULL_CLASS = getCBClass("inventory.CraftMetaSkull");
// Fields
public static final Field CB_CRAFT_META_SKULL_PROFILE_FIELD = getField(CB_CRAFT_META_SKULL_CLASS, true, "profile");
}

@ -0,0 +1,86 @@
package de.studiocode.invgui.util.reflection;
import org.bukkit.Bukkit;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import static de.studiocode.invgui.util.reflection.ReflectionRegistry.CRAFT_BUKKIT_PACKAGE_PATH;
import static de.studiocode.invgui.util.reflection.ReflectionRegistry.NET_MINECRAFT_SERVER_PACKAGE_PATH;
public class ReflectionUtils {
protected static String getCB() {
String path = Bukkit.getServer().getClass().getPackage().getName();
String version = path.substring(path.lastIndexOf(".") + 1);
return "org.bukkit.craftbukkit." + version + ".";
}
protected static String getNMS() {
String path = Bukkit.getServer().getClass().getPackage().getName();
String version = path.substring(path.lastIndexOf(".") + 1);
return "net.minecraft.server." + version + ".";
}
public static Class<?> getNMSClass(String path) {
try {
return Class.forName(NET_MINECRAFT_SERVER_PACKAGE_PATH + path);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
public static Class<?> getCBClass(String path) {
try {
return Class.forName(CRAFT_BUKKIT_PACKAGE_PATH + path);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
public static Field getField(Class<?> clazz, boolean declared, String name) {
try {
Field field = declared ? clazz.getDeclaredField(name) : clazz.getField(name);
if (declared) field.setAccessible(true);
return field;
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
return null;
}
public static Constructor<?> getConstructor(Class<?> clazz, boolean declared, Class<?> parameterTypes) {
try {
return declared ? clazz.getDeclaredConstructor(parameterTypes) : clazz.getConstructor(parameterTypes);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return null;
}
public static Method getMethod(Class<?> clazz, boolean declared, String name, Class<?>... parameterTypes) {
try {
return declared ? clazz.getDeclaredMethod(name, parameterTypes) : clazz.getMethod(name, parameterTypes);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return null;
}
public static void setFieldValue(Field field, Object obj, Object value) {
try {
field.set(obj, value);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}