diff options
author | Nassim Jahnke <[email protected]> | 2024-04-25 21:40:53 +0200 |
---|---|---|
committer | Nassim Jahnke <[email protected]> | 2024-04-25 21:57:59 +0200 |
commit | d01f6b2fd23b43f2aec598f2eb7ecfd631b95b27 (patch) | |
tree | 4d3b32d96e7f2989f8e6ee47c128ed10153de84c /patches/server/0338-Add-Raw-Byte-ItemStack-Serialization.patch | |
parent | ca9001a9366952f0a3ac77e6f98db534ba5a54eb (diff) | |
download | Paper-d01f6b2fd23b43f2aec598f2eb7ecfd631b95b27.tar.gz Paper-d01f6b2fd23b43f2aec598f2eb7ecfd631b95b27.zip |
Fix more compile issues
Diffstat (limited to 'patches/server/0338-Add-Raw-Byte-ItemStack-Serialization.patch')
-rw-r--r-- | patches/server/0338-Add-Raw-Byte-ItemStack-Serialization.patch | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/patches/server/0338-Add-Raw-Byte-ItemStack-Serialization.patch b/patches/server/0338-Add-Raw-Byte-ItemStack-Serialization.patch new file mode 100644 index 0000000000..2af7eead67 --- /dev/null +++ b/patches/server/0338-Add-Raw-Byte-ItemStack-Serialization.patch @@ -0,0 +1,65 @@ +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 9e6e6ba95d5cd6cc5d4ada6ac5637b7b82e6fc97..9d81a9893ea556b93127130e575be6c422247049 100644 +--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java ++++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java +@@ -475,6 +475,53 @@ 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((net.minecraft.nbt.CompoundTag) (item instanceof CraftItemStack ? ((CraftItemStack) item).handle : CraftItemStack.asNMSCopy(item)).save(MinecraftServer.getServer().registryAccess())); ++ } ++ ++ @Override ++ public ItemStack deserializeItem(byte[] data) { ++ Preconditions.checkNotNull(data, "null cannot be deserialized"); ++ Preconditions.checkArgument(data.length > 0, "cannot deserialize nothing"); ++ ++ net.minecraft.nbt.CompoundTag compound = deserializeNbtFromBytes(data); ++ final int dataVersion = compound.getInt("DataVersion"); ++ compound = (net.minecraft.nbt.CompoundTag) MinecraftServer.getServer().fixerUpper.update(References.ITEM_STACK, new Dynamic<>(NbtOps.INSTANCE, compound), dataVersion, this.getDataVersion()).getValue(); ++ return CraftItemStack.asCraftMirror(net.minecraft.world.item.ItemStack.parse(MinecraftServer.getServer().registryAccess(), compound).orElseThrow()); ++ } ++ ++ 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 net.minecraft.nbt.CompoundTag deserializeNbtFromBytes(byte[] data) { ++ net.minecraft.nbt.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 + + /** |