testserver/patches/server/0026-Entity-Origin-API.patch

160 lines
7.4 KiB
Diff
Raw Normal View History

2021-06-11 12:02:28 +00: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 0750cf27c1b2cac723d68d0e6c2204cbb5795571..0eb0229125fd027025b6b44dfceddf626fc05399 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/server/level/ServerLevel.java
+++ b/src/main/java/net/minecraft/server/level/ServerLevel.java
@@ -2027,6 +2027,15 @@ public class ServerLevel extends Level implements WorldGenLevel {
2021-06-11 12:02:28 +00:00
}
2021-06-18 03:37:23 +00:00
entity.valid = true; // CraftBukkit
2021-06-11 12:02:28 +00: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 12:02:28 +00: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 12:02:28 +00:00
+ // Paper end
}
2021-06-18 03:37:23 +00:00
public void onTrackingEnd(Entity entity) {
2021-06-11 12:02:28 +00:00
diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
index d778209604c71a0728748dac91a5cb993f4357b7..ae6c1abe1443f3758fce35a20387f0ed93748af5 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
2022-03-30 20:28:38 +00:00
@@ -300,7 +300,27 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
public long activatedTick = Integer.MIN_VALUE;
public void inactiveTick() { }
// Spigot end
+ // 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 07:29:05 +00:00
+ // Paper end
public float getBukkitYaw() {
return this.yRot;
}
2022-03-30 20:28:38 +00:00
@@ -1822,6 +1842,15 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
2021-06-11 22:37:16 +00:00
this.bukkitEntity.storeBukkitValues(nbt);
2021-06-11 12:02:28 +00: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-11 22:37:16 +00:00
+ nbt.put("Paper.Origin", this.newDoubleList(origin.getX(), origin.getY(), origin.getZ()));
2021-06-11 12:02:28 +00:00
+ }
+ // Paper end
2021-06-11 22:37:16 +00:00
return nbt;
2021-06-11 12:02:28 +00:00
} catch (Throwable throwable) {
CrashReport crashreport = CrashReport.forThrowable(throwable, "Saving entity NBT");
@@ -1946,6 +1975,20 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
2021-06-11 12:02:28 +00:00
}
// CraftBukkit end
+ // Paper start - Restore the entity's origin location
2021-06-11 22:37:16 +00:00
+ ListTag originTag = nbt.getList("Paper.Origin", 6);
2021-06-11 12:02:28 +00:00
+ if (!originTag.isEmpty()) {
+ UUID originWorld = null;
2021-06-11 22:37:16 +00:00
+ if (nbt.contains("Paper.OriginWorld")) {
+ originWorld = nbt.getUUID("Paper.OriginWorld");
+ } else if (this.level != null) {
+ originWorld = this.level.getWorld().getUID();
2021-06-11 12:02:28 +00:00
+ }
+ this.originWorld = originWorld;
2021-06-17 17:11:00 +00:00
+ origin = new org.bukkit.util.Vector(originTag.getDouble(0), originTag.getDouble(1), originTag.getDouble(2));
2021-06-11 12:02:28 +00: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 26ab6d0c98560e4dfebbad3482fd308861818e30..242e02646b8584a8d2a512374ad03729661d584f 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/world/entity/item/FallingBlockEntity.java
+++ b/src/main/java/net/minecraft/world/entity/item/FallingBlockEntity.java
@@ -337,6 +337,14 @@ public class FallingBlockEntity extends Entity {
2021-06-11 12:02:28 +00:00
this.blockState = Blocks.SAND.defaultBlockState();
}
+ // Paper start - Try and load origin location from the old NBT tags for backwards compatibility
2021-06-11 22:37:16 +00: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 12:02:28 +00:00
+ }
+ // Paper end
}
2021-11-23 12:15:10 +00:00
public void setHurtsEntities(float fallHurtAmount, int fallHurtMax) {
2021-06-11 12:02:28 +00: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
2021-11-23 12:15:10 +00:00
index 1f61dcc3d6f33f69fbebaaaee0554403c3e13adf..7135e22a2ee274eaef52804d60f15002176ff1db 100644
2021-06-11 12:02:28 +00: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-11 22:37:16 +00:00
@@ -120,6 +120,14 @@ public class PrimedTnt extends Entity {
2021-06-11 12:02:28 +00:00
@Override
2021-06-11 22:37:16 +00:00
protected void readAdditionalSaveData(CompoundTag nbt) {
this.setFuse(nbt.getShort("Fuse"));
2021-06-11 12:02:28 +00:00
+ // Paper start - Try and load origin location from the old NBT tags for backwards compatibility
2021-06-11 22:37:16 +00: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 12:02:28 +00: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 4b7ed15279bbdd116d993f190094f80c888aed69..fdbcf4989f72e1604a2841f565adfeebf8d45622 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
@@ -1154,4 +1154,21 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity {
2021-06-11 22:37:16 +00:00
return this.spigot;
2021-06-11 12:02:28 +00: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 12:02:28 +00:00
+ }
+ // Paper end
}