2021-06-11 14:02:28 +02:00
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
2022-06-01 08:20:12 +02:00
index 85a6204af67c5f8a9bd0dfc8598a2a2db2e3e4c1..5aebf4d330dda519efb4ab6e7bcdf4ccb174b11a 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/level/ServerPlayer.java
+++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java
2022-04-16 10:29:50 +02:00
@@ -221,6 +221,7 @@ public class ServerPlayer extends Player {
2021-06-11 14:02:28 +02:00
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
2022-06-01 08:20:12 +02:00
index 12451cc9eca34baa5d5ef6e55f4e4a63356365c6..9e3272a85e760e65217bd75c3ba9bfe21979fd35 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/players/PlayerList.java
+++ b/src/main/java/net/minecraft/server/players/PlayerList.java
2021-11-24 01:44:41 +01:00
@@ -173,6 +173,7 @@ public abstract class PlayerList {
2021-06-11 14:02:28 +02:00
}
public void placeNewPlayer(Connection connection, ServerPlayer player) {
+ player.loginTime = System.currentTimeMillis(); // Paper
GameProfile gameprofile = player.getGameProfile();
GameProfileCache usercache = this.server.getProfileCache();
2021-07-07 08:52:40 +02:00
Optional<GameProfile> optional = usercache.get(gameprofile.getId());
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftOfflinePlayer.java b/src/main/java/org/bukkit/craftbukkit/CraftOfflinePlayer.java
2022-02-12 14:20:33 +01:00
index f6665825e62a0cd912e6b06df6d68795596486f0..1f2bc88d4570c6ef00e67a772b745e0b0c98e051 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/CraftOfflinePlayer.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftOfflinePlayer.java
2022-02-12 14:20:33 +01:00
@@ -247,6 +247,61 @@ public class CraftOfflinePlayer implements OfflinePlayer, ConfigurationSerializa
2021-06-13 10:26:58 +02:00
return this.getData() != null;
2021-06-11 14:02:28 +02:00
}
+ // 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() {
2021-06-13 10:26:58 +02:00
CompoundTag data = this.getData();
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
2022-06-01 08:20:12 +02:00
index 2922c1b4d2e5a18c7c54be4a3a81782241f67367..5b06b7d97d77805debd8eb1eb3e0ed64be750bd4 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
2022-04-16 10:29:50 +02:00
@@ -171,6 +171,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
2021-06-11 14:02:28 +02:00
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) {
2022-06-01 08:20:12 +02:00
@@ -1670,6 +1671,18 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
2021-06-11 14:02:28 +02:00
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) {
2021-06-13 10:26:58 +02:00
this.hasPlayedBefore = true;
2021-06-11 14:02:28 +02:00
if (nbttagcompound.contains("bukkit")) {
2022-06-01 08:20:12 +02:00
@@ -1692,6 +1705,8 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
2021-06-11 14:02:28 +02:00
}
public void setExtraData(CompoundTag nbttagcompound) {
+ this.lastSaveTime = System.currentTimeMillis(); // Paper
+
if (!nbttagcompound.contains("bukkit")) {
nbttagcompound.put("bukkit", new CompoundTag());
}
2022-06-01 08:20:12 +02:00
@@ -1706,6 +1721,16 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
2021-06-13 10:26:58 +02:00
data.putLong("firstPlayed", this.getFirstPlayed());
2021-06-11 14:02:28 +02:00
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