Skip to content

Commit

Permalink
Add a check that restores time that is lost when logging out before a…
Browse files Browse the repository at this point in the history
…n Autorank check.
  • Loading branch information
Staartvin committed Nov 30, 2019
1 parent 1b2933c commit 3c46d9d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/me/armar/plugins/autorank/listeners/PlayerQuitListener.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package me.armar.plugins.autorank.listeners;

import me.armar.plugins.autorank.Autorank;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;

import java.util.UUID;

/**
* This listener will listen to players leaving the server
*
Expand All @@ -22,9 +23,30 @@ public PlayerQuitListener(final Autorank instance) {

@EventHandler(priority = EventPriority.HIGH)
public void onPlayerQuit(final PlayerQuitEvent event) {
final Player player = event.getPlayer();
UUID uuid = event.getPlayer().getUniqueId();

// Stop task that updates the play time of a player
plugin.getTaskManager().stopUpdatePlayTimeTask(player.getUniqueId());
plugin.getTaskManager().stopUpdatePlayTimeTask(uuid);

// Check to see when the last time was that we updated the time.
long lastPlayTimeUpdate = plugin.getTaskManager().getLastPlayTimeUpdate(uuid);

// Let's check how long it's been since we updated the time of the player.
if (lastPlayTimeUpdate > 0) {

double difference = (System.currentTimeMillis() - lastPlayTimeUpdate) / 1000.0 / 60;

if (difference > 1.0) {

// Round to the nearest integer as we store time as an integer.
int roundedDiff = (int) Math.round(difference);

// Add the 'lost' time to the player's current time.
plugin.getStorageManager().addPlayerTime(uuid, roundedDiff);

// Remove the old time.
plugin.getTaskManager().setLastPlayTimeUpdate(uuid, -1);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public void run() {
return;
}

// Set when we last updated this player's time.
plugin.getTaskManager().setLastPlayTimeUpdate(uuid, System.currentTimeMillis());

// Do calendar check to see if storage provider still has up-to-date info.
plugin.getStorageManager().doCalendarCheck();

Expand Down
22 changes: 22 additions & 0 deletions src/me/armar/plugins/autorank/tasks/TaskManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class TaskManager {
private Autorank plugin;

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

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

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

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

public void setLastPlayTimeUpdate(UUID uuid, long value) {

if (value < 0) {
// remove the value if it's smaller than 0.
lastPlayTimeUpdate.remove(uuid);
}

lastPlayTimeUpdate.put(uuid, value);
}

public long getLastPlayTimeUpdate(UUID uuid) {

if (!lastPlayTimeUpdate.containsKey(uuid)) {
return -1;
}

return lastPlayTimeUpdate.get(uuid);
}

}

0 comments on commit 3c46d9d

Please sign in to comment.