PaperMC/patches/server-remapped/Add-Raw-Byte-ItemStack-Serialization.patch
MiniDigger | Martin 95d25d98e8 Move patches around
2021-06-11 14:02:28 +02:00

102 lines
5.2 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/NbtIo.java b/src/main/java/net/minecraft/nbt/NbtIo.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/nbt/NbtIo.java
+++ b/src/main/java/net/minecraft/nbt/NbtIo.java
@@ -0,0 +0,0 @@ public class NbtIo {
return nbttagcompound;
}
+ public static CompoundTag readNBT(InputStream inputstream) throws IOException { return readCompressed(inputstream); } // Paper - OBFHELPER
public static CompoundTag readCompressed(InputStream stream) throws IOException {
DataInputStream datainputstream = new DataInputStream(new BufferedInputStream(new GZIPInputStream(stream)));
Throwable throwable = null;
@@ -0,0 +0,0 @@ public class NbtIo {
}
+ public static void writeNBT(CompoundTag nbttagcompound, OutputStream outputstream) throws IOException { writeCompressed(nbttagcompound, outputstream); } // Paper - OBFHELPER
public static void writeCompressed(CompoundTag tag, OutputStream stream) throws IOException {
DataOutputStream dataoutputstream = new DataOutputStream(new BufferedOutputStream(new GZIPOutputStream(stream)));
Throwable throwable = null;
diff --git a/src/main/java/net/minecraft/util/datafix/DataFixers.java b/src/main/java/net/minecraft/util/datafix/DataFixers.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/util/datafix/DataFixers.java
+++ b/src/main/java/net/minecraft/util/datafix/DataFixers.java
@@ -0,0 +0,0 @@ public class DataFixers {
return datafixerbuilder.build(Util.bootstrapExecutor());
}
+ public static DataFixer getDataFixer() { return getDataFixer(); } // Paper - OBFHELPER
public static DataFixer getDataFixer() {
return DataFixers.DATA_FIXER;
}
diff --git a/src/main/java/net/minecraft/world/item/ItemStack.java b/src/main/java/net/minecraft/world/item/ItemStack.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/world/item/ItemStack.java
+++ b/src/main/java/net/minecraft/world/item/ItemStack.java
@@ -0,0 +0,0 @@ public final class ItemStack {
this.updateEmptyCacheFlag();
}
+ public static ItemStack fromCompound(CompoundTag nbttagcompound) { return of(nbttagcompound); } // Paper - OBFHELPER
public static ItemStack of(CompoundTag tag) {
try {
return new ItemStack(tag);
diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
+++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
@@ -0,0 +0,0 @@ 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();
+ CompoundTag compound = (item instanceof CraftItemStack ? ((CraftItemStack) item).getHandle() : CraftItemStack.asNMSCopy(item)).save(new CompoundTag());
+ compound.putInt("DataVersion", getDataVersion());
+ try {
+ net.minecraft.nbt.NbtIo.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 {
+ CompoundTag compound = net.minecraft.nbt.NbtIo.readNBT(
+ new java.io.ByteArrayInputStream(data)
+ );
+ int dataVersion = compound.getInt("DataVersion");
+
+ Preconditions.checkArgument(dataVersion <= getDataVersion(), "Newer version! Server downgrades are not supported!");
+ Dynamic<Tag> converted = DataFixers.getDataFixer().update(References.ITEM_STACK, new Dynamic<Tag>(NbtOps.INSTANCE, compound), dataVersion, getDataVersion());
+ return CraftItemStack.asCraftMirror(net.minecraft.world.item.ItemStack.fromCompound((CompoundTag) converted.getValue()));
+ } catch (IOException ex) {
+ com.destroystokyo.paper.util.SneakyThrow.sneaky(ex);
+ throw new RuntimeException();
+ }
+ }
// Paper end
/**