Add Raw Byte Entity Serialization

== AT ==
public net.minecraft.world.entity.Entity setLevel(Lnet/minecraft/world/level/Level;)V
This commit is contained in:
Mariell Hoversholm
2021-10-24 16:20:31 -04:00
parent ef5b6401b3
commit 33aad47ee1
3 changed files with 115 additions and 64 deletions

View File

@@ -1083,6 +1083,18 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity {
}
// Paper end - tracked players API
// Paper start - raw entity serialization API
@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");
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);
}
// Paper end - raw entity serialization API
// Paper start - missing entity api
@Override
public boolean isInvisible() { // Paper - moved up from LivingEntity

View File

@@ -510,7 +510,33 @@ public final class CraftMagicNumbers implements UnsafeValues {
return CraftItemStack.asCraftMirror(net.minecraft.world.item.ItemStack.parse(MinecraftServer.getServer().registryAccess(), compound).orElseThrow());
}
private byte[] serializeNbtToBytes(CompoundTag compound) {
@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");
net.minecraft.nbt.CompoundTag compound = new net.minecraft.nbt.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");
net.minecraft.nbt.CompoundTag compound = deserializeNbtFromBytes(data);
int dataVersion = compound.getInt("DataVersion");
compound = (net.minecraft.nbt.CompoundTag) MinecraftServer.getServer().fixerUpper.update(References.ENTITY, new Dynamic<>(NbtOps.INSTANCE, compound), dataVersion, this.getDataVersion()).getValue();
if (!preserveUUID) {
// Generate a new UUID so we don't have to worry about deserializing the same entity twice
compound.remove("UUID");
}
return net.minecraft.world.entity.EntityType.create(compound, ((org.bukkit.craftbukkit.CraftWorld) world).getHandle(), net.minecraft.world.entity.EntitySpawnReason.LOAD)
.orElseThrow(() -> new IllegalArgumentException("An ID was not found for the data. Did you downgrade?")).getBukkitEntity();
}
private byte[] serializeNbtToBytes(net.minecraft.nbt.CompoundTag compound) {
compound.putInt("DataVersion", getDataVersion());
java.io.ByteArrayOutputStream outputStream = new java.io.ByteArrayOutputStream();
try {