Skip to content

Commit ba2e6ef

Browse files
committed
Add option to not compress serialized item bytes
1 parent a0f626b commit ba2e6ef

3 files changed

Lines changed: 73 additions & 15 deletions

File tree

paper-api/src/main/java/org/bukkit/UnsafeValues.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,12 @@ default com.destroystokyo.paper.util.VersionFetcher getVersionFetcher() {
164164

165165
byte[] serializeItem(ItemStack item);
166166

167+
byte[] serializeItem(ItemStack item, boolean compressed);
168+
167169
ItemStack deserializeItem(byte[] data);
168170

171+
ItemStack deserializeItem(byte[] data, boolean compressed);
172+
169173
/**
170174
* Serializes this itemstack to json format.
171175
* It is safe for data migrations as it will use the built-in data converter instead of bukkit's

paper-api/src/main/java/org/bukkit/inventory/ItemStack.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -761,20 +761,48 @@ public ItemStack ensureServerConversions() {
761761
*
762762
* This expects that the DataVersion was stored on the root of the Compound, as saved from
763763
* the {@link #serializeAsBytes()} API returned.
764+
*
765+
* This goes through mojang's compression stream.
764766
* @param bytes bytes representing an item in NBT
765767
* @return ItemStack migrated to this version of Minecraft if needed.
766768
*/
767769
public static @NotNull ItemStack deserializeBytes(final byte @NotNull [] bytes) {
768-
return org.bukkit.Bukkit.getUnsafe().deserializeItem(bytes);
770+
return deserializeBytes(bytes, true);
771+
}
772+
773+
/**
774+
* Deserializes this itemstack from raw NBT bytes. NBT is safer for data migrations as it will
775+
* use the built in data converter instead of bukkits dangerous serialization system.
776+
*
777+
* This expects that the DataVersion was stored on the root of the Compound, as saved from
778+
* the {@link #serializeAsBytes()} API returned.
779+
* @param bytes bytes representing an item in NBT
780+
* @param compressed whether the data goes through mojang's compression stream. You must use the same value when serializing.
781+
* @return ItemStack migrated to this version of Minecraft if needed.
782+
*/
783+
public static @NotNull ItemStack deserializeBytes(final byte @NotNull [] bytes, final boolean compressed) {
784+
return org.bukkit.Bukkit.getUnsafe().deserializeItem(bytes, compressed);
769785
}
770786

771787
/**
772788
* Serializes this itemstack to raw bytes in NBT. NBT is safer for data migrations as it will
773789
* use the built in data converter instead of bukkits dangerous serialization system.
790+
*
791+
* This goes through mojang's compression stream.
774792
* @return bytes representing this item in NBT.
775793
*/
776794
public byte @NotNull [] serializeAsBytes() {
777-
return org.bukkit.Bukkit.getUnsafe().serializeItem(this);
795+
return serializeAsBytes(true);
796+
}
797+
798+
/**
799+
* Serializes this itemstack to raw bytes in NBT. NBT is safer for data migrations as it will
800+
* use the built in data converter instead of bukkits dangerous serialization system.
801+
* @param compressed whether the data goes through mojang's compression stream. You must use the same value when deserializing.
802+
* @return bytes representing this item in NBT.
803+
*/
804+
public byte @NotNull [] serializeAsBytes(final boolean compressed) {
805+
return org.bukkit.Bukkit.getUnsafe().serializeItem(this, compressed);
778806
}
779807

780808
/**

paper-server/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
import io.papermc.paper.adventure.AdventureCodecs;
1717
import io.papermc.paper.adventure.PaperAdventure;
1818
import io.papermc.paper.registry.RegistryKey;
19+
20+
import java.io.DataInputStream;
21+
import java.io.DataOutputStream;
1922
import java.io.File;
2023
import java.io.IOException;
2124
import java.nio.charset.StandardCharsets;
@@ -492,23 +495,33 @@ public com.destroystokyo.paper.util.VersionFetcher getVersionFetcher() {
492495

493496
@Override
494497
public byte[] serializeItem(ItemStack item) {
498+
return this.serializeItem(item, true);
499+
}
500+
501+
@Override
502+
public byte[] serializeItem(ItemStack item, boolean compressed) {
495503
Preconditions.checkNotNull(item, "null cannot be serialized");
496504
Preconditions.checkArgument(!item.isEmpty(), "Empty itemstack cannot be serialized");
497505

498506
return serializeNbtToBytes(
499507
(CompoundTag) net.minecraft.world.item.ItemStack.CODEC.encodeStart(
500508
MinecraftServer.getServer().registryAccess().createSerializationContext(NbtOps.INSTANCE),
501509
CraftItemStack.unwrap(item)
502-
).getOrThrow()
510+
).getOrThrow(), compressed
503511
);
504512
}
505513

506514
@Override
507515
public ItemStack deserializeItem(byte[] data) {
516+
return this.deserializeItem(data, true);
517+
}
518+
519+
@Override
520+
public ItemStack deserializeItem(byte[] data, boolean compressed) {
508521
Preconditions.checkNotNull(data, "null cannot be deserialized");
509522
Preconditions.checkArgument(data.length > 0, "cannot deserialize nothing");
510523

511-
CompoundTag compound = deserializeNbtFromBytes(data);
524+
CompoundTag compound = deserializeNbtFromBytes(data, compressed);
512525
return deserializeItem(compound);
513526
}
514527

@@ -706,7 +719,7 @@ public byte[] serializeEntity(org.bukkit.entity.Entity entity, EntitySerializati
706719
throw new IllegalArgumentException("Couldn't serialize entity");
707720
}
708721
}
709-
return serializeNbtToBytes(output.buildResult());
722+
return serializeNbtToBytes(output.buildResult(), true);
710723
}
711724
}
712725

@@ -715,7 +728,7 @@ public org.bukkit.entity.Entity deserializeEntity(byte[] data, World world, bool
715728
Preconditions.checkNotNull(data, "null cannot be deserialized");
716729
Preconditions.checkArgument(data.length > 0, "Cannot deserialize empty data");
717730

718-
CompoundTag compound = deserializeNbtFromBytes(data);
731+
CompoundTag compound = deserializeNbtFromBytes(data, true);
719732
int dataVersion = compound.getIntOr("DataVersion", 0);
720733
compound = PlatformHooks.get().convertNBT(References.ENTITY, MinecraftServer.getServer().fixerUpper, compound, dataVersion, this.getDataVersion()); // Paper - possibly use dataconverter
721734
if (!preservePassengers) {
@@ -754,26 +767,39 @@ private net.minecraft.world.entity.Entity deserializeEntity(CompoundTag compound
754767
return nmsEntity;
755768
}
756769

757-
private byte[] serializeNbtToBytes(CompoundTag compound) {
770+
private byte[] serializeNbtToBytes(CompoundTag compound, boolean compressed) {
758771
compound.putInt("DataVersion", getDataVersion());
759772
java.io.ByteArrayOutputStream outputStream = new java.io.ByteArrayOutputStream();
760773
try {
761-
net.minecraft.nbt.NbtIo.writeCompressed(
762-
compound,
763-
outputStream
764-
);
774+
if (compressed) {
775+
net.minecraft.nbt.NbtIo.writeCompressed(
776+
compound,
777+
outputStream
778+
);
779+
} else {
780+
net.minecraft.nbt.NbtIo.write(
781+
compound,
782+
new DataOutputStream(outputStream)
783+
);
784+
}
765785
} catch (IOException ex) {
766786
throw new RuntimeException(ex);
767787
}
768788
return outputStream.toByteArray();
769789
}
770790

771-
private CompoundTag deserializeNbtFromBytes(byte[] data) {
791+
private CompoundTag deserializeNbtFromBytes(byte[] data, boolean compressed) {
772792
CompoundTag compound;
773793
try {
774-
compound = net.minecraft.nbt.NbtIo.readCompressed(
775-
new java.io.ByteArrayInputStream(data), net.minecraft.nbt.NbtAccounter.unlimitedHeap()
776-
);
794+
if (compressed) {
795+
compound = net.minecraft.nbt.NbtIo.readCompressed(
796+
new java.io.ByteArrayInputStream(data), net.minecraft.nbt.NbtAccounter.unlimitedHeap()
797+
);
798+
} else {
799+
compound = net.minecraft.nbt.NbtIo.read(
800+
new DataInputStream(new java.io.ByteArrayInputStream(data)), net.minecraft.nbt.NbtAccounter.unlimitedHeap()
801+
);
802+
}
777803
} catch (IOException ex) {
778804
throw new RuntimeException(ex);
779805
}

0 commit comments

Comments
 (0)