mirror of
https://github.com/PaperMC/Paper.git
synced 2024-12-29 12:27:59 +01:00
Add Raw Byte Entity Serialization (#6826)
This commit is contained in:
parent
5dc93227ab
commit
d19cd1ac56
62
patches/api/Add-Raw-Byte-Entity-Serialization.patch
Normal file
62
patches/api/Add-Raw-Byte-Entity-Serialization.patch
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
||||||
|
From: Mariell Hoversholm <proximyst@proximyst.com>
|
||||||
|
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 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
|
||||||
|
--- a/src/main/java/org/bukkit/UnsafeValues.java
|
||||||
|
+++ b/src/main/java/org/bukkit/UnsafeValues.java
|
||||||
|
@@ -0,0 +0,0 @@ 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 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
|
||||||
|
--- a/src/main/java/org/bukkit/entity/Entity.java
|
||||||
|
+++ b/src/main/java/org/bukkit/entity/Entity.java
|
||||||
|
@@ -0,0 +0,0 @@ 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
|
||||||
|
}
|
80
patches/server/Add-Raw-Byte-Entity-Serialization.patch
Normal file
80
patches/server/Add-Raw-Byte-Entity-Serialization.patch
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
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
|
||||||
|
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
|
||||||
|
--- a/src/main/java/net/minecraft/world/entity/Entity.java
|
||||||
|
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
|
||||||
|
@@ -0,0 +0,0 @@ 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 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
|
||||||
|
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
|
||||||
|
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
|
||||||
|
@@ -0,0 +0,0 @@ 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 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
|
||||||
|
--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
|
||||||
|
+++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
|
||||||
|
@@ -0,0 +0,0 @@ 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();
|
@ -19,9 +19,23 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
|||||||
+ Preconditions.checkNotNull(item, "null cannot be serialized");
|
+ Preconditions.checkNotNull(item, "null cannot be serialized");
|
||||||
+ Preconditions.checkArgument(item.getType() != Material.AIR, "air cannot be serialized");
|
+ Preconditions.checkArgument(item.getType() != Material.AIR, "air cannot be serialized");
|
||||||
+
|
+
|
||||||
+ java.io.ByteArrayOutputStream outputStream = new java.io.ByteArrayOutputStream();
|
+ return serializeNbtToBytes((item instanceof CraftItemStack ? ((CraftItemStack) item).handle : CraftItemStack.asNMSCopy(item)).save(new CompoundTag()));
|
||||||
+ CompoundTag compound = (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());
|
+ compound.putInt("DataVersion", getDataVersion());
|
||||||
|
+ java.io.ByteArrayOutputStream outputStream = new java.io.ByteArrayOutputStream();
|
||||||
+ try {
|
+ try {
|
||||||
+ net.minecraft.nbt.NbtIo.writeCompressed(
|
+ net.minecraft.nbt.NbtIo.writeCompressed(
|
||||||
+ compound,
|
+ compound,
|
||||||
@ -30,28 +44,21 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
|||||||
+ } catch (IOException ex) {
|
+ } catch (IOException ex) {
|
||||||
+ throw new RuntimeException(ex);
|
+ throw new RuntimeException(ex);
|
||||||
+ }
|
+ }
|
||||||
+
|
|
||||||
+ return outputStream.toByteArray();
|
+ return outputStream.toByteArray();
|
||||||
+ }
|
+ }
|
||||||
+
|
+
|
||||||
+ @Override
|
+ private CompoundTag deserializeNbtFromBytes(byte[] data) {
|
||||||
+ public ItemStack deserializeItem(byte[] data) {
|
+ CompoundTag compound;
|
||||||
+ Preconditions.checkNotNull(data, "null cannot be deserialized");
|
|
||||||
+ Preconditions.checkArgument(data.length > 0, "cannot deserialize nothing");
|
|
||||||
+
|
|
||||||
+ try {
|
+ try {
|
||||||
+ CompoundTag compound = net.minecraft.nbt.NbtIo.readCompressed(
|
+ compound = net.minecraft.nbt.NbtIo.readCompressed(
|
||||||
+ new java.io.ByteArrayInputStream(data)
|
+ 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) {
|
+ } catch (IOException ex) {
|
||||||
+ com.destroystokyo.paper.util.SneakyThrow.sneaky(ex);
|
+ throw new RuntimeException(ex);
|
||||||
+ throw new RuntimeException();
|
|
||||||
+ }
|
+ }
|
||||||
|
+ int dataVersion = compound.getInt("DataVersion");
|
||||||
|
+ Preconditions.checkArgument(dataVersion <= getDataVersion(), "Newer version! Server downgrades are not supported!");
|
||||||
|
+ return compound;
|
||||||
+ }
|
+ }
|
||||||
// Paper end
|
// Paper end
|
||||||
|
|
||||||
|
@ -46,8 +46,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
|||||||
--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
|
--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
|
||||||
+++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
|
+++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
|
||||||
@@ -0,0 +0,0 @@ public final class CraftMagicNumbers implements UnsafeValues {
|
@@ -0,0 +0,0 @@ public final class CraftMagicNumbers implements UnsafeValues {
|
||||||
throw new RuntimeException();
|
Preconditions.checkArgument(dataVersion <= getDataVersion(), "Newer version! Server downgrades are not supported!");
|
||||||
}
|
return compound;
|
||||||
}
|
}
|
||||||
+
|
+
|
||||||
+ @Override
|
+ @Override
|
||||||
|
Loading…
Reference in New Issue
Block a user