From 8788cf2d01d3aa95b2f842e6fcc1e0a586f2db6f Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Thu, 12 Jul 2018 19:13:44 +0300 Subject: [PATCH] Added plan.ignore.afk permission #613 --- .../plan/system/afk/AFKTracker.java | 8 +++++- .../system/listeners/bukkit/AFKListener.java | 9 ++++++- .../bukkit/CommandPreprocessListener.java | 2 +- .../listeners/sponge/SpongeAFKListener.java | 26 +++++++++++-------- .../sponge/SpongeCommandListener.java | 2 +- .../plan/system/settings/Permissions.java | 3 ++- Plan/src/main/resources/plugin.yml | 3 +++ 7 files changed, 37 insertions(+), 16 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/system/afk/AFKTracker.java b/Plan/src/main/java/com/djrapitops/plan/system/afk/AFKTracker.java index 3ddc56a5e..d9c7f85a6 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/afk/AFKTracker.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/afk/AFKTracker.java @@ -24,6 +24,10 @@ public class AFKTracker { afkThresholdMs = Settings.AFK_THRESHOLD_MINUTES.getNumber() * TimeAmount.MINUTE.ms(); } + public void hasIgnorePermission(UUID uuid) { + lastMovement.put(uuid, -1L); + } + public void usedAfkCommand(UUID uuid, long time) { usedAFKCommand.add(uuid); lastMovement.put(uuid, time - afkThresholdMs); @@ -31,6 +35,9 @@ public class AFKTracker { public void performedAction(UUID uuid, long time) { Long lastMoved = lastMovement.getOrDefault(uuid, time); + if (lastMoved == -1) { + return; + } lastMovement.put(uuid, time); try { @@ -42,7 +49,6 @@ public class AFKTracker { long removeAfkCommandEffect = usedAFKCommand.contains(uuid) ? afkThresholdMs : 0; long timeAFK = time - lastMoved - removeAfkCommandEffect; - Optional cachedSession = SessionCache.getCachedSession(uuid); if (!cachedSession.isPresent()) { return; diff --git a/Plan/src/main/java/com/djrapitops/plan/system/listeners/bukkit/AFKListener.java b/Plan/src/main/java/com/djrapitops/plan/system/listeners/bukkit/AFKListener.java index 345e6e0c2..2b7f51409 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/listeners/bukkit/AFKListener.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/listeners/bukkit/AFKListener.java @@ -1,7 +1,9 @@ package com.djrapitops.plan.system.listeners.bukkit; import com.djrapitops.plan.system.afk.AFKTracker; +import com.djrapitops.plan.system.settings.Permissions; import com.djrapitops.plugin.api.utility.log.Log; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; @@ -27,9 +29,14 @@ public class AFKListener implements Listener { private void event(PlayerEvent event) { try { - UUID uuid = event.getPlayer().getUniqueId(); + Player player = event.getPlayer(); + UUID uuid = player.getUniqueId(); long time = System.currentTimeMillis(); + if (player.hasPermission(Permissions.IGNORE_AFK.getPermission())) { + AFK_TRACKER.hasIgnorePermission(uuid); + } + AFK_TRACKER.performedAction(uuid, time); } catch (Exception e) { Log.toLog(this.getClass(), e); diff --git a/Plan/src/main/java/com/djrapitops/plan/system/listeners/bukkit/CommandPreprocessListener.java b/Plan/src/main/java/com/djrapitops/plan/system/listeners/bukkit/CommandPreprocessListener.java index 9af4a5f0d..2757a017c 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/listeners/bukkit/CommandPreprocessListener.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/listeners/bukkit/CommandPreprocessListener.java @@ -37,7 +37,7 @@ public class CommandPreprocessListener implements Listener { */ @EventHandler(priority = EventPriority.MONITOR) public void onPlayerCommand(PlayerCommandPreprocessEvent event) { - boolean hasIgnorePermission = event.getPlayer().hasPermission(Permissions.IGNORE_COMMANDUSE.getPermission()); + boolean hasIgnorePermission = event.getPlayer().hasPermission(Permissions.IGNORE_COMMAND_USE.getPermission()); if (event.isCancelled() || hasIgnorePermission) { return; } diff --git a/Plan/src/main/java/com/djrapitops/plan/system/listeners/sponge/SpongeAFKListener.java b/Plan/src/main/java/com/djrapitops/plan/system/listeners/sponge/SpongeAFKListener.java index 7aca42349..b3a777bec 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/listeners/sponge/SpongeAFKListener.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/listeners/sponge/SpongeAFKListener.java @@ -1,6 +1,7 @@ package com.djrapitops.plan.system.listeners.sponge; import com.djrapitops.plan.system.afk.AFKTracker; +import com.djrapitops.plan.system.settings.Permissions; import com.djrapitops.plugin.api.utility.log.Log; import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.event.Listener; @@ -29,10 +30,7 @@ public class SpongeAFKListener { private void event(TargetPlayerEvent event) { try { - UUID uuid = event.getTargetEntity().getUniqueId(); - long time = System.currentTimeMillis(); - - AFK_TRACKER.performedAction(uuid, time); + performedAction(event.getTargetEntity()); } catch (Exception e) { Log.toLog(this.getClass(), e); } @@ -40,26 +38,32 @@ public class SpongeAFKListener { @Listener(order = Order.POST) public void onMove(MoveEntityEvent event, @First Player player) { - UUID uuid = player.getUniqueId(); - long time = System.currentTimeMillis(); - AFK_TRACKER.performedAction(uuid, time); + performedAction(player); } @Listener(order = Order.POST) public void onPlayerChat(MessageChannelEvent.Chat event, @First Player player) { + performedAction(player); + } + + private void performedAction(Player player) { UUID uuid = player.getUniqueId(); long time = System.currentTimeMillis(); + + if (player.hasPermission(Permissions.IGNORE_AFK.getPermission())) { + AFK_TRACKER.hasIgnorePermission(uuid); + } + AFK_TRACKER.performedAction(uuid, time); } @Listener(order = Order.POST) public void onPlayerCommand(SendCommandEvent event, @First Player player) { - UUID uuid = player.getUniqueId(); - long time = System.currentTimeMillis(); - AFK_TRACKER.performedAction(uuid, time); + performedAction(player); + boolean isAfkCommand = event.getCommand().toLowerCase().startsWith("afk"); if (isAfkCommand) { - AFK_TRACKER.usedAfkCommand(uuid, System.currentTimeMillis()); + AFK_TRACKER.usedAfkCommand(player.getUniqueId(), System.currentTimeMillis()); } } diff --git a/Plan/src/main/java/com/djrapitops/plan/system/listeners/sponge/SpongeCommandListener.java b/Plan/src/main/java/com/djrapitops/plan/system/listeners/sponge/SpongeCommandListener.java index 1eccd3774..f752f0ca5 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/listeners/sponge/SpongeCommandListener.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/listeners/sponge/SpongeCommandListener.java @@ -24,7 +24,7 @@ public class SpongeCommandListener { @Listener(order = Order.POST) public void onPlayerCommand(SendCommandEvent event, @First Player player) { - boolean hasIgnorePermission = player.hasPermission(Permissions.IGNORE_COMMANDUSE.getPermission()); + boolean hasIgnorePermission = player.hasPermission(Permissions.IGNORE_COMMAND_USE.getPermission()); if (event.isCancelled() || hasIgnorePermission) { return; } diff --git a/Plan/src/main/java/com/djrapitops/plan/system/settings/Permissions.java b/Plan/src/main/java/com/djrapitops/plan/system/settings/Permissions.java index bfc2d11fe..56c294685 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/settings/Permissions.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/settings/Permissions.java @@ -25,7 +25,8 @@ public enum Permissions { MANAGE("plan.manage"), MANAGE_WEB("plan.webmanage"), - IGNORE_COMMANDUSE("plan.ignore.commanduse"); + IGNORE_COMMAND_USE("plan.ignore.commanduse"), + IGNORE_AFK("plan.ignore.afk"); private final String permission; diff --git a/Plan/src/main/resources/plugin.yml b/Plan/src/main/resources/plugin.yml index ec153acbb..77cc89f5d 100644 --- a/Plan/src/main/resources/plugin.yml +++ b/Plan/src/main/resources/plugin.yml @@ -79,6 +79,9 @@ permissions: plan.ignore.commanduse: description: Commands used by this player are not saved default: false + plan.ignore.afk: + description: AFK time by this player is ignored + default: false plan.manage: description: Manage the database, clear, move, combine. default: op