Files
Purpur/patches/server/0170-Add-unsafe-Entity-serialization-API.patch
BillyGalbreath 296267da93 Updated Upstream (Paper)
Upstream has released updates that appear to apply and compile correctly

Paper Changes:
eb11845f8 Fix creating worlds with "invalid" names (Fixes #5331)
e4d8a6279 Implement Keyed on World
bcb63dab7 [CI-SKIP] [Auto] Rebuild Patches
48342b06c Allow signs that are inside of the spawn protection to be right clicked to use their run_command tag
c229f90c1 Add Block#isValidTool
20e709c1d Add recipe to cook events
2dcf8bff4 legacy formatting will be the death of me
f597fea0d legacy formatting is worse than walking around in wet socks
7f72c4675 Use implementation-provided legacy serializer for events
27a8d99ec Adventure 4.7.0
e65bd35a1 Respect teams in legacy chat name if configured (#5321)
b31089a92 Updated Upstream (Bukkit/CraftBukkit/Spigot) (#5325)
a52b30814 Fix title swapping fadeIn and stay
54ec85949 Prevent grindstones from overstacking items
d7795080c Fix NPE for AIR in meta operations in ItemStack
2e70796c7 [CI-SKIP] Improve documentation of PreCreatureSpawnEvent (#5244)
7bb92e750 [CI-SKIP] Add JavaDoc links to Tag class pointing to custom Paper tags (#5285)
28cd686bf fix per-world difficulty command (#5306)
be7cde2c7 [CI-SKIP] Always check PATH for JDK (#5315)
2021-03-09 17:52:38 -06:00

130 lines
7.6 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Mariell Hoversholm <proximyst@proximyst.com>
Date: Sat, 9 Jan 2021 21:22:58 +0100
Subject: [PATCH] Add unsafe Entity serialization API
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
diff --git a/src/main/java/net/minecraft/server/EntityTypes.java b/src/main/java/net/minecraft/server/EntityTypes.java
index 73cf30786d2b4d6f87be5f9aad87c3edb594d18e..f5fb3b3c08a05bede7ed1141c018bd0d16a19244 100644
--- a/src/main/java/net/minecraft/server/EntityTypes.java
+++ b/src/main/java/net/minecraft/server/EntityTypes.java
@@ -394,6 +394,7 @@ public class EntityTypes<T extends Entity> {
return this.bf.create(this, world);
}
+ public static Optional<Entity> loadEntityFixedData(NBTTagCompound nbtTagCompound, World world) { return a(nbtTagCompound, world); } // Purpur - OBFHELPER
public static Optional<Entity> a(NBTTagCompound nbttagcompound, World world) {
return SystemUtils.a(a(nbttagcompound).map((entitytypes) -> {
return entitytypes.a(world);
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
index 4c592f6d4d6cbeb4c3225e7794f4b1d6c8d62394..b441dac8f31b679aa7e36d23848d34e298e6abba 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
@@ -1199,5 +1199,12 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity {
public boolean isRidableInWater() {
return getHandle().isRidableInWater();
}
+
+ @Override
+ public boolean spawnAt(Location location, org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason spawnReason) {
+ entity.world = ((CraftWorld) location.getWorld()).getHandle();
+ entity.setLocation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
+ return !entity.valid && entity.world.addEntity(entity, spawnReason);
+ }
// Purpur end
}
diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
index 7de7afae711800154735411947ff140c76d5171f..b14e9a2cd6e849fe91a2512f379036a284323420 100644
--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
+++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
@@ -390,9 +390,14 @@ public final class CraftMagicNumbers implements UnsafeValues {
Preconditions.checkNotNull(item, "null cannot be serialized");
Preconditions.checkArgument(item.getType() != Material.AIR, "air cannot be serialized");
+ // Purpur start - rework NBT <-> bytes
+ return serializeNbtToBytes(CraftItemStack.asNMSCopy(item).save(new NBTTagCompound()), true);
+ }
+
+ public byte[] serializeNbtToBytes(NBTTagCompound compound, boolean addDataVersion) {
+ if (addDataVersion) compound.setInt("DataVersion", getDataVersion());
java.io.ByteArrayOutputStream outputStream = new java.io.ByteArrayOutputStream();
- NBTTagCompound compound = (item instanceof CraftItemStack ? ((CraftItemStack) item).getHandle() : CraftItemStack.asNMSCopy(item)).save(new NBTTagCompound());
- compound.setInt("DataVersion", getDataVersion());
+ // Purpur end
try {
net.minecraft.server.NBTCompressedStreamTools.writeNBT(
compound,
@@ -405,26 +410,58 @@ public final class CraftMagicNumbers implements UnsafeValues {
return outputStream.toByteArray();
}
+ public static DynamicOpsNBT getDynamicOpsNbtInstance() { return DynamicOpsNBT.a; } // Purpur - OBFHELPER - keeping out of the class because it's FULL of decompile errors
@Override
public ItemStack deserializeItem(byte[] data) {
Preconditions.checkNotNull(data, "null cannot be deserialized");
Preconditions.checkArgument(data.length > 0, "cannot deserialize nothing");
+ // Purpur start - rework NBT <-> bytes
+ NBTTagCompound compound = deserializeNbtFromBytes(data, true);
+ Dynamic<NBTBase> converted = DataConverterRegistry.getDataFixer().update(DataConverterTypes.ITEM_STACK, new Dynamic<>(getDynamicOpsNbtInstance(), compound), compound.getInt("DataVersion"), getDataVersion()); // TODO: obfhelper
+ return net.minecraft.server.ItemStack.fromCompound((NBTTagCompound) converted.getValue()).asBukkitMirror();
+ }
+
+ public NBTTagCompound deserializeNbtFromBytes(byte[] data, boolean verifyDataVersion) {
+ // Purpur end
try {
NBTTagCompound compound = net.minecraft.server.NBTCompressedStreamTools.readNBT(
new java.io.ByteArrayInputStream(data)
);
+ if (verifyDataVersion) { // Purpur
int dataVersion = compound.getInt("DataVersion");
Preconditions.checkArgument(dataVersion <= getDataVersion(), "Newer version! Server downgrades are not supported!");
- Dynamic<NBTBase> converted = DataConverterRegistry.getDataFixer().update(DataConverterTypes.ITEM_STACK, new Dynamic<NBTBase>(DynamicOpsNBT.a, compound), dataVersion, getDataVersion());
- return CraftItemStack.asCraftMirror(net.minecraft.server.ItemStack.fromCompound((NBTTagCompound) converted.getValue()));
+ } // Purpur
+ return compound; // Purpur
} catch (IOException ex) {
com.destroystokyo.paper.util.SneakyThrow.sneaky(ex);
throw new RuntimeException();
}
}
+ // Purpur start
+ @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, "non-CraftEntity cannot be serialized");
+ NBTTagCompound compound = new NBTTagCompound();
+ compound.setString("id", ((org.bukkit.craftbukkit.entity.CraftEntity) entity).getHandle().getMinecraftKeyString());
+ return serializeNbtToBytes(((org.bukkit.craftbukkit.entity.CraftEntity) entity).getHandle().save(compound), true);
+ }
+
+ @Override
+ public org.bukkit.entity.Entity deserializeEntity(byte[] data, org.bukkit.World world) {
+ NBTTagCompound compound = deserializeNbtFromBytes(data, true);
+ Dynamic<NBTBase> converted = DataConverterRegistry.getDataFixer().update(DataConverterTypes.ENTITY, new Dynamic<>(getDynamicOpsNbtInstance(), compound), compound.getInt("DataVersion"), getDataVersion());
+ compound = (NBTTagCompound) converted.getValue();
+ compound.remove("UUID"); // Make the server make a new UUID for this entity; makes entities always spawnable.
+ return net.minecraft.server.EntityTypes.loadEntityFixedData(compound, ((org.bukkit.craftbukkit.CraftWorld) world).getHandle())
+ .orElseThrow(() -> new IllegalArgumentException("unknown ID was found for the data; did you downgrade?"))
+ .getBukkitEntity();
+ }
+ // Pupur end
+
@Override
public String getTranslationKey(Material mat) {
if (mat.isBlock()) {