mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-08-20 03:32:46 +02: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,35 +14,49 @@ 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);
|
||||||
|
|
||||||
if (time - lastMoved < afkThresholdMs) {
|
try {
|
||||||
// Threshold not crossed, no action required.
|
if (time - lastMoved < afkThresholdMs) {
|
||||||
return;
|
// Threshold not crossed, no action required.
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
long timeAFK = time - lastMoved;
|
long removeAfkCommandEffect = usedAFKCommand.contains(uuid) ? afkThresholdMs : 0;
|
||||||
|
long timeAFK = time - lastMoved - removeAfkCommandEffect;
|
||||||
|
|
||||||
Optional<Session> cachedSession = SessionCache.getCachedSession(uuid);
|
|
||||||
if (!cachedSession.isPresent()) {
|
Optional<Session> cachedSession = SessionCache.getCachedSession(uuid);
|
||||||
return;
|
if (!cachedSession.isPresent()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Session session = cachedSession.get();
|
||||||
|
session.addAFKTime(timeAFK);
|
||||||
|
} finally {
|
||||||
|
usedAFKCommand.remove(uuid);
|
||||||
}
|
}
|
||||||
Session session = cachedSession.get();
|
|
||||||
session.addAFKTime(timeAFK);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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