mirror of
https://github.com/songoda/UltimateKits.git
synced 2024-11-22 18:26:12 +01:00
Improves multi-version compatibility for ItemSerializer
(MC 1.18?)
This commit is contained in:
parent
a5db1b3b79
commit
5dbcf7099b
@ -1,7 +1,6 @@
|
||||
package com.songoda.ultimatekits.conversion.hooks;
|
||||
|
||||
import com.songoda.core.compatibility.ServerVersion;
|
||||
import com.songoda.ultimatekits.UltimateKits;
|
||||
import com.songoda.ultimatekits.conversion.Hook;
|
||||
import com.songoda.ultimatekits.utils.ItemSerializer;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
@ -10,7 +9,6 @@ import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class DefaultHook implements Hook {
|
||||
|
||||
public Set<ItemStack> getItems(String kitName) {
|
||||
Set<ItemStack> items = new HashSet<>();
|
||||
|
||||
|
@ -1,22 +1,17 @@
|
||||
package com.songoda.ultimatekits.utils;
|
||||
|
||||
import com.songoda.core.compatibility.ClassMapping;
|
||||
import com.songoda.core.compatibility.MethodMapping;
|
||||
import com.songoda.core.compatibility.ServerVersion;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class ItemSerializer {
|
||||
|
||||
// classes needed for reflections
|
||||
|
||||
private static Class<?> classMojangsonParser;
|
||||
private static Class<?> classItemStack;
|
||||
private static Class<?> classCraftItemStack;
|
||||
private static Class<?> classNBTTagCompound;
|
||||
private static Class<?> classBukkitItemStack;
|
||||
|
||||
private static Constructor<?> constructorItemStack;
|
||||
|
||||
@ -28,82 +23,63 @@ public class ItemSerializer {
|
||||
private static Method methodTobItemStack;
|
||||
private static Method methodTocItemStack;
|
||||
private static Method methodSaveTagToStack;
|
||||
private static Method methodToString;
|
||||
|
||||
/**
|
||||
* Initializes all reflection methods
|
||||
*
|
||||
* @throws NoSuchMethodException
|
||||
* @throws SecurityException
|
||||
* @throws ClassNotFoundException
|
||||
*/
|
||||
static {
|
||||
static {
|
||||
try {
|
||||
classMojangsonParser = Class.forName(ServerVersion.isServerVersionAtLeast(ServerVersion.V1_17)
|
||||
? "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"));
|
||||
Class<?> classItemStack = ClassMapping.ITEM_STACK.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);
|
||||
else if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_11))
|
||||
} else if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_11)) {
|
||||
constructorItemStack = classItemStack.getConstructor(classNBTTagCompound);
|
||||
else
|
||||
} else {
|
||||
methodCreateStack = classItemStack.getMethod("createStack", classNBTTagCompound);
|
||||
methodTobItemStack = classCraftItemStack.getMethod("asBukkitCopy", classItemStack);
|
||||
}
|
||||
methodTobItemStack = ClassMapping.CRAFT_ITEM_STACK.getClazz().getMethod("asBukkitCopy", classItemStack);
|
||||
|
||||
methodTocItemStack = classCraftItemStack.getDeclaredMethod("asNMSCopy", classBukkitItemStack);
|
||||
methodSaveTagToStack = classItemStack.getMethod("save", classNBTTagCompound);
|
||||
methodToString = classNBTTagCompound.getMethod("toString");
|
||||
} catch (NoSuchMethodException | ClassNotFoundException e) {
|
||||
e.getStackTrace();
|
||||
methodTocItemStack = MethodMapping.CB_ITEM_STACK__AS_NMS_COPY.getMethod(ClassMapping.CRAFT_ITEM_STACK.getClazz());
|
||||
methodSaveTagToStack = MethodMapping.ITEM_STACK__SAVE.getMethod(ClassMapping.ITEM_STACK.getClazz());
|
||||
} catch (NoSuchMethodException ex) {
|
||||
ex.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
|
||||
*
|
||||
* @param jsonString the JSON String to parse
|
||||
*
|
||||
* @return the deserialized ItemStack
|
||||
*/
|
||||
public static ItemStack deserializeItemStackFromJson(String jsonString) {
|
||||
try {
|
||||
Object nbtTagCompound = methodParseString.invoke(null, jsonString);
|
||||
Object citemStack;
|
||||
Object cItemStack;
|
||||
|
||||
if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13))
|
||||
citemStack = methodToItemStack.invoke(null, nbtTagCompound);
|
||||
else if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_11))
|
||||
citemStack = constructorItemStack.newInstance(nbtTagCompound);
|
||||
else
|
||||
citemStack = methodCreateStack.invoke(null, nbtTagCompound);
|
||||
if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13)) {
|
||||
cItemStack = methodToItemStack.invoke(null, nbtTagCompound);
|
||||
} else if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_11)) {
|
||||
cItemStack = constructorItemStack.newInstance(nbtTagCompound);
|
||||
} else {
|
||||
cItemStack = methodCreateStack.invoke(null, nbtTagCompound);
|
||||
}
|
||||
|
||||
return (ItemStack) methodTobItemStack.invoke(null, citemStack);
|
||||
return (ItemStack) methodTobItemStack.invoke(null, cItemStack);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializes an item stack
|
||||
*
|
||||
* @param itemStack the ItemStack to parse
|
||||
*
|
||||
* @return condensed JSON String
|
||||
*/
|
||||
public static String serializeItemStackToJson(ItemStack itemStack) {
|
||||
@ -113,10 +89,11 @@ public class ItemSerializer {
|
||||
|
||||
methodSaveTagToStack.invoke(citemStack, nbtTagCompoundObject);
|
||||
|
||||
return (String) methodToString.invoke(nbtTagCompoundObject);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
return nbtTagCompoundObject.toString();
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user