Expand on entity serialization API (#11807)

This commit is contained in:
SoSeDiK
2024-12-27 01:08:00 +02:00
committed by GitHub
parent 0efd3012c9
commit aac246ae29
8 changed files with 275 additions and 77 deletions

View File

@@ -46,6 +46,7 @@ import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.entity.Pose;
import org.bukkit.entity.SpawnCategory;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityRemoveEvent;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
@@ -955,7 +956,7 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity {
@Override
public String getAsString() {
CompoundTag tag = new CompoundTag();
if (!this.getHandle().saveAsPassenger(tag, false)) {
if (!this.getHandle().saveAsPassenger(tag, false, false, false)) {
return null;
}
@@ -988,7 +989,7 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity {
private Entity copy(net.minecraft.world.level.Level level) {
CompoundTag compoundTag = new CompoundTag();
this.getHandle().saveAsPassenger(compoundTag, false);
this.getHandle().saveAsPassenger(compoundTag, false, true, true);
return net.minecraft.world.entity.EntityType.loadEntityRecursive(compoundTag, level, EntitySpawnReason.LOAD, java.util.function.Function.identity());
}
@@ -1224,17 +1225,19 @@ 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) {
public boolean spawnAt(Location location, 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);
final boolean spawned = !this.entity.valid && this.entity.level().addFreshEntity(this.entity, reason);
if (!spawned) return false; // Do not attempt to spawn rest if root was not spawned in
// Like net.minecraft.world.level.ServerLevelAccessor.addFreshEntityWithPassengers(net.minecraft.world.entity.Entity, org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason)
this.entity.getIndirectPassengers().forEach(e -> e.level().addFreshEntity(e, reason));
return true;
}
// Paper end - raw entity serialization API
// Paper start - entity powdered snow API
@Override

View File

@@ -66,7 +66,7 @@ public class CraftEntitySnapshot implements EntitySnapshot {
public static CraftEntitySnapshot create(CraftEntity entity) {
CompoundTag tag = new CompoundTag();
if (!entity.getHandle().saveAsPassenger(tag, false)) {
if (!entity.getHandle().saveAsPassenger(tag, false, false, false)) {
return null;
}