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/nbt/ListTag.java b/src/main/java/net/minecraft/nbt/ListTag.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/nbt/ListTag.java
+++ b/src/main/java/net/minecraft/nbt/ListTag.java
@@ -0,0 +0,0 @@ public class ListTag extends CollectionTag<Tag> {
         return new long[0];
     }
 
+    public final double getDoubleAt(int i) { return this.getDouble(i); } // Paper - OBFHELPER
     public double getDouble(int index) {
         if (index >= 0 && index < this.list.size()) {
             Tag tag = this.list.get(index);
diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/server/level/ServerLevel.java
+++ b/src/main/java/net/minecraft/server/level/ServerLevel.java
@@ -0,0 +0,0 @@ public class ServerLevel extends net.minecraft.world.level.Level implements Worl
                     entityplayer.connection.send(new ClientboundBlockDestructionPacket(entityId, pos, progress));
                 }
             }
+            // Paper start - Set origin location when the entity is being added to the world
+            if (entity.origin == null) {
+                entity.origin = entity.getBukkitEntity().getLocation();
+            }
+            // Paper end
         }
 
     }
diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
@@ -0,0 +0,0 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource, n
     public org.bukkit.projectiles.ProjectileSource projectileSource; // For projectiles only
     public boolean forceExplosionKnockback; // SPIGOT-949
     public boolean persistentInvisibility = false;
+    public org.bukkit.Location origin; // Paper
     // Spigot start
     public final org.spigotmc.ActivationRange.ActivationType activationType = org.spigotmc.ActivationRange.initializeEntityActivationType(this);
     public final boolean defaultActivationState;
@@ -0,0 +0,0 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource, n
                 this.bukkitEntity.storeBukkitValues(nbt);
             }
             // CraftBukkit end
+            // Paper start - Save the entity's origin location
+            if (this.origin != null) {
+                nbt.setUUID("Paper.OriginWorld", origin.getWorld().getUID());
+                nbt.put("Paper.Origin", this.newDoubleList(origin.getX(), origin.getY(), origin.getZ()));
+            }
+            // Paper end
             return nbt;
         } catch (Throwable throwable) {
             CrashReport crashreport = CrashReport.forThrowable(throwable, "Saving entity NBT");
@@ -0,0 +0,0 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource, n
             }
             // CraftBukkit end
 
+            // Paper start - Restore the entity's origin location
+            ListTag originTag = nbt.getList("Paper.Origin", 6);
+            if (!originTag.isEmpty()) {
+                org.bukkit.World originWorld = level.getWorld();
+                if (nbt.contains("Paper.OriginWorld")) {
+                    originWorld = Bukkit.getWorld(nbt.getUUID("Paper.OriginWorld"));
+                }
+                origin = new org.bukkit.Location(originWorld, originTag.getDoubleAt(0), originTag.getDoubleAt(1), originTag.getDoubleAt(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 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/world/entity/item/FallingBlockEntity.java
+++ b/src/main/java/net/minecraft/world/entity/item/FallingBlockEntity.java
@@ -0,0 +0,0 @@ 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");
+            origin = new org.bukkit.Location(level.getWorld(), srcX, srcY, srcZ);
+        }
+        // Paper end
     }
 
     public Level getLevel() {
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 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/world/entity/item/PrimedTnt.java
+++ b/src/main/java/net/minecraft/world/entity/item/PrimedTnt.java
@@ -0,0 +0,0 @@ public class PrimedTnt extends Entity {
     @Override
     protected void readAdditionalSaveData(CompoundTag nbt) {
         this.setFuse(nbt.getShort("Fuse"));
+        // 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");
+            origin = new org.bukkit.Location(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 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
@@ -0,0 +0,0 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity {
         return this.spigot;
     }
     // Spigot end
+
+    // Paper start
+    @Override
+    public Location getOrigin() {
+        Location origin = getHandle().origin;
+        return origin == null ? null : origin.clone();
+    }
+    // Paper end
 }
