2021-11-11 03:53:27 +01:00
|
|
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
|
|
From: Mariell Hoversholm <proximyst@proximyst.com>
|
|
|
|
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
|
2022-11-03 22:03:31 +01:00
|
|
|
index 3abc8894f575e246201845b757dbef2699663859..dcbbf53b23a9347470986f052ecba0149d7503aa 100644
|
2021-11-11 03:53:27 +01:00
|
|
|
--- a/src/main/java/net/minecraft/world/entity/Entity.java
|
|
|
|
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
|
2022-10-11 14:43:10 +02:00
|
|
|
@@ -1985,6 +1985,15 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
|
2021-11-11 03:53:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
+ // 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
|
2022-11-26 03:21:40 +01:00
|
|
|
index cc86c455588f33463100a58f3be6f21e26dda5da..99a9ddbeee75611c5bab8d0e8952a9291bf73a16 100644
|
2021-11-11 03:53:27 +01:00
|
|
|
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
|
|
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
|
2022-11-26 03:21:40 +01:00
|
|
|
@@ -1330,5 +1330,15 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity {
|
2021-11-11 03:53:27 +01:00
|
|
|
}
|
|
|
|
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());
|
2021-11-25 09:10:26 +01:00
|
|
|
+ return !entity.valid && entity.level.addFreshEntity(entity, reason);
|
2021-11-11 03:53:27 +01:00
|
|
|
+ }
|
|
|
|
// Paper end
|
|
|
|
}
|
|
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
|
2022-11-26 03:21:40 +01:00
|
|
|
index 8d1212436f3a70986dae9868e80fa5b7fe893541..2f71e549c5c3f7e8ebfd10a7b0c8601fcc1d9c49 100644
|
2021-11-11 03:53:27 +01:00
|
|
|
--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
|
|
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
|
2022-11-23 05:53:50 +01:00
|
|
|
@@ -462,6 +462,30 @@ public final class CraftMagicNumbers implements UnsafeValues {
|
2021-11-11 03:53:27 +01:00
|
|
|
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");
|
2021-11-25 08:47:39 +01:00
|
|
|
+ Dynamic<Tag> converted = DataFixers.getDataFixer().update(References.ENTITY_TREE, new Dynamic<>(NbtOps.INSTANCE, compound), dataVersion, getDataVersion());
|
|
|
|
+ compound = (CompoundTag) converted.getValue();
|
2021-11-11 03:53:27 +01:00
|
|
|
+ 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();
|