From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Zach Brown <zach@zachbr.io>
Date: Wed, 2 Jan 2019 00:35:43 -0600
Subject: [PATCH] Add APIs to replace OfflinePlayer#getLastPlayed

Currently OfflinePlayer#getLastPlayed could more accurately be described
as "OfflinePlayer#getLastTimeTheirDataWasSaved".

The API doc says it should return the last time the server "witnessed"
the player, whilst also saying it should return the last time they
logged in. The current implementation does neither.

Given this interesting contradiction in the API documentation and the
current defacto implementation, I've elected to deprecate (with no
intent to remove) and replace it with two new methods, clearly named and
documented as to their purpose.

diff --git a/src/main/java/net/minecraft/server/level/ServerPlayer.java b/src/main/java/net/minecraft/server/level/ServerPlayer.java
index f51c416e7938b7905f7efb154ab14cad643962e9..71cd61112fa36d1de2f5fc0dd75993fafc3d76ab 100644
--- a/src/main/java/net/minecraft/server/level/ServerPlayer.java
+++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java
@@ -222,6 +222,7 @@ public class ServerPlayer extends Player {
     public int latency;
     public boolean wonGame;
     private int containerUpdateDelay; // Paper
+    public long loginTime; // Paper
     // Paper start - cancellable death event
     public boolean queueHealthUpdatePacket = false;
     public net.minecraft.network.protocol.game.ClientboundSetHealthPacket queuedHealthUpdatePacket;
diff --git a/src/main/java/net/minecraft/server/players/PlayerList.java b/src/main/java/net/minecraft/server/players/PlayerList.java
index e1603b674823067a55faa12e716b695171b31d32..73dd4776fee3429c42b279ab92050a4b872f64b5 100644
--- a/src/main/java/net/minecraft/server/players/PlayerList.java
+++ b/src/main/java/net/minecraft/server/players/PlayerList.java
@@ -173,6 +173,7 @@ public abstract class PlayerList {
     }
 
     public void placeNewPlayer(Connection connection, ServerPlayer player) {
+        player.loginTime = System.currentTimeMillis(); // Paper
         GameProfile gameprofile = player.getGameProfile();
         GameProfileCache usercache = this.server.getProfileCache();
         Optional<GameProfile> optional = usercache.get(gameprofile.getId());
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftOfflinePlayer.java b/src/main/java/org/bukkit/craftbukkit/CraftOfflinePlayer.java
index c7e4c6d29378675b76ebb179022ddbb02831a1dc..88bc0807e8bf66a65422f85f1112336334eb3de2 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftOfflinePlayer.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftOfflinePlayer.java
@@ -244,6 +244,61 @@ public class CraftOfflinePlayer implements OfflinePlayer, ConfigurationSerializa
         return this.getData() != null;
     }
 
+    // Paper start
+    @Override
+    public long getLastLogin() {
+        Player player = getPlayer();
+        if (player != null) return player.getLastLogin();
+
+        CompoundTag data = getPaperData();
+
+        if (data != null) {
+            if (data.contains("LastLogin")) {
+                return data.getLong("LastLogin");
+            } else {
+                // if the player file cannot provide accurate data, this is probably the closest we can approximate
+                File file = getDataFile();
+                return file.lastModified();
+            }
+        } else {
+            return 0;
+        }
+    }
+
+    @Override
+    public long getLastSeen() {
+        Player player = getPlayer();
+        if (player != null) return player.getLastSeen();
+
+        CompoundTag data = getPaperData();
+
+        if (data != null) {
+            if (data.contains("LastSeen")) {
+                return data.getLong("LastSeen");
+            } else {
+                // if the player file cannot provide accurate data, this is probably the closest we can approximate
+                File file = getDataFile();
+                return file.lastModified();
+            }
+        } else {
+            return 0;
+        }
+    }
+
+    private CompoundTag getPaperData() {
+        CompoundTag result = getData();
+
+        if (result != null) {
+            if (!result.contains("Paper")) {
+                result.put("Paper", new CompoundTag());
+            }
+            result = result.getCompound("Paper");
+        }
+
+        return result;
+    }
+    // Paper end
+
     @Override
     public Location getBedSpawnLocation() {
         CompoundTag data = this.getData();
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
index 2a313757c85faeb353fb8140db84a464cc46b524..3026243161b6a0f978a62c67ecb7825713ac5249 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
@@ -156,6 +156,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
     private org.bukkit.event.player.PlayerResourcePackStatusEvent.Status resourcePackStatus;
     private String resourcePackHash;
     private static final boolean DISABLE_CHANNEL_LIMIT = System.getProperty("paper.disableChannelLimit") != null; // Paper - add a flag to disable the channel limit
+    private long lastSaveTime;
     // Paper end
 
     public CraftPlayer(CraftServer server, ServerPlayer entity) {
@@ -1547,6 +1548,18 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
         this.firstPlayed = firstPlayed;
     }
 
+    // Paper start
+    @Override
+    public long getLastLogin() {
+        return getHandle().loginTime;
+    }
+
+    @Override
+    public long getLastSeen() {
+        return isOnline() ? System.currentTimeMillis() : this.lastSaveTime;
+    }
+    // Paper end
+
     public void readExtraData(CompoundTag nbttagcompound) {
         this.hasPlayedBefore = true;
         if (nbttagcompound.contains("bukkit")) {
@@ -1569,6 +1582,8 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
     }
 
     public void setExtraData(CompoundTag nbttagcompound) {
+        this.lastSaveTime = System.currentTimeMillis(); // Paper
+
         if (!nbttagcompound.contains("bukkit")) {
             nbttagcompound.put("bukkit", new CompoundTag());
         }
@@ -1583,6 +1598,16 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
         data.putLong("firstPlayed", this.getFirstPlayed());
         data.putLong("lastPlayed", System.currentTimeMillis());
         data.putString("lastKnownName", handle.getScoreboardName());
+
+        // Paper start - persist for use in offline save data
+        if (!nbttagcompound.contains("Paper")) {
+            nbttagcompound.put("Paper", new CompoundTag());
+        }
+
+        CompoundTag paper = nbttagcompound.getCompound("Paper");
+        paper.putLong("LastLogin", handle.loginTime);
+        paper.putLong("LastSeen", System.currentTimeMillis());
+        // Paper end
     }
 
     @Override
