Paper/patches/server/0025-Entity-Origin-API.patch

160 lines
7.6 KiB
Diff
Raw Normal View History

2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Byteflux <byte@byteflux.net>
Date: Tue, 1 Mar 2016 23:45:08 -0600
Subject: [PATCH] Entity Origin API
diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java
index 0db1607dc6bbf0def58bdc14fca33dbaa48a8e40..491bbaac18d006f9476c6aa2c584ec6efab23296 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/level/ServerLevel.java
+++ b/src/main/java/net/minecraft/server/level/ServerLevel.java
@@ -1855,6 +1855,15 @@ public class ServerLevel extends Level implements WorldGenLevel {
2021-06-11 14:02:28 +02:00
}
2021-06-18 05:37:23 +02:00
entity.valid = true; // CraftBukkit
2021-06-11 14:02:28 +02:00
+ // Paper start - Set origin location when the entity is being added to the world
+ if (entity.getOriginVector() == null) {
+ entity.setOrigin(entity.getBukkitEntity().getLocation());
2021-06-11 14:02:28 +02:00
+ }
+ // Default to current world if unknown, gross assumption but entities rarely change world
+ if (entity.getOriginWorld() == null) {
+ entity.setOrigin(entity.getOriginVector().toLocation(getWorld()));
+ }
2021-06-11 14:02:28 +02:00
+ // Paper end
}
2021-06-18 05:37:23 +02:00
public void onTrackingEnd(Entity entity) {
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
index f1230adeaf399424630412975310cb3e31ed001f..628ac6ab6b5b12c4baf451e4cecac5ce4a62b964 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
@@ -281,6 +281,27 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource, n
2021-06-11 14:02:28 +02:00
public org.bukkit.projectiles.ProjectileSource projectileSource; // For projectiles only
public boolean forceExplosionKnockback; // SPIGOT-949
public boolean persistentInvisibility = false;
+ // Paper start
+ @javax.annotation.Nullable
+ private org.bukkit.util.Vector origin;
+ @javax.annotation.Nullable
+ private UUID originWorld;
+
+ public void setOrigin(@javax.annotation.Nonnull Location location) {
+ this.origin = location.toVector();
+ this.originWorld = location.getWorld().getUID();
+ }
+
+ @javax.annotation.Nullable
+ public org.bukkit.util.Vector getOriginVector() {
+ return this.origin != null ? this.origin.clone() : null;
+ }
+
+ @javax.annotation.Nullable
+ public UUID getOriginWorld() {
+ return this.originWorld;
+ }
2021-06-16 09:29:05 +02:00
+ // Paper end
2021-06-11 14:02:28 +02:00
// Spigot start
public final org.spigotmc.ActivationRange.ActivationType activationType = org.spigotmc.ActivationRange.initializeEntityActivationType(this);
public final boolean defaultActivationState;
@@ -1813,6 +1834,15 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource, n
2021-06-12 00:37:16 +02:00
this.bukkitEntity.storeBukkitValues(nbt);
2021-06-11 14:02:28 +02:00
}
// CraftBukkit end
+ // Paper start - Save the entity's origin location
+ if (this.origin != null) {
+ UUID originWorld = this.originWorld != null ? this.originWorld : this.level != null ? this.level.getWorld().getUID() : null;
+ if (originWorld != null) {
+ nbt.putUUID("Paper.OriginWorld", originWorld);
+ }
2021-06-12 00:37:16 +02:00
+ nbt.put("Paper.Origin", this.newDoubleList(origin.getX(), origin.getY(), origin.getZ()));
2021-06-11 14:02:28 +02:00
+ }
+ // Paper end
2021-06-12 00:37:16 +02:00
return nbt;
2021-06-11 14:02:28 +02:00
} catch (Throwable throwable) {
CrashReport crashreport = CrashReport.forThrowable(throwable, "Saving entity NBT");
@@ -1939,6 +1969,20 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource, n
2021-06-11 14:02:28 +02:00
}
// CraftBukkit end
+ // Paper start - Restore the entity's origin location
2021-06-12 00:37:16 +02:00
+ ListTag originTag = nbt.getList("Paper.Origin", 6);
2021-06-11 14:02:28 +02:00
+ if (!originTag.isEmpty()) {
+ UUID originWorld = null;
2021-06-12 00:37:16 +02:00
+ if (nbt.contains("Paper.OriginWorld")) {
+ originWorld = nbt.getUUID("Paper.OriginWorld");
+ } else if (this.level != null) {
+ originWorld = this.level.getWorld().getUID();
2021-06-11 14:02:28 +02:00
+ }
+ this.originWorld = originWorld;
2021-06-17 19:11:00 +02:00
+ origin = new org.bukkit.util.Vector(originTag.getDouble(0), originTag.getDouble(1), originTag.getDouble(2));
2021-06-11 14:02:28 +02:00
+ }
+ // Paper end
+
} catch (Throwable throwable) {
CrashReport crashreport = CrashReport.forThrowable(throwable, "Loading entity NBT");
CrashReportCategory crashreportsystemdetails = crashreport.addCategory("Entity being loaded");
diff --git a/src/main/java/net/minecraft/world/entity/item/FallingBlockEntity.java b/src/main/java/net/minecraft/world/entity/item/FallingBlockEntity.java
index 91c0e425de193be1e4e9779d1c92c8ea577e29e0..1d538f490c7aa48991446fb55c7f0916bb5d5e29 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/entity/item/FallingBlockEntity.java
+++ b/src/main/java/net/minecraft/world/entity/item/FallingBlockEntity.java
@@ -330,6 +330,14 @@ public class FallingBlockEntity extends Entity {
2021-06-11 14:02:28 +02:00
this.blockState = Blocks.SAND.defaultBlockState();
}
+ // Paper start - Try and load origin location from the old NBT tags for backwards compatibility
2021-06-12 00:37:16 +02:00
+ if (nbt.contains("SourceLoc_x")) {
+ int srcX = nbt.getInt("SourceLoc_x");
+ int srcY = nbt.getInt("SourceLoc_y");
+ int srcZ = nbt.getInt("SourceLoc_z");
+ this.setOrigin(new org.bukkit.Location(level.getWorld(), srcX, srcY, srcZ));
2021-06-11 14:02:28 +02:00
+ }
+ // Paper end
}
2021-06-12 00:37:16 +02:00
public Level getLevel() {
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/world/entity/item/PrimedTnt.java b/src/main/java/net/minecraft/world/entity/item/PrimedTnt.java
index 394164f50256ad9a167e15531a9202875abb6cb6..8ad1b3cb16533d62deda643ce0cdda308743f78e 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/entity/item/PrimedTnt.java
+++ b/src/main/java/net/minecraft/world/entity/item/PrimedTnt.java
2021-06-12 00:37:16 +02:00
@@ -120,6 +120,14 @@ public class PrimedTnt extends Entity {
2021-06-11 14:02:28 +02:00
@Override
2021-06-12 00:37:16 +02:00
protected void readAdditionalSaveData(CompoundTag nbt) {
this.setFuse(nbt.getShort("Fuse"));
2021-06-11 14:02:28 +02:00
+ // Paper start - Try and load origin location from the old NBT tags for backwards compatibility
2021-06-12 00:37:16 +02:00
+ if (nbt.contains("SourceLoc_x")) {
+ int srcX = nbt.getInt("SourceLoc_x");
+ int srcY = nbt.getInt("SourceLoc_y");
+ int srcZ = nbt.getInt("SourceLoc_z");
+ this.setOrigin(new org.bukkit.Location(level.getWorld(), srcX, srcY, srcZ));
2021-06-11 14:02:28 +02:00
+ }
+ // Paper end
}
@Nullable
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
index 3f7e976d5490768d71e8ce5344d3d150039cdc21..118ba56a539471bded0cb42334576d5394aee4ab 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
@@ -1121,4 +1121,21 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity {
2021-06-12 00:37:16 +02:00
return this.spigot;
2021-06-11 14:02:28 +02:00
}
// Spigot end
+
+ // Paper start
+ @Override
+ public Location getOrigin() {
+ Vector originVector = this.getHandle().getOriginVector();
+ if (originVector == null) {
+ return null;
+ }
+ World world = this.getWorld();
+ if (this.getHandle().getOriginWorld() != null) {
+ world = org.bukkit.Bukkit.getWorld(this.getHandle().getOriginWorld());
+ }
+
+ //noinspection ConstantConditions
+ return originVector.toLocation(world);
2021-06-11 14:02:28 +02:00
+ }
+ // Paper end
}