mirror of
https://github.com/songoda/UltimateKits.git
synced 2024-11-26 12:15:13 +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;
|
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<>();
|
||||||
|
|
||||||
|
@ -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;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initializes all reflection methods
|
|
||||||
*
|
|
||||||
* @throws NoSuchMethodException
|
|
||||||
* @throws SecurityException
|
|
||||||
* @throws ClassNotFoundException
|
|
||||||
*/
|
|
||||||
static {
|
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);
|
|
||||||
|
|
||||||
methodTocItemStack = classCraftItemStack.getDeclaredMethod("asNMSCopy", classBukkitItemStack);
|
|
||||||
methodSaveTagToStack = classItemStack.getMethod("save", classNBTTagCompound);
|
|
||||||
methodToString = classNBTTagCompound.getMethod("toString");
|
|
||||||
} catch (NoSuchMethodException | ClassNotFoundException e) {
|
|
||||||
e.getStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
methodTobItemStack = ClassMapping.CRAFT_ITEM_STACK.getClazz().getMethod("asBukkitCopy", classItemStack);
|
||||||
|
|
||||||
/**
|
methodTocItemStack = MethodMapping.CB_ITEM_STACK__AS_NMS_COPY.getMethod(ClassMapping.CRAFT_ITEM_STACK.getClazz());
|
||||||
* Inserts the version declaration for any string containing NMS
|
methodSaveTagToStack = MethodMapping.ITEM_STACK__SAVE.getMethod(ClassMapping.ITEM_STACK.getClazz());
|
||||||
*
|
} catch (NoSuchMethodException ex) {
|
||||||
* @param s the string to format, must contain NMS.
|
ex.getStackTrace();
|
||||||
* @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;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user