diff options
author | Josh Roy <[email protected]> | 2021-11-10 21:53:27 -0500 |
---|---|---|
committer | GitHub <[email protected]> | 2021-11-10 21:53:27 -0500 |
commit | cc2ecbc407e3e1b496938c2b2e66b53dba6e4d94 (patch) | |
tree | 5ab1751030edcc238ac0c4fb743735681e2b05b4 | |
parent | 3f17694803e286bcc1818fdb47122fe46e3e4c29 (diff) | |
download | Paper-cc2ecbc407e3e1b496938c2b2e66b53dba6e4d94.tar.gz Paper-cc2ecbc407e3e1b496938c2b2e66b53dba6e4d94.zip |
Add Raw Byte Entity Serialization (#6826)
11 files changed, 186 insertions, 37 deletions
diff --git a/patches/api/0346-Add-Raw-Byte-Entity-Serialization.patch b/patches/api/0346-Add-Raw-Byte-Entity-Serialization.patch new file mode 100644 index 0000000000..188c6c9cf7 --- /dev/null +++ b/patches/api/0346-Add-Raw-Byte-Entity-Serialization.patch @@ -0,0 +1,62 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Mariell Hoversholm <[email protected]> +Date: Sun, 24 Oct 2021 16:19:26 -0400 +Subject: [PATCH] Add Raw Byte Entity Serialization + + +diff --git a/src/main/java/org/bukkit/UnsafeValues.java b/src/main/java/org/bukkit/UnsafeValues.java +index c8c2e5bc2ec1d8d7ea445a1203c9d5a904dfc666..014c9984018ad5e51a26228a137e1ba4eb3e80c8 100644 +--- a/src/main/java/org/bukkit/UnsafeValues.java ++++ b/src/main/java/org/bukkit/UnsafeValues.java +@@ -102,6 +102,14 @@ public interface UnsafeValues { + + ItemStack deserializeItem(byte[] data); + ++ byte[] serializeEntity(org.bukkit.entity.Entity entity); ++ ++ default org.bukkit.entity.Entity deserializeEntity(byte[] data, World world) { ++ return deserializeEntity(data, world, false); ++ } ++ ++ org.bukkit.entity.Entity deserializeEntity(byte[] data, World world, boolean preserveUUID); ++ + /** + * Return the translation key for the Material, so the client can translate it into the active + * locale when using a {@link net.kyori.adventure.text.TranslatableComponent}. +diff --git a/src/main/java/org/bukkit/entity/Entity.java b/src/main/java/org/bukkit/entity/Entity.java +index d515de1c316f14165ee327bc81f0be98b60db3bf..350384ba10435b1115e4386a78d8145f748b6b29 100644 +--- a/src/main/java/org/bukkit/entity/Entity.java ++++ b/src/main/java/org/bukkit/entity/Entity.java +@@ -767,5 +767,32 @@ public interface Entity extends Metadatable, CommandSender, Nameable, Persistent + * @return players in tracking range + */ + @NotNull Set<Player> getTrackedPlayers(); ++ ++ /** ++ * Spawns the entity in the world at the given {@link Location} with the default spawn reason. ++ * <p> ++ * This will not spawn the entity if the entity is already spawned or has previously been despawned. ++ * <p> ++ * Also, this method will fire the same events as a normal entity spawn. ++ * ++ * @param location The location to spawn the entity at. ++ * @return Whether the entity was successfully spawned. ++ */ ++ public default boolean spawnAt(@NotNull Location location) { ++ return spawnAt(location, org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason.DEFAULT); ++ } ++ ++ /** ++ * Spawns the entity in the world at the given {@link Location} with the reason given. ++ * <p> ++ * This will not spawn the entity if the entity is already spawned or has previously been despawned. ++ * <p> ++ * Also, this method will fire the same events as a normal entity spawn. ++ * ++ * @param location The location to spawn the entity at. ++ * @param reason The reason for the entity being spawned. ++ * @return Whether the entity was successfully spawned. ++ */ ++ public boolean spawnAt(@NotNull Location location, @NotNull org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason reason); + // Paper end + } diff --git a/patches/server/0399-Add-Raw-Byte-ItemStack-Serialization.patch b/patches/server/0399-Add-Raw-Byte-ItemStack-Serialization.patch index 2a434a28d8..657e963f3e 100644 --- a/patches/server/0399-Add-Raw-Byte-ItemStack-Serialization.patch +++ b/patches/server/0399-Add-Raw-Byte-ItemStack-Serialization.patch @@ -6,10 +6,10 @@ 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 f8cb210390958ddba9f9685f2ec1f8bb91690162..6b79b1ea43b713105f37de517019c033477f9835 100644 +index f8cb210390958ddba9f9685f2ec1f8bb91690162..3b727b96c4166e88ec23df3e3e1e98d6aed58637 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java +++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java -@@ -378,6 +378,46 @@ public final class CraftMagicNumbers implements UnsafeValues { +@@ -378,6 +378,53 @@ public final class CraftMagicNumbers implements UnsafeValues { public boolean isSupportedApiVersion(String apiVersion) { return apiVersion != null && SUPPORTED_API.contains(apiVersion); } @@ -19,9 +19,23 @@ index f8cb210390958ddba9f9685f2ec1f8bb91690162..6b79b1ea43b713105f37de517019c033 + 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).handle : CraftItemStack.asNMSCopy(item)).save(new CompoundTag()); ++ 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, @@ -30,28 +44,21 @@ index f8cb210390958ddba9f9685f2ec1f8bb91690162..6b79b1ea43b713105f37de517019c033 + } 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"); -+ ++ private CompoundTag deserializeNbtFromBytes(byte[] data) { ++ CompoundTag compound; + try { -+ CompoundTag compound = net.minecraft.nbt.NbtIo.readCompressed( ++ compound = net.minecraft.nbt.NbtIo.readCompressed( + 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.of((CompoundTag) converted.getValue())); + } catch (IOException ex) { -+ com.destroystokyo.paper.util.SneakyThrow.sneaky(ex); -+ throw new RuntimeException(); ++ throw new RuntimeException(ex); + } ++ int dataVersion = compound.getInt("DataVersion"); ++ Preconditions.checkArgument(dataVersion <= getDataVersion(), "Newer version! Server downgrades are not supported!"); ++ return compound; + } // Paper end diff --git a/patches/server/0519-Add-methods-to-get-translation-keys.patch b/patches/server/0519-Add-methods-to-get-translation-keys.patch index bf701c58ab..3177ac1369 100644 --- a/patches/server/0519-Add-methods-to-get-translation-keys.patch +++ b/patches/server/0519-Add-methods-to-get-translation-keys.patch @@ -42,12 +42,12 @@ index eb99e0c2462a2d1ab4508a5c3f1580b6e31d7465..c536eceef3365a7b726cd970df345ba1 public net.minecraft.world.item.enchantment.Enchantment getHandle() { diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java -index 6b79b1ea43b713105f37de517019c033477f9835..fedbe226c549b103b957beb9feea34350e6220be 100644 +index 3b727b96c4166e88ec23df3e3e1e98d6aed58637..aceb11620972d13324aa05c841b5251d0fda3fab 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java +++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java -@@ -418,6 +418,30 @@ public final class CraftMagicNumbers implements UnsafeValues { - throw new RuntimeException(); - } +@@ -425,6 +425,30 @@ public final class CraftMagicNumbers implements UnsafeValues { + Preconditions.checkArgument(dataVersion <= getDataVersion(), "Newer version! Server downgrades are not supported!"); + return compound; } + + @Override diff --git a/patches/server/0527-Expose-the-Entity-Counter-to-allow-plugins-to-use-va.patch b/patches/server/0527-Expose-the-Entity-Counter-to-allow-plugins-to-use-va.patch index 2d3dae6bb3..835ba5b5b4 100644 --- a/patches/server/0527-Expose-the-Entity-Counter-to-allow-plugins-to-use-va.patch +++ b/patches/server/0527-Expose-the-Entity-Counter-to-allow-plugins-to-use-va.patch @@ -6,7 +6,7 @@ Subject: [PATCH] Expose the Entity Counter to allow plugins to use valid and diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java -index a652bbc4b3c768fecfb6f067d21a90315256f885..9e6a2c5fc55674ab144ebd46da6152e51d3c1dba 100644 +index dd98f228135c634235dc28c8edd396fb90fc98bc..595428849a32374678fc4125cb4a6f90186a1340 100644 --- a/src/main/java/net/minecraft/world/entity/Entity.java +++ b/src/main/java/net/minecraft/world/entity/Entity.java @@ -3958,4 +3958,10 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource, n @@ -21,10 +21,10 @@ index a652bbc4b3c768fecfb6f067d21a90315256f885..9e6a2c5fc55674ab144ebd46da6152e5 + // Paper end } diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java -index fedbe226c549b103b957beb9feea34350e6220be..f212ca8756782b12a746f86933f46b747481c87e 100644 +index aceb11620972d13324aa05c841b5251d0fda3fab..ee974e615096f78906a31b4774e3dde7a156d023 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java +++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java -@@ -442,6 +442,10 @@ public final class CraftMagicNumbers implements UnsafeValues { +@@ -449,6 +449,10 @@ public final class CraftMagicNumbers implements UnsafeValues { net.minecraft.world.item.ItemStack nmsItemStack = CraftItemStack.asNMSCopy(itemStack); return nmsItemStack.getItem().getDescriptionId(nmsItemStack); } diff --git a/patches/server/0639-Item-Rarity-API.patch b/patches/server/0639-Item-Rarity-API.patch index febb7dfe33..4fd3604153 100644 --- a/patches/server/0639-Item-Rarity-API.patch +++ b/patches/server/0639-Item-Rarity-API.patch @@ -5,10 +5,10 @@ Subject: [PATCH] Item Rarity API diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java -index eb06ca21a964de3f3d6712387b72e8994f1f03bd..8ff6d0deb3532515cecdab1f50b8558ef7603f53 100644 +index 66dfc5216570be01fe3a60a8dc305c442b37d50a..d40f341af9649eececfd12c79e34719f03b45c53 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java +++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java -@@ -469,6 +469,20 @@ public final class CraftMagicNumbers implements UnsafeValues { +@@ -476,6 +476,20 @@ public final class CraftMagicNumbers implements UnsafeValues { public int nextEntityId() { return net.minecraft.world.entity.Entity.nextEntityId(); } diff --git a/patches/server/0647-Expose-protocol-version.patch b/patches/server/0647-Expose-protocol-version.patch index dbf1a7b691..b89e447d2f 100644 --- a/patches/server/0647-Expose-protocol-version.patch +++ b/patches/server/0647-Expose-protocol-version.patch @@ -5,10 +5,10 @@ Subject: [PATCH] Expose protocol version diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java -index 8ff6d0deb3532515cecdab1f50b8558ef7603f53..719a4811d9a9a754f7893c595d09187c494bbbae 100644 +index d40f341af9649eececfd12c79e34719f03b45c53..68fc9b338d5ad27cd2b56197f9f5ba4988f89ae6 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java +++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java -@@ -483,6 +483,11 @@ public final class CraftMagicNumbers implements UnsafeValues { +@@ -490,6 +490,11 @@ public final class CraftMagicNumbers implements UnsafeValues { public io.papermc.paper.inventory.ItemRarity getItemStackRarity(org.bukkit.inventory.ItemStack itemStack) { return io.papermc.paper.inventory.ItemRarity.values()[getItem(itemStack.getType()).getRarity(CraftItemStack.asNMSCopy(itemStack)).ordinal()]; } diff --git a/patches/server/0680-ItemStack-repair-check-API.patch b/patches/server/0680-ItemStack-repair-check-API.patch index 32604aaea4..a6ad9965f4 100644 --- a/patches/server/0680-ItemStack-repair-check-API.patch +++ b/patches/server/0680-ItemStack-repair-check-API.patch @@ -5,10 +5,10 @@ Subject: [PATCH] ItemStack repair check API diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java -index 719a4811d9a9a754f7893c595d09187c494bbbae..85a0639e8e65f4d085f605f09ca04a706a631d56 100644 +index 68fc9b338d5ad27cd2b56197f9f5ba4988f89ae6..d88515fcaab8672bc1ace12554372a0b6ab57947 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java +++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java -@@ -484,6 +484,14 @@ public final class CraftMagicNumbers implements UnsafeValues { +@@ -491,6 +491,14 @@ public final class CraftMagicNumbers implements UnsafeValues { return io.papermc.paper.inventory.ItemRarity.values()[getItem(itemStack.getType()).getRarity(CraftItemStack.asNMSCopy(itemStack)).ordinal()]; } diff --git a/patches/server/0687-Attributes-API-for-item-defaults.patch b/patches/server/0687-Attributes-API-for-item-defaults.patch index a0f9c4d774..aa8ce911c8 100644 --- a/patches/server/0687-Attributes-API-for-item-defaults.patch +++ b/patches/server/0687-Attributes-API-for-item-defaults.patch @@ -20,10 +20,10 @@ index 7e3826b271b2db3b48e6e21ac2e66911bf8993aa..393a19335c52b6e63d37aacdfbeff93e + // Paper end } diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java -index 85a0639e8e65f4d085f605f09ca04a706a631d56..408863b137b7bda3f3e654ac3665ddeefb6d9e7b 100644 +index d88515fcaab8672bc1ace12554372a0b6ab57947..ff1f178b69efc4f167cb34095b7fd3acd4d00680 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java +++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java -@@ -492,6 +492,19 @@ public final class CraftMagicNumbers implements UnsafeValues { +@@ -499,6 +499,19 @@ public final class CraftMagicNumbers implements UnsafeValues { return CraftMagicNumbers.getItem(itemToBeRepaired.getType()).isValidRepairItem(CraftItemStack.asNMSCopy(itemToBeRepaired), CraftItemStack.asNMSCopy(repairMaterial)); } diff --git a/patches/server/0827-Get-entity-default-attributes.patch b/patches/server/0827-Get-entity-default-attributes.patch index 00a951c1fb..98147486b9 100644 --- a/patches/server/0827-Get-entity-default-attributes.patch +++ b/patches/server/0827-Get-entity-default-attributes.patch @@ -90,10 +90,10 @@ index 0000000000000000000000000000000000000000..4ecba0b02c2813a890aecc5586987879 + } +} diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java -index 408863b137b7bda3f3e654ac3665ddeefb6d9e7b..dcaa189c17dd928d7a19e820ec2ff521e7243b7a 100644 +index ff1f178b69efc4f167cb34095b7fd3acd4d00680..26273a6b9d49f0e12fe1ff3206c7f017a2f36e44 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java +++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java -@@ -509,6 +509,18 @@ public final class CraftMagicNumbers implements UnsafeValues { +@@ -516,6 +516,18 @@ public final class CraftMagicNumbers implements UnsafeValues { public int getProtocolVersion() { return net.minecraft.SharedConstants.getCurrentVersion().getProtocolVersion(); } diff --git a/patches/server/0839-Add-isCollidable-methods-to-various-places.patch b/patches/server/0839-Add-isCollidable-methods-to-various-places.patch index 681162f8fe..84c0379606 100644 --- a/patches/server/0839-Add-isCollidable-methods-to-various-places.patch +++ b/patches/server/0839-Add-isCollidable-methods-to-various-places.patch @@ -37,10 +37,10 @@ index 7b9e943b391c061782fccd2b8d705ceec8db50fe..966ac60daebb7bb211ab8096fc0c5f33 + // Paper end } diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java -index dcaa189c17dd928d7a19e820ec2ff521e7243b7a..c6bd1c1f973157c1d8a56e8297729cb896785dd6 100644 +index 26273a6b9d49f0e12fe1ff3206c7f017a2f36e44..eb11abe4bc8f06d457da674848b39fc3b3873d1a 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java +++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java -@@ -521,6 +521,12 @@ public final class CraftMagicNumbers implements UnsafeValues { +@@ -528,6 +528,12 @@ public final class CraftMagicNumbers implements UnsafeValues { var supplier = net.minecraft.world.entity.ai.attributes.DefaultAttributes.getSupplier((net.minecraft.world.entity.EntityType<? extends net.minecraft.world.entity.LivingEntity>) net.minecraft.core.Registry.ENTITY_TYPE.get(CraftNamespacedKey.toMinecraft(bukkitEntityKey))); return new io.papermc.paper.attribute.UnmodifiableAttributeMap(supplier); } diff --git a/patches/server/0842-Add-Raw-Byte-Entity-Serialization.patch b/patches/server/0842-Add-Raw-Byte-Entity-Serialization.patch new file mode 100644 index 0000000000..f600a0f9f3 --- /dev/null +++ b/patches/server/0842-Add-Raw-Byte-Entity-Serialization.patch @@ -0,0 +1,80 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Mariell Hoversholm <[email protected]> +Date: Sun, 24 Oct 2021 16:20:31 -0400 +Subject: [PATCH] Add Raw Byte Entity Serialization + + +diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java +index 996f09eaf7d1d6c5e27e2bb2630a6bcb18854172..6d2cc9d1813c862dfd23cb6897d4f4f4845af529 100644 +--- a/src/main/java/net/minecraft/world/entity/Entity.java ++++ b/src/main/java/net/minecraft/world/entity/Entity.java +@@ -2008,6 +2008,15 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource, n + } + } + ++ // Paper start - Entity serialization api ++ public boolean serializeEntity(CompoundTag compound) { ++ List<Entity> pass = new java.util.ArrayList<>(this.getPassengers()); ++ this.passengers = ImmutableList.of(); ++ boolean result = save(compound); ++ this.passengers = ImmutableList.copyOf(pass); ++ return result; ++ } ++ // Paper end + public boolean save(CompoundTag nbt) { + return this.isPassenger() ? false : this.saveAsPassenger(nbt); + } +diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java +index ee50ea695585639d0ff184b675f3fb3b205b9f86..0bd800e1aeda87689a6c56ee6fadda381c74a4ff 100644 +--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java ++++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java +@@ -1276,5 +1276,15 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity { + } + return set; + } ++ ++ @Override ++ public boolean spawnAt(Location location, org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason reason) { ++ Preconditions.checkNotNull(location, "location cannot be null"); ++ Preconditions.checkNotNull(reason, "reason cannot be null"); ++ entity.level = ((CraftWorld) location.getWorld()).getHandle(); ++ entity.setPos(location.getX(), location.getY(), location.getZ()); ++ entity.setRot(location.getYaw(), location.getPitch()); ++ return !entity.valid && entity.level.addEntity(entity, reason); ++ } + // Paper end + } +diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java +index eb11abe4bc8f06d457da674848b39fc3b3873d1a..424f3a9a645d57ad43c52932f5b388b5f146b9f0 100644 +--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java ++++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java +@@ -421,6 +421,29 @@ public final class CraftMagicNumbers implements UnsafeValues { + return CraftItemStack.asCraftMirror(net.minecraft.world.item.ItemStack.of((CompoundTag) converted.getValue())); + } + ++ @Override ++ public byte[] serializeEntity(org.bukkit.entity.Entity entity) { ++ Preconditions.checkNotNull(entity, "null cannot be serialized"); ++ Preconditions.checkArgument(entity instanceof org.bukkit.craftbukkit.entity.CraftEntity, "only CraftEntities can be serialized"); ++ ++ CompoundTag compound = new CompoundTag(); ++ ((org.bukkit.craftbukkit.entity.CraftEntity) entity).getHandle().serializeEntity(compound); ++ return serializeNbtToBytes(compound); ++ } ++ ++ @Override ++ public org.bukkit.entity.Entity deserializeEntity(byte[] data, org.bukkit.World world, boolean preserveUUID) { ++ Preconditions.checkNotNull(data, "null cannot be deserialized"); ++ Preconditions.checkArgument(data.length > 0, "cannot deserialize nothing"); ++ ++ CompoundTag compound = deserializeNbtFromBytes(data); ++ int dataVersion = compound.getInt("DataVersion"); ++ compound = ca.spottedleaf.dataconverter.minecraft.MCDataConverter.convertTag(ca.spottedleaf.dataconverter.minecraft.datatypes.MCTypeRegistry.ENTITY, compound, dataVersion, getDataVersion()); ++ if (!preserveUUID) compound.remove("UUID"); // Generate a new UUID so we don't have to worry about deserializing the same entity twice ++ return net.minecraft.world.entity.EntityType.create(compound, ((org.bukkit.craftbukkit.CraftWorld) world).getHandle()) ++ .orElseThrow(() -> new IllegalArgumentException("An ID was not found for the data. Did you downgrade?")).getBukkitEntity(); ++ } ++ + private byte[] serializeNbtToBytes(CompoundTag compound) { + compound.putInt("DataVersion", getDataVersion()); + java.io.ByteArrayOutputStream outputStream = new java.io.ByteArrayOutputStream(); |