Improves multi-version compatibility for ItemSerializer (MC 1.18?)

This commit is contained in:
Christian Koop 2021-12-20 18:16:25 +01:00
parent a5db1b3b79
commit 5dbcf7099b
No known key found for this signature in database
GPG Key ID: 89A8181384E010A3
2 changed files with 33 additions and 58 deletions

View File

@ -1,7 +1,6 @@
package com.songoda.ultimatekits.conversion.hooks; package com.songoda.ultimatekits.conversion.hooks;
import com.songoda.core.compatibility.ServerVersion; import com.songoda.core.compatibility.ServerVersion;
import com.songoda.ultimatekits.UltimateKits;
import com.songoda.ultimatekits.conversion.Hook; import com.songoda.ultimatekits.conversion.Hook;
import com.songoda.ultimatekits.utils.ItemSerializer; import com.songoda.ultimatekits.utils.ItemSerializer;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -10,7 +9,6 @@ import java.util.HashSet;
import java.util.Set; import java.util.Set;
public class DefaultHook implements Hook { public class DefaultHook implements Hook {
public Set<ItemStack> getItems(String kitName) { public Set<ItemStack> getItems(String kitName) {
Set<ItemStack> items = new HashSet<>(); Set<ItemStack> items = new HashSet<>();

View File

@ -1,22 +1,17 @@
package com.songoda.ultimatekits.utils; package com.songoda.ultimatekits.utils;
import com.songoda.core.compatibility.ClassMapping; import com.songoda.core.compatibility.ClassMapping;
import com.songoda.core.compatibility.MethodMapping;
import com.songoda.core.compatibility.ServerVersion; import com.songoda.core.compatibility.ServerVersion;
import org.bukkit.Bukkit;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Method; import java.lang.reflect.Method;
public class ItemSerializer { public class ItemSerializer {
// classes needed for reflections // classes needed for reflections
private static Class<?> classMojangsonParser;
private static Class<?> classItemStack;
private static Class<?> classCraftItemStack;
private static Class<?> classNBTTagCompound; private static Class<?> classNBTTagCompound;
private static Class<?> classBukkitItemStack;
private static Constructor<?> constructorItemStack; private static Constructor<?> constructorItemStack;
@ -28,82 +23,63 @@ public class ItemSerializer {
private static Method methodTobItemStack; private static Method methodTobItemStack;
private static Method methodTocItemStack; private static Method methodTocItemStack;
private static Method methodSaveTagToStack; private static Method methodSaveTagToStack;
private static Method methodToString;
/** static {
* Initializes all reflection methods
*
* @throws NoSuchMethodException
* @throws SecurityException
* @throws ClassNotFoundException
*/
static {
try { try {
classMojangsonParser = Class.forName(ServerVersion.isServerVersionAtLeast(ServerVersion.V1_17) Class<?> classItemStack = ClassMapping.ITEM_STACK.getClazz();
? "net.minecraft.nbt.MojangsonParser" : formatNMS("net.minecraft.server.NMS.MojangsonParser"));
classItemStack = ClassMapping.ITEM_STACK.getClazz();
classCraftItemStack = Class.forName(formatNMS("org.bukkit.craftbukkit.NMS.inventory.CraftItemStack"));
classNBTTagCompound = ClassMapping.NBT_TAG_COMPOUND.getClazz(); classNBTTagCompound = ClassMapping.NBT_TAG_COMPOUND.getClazz();
classBukkitItemStack = Class.forName("org.bukkit.inventory.ItemStack");
methodParseString = classMojangsonParser.getMethod("parse", String.class);
if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13)) methodParseString = MethodMapping.MOJANGSON_PARSER__PARSE.getMethod(ClassMapping.MOJANGSON_PARSER.getClazz());
if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13)) {
methodToItemStack = classItemStack.getMethod("a", classNBTTagCompound); methodToItemStack = classItemStack.getMethod("a", classNBTTagCompound);
else if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_11)) } else if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_11)) {
constructorItemStack = classItemStack.getConstructor(classNBTTagCompound); constructorItemStack = classItemStack.getConstructor(classNBTTagCompound);
else } else {
methodCreateStack = classItemStack.getMethod("createStack", classNBTTagCompound); methodCreateStack = classItemStack.getMethod("createStack", classNBTTagCompound);
methodTobItemStack = classCraftItemStack.getMethod("asBukkitCopy", classItemStack); }
methodTobItemStack = ClassMapping.CRAFT_ITEM_STACK.getClazz().getMethod("asBukkitCopy", classItemStack);
methodTocItemStack = classCraftItemStack.getDeclaredMethod("asNMSCopy", classBukkitItemStack); methodTocItemStack = MethodMapping.CB_ITEM_STACK__AS_NMS_COPY.getMethod(ClassMapping.CRAFT_ITEM_STACK.getClazz());
methodSaveTagToStack = classItemStack.getMethod("save", classNBTTagCompound); methodSaveTagToStack = MethodMapping.ITEM_STACK__SAVE.getMethod(ClassMapping.ITEM_STACK.getClazz());
methodToString = classNBTTagCompound.getMethod("toString"); } catch (NoSuchMethodException ex) {
} catch (NoSuchMethodException | ClassNotFoundException e) { ex.getStackTrace();
e.getStackTrace();
} }
} }
/**
* Inserts the version declaration for any string containing NMS
*
* @param s the string to format, must contain NMS.
* @return formatted string
*/
private static String formatNMS(String s) {
String packageName = Bukkit.getServer().getClass().getPackage().getName();
String nmsVersion = packageName.substring(packageName.lastIndexOf('.') + 1);
return s.replace("NMS", nmsVersion);
}
/** /**
* Deserializes a JSON String * Deserializes a JSON String
* *
* @param jsonString the JSON String to parse * @param jsonString the JSON String to parse
*
* @return the deserialized ItemStack * @return the deserialized ItemStack
*/ */
public static ItemStack deserializeItemStackFromJson(String jsonString) { public static ItemStack deserializeItemStackFromJson(String jsonString) {
try { try {
Object nbtTagCompound = methodParseString.invoke(null, jsonString); Object nbtTagCompound = methodParseString.invoke(null, jsonString);
Object citemStack; Object cItemStack;
if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13)) if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13)) {
citemStack = methodToItemStack.invoke(null, nbtTagCompound); cItemStack = methodToItemStack.invoke(null, nbtTagCompound);
else if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_11)) } else if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_11)) {
citemStack = constructorItemStack.newInstance(nbtTagCompound); cItemStack = constructorItemStack.newInstance(nbtTagCompound);
else } else {
citemStack = methodCreateStack.invoke(null, nbtTagCompound); cItemStack = methodCreateStack.invoke(null, nbtTagCompound);
}
return (ItemStack) methodTobItemStack.invoke(null, citemStack); return (ItemStack) methodTobItemStack.invoke(null, cItemStack);
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
return null;
} }
return null;
} }
/** /**
* Serializes an item stack * Serializes an item stack
* *
* @param itemStack the ItemStack to parse * @param itemStack the ItemStack to parse
*
* @return condensed JSON String * @return condensed JSON String
*/ */
public static String serializeItemStackToJson(ItemStack itemStack) { public static String serializeItemStackToJson(ItemStack itemStack) {
@ -113,10 +89,11 @@ public class ItemSerializer {
methodSaveTagToStack.invoke(citemStack, nbtTagCompoundObject); methodSaveTagToStack.invoke(citemStack, nbtTagCompoundObject);
return (String) methodToString.invoke(nbtTagCompoundObject); return nbtTagCompoundObject.toString();
} catch (Exception e) { } catch (Exception ex) {
e.printStackTrace(); ex.printStackTrace();
return null;
} }
return null;
} }
} }