Ported afk permission optimization to sponge

- Sponge AFK listener no longer constantly asks for afk permission
This commit is contained in:
Rsl1122 2019-08-18 09:34:16 +03:00
parent aa4eecbe07
commit f9389c0ef8
3 changed files with 23 additions and 12 deletions

View File

@ -78,6 +78,7 @@ public class AFKListener implements Listener {
if (ignored) { if (ignored) {
AFK_TRACKER.hasIgnorePermission(uuid); AFK_TRACKER.hasIgnorePermission(uuid);
ignorePermissionInfo.put(uuid, true); ignorePermissionInfo.put(uuid, true);
return;
} else { } else {
ignorePermissionInfo.put(uuid, false); ignorePermissionInfo.put(uuid, false);
} }

View File

@ -16,7 +16,6 @@
*/ */
package com.djrapitops.plan.system.afk; package com.djrapitops.plan.system.afk;
import com.djrapitops.plan.data.container.Session;
import com.djrapitops.plan.system.cache.SessionCache; import com.djrapitops.plan.system.cache.SessionCache;
import com.djrapitops.plan.system.settings.config.PlanConfig; import com.djrapitops.plan.system.settings.config.PlanConfig;
import com.djrapitops.plan.system.settings.paths.TimeSettings; import com.djrapitops.plan.system.settings.paths.TimeSettings;
@ -33,11 +32,13 @@ public class AFKTracker {
private final Set<UUID> usedAFKCommand; private final Set<UUID> usedAFKCommand;
private final Map<UUID, Long> lastMovement; private final Map<UUID, Long> lastMovement;
private final PlanConfig config; private final PlanConfig config;
private Long afkThresholdMs;
public AFKTracker(PlanConfig config) { public AFKTracker(PlanConfig config) {
this.config = config; this.config = config;
usedAFKCommand = new HashSet<>(); usedAFKCommand = new HashSet<>();
lastMovement = new HashMap<>(); lastMovement = new HashMap<>();
afkThresholdMs = config.get(TimeSettings.AFK_THRESHOLD);
} }
public void hasIgnorePermission(UUID uuid) { public void hasIgnorePermission(UUID uuid) {
@ -46,31 +47,28 @@ public class AFKTracker {
public void usedAfkCommand(UUID uuid, long time) { public void usedAfkCommand(UUID uuid, long time) {
usedAFKCommand.add(uuid); usedAFKCommand.add(uuid);
lastMovement.put(uuid, time - config.get(TimeSettings.AFK_THRESHOLD)); 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);
// Ignore afk permission
if (lastMoved == -1) { if (lastMoved == -1) {
return; return;
} }
lastMovement.put(uuid, time); lastMovement.put(uuid, time);
try { try {
if (time - lastMoved < config.get(TimeSettings.AFK_THRESHOLD)) { if (time - lastMoved < afkThresholdMs) {
// Threshold not crossed, no action required. // Threshold not crossed, no action required.
return; return;
} }
long removeAfkCommandEffect = usedAFKCommand.contains(uuid) ? config.get(TimeSettings.AFK_THRESHOLD) : 0; long removeAfkCommandEffect = usedAFKCommand.contains(uuid) ? afkThresholdMs : 0;
long timeAFK = time - lastMoved - removeAfkCommandEffect; long timeAFK = time - lastMoved - removeAfkCommandEffect;
Optional<Session> cachedSession = SessionCache.getCachedSession(uuid); SessionCache.getCachedSession(uuid)
if (!cachedSession.isPresent()) { .ifPresent(session -> session.addAFKTime(timeAFK));
return;
}
Session session = cachedSession.get();
session.addAFKTime(timeAFK);
} finally { } finally {
usedAFKCommand.remove(uuid); usedAFKCommand.remove(uuid);
} }
@ -89,6 +87,6 @@ public class AFKTracker {
if (lastMoved == null || lastMoved == -1) { if (lastMoved == null || lastMoved == -1) {
return false; return false;
} }
return time - lastMoved > config.get(TimeSettings.AFK_THRESHOLD); return time - lastMoved > afkThresholdMs;
} }
} }

View File

@ -32,6 +32,8 @@ import org.spongepowered.api.event.filter.cause.First;
import org.spongepowered.api.event.message.MessageChannelEvent; import org.spongepowered.api.event.message.MessageChannelEvent;
import javax.inject.Inject; import javax.inject.Inject;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
/** /**
@ -47,11 +49,13 @@ public class SpongeAFKListener {
// Static so that /reload does not cause afk tracking to fail. // Static so that /reload does not cause afk tracking to fail.
static AFKTracker AFK_TRACKER; static AFKTracker AFK_TRACKER;
private final Map<UUID, Boolean> ignorePermissionInfo;
private final ErrorHandler errorHandler; private final ErrorHandler errorHandler;
@Inject @Inject
public SpongeAFKListener(PlanConfig config, ErrorHandler errorHandler) { public SpongeAFKListener(PlanConfig config, ErrorHandler errorHandler) {
this.errorHandler = errorHandler; this.errorHandler = errorHandler;
this.ignorePermissionInfo = new HashMap<>();
SpongeAFKListener.assignAFKTracker(config); SpongeAFKListener.assignAFKTracker(config);
} }
@ -84,8 +88,16 @@ public class SpongeAFKListener {
UUID uuid = player.getUniqueId(); UUID uuid = player.getUniqueId();
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
if (player.hasPermission(Permissions.IGNORE_AFK.getPermission())) { Boolean ignored = ignorePermissionInfo.get(uuid);
if (ignored == null) {
ignored = player.hasPermission(Permissions.IGNORE_AFK.getPermission());
}
if (ignored) {
AFK_TRACKER.hasIgnorePermission(uuid); AFK_TRACKER.hasIgnorePermission(uuid);
ignorePermissionInfo.put(uuid, true);
return;
} else {
ignorePermissionInfo.put(uuid, false);
} }
AFK_TRACKER.performedAction(uuid, time); AFK_TRACKER.performedAction(uuid, time);