From ccd75c0d3258e2ce777e36af001e26cb7855b091 Mon Sep 17 00:00:00 2001 From: caoli5288 Date: Wed, 12 Jun 2019 14:29:34 +0800 Subject: [PATCH] 1.8.8 old data compatible --- internal/v1_8_3/build.gradle | 3 + .../internal/v1_8_3/PacketDataSerializer.java | 75 +++++++++++++++++++ settings.gradle | 1 + .../mengcraft/playersql/DataSerializer.java | 4 + 4 files changed, 83 insertions(+) create mode 100644 internal/v1_8_3/build.gradle create mode 100644 internal/v1_8_3/src/main/java/com/mengcraft/playersql/internal/v1_8_3/PacketDataSerializer.java diff --git a/internal/v1_8_3/build.gradle b/internal/v1_8_3/build.gradle new file mode 100644 index 0000000..48964d2 --- /dev/null +++ b/internal/v1_8_3/build.gradle @@ -0,0 +1,3 @@ +dependencies { + compileOnly "org.spigotmc:spigot:1.8.8-R0.1-SNAPSHOT" +} diff --git a/internal/v1_8_3/src/main/java/com/mengcraft/playersql/internal/v1_8_3/PacketDataSerializer.java b/internal/v1_8_3/src/main/java/com/mengcraft/playersql/internal/v1_8_3/PacketDataSerializer.java new file mode 100644 index 0000000..59751f0 --- /dev/null +++ b/internal/v1_8_3/src/main/java/com/mengcraft/playersql/internal/v1_8_3/PacketDataSerializer.java @@ -0,0 +1,75 @@ +package com.mengcraft.playersql.internal.v1_8_3; + +import com.google.common.io.ByteArrayDataOutput; +import com.google.common.io.ByteStreams; +import com.mengcraft.playersql.internal.IPacketDataSerializer; +import io.netty.buffer.ByteBuf; +import lombok.SneakyThrows; +import net.minecraft.server.v1_8_R3.NBTReadLimiter; +import net.minecraft.server.v1_8_R3.NBTTagCompound; +import org.bukkit.Material; +import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack; +import org.bukkit.inventory.ItemStack; + +import java.io.DataInput; +import java.io.DataOutput; +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +/** + * Compatible with old 1.8.8 data format. + */ +public class PacketDataSerializer implements IPacketDataSerializer { + + private static Field handle; + private static Method save; + private static Method load; + + static { + try { + handle = CraftItemStack.class.getDeclaredField("handle"); + handle.setAccessible(true); + save = NBTTagCompound.class.getDeclaredMethod("write", DataOutput.class); + save.setAccessible(true); + load = NBTTagCompound.class.getDeclaredMethod("load", DataInput.class, int.class, NBTReadLimiter.class); + load.setAccessible(true); + } catch (Exception ignored) { + } + } + + private final net.minecraft.server.v1_8_R3.PacketDataSerializer buf; + + @SneakyThrows + public PacketDataSerializer(ByteBuf bytebuf) { + buf = new net.minecraft.server.v1_8_R3.PacketDataSerializer(bytebuf); + } + + @SneakyThrows + public void write(ItemStack input) { + if (input == null || input.getType() == Material.AIR) { + return; + } + CraftItemStack item = input instanceof CraftItemStack ? ((CraftItemStack) input) : CraftItemStack.asCraftCopy(input); + net.minecraft.server.v1_8_R3.ItemStack nms = (net.minecraft.server.v1_8_R3.ItemStack) handle.get(item); + NBTTagCompound compound = new NBTTagCompound(); + nms.save(compound); + ByteArrayDataOutput output = ByteStreams.newDataOutput(); + save.invoke(compound, output); + buf.writeBytes(output.toByteArray()); + } + + @Override + @SneakyThrows + public ItemStack readItemStack() { + byte[] tmp = new byte[buf.readableBytes()]; + buf.readBytes(tmp); + NBTTagCompound compound = new NBTTagCompound(); + load.invoke(compound, ByteStreams.newDataInput(tmp), 0, NBTReadLimiter.a); + return CraftItemStack.asCraftMirror(net.minecraft.server.v1_8_R3.ItemStack.createStack(compound)); + } + + @Override + public ByteBuf buf() { + return buf; + } +} diff --git a/settings.gradle b/settings.gradle index 0f253a9..8b3f92e 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,5 +1,6 @@ rootProject.name = "playersql" include 'internal' +include 'internal:v1_8_3' include 'internal:v1_12' include 'internal:v1_13' include 'internal:v1_13_2' diff --git a/src/main/java/com/mengcraft/playersql/DataSerializer.java b/src/main/java/com/mengcraft/playersql/DataSerializer.java index 22f86a2..31fe4ad 100644 --- a/src/main/java/com/mengcraft/playersql/DataSerializer.java +++ b/src/main/java/com/mengcraft/playersql/DataSerializer.java @@ -17,6 +17,9 @@ public class DataSerializer { static { switch (Bukkit.getServer().getClass().getPackage().getName()) { + case "org.bukkit.craftbukkit.v1_8_R3": + PACKET_DATA_SERIALIZER_FACTORY = buf -> new com.mengcraft.playersql.internal.v1_8_3.PacketDataSerializer(buf); + break; case "org.bukkit.craftbukkit.v1_12_R1": PACKET_DATA_SERIALIZER_FACTORY = buf -> new com.mengcraft.playersql.internal.v1_12.PacketDataSerializer(buf); break; @@ -30,6 +33,7 @@ public class DataSerializer { PACKET_DATA_SERIALIZER_FACTORY = null; break; } + System.out.println(String.format("PACKET_DATA_SERIALIZER_FACTORY = %s", PACKET_DATA_SERIALIZER_FACTORY)); } @SneakyThrows