1616import io .papermc .paper .adventure .AdventureCodecs ;
1717import io .papermc .paper .adventure .PaperAdventure ;
1818import io .papermc .paper .registry .RegistryKey ;
19+
20+ import java .io .DataInputStream ;
21+ import java .io .DataOutputStream ;
1922import java .io .File ;
2023import java .io .IOException ;
2124import 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