mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-10-27 13:59:56 +01:00
Added /afk command check to AFKTracker #614
This commit is contained in:
parent
efa98483a0
commit
0a90682bb2
@ -5,10 +5,7 @@ import com.djrapitops.plan.system.cache.SessionCache;
|
|||||||
import com.djrapitops.plan.system.settings.Settings;
|
import com.djrapitops.plan.system.settings.Settings;
|
||||||
import com.djrapitops.plugin.api.TimeAmount;
|
import com.djrapitops.plugin.api.TimeAmount;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.*;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Keeps track how long player has been afk during a session
|
* Keeps track how long player has been afk during a session
|
||||||
@ -17,24 +14,34 @@ import java.util.UUID;
|
|||||||
*/
|
*/
|
||||||
public class AFKTracker {
|
public class AFKTracker {
|
||||||
|
|
||||||
|
private final Set<UUID> usedAFKCommand;
|
||||||
private final Map<UUID, Long> lastMovement;
|
private final Map<UUID, Long> lastMovement;
|
||||||
private final long afkThresholdMs;
|
private final long afkThresholdMs;
|
||||||
|
|
||||||
public AFKTracker() {
|
public AFKTracker() {
|
||||||
|
usedAFKCommand = new HashSet<>();
|
||||||
lastMovement = new HashMap<>();
|
lastMovement = new HashMap<>();
|
||||||
afkThresholdMs = Settings.AFK_THRESHOLD_MINUTES.getNumber() * TimeAmount.MINUTE.ms();
|
afkThresholdMs = Settings.AFK_THRESHOLD_MINUTES.getNumber() * TimeAmount.MINUTE.ms();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void usedAfkCommand(UUID uuid, long time) {
|
||||||
|
usedAFKCommand.add(uuid);
|
||||||
|
lastMovement.put(uuid, time - afkThresholdMs);
|
||||||
|
}
|
||||||
|
|
||||||
public void performedAction(UUID uuid, long time) {
|
public void performedAction(UUID uuid, long time) {
|
||||||
Long lastMoved = lastMovement.getOrDefault(uuid, time);
|
Long lastMoved = lastMovement.getOrDefault(uuid, time);
|
||||||
lastMovement.put(uuid, time);
|
lastMovement.put(uuid, time);
|
||||||
|
|
||||||
|
try {
|
||||||
if (time - lastMoved < afkThresholdMs) {
|
if (time - lastMoved < afkThresholdMs) {
|
||||||
// Threshold not crossed, no action required.
|
// Threshold not crossed, no action required.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
long timeAFK = time - lastMoved;
|
long removeAfkCommandEffect = usedAFKCommand.contains(uuid) ? afkThresholdMs : 0;
|
||||||
|
long timeAFK = time - lastMoved - removeAfkCommandEffect;
|
||||||
|
|
||||||
|
|
||||||
Optional<Session> cachedSession = SessionCache.getCachedSession(uuid);
|
Optional<Session> cachedSession = SessionCache.getCachedSession(uuid);
|
||||||
if (!cachedSession.isPresent()) {
|
if (!cachedSession.isPresent()) {
|
||||||
@ -42,10 +49,14 @@ public class AFKTracker {
|
|||||||
}
|
}
|
||||||
Session session = cachedSession.get();
|
Session session = cachedSession.get();
|
||||||
session.addAFKTime(timeAFK);
|
session.addAFKTime(timeAFK);
|
||||||
|
} finally {
|
||||||
|
usedAFKCommand.remove(uuid);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loggedOut(UUID uuid, long time) {
|
public void loggedOut(UUID uuid, long time) {
|
||||||
performedAction(uuid, time);
|
performedAction(uuid, time);
|
||||||
lastMovement.remove(uuid);
|
lastMovement.remove(uuid);
|
||||||
|
usedAFKCommand.remove(uuid);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -49,6 +49,11 @@ public class AFKListener implements Listener {
|
|||||||
@EventHandler(priority = EventPriority.MONITOR)
|
@EventHandler(priority = EventPriority.MONITOR)
|
||||||
public void onPlayerCommand(PlayerCommandPreprocessEvent event) {
|
public void onPlayerCommand(PlayerCommandPreprocessEvent event) {
|
||||||
event(event);
|
event(event);
|
||||||
|
boolean isAfkCommand = event.getMessage().substring(1).toLowerCase().startsWith("afk");
|
||||||
|
if (isAfkCommand) {
|
||||||
|
UUID uuid = event.getPlayer().getUniqueId();
|
||||||
|
AFK_TRACKER.usedAfkCommand(uuid, System.currentTimeMillis());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -57,6 +57,10 @@ public class SpongeAFKListener {
|
|||||||
UUID uuid = player.getUniqueId();
|
UUID uuid = player.getUniqueId();
|
||||||
long time = System.currentTimeMillis();
|
long time = System.currentTimeMillis();
|
||||||
AFK_TRACKER.performedAction(uuid, time);
|
AFK_TRACKER.performedAction(uuid, time);
|
||||||
|
boolean isAfkCommand = event.getCommand().toLowerCase().startsWith("afk");
|
||||||
|
if (isAfkCommand) {
|
||||||
|
AFK_TRACKER.usedAfkCommand(uuid, System.currentTimeMillis());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Listener(order = Order.POST)
|
@Listener(order = Order.POST)
|
||||||
|
Loading…
Reference in New Issue
Block a user