From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Mariell Hoversholm 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 de50f1aa6b7cebf877e18bc3e2ae3ba707edcd4c..3a388ead677be73b0ae4425a05a45cd34e95d586 100644 --- a/src/main/java/net/minecraft/world/entity/Entity.java +++ b/src/main/java/net/minecraft/world/entity/Entity.java @@ -2018,6 +2018,15 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource { } } + // Paper start - Entity serialization api + public boolean serializeEntity(CompoundTag compound) { + List 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 38165a364dfeca6d0fb35e7b3ada92d728ec295b..0c65652866de257d2a9019f910506a6f01757422 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java @@ -1367,5 +1367,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.addFreshEntity(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 30dfdbace0f60074d0cb056d2916331882ade295..7e9b2497f4b8b0f5e55acd6c83593fb42bb4a52d 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java +++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java @@ -485,6 +485,29 @@ public final class CraftMagicNumbers implements UnsafeValues { return CraftItemStack.asCraftMirror(net.minecraft.world.item.ItemStack.of(ca.spottedleaf.dataconverter.minecraft.MCDataConverter.convertTag(ca.spottedleaf.dataconverter.minecraft.datatypes.MCTypeRegistry.ITEM_STACK, compound, dataVersion, getDataVersion()))); } + @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();