diff options
Diffstat (limited to 'src/main/java/com/leonardobishop')
7 files changed, 94 insertions, 29 deletions
diff --git a/src/main/java/com/leonardobishop/quests/QuestCompleter.java b/src/main/java/com/leonardobishop/quests/QuestCompleter.java index 2e3c4de7..97357126 100644 --- a/src/main/java/com/leonardobishop/quests/QuestCompleter.java +++ b/src/main/java/com/leonardobishop/quests/QuestCompleter.java @@ -6,11 +6,16 @@ import com.leonardobishop.quests.player.questprogressfile.QuestProgressFile; import com.leonardobishop.quests.player.questprogressfile.TaskProgress; import com.leonardobishop.quests.quests.Quest; import com.leonardobishop.quests.quests.Task; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; -import java.util.Map; +import java.util.LinkedList; +import java.util.Queue; public class QuestCompleter implements Runnable { + private final Queue<QuestProgress> completionQueue = new LinkedList<>(); + private final Queue<QuestProgressFile> fullCheckQueue = new LinkedList<>(); private final Quests plugin; public QuestCompleter(Quests plugin) { @@ -19,9 +24,34 @@ public class QuestCompleter implements Runnable { @Override public void run() { - //TODO if it still runs like shit then maybe only process a few players per X ticks rather than the whole server in one go - for (QPlayer qPlayer : plugin.getPlayerManager().getQPlayers()) { + this.processCompletionQueue(); + this.processFullCheckQueue(); + } + + private void processCompletionQueue() { + QuestProgress questProgress = completionQueue.poll(); + if (questProgress == null) return; + + Player player = Bukkit.getPlayer(questProgress.getPlayer()); + if (player != null && player.isOnline()) { + QPlayer qPlayer = plugin.getPlayerManager().getPlayer(player.getUniqueId()); QuestProgressFile questProgressFile = qPlayer.getQuestProgressFile(); + Quest quest = plugin.getQuestManager().getQuestById(questProgress.getQuestId()); + + if (!questProgressFile.hasStartedQuest(quest)) return; + + if (checkComplete(quest, questProgress)) { + questProgressFile.completeQuest(quest); + } + } + } + + private void processFullCheckQueue() { + QuestProgressFile questProgressFile = fullCheckQueue.poll(); + if (questProgressFile == null) return; + + Player player = Bukkit.getPlayer(questProgressFile.getPlayerUUID()); + if (player != null && player.isOnline()) { for (QuestProgress questProgress : questProgressFile.getAllQuestProgress()) { Quest quest = plugin.getQuestManager().getQuestById(questProgress.getQuestId()); if (quest == null) continue; @@ -41,4 +71,25 @@ public class QuestCompleter implements Runnable { } } } -}
\ No newline at end of file + + private boolean checkComplete(Quest quest, QuestProgress questProgress) { + boolean complete = true; + for (Task task : quest.getTasks()) { + TaskProgress taskProgress; + if ((taskProgress = questProgress.getTaskProgress(task.getId())) == null || !taskProgress.isCompleted()) { + complete = false; + break; + } + } + + return complete; + } + + public void queueSingular(QuestProgress questProgress) { + completionQueue.add(questProgress); + } + + public void queueFullCheck(QuestProgressFile questProgressFile) { + fullCheckQueue.add(questProgressFile); + } +} diff --git a/src/main/java/com/leonardobishop/quests/Quests.java b/src/main/java/com/leonardobishop/quests/Quests.java index 722753cc..286ed571 100644 --- a/src/main/java/com/leonardobishop/quests/Quests.java +++ b/src/main/java/com/leonardobishop/quests/Quests.java @@ -43,6 +43,7 @@ public class Quests extends JavaPlugin { private static Updater updater; private static Title title; private ItemGetter itemGetter; + private QuestCompleter questCompleter; private QuestsConfigLoader questsConfigLoader; private QuestsLogger questsLogger; private PlaceholderExpansion placeholder; @@ -101,6 +102,7 @@ public class Quests extends JavaPlugin { @Override public void onEnable() { questsLogger = new QuestsLogger(this, QuestsLogger.LoggingLevel.INFO); + questCompleter = new QuestCompleter(this); taskTypeManager = new TaskTypeManager(this); questManager = new QuestManager(this); @@ -200,6 +202,8 @@ public class Quests extends JavaPlugin { ignoreUpdates = new File(this.getDataFolder() + File.separator + "stfuQuestsUpdate").exists(); } catch (Throwable ignored) { } + Bukkit.getScheduler().runTaskTimer(this, questCompleter, 1L, 1L); + updater = new Updater(this); if (!ignoreUpdates) { Bukkit.getScheduler().runTaskAsynchronously(this, () -> { @@ -246,14 +250,6 @@ public class Quests extends JavaPlugin { qPlayer.getQuestProgressFile().saveToDisk(false); } }, autocompleteInterval, autocompleteInterval); - if (questCompleterTask != null) { - try { - questCompleterTask.cancel(); - } catch (Exception ignored) { - questsLogger.debug("Cannot cancel quest completer task"); - } - } - questCompleterTask = Bukkit.getScheduler().runTaskTimer(this, new QuestCompleter(this), 20, completerPollInterval); } public ItemStack getItemStack(String path, ConfigurationSection config, ItemGetter.Filter... excludes) { @@ -369,6 +365,10 @@ public class Quests extends JavaPlugin { } } + public QuestCompleter getQuestCompleter() { + return questCompleter; + } + public QuestsLogger getQuestsLogger() { return questsLogger; } diff --git a/src/main/java/com/leonardobishop/quests/events/EventPlayerJoin.java b/src/main/java/com/leonardobishop/quests/events/EventPlayerJoin.java index e193c5b2..b9e1723f 100644 --- a/src/main/java/com/leonardobishop/quests/events/EventPlayerJoin.java +++ b/src/main/java/com/leonardobishop/quests/events/EventPlayerJoin.java @@ -35,6 +35,8 @@ public class EventPlayerJoin implements Listener { // delay for a bit so they actually see the message Bukkit.getScheduler().runTaskLater(this.plugin, () -> event.getPlayer().sendMessage(plugin.getUpdater().getMessage()), 50L); } + // run a full check to check for any missed quest completions + plugin.getQuestCompleter().queueFullCheck(plugin.getPlayerManager().getPlayer(playerUuid).getQuestProgressFile()); } } diff --git a/src/main/java/com/leonardobishop/quests/player/QPlayerManager.java b/src/main/java/com/leonardobishop/quests/player/QPlayerManager.java index 397f6fb6..d531f168 100644 --- a/src/main/java/com/leonardobishop/quests/player/QPlayerManager.java +++ b/src/main/java/com/leonardobishop/quests/player/QPlayerManager.java @@ -90,14 +90,14 @@ public class QPlayerManager { boolean completedBefore = data.getBoolean("quest-progress." + id + ".completed-before"); long completionDate = data.getLong("quest-progress." + id + ".completion-date"); - QuestProgress questProgress = new QuestProgress(id, completed, completedBefore, completionDate, uuid, started, true); + QuestProgress questProgress = new QuestProgress(plugin, id, completed, completedBefore, completionDate, uuid, started, true); if (data.isConfigurationSection("quest-progress." + id + ".task-progress")) { for (String taskid : data.getConfigurationSection("quest-progress." + id + ".task-progress").getKeys(false)) { boolean taskCompleted = data.getBoolean("quest-progress." + id + ".task-progress." + taskid + ".completed"); Object taskProgression = data.get("quest-progress." + id + ".task-progress." + taskid + ".progress"); - TaskProgress taskProgress = new TaskProgress(taskid, taskProgression, uuid, taskCompleted, false); + TaskProgress taskProgress = new TaskProgress(questProgress, taskid, taskProgression, uuid, taskCompleted, false); questProgress.addTaskProgress(taskProgress); } } diff --git a/src/main/java/com/leonardobishop/quests/player/questprogressfile/QuestProgress.java b/src/main/java/com/leonardobishop/quests/player/questprogressfile/QuestProgress.java index ab9096a6..b4e4fc54 100644 --- a/src/main/java/com/leonardobishop/quests/player/questprogressfile/QuestProgress.java +++ b/src/main/java/com/leonardobishop/quests/player/questprogressfile/QuestProgress.java @@ -1,5 +1,7 @@ package com.leonardobishop.quests.player.questprogressfile; +import com.leonardobishop.quests.Quests; + import java.util.Collection; import java.util.HashMap; import java.util.Map; @@ -7,6 +9,8 @@ import java.util.UUID; public class QuestProgress { + private final Quests plugin; + private Map<String, TaskProgress> taskProgress = new HashMap<>(); private String questid; private boolean started; @@ -16,7 +20,8 @@ public class QuestProgress { private UUID player; private boolean modified; - public QuestProgress(String questid, boolean completed, boolean completedBefore, long completionDate, UUID player, boolean started) { + public QuestProgress(Quests plugin, String questid, boolean completed, boolean completedBefore, long completionDate, UUID player, boolean started) { + this.plugin = plugin; this.questid = questid; this.completed = completed; this.completedBefore = completedBefore; @@ -25,8 +30,8 @@ public class QuestProgress { this.started = started; } - public QuestProgress(String questid, boolean completed, boolean completedBefore, long completionDate, UUID player, boolean started, boolean modified) { - this(questid, completed, completedBefore, completionDate, player, started); + public QuestProgress(Quests plugin, String questid, boolean completed, boolean completedBefore, long completionDate, UUID player, boolean started, boolean modified) { + this(plugin, questid, completed, completedBefore, completionDate, player, started); this.modified = modified; } @@ -96,10 +101,11 @@ public class QuestProgress { } public void repairTaskProgress(String taskid) { - TaskProgress taskProgress = new TaskProgress(taskid, null, player, false, false); + TaskProgress taskProgress = new TaskProgress(this, taskid, null, player, false, false); this.addTaskProgress(taskProgress); } + @Deprecated // this shit is annoying to maintain public boolean isWorthSaving() { if (modified) return true; else { @@ -110,6 +116,10 @@ public class QuestProgress { } } + public void queueForCompletionTest() { + plugin.getQuestCompleter().queueSingular(this); + } + public void resetModified() { this.modified = false; for (TaskProgress progress : this.taskProgress.values()) { diff --git a/src/main/java/com/leonardobishop/quests/player/questprogressfile/QuestProgressFile.java b/src/main/java/com/leonardobishop/quests/player/questprogressfile/QuestProgressFile.java index 6d9b2972..ff9bc3e3 100644 --- a/src/main/java/com/leonardobishop/quests/player/questprogressfile/QuestProgressFile.java +++ b/src/main/java/com/leonardobishop/quests/player/questprogressfile/QuestProgressFile.java @@ -374,9 +374,9 @@ public class QuestProgressFile { public boolean generateBlankQuestProgress(String questid) { if (plugin.getQuestManager().getQuestById(questid) != null) { Quest quest = plugin.getQuestManager().getQuestById(questid); - QuestProgress questProgress = new QuestProgress(quest.getId(), false, false, 0, playerUUID, false, false); + QuestProgress questProgress = new QuestProgress(plugin, quest.getId(), false, false, 0, playerUUID, false, false); for (Task task : quest.getTasks()) { - TaskProgress taskProgress = new TaskProgress(task.getId(), null, playerUUID, false, false); + TaskProgress taskProgress = new TaskProgress(questProgress, task.getId(), null, playerUUID, false, false); questProgress.addTaskProgress(taskProgress); } @@ -410,13 +410,8 @@ public class QuestProgressFile { } YamlConfiguration data = YamlConfiguration.loadConfiguration(file); - if (fullWrite) { - data.set("quest-progress", null); - } + data.set("quest-progress", null); for (QuestProgress questProgress : questProgress.values()) { - if (!questProgress.isWorthSaving() && !fullWrite) { - continue; - } data.set("quest-progress." + questProgress.getQuestId() + ".started", questProgress.isStarted()); data.set("quest-progress." + questProgress.getQuestId() + ".completed", questProgress.isCompleted()); data.set("quest-progress." + questProgress.getQuestId() + ".completed-before", questProgress.isCompletedBefore()); diff --git a/src/main/java/com/leonardobishop/quests/player/questprogressfile/TaskProgress.java b/src/main/java/com/leonardobishop/quests/player/questprogressfile/TaskProgress.java index a4c71460..3ae38664 100644 --- a/src/main/java/com/leonardobishop/quests/player/questprogressfile/TaskProgress.java +++ b/src/main/java/com/leonardobishop/quests/player/questprogressfile/TaskProgress.java @@ -4,20 +4,23 @@ import java.util.UUID; public class TaskProgress { + private final QuestProgress questProgress; + private boolean modified; private String taskid; private Object progress; private UUID player; private boolean completed; - public TaskProgress(String taskid, Object progress, UUID player, boolean completed) { + public TaskProgress(QuestProgress questProgress, String taskid, Object progress, UUID player, boolean completed) { + this.questProgress = questProgress; this.taskid = taskid; this.progress = progress; this.completed = completed; } - public TaskProgress(String taskid, Object progress, UUID player, boolean completed, boolean modified) { - this(taskid, progress, player, completed); + public TaskProgress(QuestProgress questProgress, String taskid, Object progress, UUID player, boolean completed, boolean modified) { + this(questProgress, taskid, progress, player, completed); this.modified = modified; } @@ -45,6 +48,10 @@ public class TaskProgress { public void setCompleted(boolean complete) { this.completed = complete; this.modified = true; + + if (complete) { + questProgress.queueForCompletionTest(); + } } public boolean isModified() { |
