2020-04-30 14:59:24 +00:00
|
|
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
|
|
From: Mariell Hoversholm <proximyst@proximyst.com>
|
|
|
|
Date: Thu, 30 Apr 2020 16:56:54 +0200
|
|
|
|
Subject: [PATCH] Add Raw Byte ItemStack Serialization
|
|
|
|
|
|
|
|
Serializes using NBT which is safer for server data migrations than bukkits format.
|
|
|
|
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/DataConverterRegistry.java b/src/main/java/net/minecraft/server/DataConverterRegistry.java
|
2020-06-26 01:53:21 +00:00
|
|
|
index d7ebbce930fc65437697d25637346b9c99e6a8fa..5d807082281527c78a08d01430e42b3ecee6d2cf 100644
|
2020-04-30 14:59:24 +00:00
|
|
|
--- a/src/main/java/net/minecraft/server/DataConverterRegistry.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/DataConverterRegistry.java
|
2020-06-26 01:53:21 +00:00
|
|
|
@@ -24,6 +24,7 @@ public class DataConverterRegistry {
|
2020-04-30 14:59:24 +00:00
|
|
|
return datafixerbuilder.build(SystemUtils.e());
|
|
|
|
}
|
|
|
|
|
|
|
|
+ public static DataFixer getDataFixer() { return a(); } // Paper - OBFHELPER
|
|
|
|
public static DataFixer a() {
|
|
|
|
return DataConverterRegistry.c;
|
|
|
|
}
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/ItemStack.java b/src/main/java/net/minecraft/server/ItemStack.java
|
2020-07-22 23:35:44 +00:00
|
|
|
index d1e4ee64aa395b38cad22f316308c4687ea56b9f..9a9678ed76b95131df0c12af5bba3440789699aa 100644
|
2020-04-30 14:59:24 +00:00
|
|
|
--- a/src/main/java/net/minecraft/server/ItemStack.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/ItemStack.java
|
2020-06-26 01:53:21 +00:00
|
|
|
@@ -144,6 +144,7 @@ public final class ItemStack {
|
2020-04-30 14:59:24 +00:00
|
|
|
this.checkEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
+ public static ItemStack fromCompound(NBTTagCompound nbttagcompound) { return a(nbttagcompound); } // Paper - OBFHELPER
|
|
|
|
public static ItemStack a(NBTTagCompound nbttagcompound) {
|
|
|
|
try {
|
|
|
|
return new ItemStack(nbttagcompound);
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/NBTCompressedStreamTools.java b/src/main/java/net/minecraft/server/NBTCompressedStreamTools.java
|
2020-06-26 12:04:38 +00:00
|
|
|
index f48b62af436de47484eb1cf496e97a29168586b0..c948e590eeff0aea53573532f8c507d638cc0bdf 100644
|
2020-04-30 14:59:24 +00:00
|
|
|
--- a/src/main/java/net/minecraft/server/NBTCompressedStreamTools.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/NBTCompressedStreamTools.java
|
2020-06-26 12:04:38 +00:00
|
|
|
@@ -15,6 +15,7 @@ import io.netty.buffer.ByteBufInputStream; // Paper
|
2020-04-30 14:59:24 +00:00
|
|
|
|
|
|
|
public class NBTCompressedStreamTools {
|
|
|
|
|
|
|
|
+ public static NBTTagCompound readNBT(InputStream inputstream) throws IOException { return a(inputstream); } // Paper - OBFHELPER
|
|
|
|
public static NBTTagCompound a(InputStream inputstream) throws IOException {
|
|
|
|
DataInputStream datainputstream = new DataInputStream(new BufferedInputStream(new GZIPInputStream(inputstream)));
|
|
|
|
Throwable throwable = null;
|
2020-06-26 12:04:38 +00:00
|
|
|
@@ -44,6 +45,7 @@ public class NBTCompressedStreamTools {
|
2020-04-30 14:59:24 +00:00
|
|
|
return nbttagcompound;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ public static void writeNBT(NBTTagCompound nbttagcompound, OutputStream outputstream) throws IOException { a(nbttagcompound, outputstream); } // Paper - OBFHELPER
|
|
|
|
public static void a(NBTTagCompound nbttagcompound, OutputStream outputstream) throws IOException {
|
|
|
|
DataOutputStream dataoutputstream = new DataOutputStream(new BufferedOutputStream(new GZIPOutputStream(outputstream)));
|
|
|
|
Throwable throwable = null;
|
|
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
|
2020-07-28 08:36:52 +00:00
|
|
|
index d288d5fadbdb7cd4e5a42f94cdba6f825b2ed877..90c1067ca23e566a96d71e06c273d44397928b64 100644
|
2020-04-30 14:59:24 +00:00
|
|
|
--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
|
|
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
|
2020-07-28 08:36:52 +00:00
|
|
|
@@ -333,6 +333,46 @@ public final class CraftMagicNumbers implements UnsafeValues {
|
2020-04-30 14:59:24 +00:00
|
|
|
public boolean isSupportedApiVersion(String apiVersion) {
|
|
|
|
return apiVersion != null && SUPPORTED_API.contains(apiVersion);
|
|
|
|
}
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public byte[] serializeItem(ItemStack item) {
|
|
|
|
+ Preconditions.checkNotNull(item, "null cannot be serialized");
|
|
|
|
+ Preconditions.checkArgument(item.getType() != Material.AIR, "air cannot be serialized");
|
|
|
|
+
|
|
|
|
+ java.io.ByteArrayOutputStream outputStream = new java.io.ByteArrayOutputStream();
|
|
|
|
+ NBTTagCompound compound = (item instanceof CraftItemStack ? ((CraftItemStack) item).getHandle() : CraftItemStack.asNMSCopy(item)).save(new NBTTagCompound());
|
|
|
|
+ compound.setInt("DataVersion", getDataVersion());
|
|
|
|
+ try {
|
|
|
|
+ net.minecraft.server.NBTCompressedStreamTools.writeNBT(
|
|
|
|
+ compound,
|
|
|
|
+ outputStream
|
|
|
|
+ );
|
|
|
|
+ } catch (IOException ex) {
|
|
|
|
+ throw new RuntimeException(ex);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return outputStream.toByteArray();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public ItemStack deserializeItem(byte[] data) {
|
|
|
|
+ Preconditions.checkNotNull(data, "null cannot be deserialized");
|
|
|
|
+ Preconditions.checkArgument(data.length > 0, "cannot deserialize nothing");
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ NBTTagCompound compound = net.minecraft.server.NBTCompressedStreamTools.readNBT(
|
|
|
|
+ new java.io.ByteArrayInputStream(data)
|
|
|
|
+ );
|
|
|
|
+ int dataVersion = compound.getInt("DataVersion");
|
|
|
|
+
|
2020-05-18 17:24:06 +00:00
|
|
|
+ Preconditions.checkArgument(dataVersion <= getDataVersion(), "Newer version! Server downgrades are not supported!");
|
2020-05-22 05:08:18 +00:00
|
|
|
+ Dynamic<NBTBase> converted = DataConverterRegistry.getDataFixer().update(DataConverterTypes.ITEM_STACK, new Dynamic<NBTBase>(DynamicOpsNBT.a, compound), dataVersion, getDataVersion());
|
2020-04-30 14:59:24 +00:00
|
|
|
+ return CraftItemStack.asCraftMirror(net.minecraft.server.ItemStack.fromCompound((NBTTagCompound) converted.getValue()));
|
|
|
|
+ } catch (IOException ex) {
|
|
|
|
+ com.destroystokyo.paper.util.SneakyThrow.sneaky(ex);
|
|
|
|
+ throw new RuntimeException();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
// Paper end
|
|
|
|
|
|
|
|
/**
|