Guest User

Untitled

a guest
Apr 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. import com.esotericsoftware.kryo.Kryo;
  2. import com.esotericsoftware.kryo.Serializer;
  3. import com.esotericsoftware.kryo.io.Input;
  4. import com.esotericsoftware.kryo.io.Output;
  5.  
  6. import java.io.DataInputStream;
  7. import java.io.DataOutput;
  8. import java.io.DataOutputStream;
  9. import java.lang.reflect.Constructor;
  10. import java.lang.reflect.Method;
  11.  
  12. import static java.lang.Class.forName;
  13. import static java.lang.String.format;
  14. import static org.bukkit.Bukkit.getServer;
  15.  
  16. public class BukkitSerializers {
  17. private static final Class<?> CLASS_ITEM;
  18. private static final Constructor<?> CONSTRUCTOR_ITEM;
  19. private static final Method METHOD_ITEM_SAVE;
  20.  
  21. private static final Constructor<?> CONSTRUCTOR_NBT;
  22. private static final Method METHOD_NBT_SAVE;
  23. private static final Method METHOD_NBT_LOAD;
  24.  
  25. static {
  26. try {
  27. final String version = getServer().getClass().getName().split("\\.")[3];
  28. final String nms = "net.minecraft.server.%s.%s";
  29. final Class<?> nbt = forName(format(nms, version, "NBTTagCompound"));
  30.  
  31. CLASS_ITEM = forName(format(nms, version, "ItemStack"));
  32. CONSTRUCTOR_NBT = nbt.getConstructor();
  33. CONSTRUCTOR_NBT.setAccessible(true);
  34. CONSTRUCTOR_ITEM = CLASS_ITEM.getConstructor(nbt);
  35. CONSTRUCTOR_ITEM.setAccessible(true);
  36. METHOD_ITEM_SAVE = CLASS_ITEM.getDeclaredMethod("save", nbt);
  37. METHOD_ITEM_SAVE.setAccessible(true);
  38. Method save = null, load = null;
  39. for (Method method : forName(format(nms, version, "NBTCompressedStreamTools")).getDeclaredMethods()) {
  40. final Class<?>[] params = method.getParameterTypes();
  41. if (params.length == 1 && params[0] == DataInputStream.class)
  42. load = method;
  43. else if (params.length == 2 && params[1] == DataOutput.class)
  44. save = method;
  45. }
  46. if ((METHOD_NBT_LOAD = load) == null)
  47. throw new IllegalStateException("Could not find a load method in tools.");
  48. if ((METHOD_NBT_SAVE = save) == null)
  49. throw new IllegalStateException("Could not find a save method in tools.");
  50. METHOD_NBT_LOAD.setAccessible(true);
  51. METHOD_NBT_SAVE.setAccessible(true);
  52. } catch (Exception e) {
  53. throw new IllegalStateException("Could not initialize reflection!", e);
  54. }
  55. }
  56.  
  57. public static void registerSerializer(Kryo kryo) {
  58. kryo.register(CLASS_ITEM, new Serializer<Object>() {
  59. @Override
  60. public void write(Kryo kryo, Output output, Object item) {
  61. try {
  62. METHOD_NBT_SAVE.invoke(null,
  63. METHOD_ITEM_SAVE.invoke(item, CONSTRUCTOR_NBT.newInstance()),
  64. new DataOutputStream(output)
  65. );
  66. } catch (Exception e) {
  67. throw new IllegalStateException("Failed to serialize NBT for an ItemStack.", e);
  68. }
  69. }
  70.  
  71. @Override
  72. public Object read(Kryo kryo, Input input, Class<Object> type) {
  73. try {
  74. return CONSTRUCTOR_ITEM.newInstance(
  75. METHOD_NBT_LOAD.invoke(null, new DataInputStream(input))
  76. );
  77. } catch (Exception e) {
  78. throw new IllegalStateException("Failed to serialize NBT for an ItemStack.", e);
  79. }
  80. }
  81. });
  82. }
  83. }
Add Comment
Please, Sign In to add comment