2021-06-14 01:06:38 +00: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
|
2022-03-01 02:44:07 +00:00
|
|
|
index 7739b98398ee08b10c545cee6f9bd2c698a38fe2..6c2d35ab30285ffdd0e2ceda8af1293cb140f4d0 100644
|
2021-06-14 01:06:38 +00:00
|
|
|
--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
|
|
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
|
2022-01-01 03:05:42 +00:00
|
|
|
@@ -422,6 +422,53 @@ public final class CraftMagicNumbers implements UnsafeValues {
|
2021-06-14 01:06:38 +00:00
|
|
|
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");
|
|
|
|
+
|
2021-11-11 02:53:27 +00:00
|
|
|
+ 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");
|
|
|
|
+ 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.of((CompoundTag) converted.getValue()));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private byte[] serializeNbtToBytes(CompoundTag compound) {
|
2021-06-14 01:06:38 +00:00
|
|
|
+ compound.putInt("DataVersion", getDataVersion());
|
2021-11-11 02:53:27 +00:00
|
|
|
+ java.io.ByteArrayOutputStream outputStream = new java.io.ByteArrayOutputStream();
|
2021-06-14 01:06:38 +00:00
|
|
|
+ try {
|
|
|
|
+ net.minecraft.nbt.NbtIo.writeCompressed(
|
|
|
|
+ compound,
|
|
|
|
+ outputStream
|
|
|
|
+ );
|
|
|
|
+ } catch (IOException ex) {
|
|
|
|
+ throw new RuntimeException(ex);
|
|
|
|
+ }
|
|
|
|
+ return outputStream.toByteArray();
|
|
|
|
+ }
|
|
|
|
+
|
2021-11-11 02:53:27 +00:00
|
|
|
+ private CompoundTag deserializeNbtFromBytes(byte[] data) {
|
|
|
|
+ CompoundTag compound;
|
2021-06-14 01:06:38 +00:00
|
|
|
+ try {
|
2021-11-11 02:53:27 +00:00
|
|
|
+ compound = net.minecraft.nbt.NbtIo.readCompressed(
|
2021-06-14 01:06:38 +00:00
|
|
|
+ new java.io.ByteArrayInputStream(data)
|
|
|
|
+ );
|
|
|
|
+ } catch (IOException ex) {
|
2021-11-11 02:53:27 +00:00
|
|
|
+ throw new RuntimeException(ex);
|
2021-06-14 01:06:38 +00:00
|
|
|
+ }
|
2021-11-11 02:53:27 +00:00
|
|
|
+ int dataVersion = compound.getInt("DataVersion");
|
|
|
|
+ Preconditions.checkArgument(dataVersion <= getDataVersion(), "Newer version! Server downgrades are not supported!");
|
|
|
|
+ return compound;
|
2021-06-14 01:06:38 +00:00
|
|
|
+ }
|
|
|
|
// Paper end
|
|
|
|
|
|
|
|
/**
|