aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/0033-Entity-Origin-API.patch
blob: ab8af63f251676b3e94274a68348bdcfc52e8b42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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 9969b66e39fb6ce8a9ba24ccbd7ef5d3e363197f..363f94a2fb54b7a55f392cc65a6e4d68f313fb07 100644
--- a/src/main/java/net/minecraft/server/level/ServerLevel.java
+++ b/src/main/java/net/minecraft/server/level/ServerLevel.java
@@ -2389,6 +2389,15 @@ public class ServerLevel extends Level implements WorldGenLevel {
             entity.updateDynamicGameEventListener(DynamicGameEventListener::add);
             entity.inWorld = true; // CraftBukkit - Mark entity as in world
             entity.valid = true; // CraftBukkit
+            // Paper start - Set origin location when the entity is being added to the world
+            if (entity.getOriginVector() == null) {
+                entity.setOrigin(entity.getBukkitEntity().getLocation());
+            }
+            // Default to current world if unknown, gross assumption but entities rarely change world
+            if (entity.getOriginWorld() == null) {
+                entity.setOrigin(entity.getOriginVector().toLocation(getWorld()));
+            }
+            // Paper end
         }
 
         public void onTrackingEnd(Entity entity) {
diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
index fa3d294b36eb0c23b293466fc72bd2970a452b55..9f3598371c854ec74f3b5752c9779d6c0d99ec39 100644
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
@@ -317,7 +317,27 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource, S
     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;
+    }
+    // Paper end
     public float getBukkitYaw() {
         return this.yRot;
     }
@@ -2093,6 +2113,15 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource, S
                 this.bukkitEntity.storeBukkitValues(nbttagcompound);
             }
             // 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) {
+                    nbttagcompound.putUUID("Paper.OriginWorld", originWorld);
+                }
+                nbttagcompound.put("Paper.Origin", this.newDoubleList(origin.getX(), origin.getY(), origin.getZ()));
+            }
+            // Paper end
             return nbttagcompound;
         } catch (Throwable throwable) {
             CrashReport crashreport = CrashReport.forThrowable(throwable, "Saving entity NBT");
@@ -2220,6 +2249,20 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource, S
             }
             // CraftBukkit end
 
+            // Paper start - Restore the entity's origin location
+            ListTag originTag = nbt.getList("Paper.Origin", 6);
+            if (!originTag.isEmpty()) {
+                UUID originWorld = null;
+                if (nbt.contains("Paper.OriginWorld")) {
+                    originWorld = nbt.getUUID("Paper.OriginWorld");
+                } else if (this.level != null) {
+                    originWorld = this.level.getWorld().getUID();
+                }
+                this.originWorld = originWorld;
+                origin = new org.bukkit.util.Vector(originTag.getDouble(0), originTag.getDouble(1), originTag.getDouble(2));
+            }
+            // 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 5d09790876c3c50d126678565001645f6aece16f..6c4224e4752b655710c5b992d9acf9563b183483 100644
--- a/src/main/java/net/minecraft/world/entity/item/FallingBlockEntity.java
+++ b/src/main/java/net/minecraft/world/entity/item/FallingBlockEntity.java
@@ -343,6 +343,14 @@ public class FallingBlockEntity extends Entity {
             this.blockState = Blocks.SAND.defaultBlockState();
         }
 
+        // Paper start - Try and load origin location from the old NBT tags for backwards compatibility
+        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(this.level().getWorld(), srcX, srcY, srcZ));
+        }
+        // Paper end
     }
 
     public void setHurtsEntities(float fallHurtAmount, int fallHurtMax) {
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 f08c021f867c00611139a17db48352944eb2aa99..5c3b86e2301079e775971aa4da6a8f2dc6a40d1f 100644
--- a/src/main/java/net/minecraft/world/entity/item/PrimedTnt.java
+++ b/src/main/java/net/minecraft/world/entity/item/PrimedTnt.java
@@ -132,6 +132,14 @@ public class PrimedTnt extends Entity implements TraceableEntity {
             this.setBlockState(NbtUtils.readBlockState(this.level().holderLookup(Registries.BLOCK), nbt.getCompound("block_state")));
         }
 
+        // Paper start - Try and load origin location from the old NBT tags for backwards compatibility
+        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(this.level().getWorld(), srcX, srcY, srcZ));
+        }
+        // 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 9efb4615d6d196a658940a2211c73af46e1c54b5..beae7b8cf726f61b25c5afb553da8c34dce1dd37 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
@@ -1316,5 +1316,20 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity {
 
         return ret;
     }
+
+    @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);
+    }
     // Paper end
 }