Mirror von
https://github.com/PaperMC/Paper.git
synchronisiert 2024-11-16 04:50:05 +01:00
66 Zeilen
3.0 KiB
Diff
66 Zeilen
3.0 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/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
|
|
index 99322a92b16010ed2ed2505067096e6baef2f616..92cf689c0c2a6602a42319d0a665b3635ca88da5 100644
|
|
--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
|
|
@@ -422,6 +422,53 @@ 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");
|
|
+
|
|
+ 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) {
|
|
+ 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)
|
|
+ );
|
|
+ } 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
|
|
|
|
/**
|