Added /afk command check to AFKTracker #614

This commit is contained in:
Rsl1122 2018-07-12 14:00:09 +03:00
parent efa98483a0
commit 0a90682bb2
3 changed files with 34 additions and 14 deletions

View File

@ -5,10 +5,7 @@ import com.djrapitops.plan.system.cache.SessionCache;
import com.djrapitops.plan.system.settings.Settings;
import com.djrapitops.plugin.api.TimeAmount;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.*;
/**
* Keeps track how long player has been afk during a session
@ -17,35 +14,49 @@ import java.util.UUID;
*/
public class AFKTracker {
private final Set<UUID> usedAFKCommand;
private final Map<UUID, Long> lastMovement;
private final long afkThresholdMs;
public AFKTracker() {
usedAFKCommand = new HashSet<>();
lastMovement = new HashMap<>();
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) {
Long lastMoved = lastMovement.getOrDefault(uuid, time);
lastMovement.put(uuid, time);
if (time - lastMoved < afkThresholdMs) {
// Threshold not crossed, no action required.
return;
}
try {
if (time - lastMoved < afkThresholdMs) {
// 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()) {
return;
Optional<Session> cachedSession = SessionCache.getCachedSession(uuid);
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) {
performedAction(uuid, time);
lastMovement.remove(uuid);
usedAFKCommand.remove(uuid);
}
}

View File

@ -49,6 +49,11 @@ public class AFKListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerCommand(PlayerCommandPreprocessEvent 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());
}
}
}

View File

@ -57,6 +57,10 @@ public class SpongeAFKListener {
UUID uuid = player.getUniqueId();
long time = System.currentTimeMillis();
AFK_TRACKER.performedAction(uuid, time);
boolean isAfkCommand = event.getCommand().toLowerCase().startsWith("afk");
if (isAfkCommand) {
AFK_TRACKER.usedAfkCommand(uuid, System.currentTimeMillis());
}
}
@Listener(order = Order.POST)