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
|
|
|
|
|
2023-06-08 21:35:20 +02:00
|
|
|
== AT ==
|
|
|
|
public net.minecraft.world.entity.Entity setLevel(Lnet/minecraft/world/level/Level;)V
|
2021-11-11 03:53:27 +01:00
|
|
|
|
|
|
|
diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
|
2024-08-31 20:29:50 +02:00
|
|
|
index 836356ecb3c91fd7886ac9ae83f1cc835469e198..f21bf6c8bbf30c59f1588c2105dbd7f80c07a0f1 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
|
2024-07-29 00:28:28 +02:00
|
|
|
@@ -2144,6 +2144,15 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
|
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;
|
|
|
|
+ }
|
2024-01-19 12:30:04 +01:00
|
|
|
+ // Paper end - Entity serialization api
|
2021-11-11 03:53:27 +01:00
|
|
|
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
|
2024-07-29 00:28:28 +02:00
|
|
|
index 7c04eb9e7eb5ff728465b46e3739eb2598ef1204..6fab713531665298d3b03e7960a17ecb1471a6d7 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
|
2024-07-29 00:28:28 +02:00
|
|
|
@@ -1087,6 +1087,18 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity {
|
2021-11-11 03:53:27 +01:00
|
|
|
}
|
2024-04-06 22:38:37 +02:00
|
|
|
// Paper end - tracked players API
|
|
|
|
|
|
|
|
+ // Paper start - raw entity serialization API
|
2021-11-11 03:53:27 +01:00
|
|
|
+ @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");
|
2023-06-08 21:35:20 +02:00
|
|
|
+ this.entity.setLevel(((CraftWorld) location.getWorld()).getHandle());
|
|
|
|
+ this.entity.setPos(location.getX(), location.getY(), location.getZ());
|
|
|
|
+ this.entity.setRot(location.getYaw(), location.getPitch());
|
|
|
|
+ return !this.entity.valid && this.entity.level().addFreshEntity(this.entity, reason);
|
2021-11-11 03:53:27 +01:00
|
|
|
+ }
|
2024-04-06 22:38:37 +02:00
|
|
|
+ // Paper end - raw entity serialization API
|
|
|
|
+
|
2024-03-23 22:26:17 +01:00
|
|
|
// Paper start - missing entity api
|
2024-04-06 22:38:37 +02:00
|
|
|
@Override
|
|
|
|
public boolean isInvisible() { // Paper - moved up from LivingEntity
|
2021-11-11 03:53:27 +01:00
|
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
|
2024-08-31 20:29:50 +02:00
|
|
|
index 95a6f26f75efda7db41db9091107502e8d4e0453..0e849b49329054005c20a5773de2ff15bf3f6e5d 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
|
2024-07-19 13:08:08 +02:00
|
|
|
@@ -488,7 +488,33 @@ public final class CraftMagicNumbers implements UnsafeValues {
|
2024-04-25 07:16:04 +02:00
|
|
|
return CraftItemStack.asCraftMirror(net.minecraft.world.item.ItemStack.parse(MinecraftServer.getServer().registryAccess(), compound).orElseThrow());
|
2021-11-11 03:53:27 +01:00
|
|
|
}
|
|
|
|
|
2024-04-25 07:16:04 +02:00
|
|
|
- private byte[] serializeNbtToBytes(CompoundTag compound) {
|
2021-11-11 03:53:27 +01:00
|
|
|
+ @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");
|
|
|
|
+
|
2024-04-25 07:16:04 +02:00
|
|
|
+ net.minecraft.nbt.CompoundTag compound = new net.minecraft.nbt.CompoundTag();
|
2021-11-11 03:53:27 +01:00
|
|
|
+ ((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");
|
|
|
|
+
|
2024-04-25 07:16:04 +02:00
|
|
|
+ net.minecraft.nbt.CompoundTag compound = deserializeNbtFromBytes(data);
|
2021-11-11 03:53:27 +01:00
|
|
|
+ int dataVersion = compound.getInt("DataVersion");
|
2024-04-25 07:16:04 +02:00
|
|
|
+ compound = (net.minecraft.nbt.CompoundTag) MinecraftServer.getServer().fixerUpper.update(References.ENTITY, new Dynamic<>(NbtOps.INSTANCE, compound), dataVersion, this.getDataVersion()).getValue();
|
2024-01-25 10:54:46 +01:00
|
|
|
+ if (!preserveUUID) {
|
|
|
|
+ // Generate a new UUID so we don't have to worry about deserializing the same entity twice
|
|
|
|
+ compound.remove("UUID");
|
|
|
|
+ }
|
2021-11-11 03:53:27 +01:00
|
|
|
+ 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();
|
|
|
|
+ }
|
|
|
|
+
|
2024-04-25 07:16:04 +02:00
|
|
|
+ private byte[] serializeNbtToBytes(net.minecraft.nbt.CompoundTag compound) {
|
2021-11-11 03:53:27 +01:00
|
|
|
compound.putInt("DataVersion", getDataVersion());
|
|
|
|
java.io.ByteArrayOutputStream outputStream = new java.io.ByteArrayOutputStream();
|
2024-04-25 07:16:04 +02:00
|
|
|
try {
|