diff options
| author | Krakenied <Krakenied1@gmail.com> | 2024-02-20 06:42:14 +0100 |
|---|---|---|
| committer | Leonardo Bishop <13875753+LMBishop@users.noreply.github.com> | 2024-02-22 16:33:04 +0000 |
| commit | 32aa65fa349837c1af172d22aa9c61e4a00e01d9 (patch) | |
| tree | 2eea14c1481901dc5efbfcedc603560580d0ed13 /bukkit/src/main/java | |
| parent | 52ded1a703d94a0354bb6b28ba6b4946e51dbf97 (diff) | |
Add CMI ignore-afk support
Closes https://github.com/LMBishop/Quests/issues/566
Diffstat (limited to 'bukkit/src/main/java')
5 files changed, 102 insertions, 37 deletions
diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/BukkitQuestsPlugin.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/BukkitQuestsPlugin.java index 7b944419..af1abed1 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/BukkitQuestsPlugin.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/BukkitQuestsPlugin.java @@ -10,6 +10,8 @@ import com.leonardobishop.quests.bukkit.hook.actionbar.QuestsActionBar; import com.leonardobishop.quests.bukkit.hook.bossbar.BossBar_Bukkit; import com.leonardobishop.quests.bukkit.hook.bossbar.BossBar_Nothing; import com.leonardobishop.quests.bukkit.hook.bossbar.QuestsBossBar; +import com.leonardobishop.quests.bukkit.hook.cmi.AbstractCMIHook; +import com.leonardobishop.quests.bukkit.hook.cmi.CMIHook; import com.leonardobishop.quests.bukkit.hook.coreprotect.AbstractCoreProtectHook; import com.leonardobishop.quests.bukkit.hook.coreprotect.CoreProtectHook; import com.leonardobishop.quests.bukkit.hook.essentials.AbstractEssentialsHook; @@ -181,6 +183,7 @@ public class BukkitQuestsPlugin extends JavaPlugin implements Quests { private QuestItemRegistry questItemRegistry; private MenuController menuController; private AbstractPlaceholderAPIHook placeholderAPIHook; + private AbstractCMIHook cmiHook; private AbstractCoreProtectHook coreProtectHook; private AbstractEssentialsHook essentialsHook; private AbstractPlayerBlockTrackerHook playerBlockTrackerHook; @@ -376,6 +379,10 @@ public class BukkitQuestsPlugin extends JavaPlugin implements Quests { this.placeholderAPIProcessor = (player, s) -> placeholderAPIHook.replacePlaceholders(player, s); } + if (CompatUtils.isPluginEnabled("CMI")) { + this.cmiHook = new CMIHook(); + } + if (CompatUtils.isPluginEnabled("CoreProtect")) { this.coreProtectHook = new CoreProtectHook(this); } @@ -760,6 +767,10 @@ public class BukkitQuestsPlugin extends JavaPlugin implements Quests { return placeholderAPIHook; } + public @Nullable AbstractCMIHook getCMIHook() { + return cmiHook; + } + public @Nullable AbstractCoreProtectHook getCoreProtectHook() { return coreProtectHook; } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/cmi/AbstractCMIHook.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/cmi/AbstractCMIHook.java new file mode 100644 index 00000000..df424324 --- /dev/null +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/cmi/AbstractCMIHook.java @@ -0,0 +1,13 @@ +package com.leonardobishop.quests.bukkit.hook.cmi; + +import org.bukkit.entity.Player; + +public interface AbstractCMIHook { + /** + * Check whether or not the passed player is marked AFK by CMI + * + * @param player the block + * @return true if afk, false otherwise + */ + boolean isAfk(Player player); +} diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/cmi/CMIHook.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/cmi/CMIHook.java new file mode 100644 index 00000000..1043cdac --- /dev/null +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/cmi/CMIHook.java @@ -0,0 +1,18 @@ +package com.leonardobishop.quests.bukkit.hook.cmi; + +import com.Zrips.CMI.CMI; +import org.bukkit.entity.Player; + +public class CMIHook implements AbstractCMIHook { + + private final CMI cmi; + + public CMIHook() { + this.cmi = CMI.getInstance(); + } + + @Override + public boolean isAfk(Player player) { + return cmi.getPlayerManager().getUser(player).isAfk(); + } +} diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/essentials/EssentialsHook.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/essentials/EssentialsHook.java index b18d9824..9f6e5ebe 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/essentials/EssentialsHook.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/essentials/EssentialsHook.java @@ -9,7 +9,7 @@ public class EssentialsHook implements AbstractEssentialsHook { private final Essentials ess; public EssentialsHook() { - ess = ((Essentials) Bukkit.getPluginManager().getPlugin("Essentials")); + this.ess = ((Essentials) Bukkit.getPluginManager().getPlugin("Essentials")); } @Override diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/PlaytimeTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/PlaytimeTaskType.java index fae9715d..b7af83d2 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/PlaytimeTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/PlaytimeTaskType.java @@ -1,10 +1,13 @@ package com.leonardobishop.quests.bukkit.tasktype.type; import com.leonardobishop.quests.bukkit.BukkitQuestsPlugin; +import com.leonardobishop.quests.bukkit.hook.cmi.AbstractCMIHook; +import com.leonardobishop.quests.bukkit.hook.essentials.AbstractEssentialsHook; import com.leonardobishop.quests.bukkit.scheduler.WrappedRunnable; import com.leonardobishop.quests.bukkit.scheduler.WrappedTask; import com.leonardobishop.quests.bukkit.tasktype.BukkitTaskType; import com.leonardobishop.quests.bukkit.util.TaskUtils; +import com.leonardobishop.quests.bukkit.util.constraint.TaskConstraintSet; import com.leonardobishop.quests.common.player.QPlayer; import com.leonardobishop.quests.common.player.questprogressfile.TaskProgress; import com.leonardobishop.quests.common.quest.Quest; @@ -21,57 +24,77 @@ public final class PlaytimeTaskType extends BukkitTaskType { super("playtime", TaskUtils.TASK_ATTRIBUTION_STRING, "Track the amount of playing time a user has been on"); this.plugin = plugin; + super.addConfigValidator(TaskUtils.useBooleanConfigValidator(this, "ignore-afk")); super.addConfigValidator(TaskUtils.useRequiredConfigValidator(this, "minutes")); super.addConfigValidator(TaskUtils.useIntegerConfigValidator(this, "minutes")); } - @Override public void onReady() { - if (this.poll == null) { - this.poll = new WrappedRunnable() { - @Override - public void run() { - for (Player player : Bukkit.getOnlinePlayers()) { - QPlayer qPlayer = plugin.getPlayerManager().getPlayer(player.getUniqueId()); - if (qPlayer == null) { - continue; - } + if (this.poll != null) { + return; + } - for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, PlaytimeTaskType.this)) { - Quest quest = pendingTask.quest(); - Task task = pendingTask.task(); - TaskProgress taskProgress = pendingTask.taskProgress(); + this.poll = new WrappedRunnable() { + @Override + public void run() { + for (Player player : Bukkit.getOnlinePlayers()) { + handle(player); + } + } + }.runTaskTimer(plugin.getScheduler(), 1200L, 1200L); + } - PlaytimeTaskType.super.debug("Polling playtime for player", quest.getId(), task.getId(), player.getUniqueId()); + private void handle(Player player) { + if (player.hasMetadata("NPC")) { + return; + } - boolean ignoreAfk = TaskUtils.getConfigBoolean(task, "ignore-afk"); + QPlayer qPlayer = plugin.getPlayerManager().getPlayer(player.getUniqueId()); + if (qPlayer == null) { + return; + } - if (ignoreAfk && plugin.getEssentialsHook() == null) { - PlaytimeTaskType.super.debug("ignore-afk is enabled, but Essentials is not detected on the server", quest.getId(), task.getId(), player.getUniqueId()); - } + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this, TaskConstraintSet.ALL)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - if (ignoreAfk - && plugin.getEssentialsHook() != null - && plugin.getEssentialsHook().isAfk(player)) { - PlaytimeTaskType.super.debug("ignore-afk is enabled and Essentials reports player as afk, continuing...", quest.getId(), task.getId(), player.getUniqueId()); - continue; - } + super.debug("Polling playtime for player", quest.getId(), task.getId(), player.getUniqueId()); - int minutes = (int) task.getConfigValue("minutes"); - int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress); - PlaytimeTaskType.super.debug("Incrementing task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId()); + boolean ignoreAfk = TaskUtils.getConfigBoolean(task, "ignore-afk", false); - if (progress >= minutes) { - PlaytimeTaskType.super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); - taskProgress.setCompleted(true); - } + if (ignoreAfk) { + super.debug("ignore-afk is enabled, checking hooks...", quest.getId(), task.getId(), player.getUniqueId()); - TaskUtils.sendTrackAdvancement(player, quest, task, taskProgress, minutes); - } - } + AbstractCMIHook cmiHook = plugin.getCMIHook(); + if (cmiHook != null && cmiHook.isAfk(player)) { + super.debug("CMI reports player as afk, continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; } - }.runTaskTimer(plugin.getScheduler(), 1200L, 1200L); + + AbstractEssentialsHook essentialsHook = plugin.getEssentialsHook(); + if (essentialsHook != null && essentialsHook.isAfk(player)) { + super.debug("Essentials reports player as afk, continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; + } + + if (cmiHook == null && essentialsHook == null) { + super.debug("ignore-afk is enabled, but no hooks found, continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; + } + } + + int minutes = (int) task.getConfigValue("minutes"); + int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress); + super.debug("Incrementing task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId()); + + if (progress >= minutes) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setCompleted(true); + } + + TaskUtils.sendTrackAdvancement(player, quest, task, taskProgress, minutes); } } |
