From f9389c0ef89a3b382bd17ad434f55b03f40be09f Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Sun, 18 Aug 2019 09:34:16 +0300 Subject: [PATCH] Ported afk permission optimization to sponge - Sponge AFK listener no longer constantly asks for afk permission --- .../system/listeners/bukkit/AFKListener.java | 1 + .../plan/system/afk/AFKTracker.java | 20 +++++++++---------- .../listeners/sponge/SpongeAFKListener.java | 14 ++++++++++++- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/Plan/bukkit/src/main/java/com/djrapitops/plan/system/listeners/bukkit/AFKListener.java b/Plan/bukkit/src/main/java/com/djrapitops/plan/system/listeners/bukkit/AFKListener.java index f7acecc79..76de01411 100644 --- a/Plan/bukkit/src/main/java/com/djrapitops/plan/system/listeners/bukkit/AFKListener.java +++ b/Plan/bukkit/src/main/java/com/djrapitops/plan/system/listeners/bukkit/AFKListener.java @@ -78,6 +78,7 @@ public class AFKListener implements Listener { if (ignored) { AFK_TRACKER.hasIgnorePermission(uuid); ignorePermissionInfo.put(uuid, true); + return; } else { ignorePermissionInfo.put(uuid, false); } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/afk/AFKTracker.java b/Plan/common/src/main/java/com/djrapitops/plan/system/afk/AFKTracker.java index fc8bab177..6359b4f7f 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/system/afk/AFKTracker.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/afk/AFKTracker.java @@ -16,7 +16,6 @@ */ 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.settings.config.PlanConfig; import com.djrapitops.plan.system.settings.paths.TimeSettings; @@ -33,11 +32,13 @@ public class AFKTracker { private final Set usedAFKCommand; private final Map lastMovement; private final PlanConfig config; + private Long afkThresholdMs; public AFKTracker(PlanConfig config) { this.config = config; usedAFKCommand = new HashSet<>(); lastMovement = new HashMap<>(); + afkThresholdMs = config.get(TimeSettings.AFK_THRESHOLD); } public void hasIgnorePermission(UUID uuid) { @@ -46,31 +47,28 @@ public class AFKTracker { public void usedAfkCommand(UUID uuid, long time) { usedAFKCommand.add(uuid); - lastMovement.put(uuid, time - config.get(TimeSettings.AFK_THRESHOLD)); + lastMovement.put(uuid, time - afkThresholdMs); } public void performedAction(UUID uuid, long time) { Long lastMoved = lastMovement.getOrDefault(uuid, time); + // Ignore afk permission if (lastMoved == -1) { return; } lastMovement.put(uuid, time); try { - if (time - lastMoved < config.get(TimeSettings.AFK_THRESHOLD)) { + if (time - lastMoved < afkThresholdMs) { // Threshold not crossed, no action required. return; } - long removeAfkCommandEffect = usedAFKCommand.contains(uuid) ? config.get(TimeSettings.AFK_THRESHOLD) : 0; + long removeAfkCommandEffect = usedAFKCommand.contains(uuid) ? afkThresholdMs : 0; long timeAFK = time - lastMoved - removeAfkCommandEffect; - Optional cachedSession = SessionCache.getCachedSession(uuid); - if (!cachedSession.isPresent()) { - return; - } - Session session = cachedSession.get(); - session.addAFKTime(timeAFK); + SessionCache.getCachedSession(uuid) + .ifPresent(session -> session.addAFKTime(timeAFK)); } finally { usedAFKCommand.remove(uuid); } @@ -89,6 +87,6 @@ public class AFKTracker { if (lastMoved == null || lastMoved == -1) { return false; } - return time - lastMoved > config.get(TimeSettings.AFK_THRESHOLD); + return time - lastMoved > afkThresholdMs; } } \ No newline at end of file diff --git a/Plan/sponge/src/main/java/com/djrapitops/plan/system/listeners/sponge/SpongeAFKListener.java b/Plan/sponge/src/main/java/com/djrapitops/plan/system/listeners/sponge/SpongeAFKListener.java index ea1192b24..bc1a60927 100644 --- a/Plan/sponge/src/main/java/com/djrapitops/plan/system/listeners/sponge/SpongeAFKListener.java +++ b/Plan/sponge/src/main/java/com/djrapitops/plan/system/listeners/sponge/SpongeAFKListener.java @@ -32,6 +32,8 @@ import org.spongepowered.api.event.filter.cause.First; import org.spongepowered.api.event.message.MessageChannelEvent; import javax.inject.Inject; +import java.util.HashMap; +import java.util.Map; import java.util.UUID; /** @@ -47,11 +49,13 @@ public class SpongeAFKListener { // Static so that /reload does not cause afk tracking to fail. static AFKTracker AFK_TRACKER; + private final Map ignorePermissionInfo; private final ErrorHandler errorHandler; @Inject public SpongeAFKListener(PlanConfig config, ErrorHandler errorHandler) { this.errorHandler = errorHandler; + this.ignorePermissionInfo = new HashMap<>(); SpongeAFKListener.assignAFKTracker(config); } @@ -84,8 +88,16 @@ public class SpongeAFKListener { UUID uuid = player.getUniqueId(); 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); + ignorePermissionInfo.put(uuid, true); + return; + } else { + ignorePermissionInfo.put(uuid, false); } AFK_TRACKER.performedAction(uuid, time);