Skip to content

Commit 3c46d9d

Browse files
committed
Add a check that restores time that is lost when logging out before an Autorank check.
1 parent 1b2933c commit 3c46d9d

3 files changed

Lines changed: 50 additions & 3 deletions

File tree

src/me/armar/plugins/autorank/listeners/PlayerQuitListener.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package me.armar.plugins.autorank.listeners;
22

33
import me.armar.plugins.autorank.Autorank;
4-
import org.bukkit.entity.Player;
54
import org.bukkit.event.EventHandler;
65
import org.bukkit.event.EventPriority;
76
import org.bukkit.event.Listener;
87
import org.bukkit.event.player.PlayerQuitEvent;
98

9+
import java.util.UUID;
10+
1011
/**
1112
* This listener will listen to players leaving the server
1213
*
@@ -22,9 +23,30 @@ public PlayerQuitListener(final Autorank instance) {
2223

2324
@EventHandler(priority = EventPriority.HIGH)
2425
public void onPlayerQuit(final PlayerQuitEvent event) {
25-
final Player player = event.getPlayer();
26+
UUID uuid = event.getPlayer().getUniqueId();
2627

2728
// Stop task that updates the play time of a player
28-
plugin.getTaskManager().stopUpdatePlayTimeTask(player.getUniqueId());
29+
plugin.getTaskManager().stopUpdatePlayTimeTask(uuid);
30+
31+
// Check to see when the last time was that we updated the time.
32+
long lastPlayTimeUpdate = plugin.getTaskManager().getLastPlayTimeUpdate(uuid);
33+
34+
// Let's check how long it's been since we updated the time of the player.
35+
if (lastPlayTimeUpdate > 0) {
36+
37+
double difference = (System.currentTimeMillis() - lastPlayTimeUpdate) / 1000.0 / 60;
38+
39+
if (difference > 1.0) {
40+
41+
// Round to the nearest integer as we store time as an integer.
42+
int roundedDiff = (int) Math.round(difference);
43+
44+
// Add the 'lost' time to the player's current time.
45+
plugin.getStorageManager().addPlayerTime(uuid, roundedDiff);
46+
47+
// Remove the old time.
48+
plugin.getTaskManager().setLastPlayTimeUpdate(uuid, -1);
49+
}
50+
}
2951
}
3052
}

src/me/armar/plugins/autorank/playtimes/UpdateTimePlayedTask.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public void run() {
3434
return;
3535
}
3636

37+
// Set when we last updated this player's time.
38+
plugin.getTaskManager().setLastPlayTimeUpdate(uuid, System.currentTimeMillis());
39+
3740
// Do calendar check to see if storage provider still has up-to-date info.
3841
plugin.getStorageManager().doCalendarCheck();
3942

src/me/armar/plugins/autorank/tasks/TaskManager.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class TaskManager {
1818
private Autorank plugin;
1919

2020
private Map<UUID, Integer> updatePlayTimeTaskIds = new HashMap<>();
21+
private Map<UUID, Long> lastPlayTimeUpdate = new HashMap<>();
2122

2223
public TaskManager(Autorank plugin) {
2324
this.plugin = plugin;
@@ -36,6 +37,8 @@ public void startUpdatePlayTimeTask(UUID uuid) {
3637

3738
// Store taskID so we can refer to it later.
3839
updatePlayTimeTaskIds.put(uuid, task.getTaskId());
40+
// Register when we started the task.
41+
lastPlayTimeUpdate.put(uuid, System.currentTimeMillis());
3942

4043
plugin.debugMessage("Registered update play time task for player " + uuid + " (" + task.getTaskId() + ").");
4144
}
@@ -56,4 +59,23 @@ public void stopUpdatePlayTimeTask(UUID uuid) {
5659
updatePlayTimeTaskIds.remove(uuid);
5760
}
5861

62+
public void setLastPlayTimeUpdate(UUID uuid, long value) {
63+
64+
if (value < 0) {
65+
// remove the value if it's smaller than 0.
66+
lastPlayTimeUpdate.remove(uuid);
67+
}
68+
69+
lastPlayTimeUpdate.put(uuid, value);
70+
}
71+
72+
public long getLastPlayTimeUpdate(UUID uuid) {
73+
74+
if (!lastPlayTimeUpdate.containsKey(uuid)) {
75+
return -1;
76+
}
77+
78+
return lastPlayTimeUpdate.get(uuid);
79+
}
80+
5981
}

0 commit comments

Comments
 (0)