aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/0341-Add-Raw-Byte-ItemStack-Serialization.patch
diff options
context:
space:
mode:
authorNassim Jahnke <[email protected]>2024-01-23 15:43:48 +0100
committerNassim Jahnke <[email protected]>2024-01-23 15:57:42 +0100
commite66037960b02c7b22bbed75318904c55a4177977 (patch)
treed0b197f87dbd7c547018af9defdb33691ddce30b /patches/server/0341-Add-Raw-Byte-ItemStack-Serialization.patch
parent52619e7a2162d0feb8f142227fe8ad34e21ce860 (diff)
downloadPaper-e66037960b02c7b22bbed75318904c55a4177977.tar.gz
Paper-e66037960b02c7b22bbed75318904c55a4177977.zip
[ci skip] Move some disruptive patches back
Diffstat (limited to 'patches/server/0341-Add-Raw-Byte-ItemStack-Serialization.patch')
-rw-r--r--patches/server/0341-Add-Raw-Byte-ItemStack-Serialization.patch64
1 files changed, 64 insertions, 0 deletions
diff --git a/patches/server/0341-Add-Raw-Byte-ItemStack-Serialization.patch b/patches/server/0341-Add-Raw-Byte-ItemStack-Serialization.patch
new file mode 100644
index 0000000000..afd9646f91
--- /dev/null
+++ b/patches/server/0341-Add-Raw-Byte-ItemStack-Serialization.patch
@@ -0,0 +1,64 @@
+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+From: Mariell Hoversholm <[email protected]>
+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/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
+index e31ead0d99203a018757cb2e765b5d28dd373eef..2ff5d2c357a4832e2624ef53fb0110d61fddc6fc 100644
+--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
++++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
+@@ -458,6 +458,52 @@ public final class CraftMagicNumbers implements UnsafeValues {
+ public com.destroystokyo.paper.util.VersionFetcher getVersionFetcher() {
+ return new com.destroystokyo.paper.PaperVersionFetcher();
+ }
++
++ @Override
++ public byte[] serializeItem(ItemStack item) {
++ Preconditions.checkNotNull(item, "null cannot be serialized");
++ Preconditions.checkArgument(item.getType() != Material.AIR, "air cannot be serialized");
++
++ return serializeNbtToBytes((item instanceof CraftItemStack ? ((CraftItemStack) item).handle : CraftItemStack.asNMSCopy(item)).save(new CompoundTag()));
++ }
++
++ @Override
++ public ItemStack deserializeItem(byte[] data) {
++ Preconditions.checkNotNull(data, "null cannot be deserialized");
++ Preconditions.checkArgument(data.length > 0, "cannot deserialize nothing");
++
++ CompoundTag compound = deserializeNbtFromBytes(data);
++ int dataVersion = compound.getInt("DataVersion");
++ return CraftItemStack.asCraftMirror(net.minecraft.world.item.ItemStack.of(ca.spottedleaf.dataconverter.minecraft.MCDataConverter.convertTag(ca.spottedleaf.dataconverter.minecraft.datatypes.MCTypeRegistry.ITEM_STACK, compound, dataVersion, getDataVersion())));
++ }
++
++ private byte[] serializeNbtToBytes(CompoundTag compound) {
++ compound.putInt("DataVersion", getDataVersion());
++ java.io.ByteArrayOutputStream outputStream = new java.io.ByteArrayOutputStream();
++ try {
++ net.minecraft.nbt.NbtIo.writeCompressed(
++ compound,
++ outputStream
++ );
++ } catch (IOException ex) {
++ throw new RuntimeException(ex);
++ }
++ return outputStream.toByteArray();
++ }
++
++ private CompoundTag deserializeNbtFromBytes(byte[] data) {
++ CompoundTag compound;
++ try {
++ compound = net.minecraft.nbt.NbtIo.readCompressed(
++ new java.io.ByteArrayInputStream(data), net.minecraft.nbt.NbtAccounter.unlimitedHeap()
++ );
++ } catch (IOException ex) {
++ throw new RuntimeException(ex);
++ }
++ int dataVersion = compound.getInt("DataVersion");
++ Preconditions.checkArgument(dataVersion <= getDataVersion(), "Newer version! Server downgrades are not supported!");
++ return compound;
++ }
+ // Paper end
+
+ /**