Paper/Spigot-Server-Patches/0434-Add-Raw-Byte-ItemStack-Serialization.patch
Aikar c953e51dd7
[Auto] Updated Upstream (CraftBukkit/Spigot)
Upstream has released updates that appear to apply and compile correctly.
This update has not been tested by PaperMC and as with ANY update, please do your own testing

CraftBukkit Changes:
221aed6cf SPIGOT-6413: Server Corruption Changing Blocks in Piston Events
721c4966b SPIGOT-6411: The PlayerEditBookEvent is not called when the player edits a book in the off-hand.
be0e94581 Add mc-dev imports

Spigot Changes:
a25e8ed2 Remove mc-dev imports
2021-04-07 01:17:32 -04:00

103 lines
5.5 KiB
Diff

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/nbt/NBTCompressedStreamTools.java b/src/main/java/net/minecraft/nbt/NBTCompressedStreamTools.java
index 20410a5853e34c90c872f5e9592d50c4727e914d..860f084de38dc3f8723d881ff78fb1873f2b602a 100644
--- a/src/main/java/net/minecraft/nbt/NBTCompressedStreamTools.java
+++ b/src/main/java/net/minecraft/nbt/NBTCompressedStreamTools.java
@@ -51,6 +51,7 @@ public class NBTCompressedStreamTools {
return nbttagcompound;
}
+ 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;
@@ -106,6 +107,7 @@ public class NBTCompressedStreamTools {
}
+ 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/net/minecraft/util/datafix/DataConverterRegistry.java b/src/main/java/net/minecraft/util/datafix/DataConverterRegistry.java
index 6527509e6aed7187667c681af682e9a02937a224..28e36ee76da533f8aa0a09cfc4f1fc0f744af715 100644
--- a/src/main/java/net/minecraft/util/datafix/DataConverterRegistry.java
+++ b/src/main/java/net/minecraft/util/datafix/DataConverterRegistry.java
@@ -208,6 +208,7 @@ public class DataConverterRegistry {
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/world/item/ItemStack.java b/src/main/java/net/minecraft/world/item/ItemStack.java
index 661f400ae4f5cebef5d1743819529ecf647b6681..0468f80b7f52ee45fc9364470b23f80f7cd0cb57 100644
--- a/src/main/java/net/minecraft/world/item/ItemStack.java
+++ b/src/main/java/net/minecraft/world/item/ItemStack.java
@@ -198,6 +198,7 @@ public final class ItemStack {
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/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
index 79ac4e1f3aad1a855a5d7ef7df2a3bfd1c4362f2..51aa0c37df947a229eddaa3c4e3372f64575617e 100644
--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
+++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
@@ -360,6 +360,46 @@ public final class CraftMagicNumbers implements UnsafeValues {
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.nbt.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.nbt.NBTCompressedStreamTools.readNBT(
+ new java.io.ByteArrayInputStream(data)
+ );
+ int dataVersion = compound.getInt("DataVersion");
+
+ Preconditions.checkArgument(dataVersion <= getDataVersion(), "Newer version! Server downgrades are not supported!");
+ Dynamic<NBTBase> converted = DataConverterRegistry.getDataFixer().update(DataConverterTypes.ITEM_STACK, new Dynamic<NBTBase>(DynamicOpsNBT.a, compound), dataVersion, getDataVersion());
+ return CraftItemStack.asCraftMirror(net.minecraft.world.item.ItemStack.fromCompound((NBTTagCompound) converted.getValue()));
+ } catch (IOException ex) {
+ com.destroystokyo.paper.util.SneakyThrow.sneaky(ex);
+ throw new RuntimeException();
+ }
+ }
// Paper end
/**