mirror of
https://github.com/PurpurMC/Purpur.git
synced 2026-02-18 00:47:42 +01:00
Upstream has released updates that appear to apply and compile correctly Paper Changes: 8c74d3126 Updated Upstream (Bukkit) (#5359) fd3c66a91 bug #5362 - correctly pass "render type" when registering a new scoreboard objective 39c487b37 Add per-command perms for paper command cdbf2578c Add Item Rarity API (#5352) d80e43647 [CI-SKIP] Removal from the MIT list (#5345) Tuinity Changes: aea6b8347 Merge dev/playerchunkloading 722c7ca8a Use hash table for maintaing changed block set 98ae59d85 Custom table implementation for blockstate state lookups 8b8704fb6 Oprimise map impl for tracked players ea71d6ba4 Optimise snow & ice in chunk ticking 9871d4ce5 Remove chunk lookup & lambda allocation from counting mobs 5a4a35f3e Add patreon 7d93d9618 Refactor data management for region manager c3035219f Change license from MIT to LGPLv3 Airplane Changes: 580f380b6 Updated Upstream (Tuinity) 82253fd36 Early return optimization for target finding 9572643bb Cache entityhuman display name 5df98254f Remove iterators from inventory contains 18d2be193 Merge pull request #14 from violetwtf/patch-1 f716d4c33 Merge pull request #13 from violetwtf/master 128cbe519 Reduce entity chunk ticking checks from 3 to 1 03ac0933b Skip copying unloading tile entities 97dd027b5 Smaller pool size for tracking 9e9f57be4 Only set up Flare if token is available
130 lines
7.6 KiB
Diff
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 4de08939f8a5de464e510e47228291fc513b8104..b38e7c40fe9cb56cb37769ce3a8c7969e2ee5372 100644
|
|
--- a/src/main/java/net/minecraft/server/EntityTypes.java
|
|
+++ b/src/main/java/net/minecraft/server/EntityTypes.java
|
|
@@ -396,6 +396,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 81cff601a5161c30df90de3cefc18d72cd7ee347..980696490709d04741c1617f528357100696d0e5 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()) {
|