Skip to content

Commit

Permalink
1.8.8 old data compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
caoli5288 committed Jun 12, 2019
1 parent 8b67411 commit ccd75c0
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
3 changes: 3 additions & 0 deletions internal/v1_8_3/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dependencies {
compileOnly "org.spigotmc:spigot:1.8.8-R0.1-SNAPSHOT"
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -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'
4 changes: 4 additions & 0 deletions src/main/java/com/mengcraft/playersql/DataSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit ccd75c0

Please sign in to comment.