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) {
AFK_TRACKER.hasIgnorePermission(uuid);
ignorePermissionInfo.put(uuid, true);
return;
} else {
ignorePermissionInfo.put(uuid, false);
}

View File

@ -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<UUID> usedAFKCommand;
private final Map<UUID, Long> 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<Session> 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;
}
}

View File

@ -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<UUID, Boolean> 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);