2021-06-14 03:06:38 +02: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/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
|
2024-10-24 19:29:35 +02:00
|
|
|
index d22eb174d9c0cc5dd6418c478470c616b75801b2..f78459e65fd27a5fd9839d2ad1c95758c8d95489 100644
|
2021-06-14 03:06:38 +02:00
|
|
|
--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
|
|
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
|
2024-10-21 00:06:54 +02:00
|
|
|
@@ -464,6 +464,53 @@ public final class CraftMagicNumbers implements UnsafeValues {
|
2023-02-19 15:57:10 +01:00
|
|
|
public com.destroystokyo.paper.util.VersionFetcher getVersionFetcher() {
|
|
|
|
return new com.destroystokyo.paper.PaperVersionFetcher();
|
2021-06-14 03:06:38 +02:00
|
|
|
}
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public byte[] serializeItem(ItemStack item) {
|
|
|
|
+ Preconditions.checkNotNull(item, "null cannot be serialized");
|
|
|
|
+ Preconditions.checkArgument(item.getType() != Material.AIR, "air cannot be serialized");
|
|
|
|
+
|
2024-04-25 07:16:04 +02:00
|
|
|
+ return serializeNbtToBytes((net.minecraft.nbt.CompoundTag) (item instanceof CraftItemStack ? ((CraftItemStack) item).handle : CraftItemStack.asNMSCopy(item)).save(MinecraftServer.getServer().registryAccess()));
|
2021-11-11 03:53:27 +01:00
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public ItemStack deserializeItem(byte[] data) {
|
|
|
|
+ Preconditions.checkNotNull(data, "null cannot be deserialized");
|
|
|
|
+ Preconditions.checkArgument(data.length > 0, "cannot deserialize nothing");
|
|
|
|
+
|
2024-04-25 07:16:04 +02:00
|
|
|
+ net.minecraft.nbt.CompoundTag compound = deserializeNbtFromBytes(data);
|
2024-01-25 10:54:46 +01:00
|
|
|
+ final int dataVersion = compound.getInt("DataVersion");
|
2024-04-25 07:16:04 +02:00
|
|
|
+ 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());
|
2021-11-11 03:53:27 +01:00
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private byte[] serializeNbtToBytes(CompoundTag compound) {
|
2021-06-14 03:06:38 +02:00
|
|
|
+ compound.putInt("DataVersion", getDataVersion());
|
2021-11-11 03:53:27 +01:00
|
|
|
+ java.io.ByteArrayOutputStream outputStream = new java.io.ByteArrayOutputStream();
|
2021-06-14 03:06:38 +02:00
|
|
|
+ try {
|
|
|
|
+ net.minecraft.nbt.NbtIo.writeCompressed(
|
|
|
|
+ compound,
|
|
|
|
+ outputStream
|
|
|
|
+ );
|
|
|
|
+ } catch (IOException ex) {
|
|
|
|
+ throw new RuntimeException(ex);
|
|
|
|
+ }
|
|
|
|
+ return outputStream.toByteArray();
|
|
|
|
+ }
|
|
|
|
+
|
2024-04-25 07:16:04 +02:00
|
|
|
+ private net.minecraft.nbt.CompoundTag deserializeNbtFromBytes(byte[] data) {
|
|
|
|
+ net.minecraft.nbt.CompoundTag compound;
|
2021-06-14 03:06:38 +02:00
|
|
|
+ try {
|
2021-11-11 03:53:27 +01:00
|
|
|
+ compound = net.minecraft.nbt.NbtIo.readCompressed(
|
2023-12-06 20:10:59 +01:00
|
|
|
+ new java.io.ByteArrayInputStream(data), net.minecraft.nbt.NbtAccounter.unlimitedHeap()
|
2021-06-14 03:06:38 +02:00
|
|
|
+ );
|
|
|
|
+ } catch (IOException ex) {
|
2021-11-11 03:53:27 +01:00
|
|
|
+ throw new RuntimeException(ex);
|
2021-06-14 03:06:38 +02:00
|
|
|
+ }
|
2021-11-11 03:53:27 +01:00
|
|
|
+ int dataVersion = compound.getInt("DataVersion");
|
|
|
|
+ Preconditions.checkArgument(dataVersion <= getDataVersion(), "Newer version! Server downgrades are not supported!");
|
|
|
|
+ return compound;
|
2021-06-14 03:06:38 +02:00
|
|
|
+ }
|
|
|
|
// Paper end
|
|
|
|
|
2024-05-11 23:48:37 +02:00
|
|
|
@Override
|