diff options
| author | LMBishop <13875753+LMBishop@users.noreply.github.com> | 2022-07-06 15:53:21 +0100 |
|---|---|---|
| committer | LMBishop <13875753+LMBishop@users.noreply.github.com> | 2022-07-06 15:53:21 +0100 |
| commit | cd4dfb95872080123f69eeaa1d817d9305b4e5b5 (patch) | |
| tree | cb5ca5a6781db8e61a14e6ac63e2218f32def448 /bukkit/src/main/java/com | |
| parent | 196410e28f3d243355552e19c8e8df972d6c5cb7 (diff) | |
Move applicable tasks to TaskUtils and add debug messages
Diffstat (limited to 'bukkit/src/main/java/com')
50 files changed, 1259 insertions, 1606 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 8cf42b37..5d809235 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/BukkitQuestsPlugin.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/BukkitQuestsPlugin.java @@ -348,7 +348,7 @@ public class BukkitQuestsPlugin extends JavaPlugin implements Quests { if (Bukkit.getPluginManager().isPluginEnabled("MythicMobs")) { String mythicMobsVersion = Bukkit.getPluginManager().getPlugin("MythicMobs").getDescription().getVersion(); if (mythicMobsVersion.startsWith("4") || mythicMobsVersion.startsWith("5")) { - taskTypeManager.registerTaskType(new MythicMobsKillingType(this, mythicMobsVersion)); + taskTypeManager.registerTaskType(new MythicMobsKillingTaskType(this, mythicMobsVersion)); } } if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) { @@ -378,7 +378,7 @@ public class BukkitQuestsPlugin extends JavaPlugin implements Quests { } if (Bukkit.getPluginManager().isPluginEnabled("Votifier")) { // not tested - taskTypeManager.registerTaskType(new NuVotifierVoteType(this)); + taskTypeManager.registerTaskType(new NuVotifierVoteTaskType(this)); } taskTypeManager.closeRegistrations(); diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/command/AdminDebugQuestCommandHandler.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/command/AdminDebugQuestCommandHandler.java index dc7b8178..4a32aae3 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/command/AdminDebugQuestCommandHandler.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/command/AdminDebugQuestCommandHandler.java @@ -58,6 +58,11 @@ public class AdminDebugQuestCommandHandler implements CommandHandler { QPlayerPreferences preferences = qPlayer.getPlayerPreferences(); QPlayerPreferences.DebugType currentDebugType = preferences.getDebug(questId); if (currentDebugType == null) { + if (args.length < 5) { + sender.sendMessage(ChatColor.RED + "You must specify a debug type."); + return; + } + String debugType = args[4]; QPlayerPreferences.DebugType debugTypeEnum; @@ -70,7 +75,7 @@ public class AdminDebugQuestCommandHandler implements CommandHandler { preferences.setDebug(questId, debugTypeEnum); sender.sendMessage(ChatColor.GREEN + "Debugging enabled for quest '" + questId + "'."); - sender.sendMessage(ChatColor.GRAY + "You will now see debug logs for quest '" + quest + "' for " + + sender.sendMessage(ChatColor.GRAY + "You will now see debug logs for quest '" + quest.getId() + "' for " + (debugTypeEnum == QPlayerPreferences.DebugType.SELF ? "yourself" : "everybody on the server") + ". This may generate a lot of spam."); sender.sendMessage(ChatColor.DARK_GRAY + "Use '/quests admin debug " + questId + "' to disable."); @@ -89,7 +94,7 @@ public class AdminDebugQuestCommandHandler implements CommandHandler { if (args.length == 4) { return TabHelper.tabCompleteQuests(args[3]); } else if (args.length == 5) { - return TabHelper.matchTabComplete(args[2], Arrays.asList("self", "all")); + return TabHelper.matchTabComplete(args[4], Arrays.asList("self", "all")); } return Collections.emptyList(); } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/BukkitTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/BukkitTaskType.java index b187b794..8f4078a2 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/BukkitTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/BukkitTaskType.java @@ -18,8 +18,8 @@ public abstract class BukkitTaskType extends TaskType implements Listener { super(type); } - public final void debug(@NotNull String message, String questId, @NotNull UUID player) { - taskTypeManager.sendDebug(message, super.getType(), questId, player); + public final void debug(@NotNull String message, String questId, String taskId, @NotNull UUID player) { + taskTypeManager.sendDebug(message, super.getType(), questId, taskId, player); } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/BukkitTaskTypeManager.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/BukkitTaskTypeManager.java index 46ad24ae..3ad57e90 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/BukkitTaskTypeManager.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/BukkitTaskTypeManager.java @@ -6,6 +6,7 @@ import com.leonardobishop.quests.common.player.QPlayerPreferences; import com.leonardobishop.quests.common.tasktype.TaskType; import com.leonardobishop.quests.common.tasktype.TaskTypeManager; import org.bukkit.Bukkit; +import org.bukkit.ChatColor; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; @@ -37,21 +38,24 @@ public class BukkitTaskTypeManager extends TaskTypeManager { return false; } - public void sendDebug(@NotNull String message, @NotNull String taskType, @NotNull String questId, @NotNull UUID associatedPlayer) { + public void sendDebug(@NotNull String message, @NotNull String taskType, @NotNull String questId, @NotNull String taskId, @NotNull UUID associatedPlayer) { for (QPlayer qPlayer : plugin.getPlayerManager().getQPlayers()) { QPlayerPreferences.DebugType debugType = qPlayer.getPlayerPreferences().getDebug(questId); Player player = Bukkit.getPlayer(qPlayer.getPlayerUUID()); Player otherPlayer = Bukkit.getPlayer(associatedPlayer); String associatedName = otherPlayer == null ? associatedPlayer.toString() : otherPlayer.getName(); - String chatMessage = "[" + taskType + ": " + associatedName + " on '" + questId + "'] " + message; + String chatHeader = ChatColor.GRAY + "[" + associatedName + " - " + questId + "/" + taskId + " - type '" + taskType + "']"; if (player != null && debugType != null) { switch (debugType) { - case ALL -> player.sendMessage(chatMessage); + case ALL -> { + player.sendMessage(chatHeader); + player.sendMessage(message); + } case SELF -> { if (player.getUniqueId().equals(associatedPlayer)) { - player.sendMessage(chatMessage); - } + player.sendMessage(chatHeader); + player.sendMessage(message); } } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/BreedingTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/BreedingTaskType.java index 3d4e9dfd..644bfd7b 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/BreedingTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/BreedingTaskType.java @@ -5,7 +5,6 @@ import com.leonardobishop.quests.bukkit.tasktype.BukkitTaskType; import com.leonardobishop.quests.bukkit.util.TaskUtils; import com.leonardobishop.quests.common.config.ConfigProblem; import com.leonardobishop.quests.common.player.QPlayer; -import com.leonardobishop.quests.common.player.questprogressfile.QuestProgress; import com.leonardobishop.quests.common.player.questprogressfile.TaskProgress; import com.leonardobishop.quests.common.quest.Quest; import com.leonardobishop.quests.common.quest.Task; @@ -52,41 +51,27 @@ public final class BreedingTaskType extends BukkitTaskType { } // Check if there is a player in the list, otherwise: return. for (Entity current : entList) { - if (current instanceof Player && !current.hasMetadata("NPC")) { - Player player = (Player) current; + if (current instanceof Player player && !current.hasMetadata("NPC")) { QPlayer qPlayer = plugin.getPlayerManager().getPlayer(player.getUniqueId()); if (qPlayer == null) { continue; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player.getPlayer(), qPlayer, this, TaskUtils.TaskConstraint.WORLD)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - for (Task task : quest.getTasksOfType(super.getType())) { - if (!TaskUtils.validateWorld(player, task)) continue; + super.debug("Player detected near bred animal", quest.getId(), task.getId(), player.getUniqueId()); - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); + int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress); + super.debug("Incrementing task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId()); - if (taskProgress.isCompleted()) { - continue; - } + int breedingNeeded = (int) task.getConfigValue("amount"); - int breedingNeeded = (int) task.getConfigValue("amount"); - int breedingProgress; - - if (taskProgress.getProgress() == null) { - breedingProgress = 0; - } else { - breedingProgress = (int) taskProgress.getProgress(); - } - - taskProgress.setProgress(breedingProgress + 1); - - if (((int) taskProgress.getProgress()) >= breedingNeeded) { - taskProgress.setCompleted(true); - } - } + if (progress >= breedingNeeded) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setCompleted(true); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/BrewingTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/BrewingTaskType.java index 3737d9be..e7259a90 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/BrewingTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/BrewingTaskType.java @@ -5,7 +5,6 @@ import com.leonardobishop.quests.bukkit.tasktype.BukkitTaskType; import com.leonardobishop.quests.bukkit.util.TaskUtils; import com.leonardobishop.quests.common.config.ConfigProblem; import com.leonardobishop.quests.common.player.QPlayer; -import com.leonardobishop.quests.common.player.questprogressfile.QuestProgress; import com.leonardobishop.quests.common.player.questprogressfile.TaskProgress; import com.leonardobishop.quests.common.quest.Quest; import com.leonardobishop.quests.common.quest.Task; @@ -18,7 +17,6 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.block.Action; import org.bukkit.event.inventory.BrewEvent; import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; @@ -68,38 +66,29 @@ public final class BrewingTaskType extends BukkitTaskType { return; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player.getPlayer(), qPlayer, this, TaskUtils.TaskConstraint.WORLD)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - for (Task task : quest.getTasksOfType(super.getType())) { - if (!TaskUtils.validateWorld(player, task)) continue; + super.debug("Player brewed potion", quest.getId(), task.getId(), player.getUniqueId()); - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); + int progress = 0; - if (taskProgress.isCompleted()) { - continue; - } - - int potionsNeeded = (int) task.getConfigValue("amount"); - - int progress; - if (taskProgress.getProgress() == null) { - progress = 0; - } else { - progress = (int) taskProgress.getProgress(); - } - - ItemStack potion1 = event.getContents().getItem(0); - ItemStack potion2 = event.getContents().getItem(1); - ItemStack potion3 = event.getContents().getItem(2); + for (int i = 0; i < 3; i++) { + if (event.getContents().getItem(i) != null) { + progress = TaskUtils.incrementIntegerTaskProgress(taskProgress); + super.debug("Incrementing task progress for brewed potion in slot " + i + " (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId()); + } else { + super.debug("Slot " + i + " does not have a brewed potion", quest.getId(), task.getId(), player.getUniqueId()); + } + } - taskProgress.setProgress(progress + (potion1 == null ? 0 : 1) + (potion2 == null ? 0 : 1) + (potion3 == null ? 0 : 1)); + int potionsNeeded = (int) task.getConfigValue("amount"); - if (((int) taskProgress.getProgress()) >= potionsNeeded) { - taskProgress.setCompleted(true); - } - } + if (progress >= potionsNeeded) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setCompleted(true); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/BucketInteractionTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/BucketInteractionTaskType.java index 52f0eb5b..e709f5d7 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/BucketInteractionTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/BucketInteractionTaskType.java @@ -6,7 +6,6 @@ import com.leonardobishop.quests.bukkit.util.TaskUtils; import com.leonardobishop.quests.common.config.ConfigProblem; import com.leonardobishop.quests.common.config.ConfigProblemDescriptions; import com.leonardobishop.quests.common.player.QPlayer; -import com.leonardobishop.quests.common.player.questprogressfile.QuestProgress; import com.leonardobishop.quests.common.player.questprogressfile.TaskProgress; import com.leonardobishop.quests.common.quest.Quest; import com.leonardobishop.quests.common.quest.Task; @@ -52,41 +51,28 @@ public abstract class BucketInteractionTaskType extends BukkitTaskType { if (qPlayer == null) return; - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player.getPlayer(), qPlayer, this, TaskUtils.TaskConstraint.WORLD)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - for (Task task : quest.getTasksOfType(super.getType())) { - if (!TaskUtils.validateWorld(player, task)) continue; + int amount = (int) task.getConfigValue("amount"); + Object configBucket = task.getConfigValue("bucket"); + Material material = Material.getMaterial((String) configBucket); - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); + super.debug("Player used bucket of type " + bucket, quest.getId(), task.getId(), player.getUniqueId()); - if (taskProgress.isCompleted()) { - continue; - } - - int amount = (int) task.getConfigValue("amount"); - Object configBucket = task.getConfigValue("bucket"); - Material material = Material.getMaterial((String) configBucket); - - if (bucket != material) { - continue; - } - - int progress; - if (taskProgress.getProgress() == null) { - progress = 0; - } else { - progress = (int) taskProgress.getProgress(); - } + if (bucket != material) { + super.debug("Player bucket does not match required bucket '" + material + "', continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; + } - taskProgress.setProgress(progress + 1); + int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress); + super.debug("Incrementing task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId()); - if ((int) taskProgress.getProgress() >= amount) { - taskProgress.setProgress(amount); - taskProgress.setCompleted(true); - } - } + if ((int) taskProgress.getProgress() >= amount) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setCompleted(true); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/BuildingCertainTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/BuildingCertainTaskType.java index a8e59494..82b70bec 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/BuildingCertainTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/BuildingCertainTaskType.java @@ -12,6 +12,7 @@ import com.leonardobishop.quests.common.quest.Quest; import com.leonardobishop.quests.common.quest.Task; import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.block.BlockBreakEvent; @@ -79,22 +80,24 @@ public final class BuildingCertainTaskType extends BukkitTaskType { return; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + Player player = event.getPlayer(); - for (Task task : quest.getTasksOfType(super.getType())) { - if (!TaskUtils.validateWorld(event.getPlayer(), task)) continue; + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(event.getPlayer(), qPlayer, this, TaskUtils.TaskConstraint.WORLD)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); + super.debug("Player placed block " + event.getBlock().getType(), quest.getId(), task.getId(), event.getPlayer().getUniqueId()); - if (taskProgress.isCompleted()) { - continue; - } + if (TaskUtils.matchBlock(this, pendingTask, event.getBlock(), player.getUniqueId())) { + int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress); + super.debug("Incrementing task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId()); - if (matchBlock(task, event.getBlock())) { - increment(task, taskProgress, 1); - } + int blocksNeeded = (int) task.getConfigValue("amount"); + + if (progress >= blocksNeeded) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setCompleted(true); } } } @@ -110,81 +113,26 @@ public final class BuildingCertainTaskType extends BukkitTaskType { return; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); - - for (Task task : quest.getTasksOfType(super.getType())) { - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); - - if (taskProgress.isCompleted()) { - continue; - } - - if (task.getConfigValue("reverse-if-broken") != null && ((boolean) task.getConfigValue("reverse-if-broken"))) { - if (matchBlock(task, event.getBlock())) { - increment(task, taskProgress, -1); - } - } - } - } - } - } + Player player = event.getPlayer(); - @SuppressWarnings("deprecation") - private boolean matchBlock(Task task, Block block) { - Material material; + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(event.getPlayer(), qPlayer, this, TaskUtils.TaskConstraint.WORLD)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - Object configBlock = task.getConfigValues().containsKey("block") ? task.getConfigValue("block") : task.getConfigValue("blocks"); - Object configData = task.getConfigValue("data"); - Object configSimilarBlocks = task.getConfigValue("use-similar-blocks"); + super.debug("Player mined block " + event.getBlock().getType(), quest.getId(), task.getId(), event.getPlayer().getUniqueId()); - List<String> checkBlocks = new ArrayList<>(); - if (configBlock instanceof List) { - checkBlocks.addAll((List) configBlock); - } else { - checkBlocks.add(String.valueOf(configBlock)); - } - for (String materialName : checkBlocks) { - // LOG:1 LOG:2 LOG should all be supported with this - String[] split = materialName.split(":"); - int comparableData = 0; - if (configData != null) { - comparableData = (int) configData; - } - if (split.length > 1) { - comparableData = Integer.parseInt(split[1]); - } - - material = Material.getMaterial(String.valueOf(split[0])); - Material blockType = block.getType(); - - short blockData = block.getData(); - - if (blockType == material) { - if (((split.length == 1 && configData == null) || ((int) blockData) == comparableData)) - return true; + if (task.getConfigValue("reverse-if-broken") != null && ((boolean) task.getConfigValue("reverse-if-placed"))) { + super.debug("reverse-if-broken is enabled, checking block", quest.getId(), task.getId(), event.getPlayer().getUniqueId()); + if (TaskUtils.matchBlock(this, pendingTask, event.getBlock(), player.getUniqueId())) { + int progress = TaskUtils.getIntegerTaskProgress(taskProgress); + taskProgress.setProgress(--progress); + super.debug("Decrementing task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId()); + } } } - return false; } - private void increment(Task task, TaskProgress taskProgress, int amount) { - int brokenBlocksNeeded = (int) task.getConfigValue("amount"); - - int progressBlocksBroken; - if (taskProgress.getProgress() == null) { - progressBlocksBroken = 0; - } else { - progressBlocksBroken = (int) taskProgress.getProgress(); - } - - taskProgress.setProgress(progressBlocksBroken + amount); - - if (((int) taskProgress.getProgress()) >= brokenBlocksNeeded) { - taskProgress.setCompleted(true); - } - } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/BuildingTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/BuildingTaskType.java index 1533d92c..192d53be 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/BuildingTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/BuildingTaskType.java @@ -5,7 +5,6 @@ import com.leonardobishop.quests.bukkit.tasktype.BukkitTaskType; import com.leonardobishop.quests.bukkit.util.TaskUtils; import com.leonardobishop.quests.common.config.ConfigProblem; import com.leonardobishop.quests.common.player.QPlayer; -import com.leonardobishop.quests.common.player.questprogressfile.QuestProgress; import com.leonardobishop.quests.common.player.questprogressfile.TaskProgress; import com.leonardobishop.quests.common.quest.Quest; import com.leonardobishop.quests.common.quest.Task; @@ -44,34 +43,21 @@ public final class BuildingTaskType extends BukkitTaskType { return; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(event.getPlayer(), qPlayer, this, TaskUtils.TaskConstraint.WORLD)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - for (Task task : quest.getTasksOfType(super.getType())) { - if (!TaskUtils.validateWorld(event.getPlayer(), task)) continue; + super.debug("Player placed block", quest.getId(), task.getId(), event.getPlayer().getUniqueId()); - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); + int brokenBlocksNeeded = (int) task.getConfigValue("amount"); - if (taskProgress.isCompleted()) { - continue; - } + int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress); + super.debug("Incrementing task progress (now " + progress + ")", quest.getId(), task.getId(), event.getPlayer().getUniqueId()); - int brokenBlocksNeeded = (int) task.getConfigValue("amount"); - - int progressBlocksBroken; - if (taskProgress.getProgress() == null) { - progressBlocksBroken = 0; - } else { - progressBlocksBroken = (int) taskProgress.getProgress(); - } - - taskProgress.setProgress(progressBlocksBroken + 1); - - if (((int) taskProgress.getProgress()) >= brokenBlocksNeeded) { - taskProgress.setCompleted(true); - } - } + if (progress >= brokenBlocksNeeded) { + super.debug("Marking task as complete", quest.getId(), task.getId(), event.getPlayer().getUniqueId()); + taskProgress.setCompleted(true); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/CommandTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/CommandTaskType.java index 5aca23b4..bc8789f5 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/CommandTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/CommandTaskType.java @@ -47,44 +47,28 @@ public final class CommandTaskType extends BukkitTaskType { return; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this, TaskUtils.TaskConstraint.WORLD)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - for (Task task : quest.getTasksOfType(super.getType())) { - if (!TaskUtils.validateWorld(player, task)) continue; + boolean ignoreCasing = TaskUtils.getConfigBoolean(task, "ignore-case"); + List<String> commands = TaskUtils.getConfigStringList(task, "command"); - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); - - if (taskProgress.isCompleted()) { - continue; - } - Object configCommand = task.getConfigValue("command"); - Object configIgnoreCase = task.getConfigValue("ignore-case"); - - List<String> commands = new ArrayList<>(); - if (configCommand instanceof List) { - commands.addAll((List) configCommand); - } else { - commands.add(String.valueOf(configCommand)); - } + String message = e.getMessage(); + if (message.length() >= 1) { + message = message.substring(1); + } - boolean ignoreCasing = false; - if (configIgnoreCase != null) { - ignoreCasing = (boolean) task.getConfigValue("ignore-case"); - } - String message = e.getMessage(); - if (message.length() >= 1) { - message = message.substring(1); - } + super.debug("Player sent command '/" + message + "'", quest.getId(), task.getId(), player.getUniqueId()); - for (String command : commands) { - if (ignoreCasing && command.equalsIgnoreCase(message)) { - taskProgress.setCompleted(true); - } else if (!ignoreCasing && command.equals(message)) { - taskProgress.setCompleted(true); - } - } + for (String command : commands) { + super.debug("Checking command against '/" + command + "' (ignore case = " + ignoreCasing + ")", quest.getId(), task.getId(), player.getUniqueId()); + if ((ignoreCasing && command.equalsIgnoreCase(message)) + || (!ignoreCasing && command.equals(message))) { + super.debug("Command '/" + message + "' matches task command '" + command + "'", quest.getId(), task.getId(), player.getUniqueId()); + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setCompleted(true); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/ConsumeTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/ConsumeTaskType.java index 37da2828..c3c13fb6 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/ConsumeTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/ConsumeTaskType.java @@ -3,23 +3,18 @@ package com.leonardobishop.quests.bukkit.tasktype.type; import com.google.common.collect.HashBasedTable; import com.google.common.collect.Table; import com.leonardobishop.quests.bukkit.BukkitQuestsPlugin; -import com.leonardobishop.quests.bukkit.item.ParsedQuestItem; import com.leonardobishop.quests.bukkit.item.QuestItem; import com.leonardobishop.quests.bukkit.tasktype.BukkitTaskType; import com.leonardobishop.quests.bukkit.util.TaskUtils; import com.leonardobishop.quests.common.config.ConfigProblem; import com.leonardobishop.quests.common.player.QPlayer; -import com.leonardobishop.quests.common.player.questprogressfile.QuestProgress; import com.leonardobishop.quests.common.player.questprogressfile.TaskProgress; import com.leonardobishop.quests.common.quest.Quest; import com.leonardobishop.quests.common.quest.Task; -import org.bukkit.Material; -import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.player.PlayerItemConsumeEvent; -import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; @@ -65,60 +60,32 @@ public final class ConsumeTaskType extends BukkitTaskType { if (qPlayer == null) return; - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this, TaskUtils.TaskConstraint.WORLD)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - for (Task task : quest.getTasksOfType(super.getType())) { - if (!TaskUtils.validateWorld(player, task)) continue; + int amount = (int) task.getConfigValue("amount"); - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); - - if (taskProgress.isCompleted()) { - continue; - } - - Material material; - int amount = (int) task.getConfigValue("amount"); - Object configBlock = task.getConfigValue("item"); - Object configData = task.getConfigValue("data"); - - QuestItem qi; - if ((qi = fixedQuestItemCache.get(quest.getId(), task.getId())) == null) { - if (configBlock instanceof ConfigurationSection) { - qi = plugin.getConfiguredQuestItem("", (ConfigurationSection) configBlock); - } else { - material = Material.getMaterial(String.valueOf(configBlock)); - ItemStack is; - if (material == null) { - continue; - } - if (configData != null) { - is = new ItemStack(material, 1, ((Integer) configData).shortValue()); - } else { - is = new ItemStack(material, 1); - } - qi = new ParsedQuestItem("parsed", null, is); - } - fixedQuestItemCache.put(quest.getId(), task.getId(), qi); - } - - if (!qi.compareItemStack(event.getItem())) continue; + QuestItem qi; + if ((qi = fixedQuestItemCache.get(quest.getId(), task.getId())) == null) { + QuestItem fetchedItem = TaskUtils.getConfigQuestItem(task, "item", "data"); + fixedQuestItemCache.put(quest.getId(), task.getId(), fetchedItem); + qi = fetchedItem; + } - int progress; - if (taskProgress.getProgress() == null) { - progress = 0; - } else { - progress = (int) taskProgress.getProgress(); - } + super.debug("Player consumed item of type " + event.getItem().getType(), quest.getId(), task.getId(), event.getPlayer().getUniqueId()); + if (!qi.compareItemStack(event.getItem())) { + super.debug("Item does not match required item, continuing...", quest.getId(), task.getId(), event.getPlayer().getUniqueId()); + continue; + } - taskProgress.setProgress(progress + 1); + int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress); + super.debug("Incrementing task progress (now " + progress + ")", quest.getId(), task.getId(), event.getPlayer().getUniqueId()); - if ((int) taskProgress.getProgress() >= amount) { - taskProgress.setProgress(amount); - taskProgress.setCompleted(true); - } - } + if (progress >= amount) { + super.debug("Marking task as complete", quest.getId(), task.getId(), event.getPlayer().getUniqueId()); + taskProgress.setCompleted(true); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/CraftingTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/CraftingTaskType.java index 26a1517a..e1981d78 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/CraftingTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/CraftingTaskType.java @@ -74,60 +74,35 @@ public final class CraftingTaskType extends BukkitTaskType { return; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this, TaskUtils.TaskConstraint.WORLD)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - for (Task task : quest.getTasksOfType(super.getType())) { - if (!TaskUtils.validateWorld(player, task)) continue; + int amount = (int) task.getConfigValue("amount"); - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); - - if (taskProgress.isCompleted()) { - continue; - } - - Material material; - int amount = (int) task.getConfigValue("amount"); - Object configBlock = task.getConfigValue("item"); - Object configData = task.getConfigValue("data"); - - QuestItem qi; - if ((qi = fixedQuestItemCache.get(quest.getId(), task.getId())) == null) { - if (configBlock instanceof ConfigurationSection) { - qi = plugin.getConfiguredQuestItem("", (ConfigurationSection) configBlock); - } else { - material = Material.getMaterial(String.valueOf(configBlock)); - ItemStack is; - if (material == null) { - continue; - } - if (configData != null) { - is = new ItemStack(material, 1, ((Integer) configData).shortValue()); - } else { - is = new ItemStack(material, 1); - } - qi = new ParsedQuestItem("parsed", null, is); - } - fixedQuestItemCache.put(quest.getId(), task.getId(), qi); - } + QuestItem qi; + if ((qi = fixedQuestItemCache.get(quest.getId(), task.getId())) == null) { + QuestItem fetchedItem = TaskUtils.getConfigQuestItem(task, "item", "data"); + fixedQuestItemCache.put(quest.getId(), task.getId(), fetchedItem); + qi = fetchedItem; + } - if (!qi.compareItemStack(clickedItem)) continue; + super.debug("Player crafted " + clickedAmount + " of " + clickedItem.getType(), quest.getId(), task.getId(), player.getUniqueId()); - int progress; - if (taskProgress.getProgress() == null) { - progress = 0; - } else { - progress = (int) taskProgress.getProgress(); - } + if (!qi.compareItemStack(clickedItem)) { + super.debug("Item does not match, continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; + } - taskProgress.setProgress(progress + clickedAmount); + int progress = TaskUtils.getIntegerTaskProgress(taskProgress); + taskProgress.setProgress(progress + clickedAmount); + super.debug("Updating task progress (now " + (progress + clickedAmount) + ")", quest.getId(), task.getId(), player.getUniqueId()); - if ((int) taskProgress.getProgress() >= amount) { - taskProgress.setProgress(amount); - taskProgress.setCompleted(true); - } - } + if ((int) taskProgress.getProgress() >= amount) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setProgress(amount); + taskProgress.setCompleted(true); } } } @@ -170,7 +145,7 @@ public final class CraftingTaskType extends BukkitTaskType { } } - return amountOfItems > amountCanBeMade ? amountCanBeMade : amountOfItems; + return Math.min(amountOfItems, amountCanBeMade); } else { return e.getRecipe().getResult().getAmount(); } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/DealDamageTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/DealDamageTaskType.java index b49d2908..537e9fad 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/DealDamageTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/DealDamageTaskType.java @@ -10,6 +10,7 @@ import com.leonardobishop.quests.common.player.questprogressfile.TaskProgress; import com.leonardobishop.quests.common.quest.Quest; import com.leonardobishop.quests.common.quest.Task; import org.bukkit.entity.Creature; +import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -45,6 +46,7 @@ public final class DealDamageTaskType extends BukkitTaskType { } Player player = (Player) event.getDamager(); + Entity entity = event.getEntity(); double damage = event.getDamage(); if (player.hasMetadata("NPC")) return; @@ -54,41 +56,29 @@ public final class DealDamageTaskType extends BukkitTaskType { return; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this, TaskUtils.TaskConstraint.WORLD)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - for (Task task : quest.getTasksOfType(super.getType())) { - if (!TaskUtils.validateWorld(player, task)) continue; + super.debug("Player damaged " + entity.getType() + " for " + damage, quest.getId(), task.getId(), player.getUniqueId()); - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); - - if (taskProgress.isCompleted()) { - continue; - } - - // Do not count non-creatures damage (e.g. ArmorStands) - boolean allowOnlyCreatures = (boolean) task.getConfigValue("allow-only-creatures", true); - if (allowOnlyCreatures && !(event.getEntity() instanceof Creature)) { - continue; - } - - double progressDamage; - int damageNeeded = (int) task.getConfigValue("amount"); + boolean allowOnlyCreatures = TaskUtils.getConfigBoolean(task, "allow-only-creatures", true); + if (allowOnlyCreatures && !(event.getEntity() instanceof Creature)) { + super.debug(entity.getType() + " is not a creature but allow-only-creatures is true, continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; + } - if (taskProgress.getProgress() == null) { - progressDamage = 0.0; - } else { - progressDamage = (double) taskProgress.getProgress(); - } + double progressDamage = TaskUtils.getDecimalTaskProgress(taskProgress); + int damageNeeded = (int) task.getConfigValue("amount"); - taskProgress.setProgress(progressDamage + damage); + taskProgress.setProgress(progressDamage + damage); + super.debug("Updating task progress (now " + (progressDamage + damage) + ")", quest.getId(), task.getId(), player.getUniqueId()); - if (((double) taskProgress.getProgress()) >= (double) damageNeeded) { - taskProgress.setProgress(damageNeeded); - taskProgress.setCompleted(true); - } - } + if (((double) taskProgress.getProgress()) >= (double) damageNeeded) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setProgress(damageNeeded); + taskProgress.setCompleted(true); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/DistancefromTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/DistancefromTaskType.java index 6fb15fad..ae66a90d 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/DistancefromTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/DistancefromTaskType.java @@ -74,34 +74,34 @@ public final class DistancefromTaskType extends BukkitTaskType { return; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - for (Task task : quest.getTasksOfType(super.getType())) { - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); + super.debug("Player moved", quest.getId(), task.getId(), player.getUniqueId()); - if (taskProgress.isCompleted()) { - continue; - } + int x = (int) task.getConfigValue("x"); + int y = (int) task.getConfigValue("y"); + int z = (int) task.getConfigValue("z"); + String worldString = (String) task.getConfigValue("world"); + int distance = (int) task.getConfigValue("distance"); + int distanceSquared = distance * distance; - int x = (int) task.getConfigValue("x"); - int y = (int) task.getConfigValue("y"); - int z = (int) task.getConfigValue("z"); - String worldString = (String) task.getConfigValue("world"); - int distance = (int) task.getConfigValue("distance"); - int distanceSquared = distance * distance; + World world = Bukkit.getWorld(worldString); + if (world == null) { + super.debug("World " + worldString + " does not exist, continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; + } + + Location location = new Location(world, x, y, z); + double playerDistanceSquared = player.getLocation().distanceSquared(location); - World world = Bukkit.getWorld(worldString); - if (world == null) { - continue; - } + super.debug("Player is " + playerDistanceSquared + "m squared away", quest.getId(), task.getId(), player.getUniqueId()); - Location location = new Location(world, x, y, z); - if (player.getWorld().equals(world) && player.getLocation().distanceSquared(location) > distanceSquared) { - taskProgress.setCompleted(true); - } - } + if (player.getWorld().equals(world) && playerDistanceSquared > distanceSquared) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setCompleted(true); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/EnchantingTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/EnchantingTaskType.java index 3eda3316..06ddd019 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/EnchantingTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/EnchantingTaskType.java @@ -47,34 +47,21 @@ public final class EnchantingTaskType extends BukkitTaskType { return; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - for (Task task : quest.getTasksOfType(super.getType())) { - if (!TaskUtils.validateWorld(player, task)) continue; + super.debug("Player enchanted item", quest.getId(), task.getId(), player.getUniqueId()); - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); + int enchantsNeeded = (int) task.getConfigValue("amount"); - if (taskProgress.isCompleted()) { - continue; - } + int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress); + super.debug("Incrementing task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId()); - int enchantsNeeded = (int) task.getConfigValue("amount"); - - int progressEnchant; - if (taskProgress.getProgress() == null) { - progressEnchant = 0; - } else { - progressEnchant = (int) taskProgress.getProgress(); - } - - taskProgress.setProgress(progressEnchant + 1); - - if (((int) taskProgress.getProgress()) >= enchantsNeeded) { - taskProgress.setCompleted(true); - } - } + if (progress >= enchantsNeeded) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setCompleted(true); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/ExpEarnTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/ExpEarnTaskType.java index ccb12e2f..33983ef3 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/ExpEarnTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/ExpEarnTaskType.java @@ -9,6 +9,7 @@ import com.leonardobishop.quests.common.player.questprogressfile.QuestProgress; import com.leonardobishop.quests.common.player.questprogressfile.TaskProgress; import com.leonardobishop.quests.common.quest.Quest; import com.leonardobishop.quests.common.quest.Task; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.player.PlayerExpChangeEvent; @@ -37,43 +38,36 @@ public final class ExpEarnTaskType extends BukkitTaskType { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onExpEarn(PlayerExpChangeEvent e) { - if (e.getPlayer().hasMetadata("NPC")) return; + Player player = e.getPlayer(); + + if (player.hasMetadata("NPC")) return; QPlayer qPlayer = plugin.getPlayerManager().getPlayer(e.getPlayer().getUniqueId()); if (qPlayer == null) { return; } + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); + + int amountEarned = e.getAmount(); + + super.debug("Player earned " + amountEarned + " XP", quest.getId(), task.getId(), player.getUniqueId()); - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); - - for (Task task : quest.getTasksOfType(super.getType())) { - if (!TaskUtils.validateWorld(e.getPlayer(), task)) continue; + int expNeeded = (int) task.getConfigValue("amount"); - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); - - if (taskProgress.isCompleted()) { - continue; - } - int amount = e.getAmount(); - int expNeeded = (int) task.getConfigValue("amount"); - - int progressExp; - if (taskProgress.getProgress() == null) { - progressExp = 0; - } else { - progressExp = (int) taskProgress.getProgress(); - } - - taskProgress.setProgress(progressExp + amount); - - if (((int) taskProgress.getProgress()) >= expNeeded) { - taskProgress.setCompleted(true); - } - } + int progress = TaskUtils.getIntegerTaskProgress(taskProgress); + int newProgress = progress + amountEarned; + taskProgress.setProgress(newProgress); + super.debug("Updating task progress (now " + (newProgress) + ")", quest.getId(), task.getId(), player.getUniqueId()); + + if (newProgress >= expNeeded) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setProgress(newProgress); + taskProgress.setCompleted(true); } - } + } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/FarmingCertainTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/FarmingCertainTaskType.java index 4f475780..042c81a3 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/FarmingCertainTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/FarmingCertainTaskType.java @@ -100,64 +100,48 @@ public final class FarmingCertainTaskType extends BukkitTaskType { return; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this, TaskUtils.TaskConstraint.WORLD)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - for (Task task : quest.getTasksOfType(super.getType())) { - if (!TaskUtils.validateWorld(player, task)) continue; + super.debug("Player farmed crop " + crop.getMaterial() + " (mode = " + mode + ")", quest.getId(), task.getId(), player.getUniqueId()); - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); - - if (taskProgress.isCompleted()) { - continue; - } - - if (task.getConfigValue("mode") != null - && !mode.equalsIgnoreCase(task.getConfigValue("mode").toString())) { - continue; - } - - if (matchBlock(task, block)) { - int blocksNeeded = (int) task.getConfigValue("amount"); + if (task.getConfigValue("mode") != null + && !mode.equalsIgnoreCase(task.getConfigValue("mode").toString())) { + super.debug("Mode does not match required mode, continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; + } - int progressBlocks; - if (taskProgress.getProgress() == null) { - progressBlocks = 0; - } else { - progressBlocks = (int) taskProgress.getProgress(); - } + if (matchBlock(task, quest, player, block)) { + int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress); + super.debug("Incrementing task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId()); - taskProgress.setProgress(progressBlocks + 1); + int amount = (int) task.getConfigValue("amount"); - if (((int) taskProgress.getProgress()) >= blocksNeeded) { - taskProgress.setCompleted(true); - } - } + if (progress >= amount) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setCompleted(true); } } } } - private boolean matchBlock(Task task, Block block) { + private boolean matchBlock(Task task, Quest quest, Player player, Block block) { Material material; - Object configBlock = task.getConfigValues().containsKey("block") ? task.getConfigValue("block") : task.getConfigValue("blocks"); - Object configData = task.getConfigValue("data"); - - List<String> checkBlocks = new ArrayList<>(); - if (configBlock instanceof List) { - checkBlocks.addAll((List) configBlock); - } else { - checkBlocks.add(String.valueOf(configBlock)); - } + List<String> checkBlocks = TaskUtils.getConfigStringList(task, task.getConfigValues().containsKey("block") ? "block" : "blocks"); for (String materialName : checkBlocks) { + super.debug("Checking against '" + materialName + "'", quest.getId(), task.getId(), player.getUniqueId()); material = Material.getMaterial(String.valueOf(materialName)); Material blockType = block.getType(); if (blockType == material) { + super.debug("Type match", quest.getId(), task.getId(), player.getUniqueId()); return true; + } else { + super.debug("Type mismatch", quest.getId(), task.getId(), player.getUniqueId()); } } return false; diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/FishingCertainTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/FishingCertainTaskType.java index fbe846fa..44bde9f5 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/FishingCertainTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/FishingCertainTaskType.java @@ -1,6 +1,9 @@ package com.leonardobishop.quests.bukkit.tasktype.type; +import com.google.common.collect.HashBasedTable; +import com.google.common.collect.Table; import com.leonardobishop.quests.bukkit.BukkitQuestsPlugin; +import com.leonardobishop.quests.bukkit.item.QuestItem; import com.leonardobishop.quests.bukkit.tasktype.BukkitTaskType; import com.leonardobishop.quests.bukkit.util.TaskUtils; import com.leonardobishop.quests.common.config.ConfigProblem; @@ -26,6 +29,7 @@ import java.util.List; public final class FishingCertainTaskType extends BukkitTaskType { private final BukkitQuestsPlugin plugin; + private final Table<String, String, QuestItem> fixedQuestItemCache = HashBasedTable.create(); public FishingCertainTaskType(BukkitQuestsPlugin plugin) { super("fishingcertain", TaskUtils.TASK_ATTRIBUTION_STRING, "Catch a set amount of a specific item from the sea."); @@ -33,6 +37,11 @@ public final class FishingCertainTaskType extends BukkitTaskType { } @Override + public void onReady() { + fixedQuestItemCache.clear(); + } + + @Override public @NotNull List<ConfigProblem> validateConfig(@NotNull String root, @NotNull HashMap<String, Object> config) { ArrayList<ConfigProblem> problems = new ArrayList<>(); if (TaskUtils.configValidateExists(root + ".item", config.get("item"), problems, "item", super.getType())) { @@ -58,61 +67,40 @@ public final class FishingCertainTaskType extends BukkitTaskType { } Player player = event.getPlayer(); - if (!(event.getCaught() instanceof Item)) { + if (!(event.getCaught() instanceof Item caught)) { return; } - Item caught = (Item) event.getCaught(); QPlayer qPlayer = plugin.getPlayerManager().getPlayer(player.getUniqueId()); if (qPlayer == null) { return; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); - - for (Task task : quest.getTasksOfType(super.getType())) { - if (!TaskUtils.validateWorld(player, task)) continue; - - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - if (taskProgress.isCompleted()) { - continue; - } - - int catchesNeeded = (int) task.getConfigValue("amount"); - String configItem = (String) task.getConfigValue("item"); - Object configData = task.getConfigValue("data"); - - ItemStack is; - Material material = Material.getMaterial(String.valueOf(configItem)); - - if (material == null) { - continue; - } - if (configData != null) { - is = new ItemStack(material, 1, ((Integer) configData).shortValue()); - } else { - is = new ItemStack(material, 1); - } + QuestItem qi; + if ((qi = fixedQuestItemCache.get(quest.getId(), task.getId())) == null) { + QuestItem fetchedItem = TaskUtils.getConfigQuestItem(task, "item", "data"); + fixedQuestItemCache.put(quest.getId(), task.getId(), fetchedItem); + qi = fetchedItem; + } - if (!caught.getItemStack().isSimilar(is)) { - continue; - } + super.debug("Player fished item of type " + caught.getItemStack().getType(), quest.getId(), task.getId(), event.getPlayer().getUniqueId()); + if (!qi.compareItemStack(caught.getItemStack())) { + super.debug("Item does not match required item, continuing...", quest.getId(), task.getId(), event.getPlayer().getUniqueId()); + continue; + } - int progressCatches; - if (taskProgress.getProgress() == null) { - progressCatches = 0; - } else { - progressCatches = (int) taskProgress.getProgress(); - } + int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress); + super.debug("Incrementing task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId()); - taskProgress.setProgress(progressCatches + 1); + int catchesNeeded = (int) task.getConfigValue("amount"); - if (((int) taskProgress.getProgress()) >= catchesNeeded) { - taskProgress.setCompleted(true); - } - } + if (progress >= catchesNeeded) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setCompleted(true); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/InventoryTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/InventoryTaskType.java index 5d6f757b..898133ec 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/InventoryTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/InventoryTaskType.java @@ -106,116 +106,65 @@ public final class InventoryTaskType extends BukkitTaskType { return; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this, TaskUtils.TaskConstraint.WORLD)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - for (Task task : quest.getTasksOfType(super.getType())) { - if (!TaskUtils.validateWorld(player, task)) continue; + super.debug("Inventory check triggered", quest.getId(), task.getId(), player.getUniqueId()); - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); + int itemsNeeded = (int) task.getConfigValue("amount"); + boolean remove = TaskUtils.getConfigBoolean(task, "remove-items-when-complete"); + boolean allowPartial = TaskUtils.getConfigBoolean(task, "allow-partial-completion", true); - if (taskProgress.isCompleted()) { - continue; - } - - int itemsNeeded = (int) task.getConfigValue("amount"); - Object configBlock = task.getConfigValue("item"); - Object configData = task.getConfigValue("data"); - boolean remove = (boolean) task.getConfigValue("remove-items-when-complete", false); - boolean allowPartial = (boolean) task.getConfigValue("allow-partial-completion", false); - - QuestItem qi; - if ((qi = fixedQuestItemCache.get(quest.getId(), task.getId())) == null) { - if (configBlock instanceof ConfigurationSection) { - qi = plugin.getConfiguredQuestItem("", (ConfigurationSection) configBlock); - } else { - Material material = Material.getMaterial(String.valueOf(configBlock)); - if (material == null) { - continue; - } - - ItemStack is; - if (configData != null) { - is = new ItemStack(material, 1, ((Integer) configData).shortValue()); - } else { - is = new ItemStack(material, 1); - } - - qi = new ParsedQuestItem("parsed", null, is); - } - fixedQuestItemCache.put(quest.getId(), task.getId(), qi); - } + QuestItem qi; + if ((qi = fixedQuestItemCache.get(quest.getId(), task.getId())) == null) { + QuestItem fetchedItem = TaskUtils.getConfigQuestItem(task, "item", "data"); + fixedQuestItemCache.put(quest.getId(), task.getId(), fetchedItem); + qi = fetchedItem; + } - int progress; - if (taskProgress.getProgress() == null) { - progress = 0; - } else { - progress = (int) taskProgress.getProgress(); - } + int progress = TaskUtils.getIntegerTaskProgress(taskProgress); - int total; - int[] amountPerSlot = getAmountsPerSlot(player, qi); + int total; + int[] amountPerSlot = TaskUtils.getAmountsPerSlot(player, qi); + super.debug("Player has " + amountPerSlot[36] + " of the required item", quest.getId(), task.getId(), player.getUniqueId()); - if (allowPartial) { - total = Math.min(amountPerSlot[36], itemsNeeded - progress); + if (allowPartial) { + total = Math.min(amountPerSlot[36], itemsNeeded - progress); - if (total == 0) { - continue; - } + if (total == 0) { + continue; + } - // We must ALWAYS remove items if partial completion is allowed - // https://github.com/LMBishop/Quests/issues/375 - removeItemsInSlots(player, amountPerSlot, total); + // We must ALWAYS remove items if partial completion is allowed + // https://github.com/LMBishop/Quests/issues/375 + TaskUtils.removeItemsInSlots(player, amountPerSlot, total); + super.debug("Removing items from inventory", quest.getId(), task.getId(), player.getUniqueId()); - progress += total; - taskProgress.setProgress(progress); + progress += total; + taskProgress.setProgress(progress); + super.debug("Updating task progress (now " + (progress) + ")", quest.getId(), task.getId(), player.getUniqueId()); - if (progress >= itemsNeeded) { - taskProgress.setCompleted(true); - } - } else { - total = Math.min(amountPerSlot[36], itemsNeeded); + if (progress >= itemsNeeded) { + taskProgress.setCompleted(true); + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + } + } else { + total = Math.min(amountPerSlot[36], itemsNeeded); - taskProgress.setProgress(total); - if (total >= itemsNeeded) { - taskProgress.setCompleted(true); + taskProgress.setProgress(total); + if (total >= itemsNeeded) { + taskProgress.setCompleted(true); + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); - if (remove) { - removeItemsInSlots(player, amountPerSlot, total); - } - } + if (remove) { + TaskUtils.removeItemsInSlots(player, amountPerSlot, total); + super.debug("Removing items from inventory", quest.getId(), task.getId(), player.getUniqueId()); } } } } } - private int[] getAmountsPerSlot(Player player, QuestItem qi) { - int[] slotToAmount = new int[37]; - // idx 36 = total - for (int i = 0; i < 36; i++) { - ItemStack slot = player.getInventory().getItem(i); - if (slot == null || !qi.compareItemStack(slot)) - continue; - slotToAmount[36] = slotToAmount[36] + slot.getAmount(); - slotToAmount[i] = slot.getAmount(); - } - return slotToAmount; - } - - private void removeItemsInSlots(Player player, int[] amountPerSlot, int amountToRemove) { - for (int i = 0; i < 36; i++) { - if (amountPerSlot[i] == 0) continue; - - ItemStack slot = player.getInventory().getItem(i); - if (slot == null) continue; - - int amountInStack = slot.getAmount(); - int min = Math.max(0, amountInStack - amountToRemove); - slot.setAmount(min); - amountToRemove = amountToRemove - amountInStack; - if (amountToRemove <= 0) break; - } - } }
\ No newline at end of file diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/MilkingTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/MilkingTaskType.java index 6a2d93c3..811f528a 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/MilkingTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/MilkingTaskType.java @@ -55,36 +55,21 @@ public final class MilkingTaskType extends BukkitTaskType { return; } - QuestProgressFile questProgressFile = qPlayer.getQuestProgressFile(); + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player.getPlayer(), qPlayer, this, TaskUtils.TaskConstraint.WORLD)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + super.debug("Player milked cow", quest.getId(), task.getId(), player.getUniqueId()); - for (Task task : quest.getTasksOfType(super.getType())) { - if (!TaskUtils.validateWorld(player, task)) continue; + int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress); + super.debug("Incrementing task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId()); - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); + int breedingNeeded = (int) task.getConfigValue("amount"); - if (taskProgress.isCompleted()) { - continue; - } - - int cowsNeeded = (int) task.getConfigValue("amount"); - - int progressMilked; - if (taskProgress.getProgress() == null) { - progressMilked = 0; - } else { - progressMilked = (int) taskProgress.getProgress(); - } - - taskProgress.setProgress(progressMilked + 1); - - if (((int) taskProgress.getProgress()) >= cowsNeeded) { - taskProgress.setCompleted(true); - } - } + if (progress >= breedingNeeded) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setCompleted(true); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/MiningCertainTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/MiningCertainTaskType.java index 35fc9b81..bfb746d7 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/MiningCertainTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/MiningCertainTaskType.java @@ -12,6 +12,7 @@ import com.leonardobishop.quests.common.quest.Quest; import com.leonardobishop.quests.common.quest.Task; import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.block.BlockBreakEvent; @@ -81,30 +82,36 @@ public final class MiningCertainTaskType extends BukkitTaskType { return; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + Player player = event.getPlayer(); - for (Task task : quest.getTasksOfType(super.getType())) { - if (!TaskUtils.validateWorld(event.getPlayer(), task)) continue; + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(event.getPlayer(), qPlayer, this, TaskUtils.TaskConstraint.WORLD)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); + super.debug("Player mined block " + event.getBlock().getType(), quest.getId(), task.getId(), event.getPlayer().getUniqueId()); - if (taskProgress.isCompleted()) { - continue; - } + if (TaskUtils.matchBlock(this, pendingTask, event.getBlock(), player.getUniqueId())) { + boolean coreProtectEnabled = (boolean) task.getConfigValue("check-coreprotect", false); + int coreProtectTime = (int) task.getConfigValue("check-coreprotect-time", 3600); - if (matchBlock(task, event.getBlock())) { - boolean coreProtectEnabled = (boolean) task.getConfigValue("check-coreprotect", false); - int coreProtectTime = (int) task.getConfigValue("check-coreprotect-time", 3600); + if (coreProtectEnabled && plugin.getCoreProtectHook() == null) { + super.debug("check-coreprotect is enabled, but CoreProtect is not detected on the server", quest.getId(), task.getId(), player.getUniqueId()); + } - if (coreProtectEnabled - && plugin.getCoreProtectHook() != null - && plugin.getCoreProtectHook().checkBlock(event.getBlock(), coreProtectTime)) { - continue; - } - increment(task, taskProgress, 1); - } + if (plugin.getCoreProtectHook() != null && plugin.getCoreProtectHook().checkBlock(event.getBlock(), coreProtectTime)) { + super.debug("CoreProtect indicates blocks was recently placed, continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; + } + + int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress); + super.debug("Incrementing task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId()); + + int blocksNeeded = (int) task.getConfigValue("amount"); + + if (progress >= blocksNeeded) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setCompleted(true); } } } @@ -120,81 +127,25 @@ public final class MiningCertainTaskType extends BukkitTaskType { return; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); - - for (Task task : quest.getTasksOfType(super.getType())) { - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); - - if (taskProgress.isCompleted()) { - continue; - } - - if (task.getConfigValue("reverse-if-placed") != null && ((boolean) task.getConfigValue("reverse-if-placed"))) { - if (matchBlock(task, event.getBlock())) { - increment(task, taskProgress, -1); - } - } - } - } - } - } - - @SuppressWarnings("deprecation") - private boolean matchBlock(Task task, Block block) { - Material material; - - Object configBlock = task.getConfigValues().containsKey("block") ? task.getConfigValue("block") : task.getConfigValue("blocks"); - Object configData = task.getConfigValue("data"); - Object configSimilarBlocks = task.getConfigValue("use-similar-blocks"); + Player player = event.getPlayer(); - List<String> checkBlocks = new ArrayList<>(); - if (configBlock instanceof List) { - checkBlocks.addAll((List) configBlock); - } else { - checkBlocks.add(String.valueOf(configBlock)); - } + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(event.getPlayer(), qPlayer, this, TaskUtils.TaskConstraint.WORLD)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - for (String materialName : checkBlocks) { - // LOG:1 LOG:2 LOG should all be supported with this - String[] split = materialName.split(":"); - int comparableData = 0; - if (configData != null) { - comparableData = (int) configData; - } - if (split.length > 1) { - comparableData = Integer.parseInt(split[1]); - } + super.debug("Player placed block " + event.getBlock().getType(), quest.getId(), task.getId(), event.getPlayer().getUniqueId()); - material = Material.getMaterial(String.valueOf(split[0])); - Material blockType = block.getType(); - short blockData = block.getData(); - - if (blockType == material) { - if (((split.length == 1 && configData == null) || ((int) blockData) == comparableData)) - return true; + if (task.getConfigValue("reverse-if-placed") != null && ((boolean) task.getConfigValue("reverse-if-placed"))) { + super.debug("reverse-if-placed is enabled, checking block", quest.getId(), task.getId(), event.getPlayer().getUniqueId()); + if (TaskUtils.matchBlock(this, pendingTask, event.getBlock(), player.getUniqueId())) { + int progress = TaskUtils.getIntegerTaskProgress(taskProgress); + taskProgress.setProgress(--progress); + super.debug("Decrementing task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId()); + } } } - return false; - } - - private void increment(Task task, TaskProgress taskProgress, int amount) { - int brokenBlocksNeeded = (int) task.getConfigValue("amount"); - - int progressBlocksBroken; - if (taskProgress.getProgress() == null) { - progressBlocksBroken = 0; - } else { - progressBlocksBroken = (int) taskProgress.getProgress(); - } - - taskProgress.setProgress(progressBlocksBroken + amount); - - if (((int) taskProgress.getProgress()) >= brokenBlocksNeeded) { - taskProgress.setCompleted(true); - } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/MiningTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/MiningTaskType.java index addbde02..35a7b174 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/MiningTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/MiningTaskType.java @@ -5,7 +5,6 @@ import com.leonardobishop.quests.bukkit.tasktype.BukkitTaskType; import com.leonardobishop.quests.bukkit.util.TaskUtils; import com.leonardobishop.quests.common.config.ConfigProblem; import com.leonardobishop.quests.common.player.QPlayer; -import com.leonardobishop.quests.common.player.questprogressfile.QuestProgress; import com.leonardobishop.quests.common.player.questprogressfile.TaskProgress; import com.leonardobishop.quests.common.quest.Quest; import com.leonardobishop.quests.common.quest.Task; @@ -23,7 +22,6 @@ public final class MiningTaskType extends BukkitTaskType { private final BukkitQuestsPlugin plugin; public MiningTaskType(BukkitQuestsPlugin plugin) { - // type, author, description super("blockbreak", TaskUtils.TASK_ATTRIBUTION_STRING, "Break a set amount of blocks."); this.plugin = plugin; } @@ -38,41 +36,28 @@ public final class MiningTaskType extends BukkitTaskType { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onBlockBreak(BlockBreakEvent event) { - if (event.getPlayer().hasMetadata("NPC")) return; // citizens also causes these events to fire + if (event.getPlayer().hasMetadata("NPC")) return; - QPlayer qPlayer = plugin.getPlayerManager().getPlayer(event.getPlayer().getUniqueId()); // get the qplayer so you can get their progress + QPlayer qPlayer = plugin.getPlayerManager().getPlayer(event.getPlayer().getUniqueId()); if (qPlayer == null) { return; } - for (Quest quest : super.getRegisteredQuests()) { // iterate through all quests which are registered to use this task type - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(event.getPlayer(), qPlayer, this, TaskUtils.TaskConstraint.WORLD)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - for (Task task : quest.getTasksOfType(super.getType())) { // get all tasks of this type - if (!TaskUtils.validateWorld(event.getPlayer(), task)) continue; + super.debug("Player mined block", quest.getId(), task.getId(), event.getPlayer().getUniqueId()); - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); // get the task progress and increment progress by 1 + int brokenBlocksNeeded = (int) task.getConfigValue("amount"); - if (taskProgress.isCompleted()) { // dont need to increment a completed task - continue; - } + int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress); + super.debug("Incrementing task progress (now " + progress + ")", quest.getId(), task.getId(), event.getPlayer().getUniqueId()); - int brokenBlocksNeeded = (int) task.getConfigValue("amount"); // this will retrieve a value from the config under the key "value" - - int progressBlocksBroken; - if (taskProgress.getProgress() == null) { // note: if the player has never progressed before, getProgress() will return null - progressBlocksBroken = 0; - } else { - progressBlocksBroken = (int) taskProgress.getProgress(); - } - - taskProgress.setProgress(progressBlocksBroken + 1); // the progress does not have to be an int, although must be serializable by the yaml provider - - if (((int) taskProgress.getProgress()) >= brokenBlocksNeeded) { // completion statement, if true the task is complete - taskProgress.setCompleted(true); - } - } + if (progress >= brokenBlocksNeeded) { + super.debug("Marking task as complete", quest.getId(), task.getId(), event.getPlayer().getUniqueId()); + taskProgress.setCompleted(true); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/MobkillingCertainTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/MobkillingCertainTaskType.java index 631d88d7..74abe2d9 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/MobkillingCertainTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/MobkillingCertainTaskType.java @@ -89,76 +89,63 @@ public final class MobkillingCertainTaskType extends BukkitTaskType { return; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(killer, qPlayer, this, TaskUtils.TaskConstraint.WORLD)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - for (Task task : quest.getTasksOfType(super.getType())) { - if (!TaskUtils.validateWorld(killer, task)) continue; + List<String> configEntities = TaskUtils.getConfigStringList(task, task.getConfigValues().containsKey("mob") ? "mob" : "mobs"); - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); + super.debug("Player killed " + mob.getType(), quest.getId(), task.getId(), killer.getUniqueId()); - if (taskProgress.isCompleted()) { - continue; - } - - Object configEntity = task.getConfigValues().containsKey("mob") ? task.getConfigValue("mob") : task.getConfigValue("mobs"); - - List<String> configEntities = new ArrayList<>(); - if (configEntity instanceof List) { - configEntities.addAll((List) configEntity); - } else { - configEntities.add(String.valueOf(configEntity)); - } - boolean validMob = false; - for (String entry : configEntities) { - try { - EntityType entity = EntityType.valueOf(entry); - if (mob.getType() == entity) { - validMob = true; - } - } catch (IllegalArgumentException ignored) { } + boolean validMob = false; + for (String entry : configEntities) { + super.debug("Checking against mob '" + entry + "'", quest.getId(), task.getId(), killer.getUniqueId()); + try { + EntityType entity = EntityType.valueOf(entry); + if (mob.getType() == entity) { + super.debug("Mob is valid", quest.getId(), task.getId(), killer.getUniqueId()); + validMob = true; + break; } + } catch (IllegalArgumentException ignored) { } + } - if (!validMob) continue; - - Object configName = task.getConfigValues().containsKey("name") ? task.getConfigValue("name") : task.getConfigValue("names"); + if (!validMob) { + super.debug("Mob is not in list of required mobs, continuing...", quest.getId(), task.getId(), killer.getUniqueId()); + continue; + } - if (configName != null) { - List<String> configNames = new ArrayList<>(); - if (configName instanceof List) { - configNames.addAll((List) configName); - } else { - configNames.add(String.valueOf(configName)); - } + List<String> configNames = TaskUtils.getConfigStringList(task, task.getConfigValues().containsKey("name") ? "name" : "names"); - boolean validName = false; - for (String name : configNames) { - name = Chat.legacyColor(name); - if (mob.getCustomName() != null && !mob.getCustomName().equals(name)) { - validName = true; - break; - } - } + if (!configNames.isEmpty()) { + super.debug("List of required names exists; mob name is '" + Chat.legacyStrip(mob.getCustomName()) + "'", quest.getId(), task.getId(), killer.getUniqueId()); - if (!validName) continue; + boolean validName = false; + for (String name : configNames) { + super.debug("Checking against name '" + name + "'", quest.getId(), task.getId(), killer.getUniqueId()); + name = Chat.legacyColor(name); + if (mob.getCustomName() != null && !mob.getCustomName().equals(name)) { + super.debug("Mob has valid name", quest.getId(), task.getId(), killer.getUniqueId()); + validName = true; + break; } + } - int mobKillsNeeded = (int) task.getConfigValue("amount"); + if (!validName) { + super.debug("Mob name is not in list of valid name, continuing...", quest.getId(), task.getId(), killer.getUniqueId()); + continue; + } + } - int progressKills; - if (taskProgress.getProgress() == null) { - progressKills = 0; - } else { - progressKills = (int) taskProgress.getProgress(); - } + int mobKillsNeeded = (int) task.getConfigValue("amount"); - taskProgress.setProgress(progressKills + 1); + int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress); + super.debug("Incrementing task progress (now " + progress + ")", quest.getId(), task.getId(), killer.getUniqueId()); - if (((int) taskProgress.getProgress()) >= mobKillsNeeded) { - taskProgress.setCompleted(true); - } - } + if (progress >= mobKillsNeeded) { + super.debug("Marking task as complete", quest.getId(), task.getId(), killer.getUniqueId()); + taskProgress.setCompleted(true); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/PermissionTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/PermissionTaskType.java index 33dc0db9..2e86db85 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/PermissionTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/PermissionTaskType.java @@ -33,20 +33,22 @@ public final class PermissionTaskType extends BukkitTaskType { if (qPlayer == null) { continue; } - for (Quest quest : PermissionTaskType.super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); - for (Task task : quest.getTasksOfType(PermissionTaskType.super.getType())) { - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); - if (taskProgress.isCompleted()) { - continue; - } - String permission = (String) task.getConfigValue("permission"); - if (permission != null) { - if (player.hasPermission(permission)) { - taskProgress.setCompleted(true); - } - } + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, PermissionTaskType.this)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); + + PermissionTaskType.super.debug("Polling permissions for player", quest.getId(), task.getId(), player.getUniqueId()); + + String permission = (String) task.getConfigValue("permission"); + if (permission != null) { + PermissionTaskType.super.debug("Checking permission '" + permission + "'", quest.getId(), task.getId(), player.getUniqueId()); + if (player.hasPermission(permission)) { + PermissionTaskType.super.debug("Player has permission", quest.getId(), task.getId(), player.getUniqueId()); + PermissionTaskType.super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setCompleted(true); + } else { + PermissionTaskType.super.debug("Player does not have permission", quest.getId(), task.getId(), player.getUniqueId()); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/PlayerkillingTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/PlayerkillingTaskType.java index 670b233f..38fe8b6b 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/PlayerkillingTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/PlayerkillingTaskType.java @@ -54,34 +54,19 @@ public final class PlayerkillingTaskType extends BukkitTaskType { return; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(killer, qPlayer, this, TaskUtils.TaskConstraint.WORLD)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - for (Task task : quest.getTasksOfType(super.getType())) { - if (!TaskUtils.validateWorld(killer, task)) continue; + int playerKillsNeeded = (int) task.getConfigValue("amount"); - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); + int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress); + super.debug("Incrementing task progress (now " + progress + ")", quest.getId(), task.getId(), killer.getUniqueId()); - if (taskProgress.isCompleted()) { - continue; - } - - int playerKillsNeeded = (int) task.getConfigValue("amount"); - - int progressKills; - if (taskProgress.getProgress() == null) { - progressKills = 0; - } else { - progressKills = (int) taskProgress.getProgress(); - } - - taskProgress.setProgress(progressKills + 1); - - if (((int) taskProgress.getProgress()) >= playerKillsNeeded) { - taskProgress.setCompleted(true); - } - } + if (progress >= playerKillsNeeded) { + super.debug("Marking task as complete", quest.getId(), task.getId(), killer.getUniqueId()); + taskProgress.setCompleted(true); } } } 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 598b7647..ebb92cb8 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 @@ -50,31 +50,33 @@ public final class PlaytimeTaskType extends BukkitTaskType { continue; } - for (Quest quest : PlaytimeTaskType.super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); - for (Task task : quest.getTasksOfType(PlaytimeTaskType.super.getType())) { - if ((boolean) task.getConfigValue("ignore-afk", false) - && plugin.getEssentialsHook() != null - && plugin.getEssentialsHook().isAfk(player)) { - continue; - } - if (!TaskUtils.validateWorld(player, task)) continue; + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, PlaytimeTaskType.this)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); - if (taskProgress.isCompleted()) { - continue; - } - int minutes = (int) task.getConfigValue("minutes"); - if (taskProgress.getProgress() == null) { - taskProgress.setProgress(1); - } else { - taskProgress.setProgress((int) taskProgress.getProgress() + 1); - } - if (((int) taskProgress.getProgress()) >= minutes) { - taskProgress.setCompleted(true); - } - } + PlaytimeTaskType.super.debug("Polling playtime for player", quest.getId(), task.getId(), player.getUniqueId()); + + boolean ignoreAfk = (boolean) task.getConfigValue("ignore-afk", false); + + 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()); + } + + 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; + } + + 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()); + + if (progress >= minutes) { + PlaytimeTaskType.super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setCompleted(true); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/PositionTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/PositionTaskType.java index d21acbc3..666e8159 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/PositionTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/PositionTaskType.java @@ -60,37 +60,42 @@ public final class PositionTaskType extends BukkitTaskType { return; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - for (Task task : quest.getTasksOfType(super.getType())) { - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); + super.debug("Player moved", quest.getId(), task.getId(), player.getUniqueId()); - if (taskProgress.isCompleted()) { - continue; - } + int x = (int) task.getConfigValue("x"); + int y = (int) task.getConfigValue("y"); + int z = (int) task.getConfigValue("z"); + String worldString = (String) task.getConfigValue("world"); + int padding = 0; + if (task.getConfigValue("distance-padding") != null) { + padding = (int) task.getConfigValue("distance-padding"); + } + int paddingSquared = padding * padding; + World world = Bukkit.getWorld(worldString); + if (world == null) { + super.debug("World " + worldString + " does not exist, continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; + } + + Location location = new Location(world, x, y, z); + if (player.getWorld().equals(world) && player.getLocation().getBlockX() == location.getBlockX() && player.getLocation().getBlockY() == location.getBlockY() && player.getLocation().getBlockZ() == location.getBlockZ()) { + super.debug("Player is precisely at location", quest.getId(), task.getId(), player.getUniqueId()); + super.debug("Marking task as complete", quest.getId(), task.getId(), event.getPlayer().getUniqueId()); + taskProgress.setCompleted(true); + } else if (padding != 0 && player.getWorld().equals(world)) { + double playerDistanceSquared = player.getLocation().distanceSquared(location); - int x = (int) task.getConfigValue("x"); - int y = (int) task.getConfigValue("y"); - int z = (int) task.getConfigValue("z"); - String worldString = (String) task.getConfigValue("world"); - int padding = 0; - if (task.getConfigValue("distance-padding") != null) { - padding = (int) task.getConfigValue("distance-padding"); - } - int paddingSquared = padding * padding; - World world = Bukkit.getWorld(worldString); - if (world == null) { - continue; - } + super.debug("Player is " + playerDistanceSquared + "m squared away (padding squared = " + paddingSquared + ")", quest.getId(), task.getId(), player.getUniqueId()); - Location location = new Location(world, x, y, z); - if (player.getWorld().equals(world) && player.getLocation().getBlockX() == location.getBlockX() && player.getLocation().getBlockY() == location.getBlockY() && player.getLocation().getBlockZ() == location.getBlockZ()) { - taskProgress.setCompleted(true); - } else if (padding != 0 && player.getWorld().equals(world) && player.getLocation().distanceSquared(location) < paddingSquared) { - taskProgress.setCompleted(true); - } + if (playerDistanceSquared <= paddingSquared) { + super.debug("Player is within distance padding", quest.getId(), task.getId(), player.getUniqueId()); + super.debug("Marking task as complete", quest.getId(), task.getId(), event.getPlayer().getUniqueId()); + taskProgress.setCompleted(true); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/ShearingTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/ShearingTaskType.java index 842375a0..ccee2b35 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/ShearingTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/ShearingTaskType.java @@ -52,34 +52,21 @@ public final class ShearingTaskType extends BukkitTaskType { return; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player.getPlayer(), qPlayer, this, TaskUtils.TaskConstraint.WORLD)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - for (Task task : quest.getTasksOfType(super.getType())) { - if (!TaskUtils.validateWorld(player, task)) continue; + super.debug("Player sheared animal", quest.getId(), task.getId(), player.getUniqueId()); - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); + int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress); + super.debug("Incrementing task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId()); - if (taskProgress.isCompleted()) { - continue; - } + int sheepNeeded = (int) task.getConfigValue("amount"); - int sheepNeeded = (int) task.getConfigValue("amount"); - - int progressSheared; - if (taskProgress.getProgress() == null) { - progressSheared = 0; - } else { - progressSheared = (int) taskProgress.getProgress(); - } - - taskProgress.setProgress(progressSheared + 1); - - if (((int) taskProgress.getProgress()) >= sheepNeeded) { - taskProgress.setCompleted(true); - } - } + if (progress >= sheepNeeded) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setCompleted(true); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/SmeltingCertainTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/SmeltingCertainTaskType.java index fcbce83b..f737bd89 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/SmeltingCertainTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/SmeltingCertainTaskType.java @@ -66,12 +66,10 @@ public final class SmeltingCertainTaskType extends BukkitTaskType { || item == null || item.getType() == Material.AIR || event.getAction() == InventoryAction.NOTHING || event.getAction() == InventoryAction.COLLECT_TO_CURSOR && cursor != null && cursor.getAmount() == cursor.getMaxStackSize() || event.getClick() == ClickType.NUMBER_KEY && event.getAction() == InventoryAction.HOTBAR_MOVE_AND_READD - || !(event.getWhoClicked() instanceof Player)) { + || !(event.getWhoClicked() instanceof Player player)) { return; } - Player player = (Player) event.getWhoClicked(); - int eventAmount = item.getAmount(); if (event.isShiftClick()) { eventAmount = Math.min(eventAmount, plugin.getVersionSpecificHandler().getAvailableSpace(player, item)); @@ -85,65 +83,41 @@ public final class SmeltingCertainTaskType extends BukkitTaskType { return; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); - - for (Task task : quest.getTasksOfType(super.getType())) { - if (!TaskUtils.validateWorld(player, task)) continue; - - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); - - if (taskProgress.isCompleted()) { - continue; - } + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player.getPlayer(), qPlayer, this, TaskUtils.TaskConstraint.WORLD)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - if (task.getConfigValue("mode") != null - && !inventoryType.toString().equalsIgnoreCase(task.getConfigValue("mode").toString())) { - continue; - } + super.debug("Player smelted item", quest.getId(), task.getId(), player.getUniqueId()); - QuestItem qi; - Object configItem = task.getConfigValue("item"); - Object configData = task.getConfigValue("data"); - - if ((qi = fixedQuestItemCache.get(quest.getId(), task.getId())) == null) { - if (configItem instanceof ConfigurationSection) { - qi = plugin.getConfiguredQuestItem("", (ConfigurationSection) configItem); - } else { - Material material = Material.getMaterial(String.valueOf(configItem)); - if (material == null) { - continue; - } - - ItemStack is; - if (configData != null) { - is = new ItemStack(material, 1, ((Integer) configData).shortValue()); - } else { - is = new ItemStack(material, 1); - } - qi = new ParsedQuestItem("parsed", null, is); - } - fixedQuestItemCache.put(quest.getId(), task.getId(), qi); - } + if (task.getConfigValue("mode") != null + && !inventoryType.toString().equalsIgnoreCase(task.getConfigValue("mode").toString())) { + super.debug("Specific mode is required, but the actual mode '" + inventoryType + "' does not match, continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; + } - if (qi.compareItemStack(item)) { - int smeltedItemsNeeded = (int) task.getConfigValue("amount"); + QuestItem qi; + if ((qi = fixedQuestItemCache.get(quest.getId(), task.getId())) == null) { + QuestItem fetchedItem = TaskUtils.getConfigQuestItem(task, "item", "data"); + fixedQuestItemCache.put(quest.getId(), task.getId(), fetchedItem); + qi = fetchedItem; + } - int progressItemsSmelted; - if (taskProgress.getProgress() == null) { - progressItemsSmelted = 0; - } else { - progressItemsSmelted = (int) taskProgress.getProgress(); - } + if (qi.compareItemStack(item)) { + int smeltedItemsNeeded = (int) task.getConfigValue("amount"); - taskProgress.setProgress(progressItemsSmelted + eventAmount); + int progress = TaskUtils.getIntegerTaskProgress(taskProgress); + int newAmount = progress + eventAmount; + taskProgress.setProgress(newAmount); + super.debug("Updating task progress (now " + (newAmount) + ")", quest.getId(), task.getId(), player.getUniqueId()); - if (((int) taskProgress.getProgress()) >= smeltedItemsNeeded) { - taskProgress.setCompleted(true); - } - } + if (newAmount >= smeltedItemsNeeded) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setProgress(newAmount); + taskProgress.setCompleted(true); } + } else { + super.debug("Item does not match required item, continuing...", quest.getId(), task.getId(), player.getUniqueId()); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/TamingCertainTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/TamingCertainTaskType.java index 70aef33b..24a7128b 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/TamingCertainTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/TamingCertainTaskType.java @@ -63,46 +63,34 @@ public final class TamingCertainTaskType extends BukkitTaskType { return; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player.getPlayer(), qPlayer, this, TaskUtils.TaskConstraint.WORLD)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - for (Task task : quest.getTasksOfType(super.getType())) { - if (!TaskUtils.validateWorld(player, task)) continue; + super.debug("Played tamed entity", quest.getId(), task.getId(), player.getUniqueId()); - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); - - if (taskProgress.isCompleted()) { - continue; - } - - EntityType entityType; - try { - entityType = EntityType.valueOf((String) task.getConfigValue("mob")); - } catch (IllegalArgumentException ex) { - continue; - } - - if (event.getEntity().getType() != entityType) { - continue; - } + EntityType entityType; + try { + entityType = EntityType.valueOf((String) task.getConfigValue("mob")); + } catch (IllegalArgumentException ex) { + super.debug("Invalid entity in configuration, continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; + } - int tamesNeeded = (int) task.getConfigValue("amount"); + if (event.getEntity().getType() != entityType) { + super.debug("Tamed entity is not the correct type, continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; + } - int progress; - if (taskProgress.getProgress() == null) { - progress = 0; - } else { - progress = (int) taskProgress.getProgress(); - } + int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress); + super.debug("Incrementing task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId()); - progress += 1; - taskProgress.setProgress(progress); + int amount = (int) task.getConfigValue("amount"); - if (progress >= tamesNeeded) { - taskProgress.setCompleted(true); - } - } + if (progress >= amount) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setCompleted(true); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/TamingTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/TamingTaskType.java index e1fc1e73..976c875c 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/TamingTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/TamingTaskType.java @@ -51,35 +51,25 @@ public final class TamingTaskType extends BukkitTaskType { return; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player.getPlayer(), qPlayer, this, TaskUtils.TaskConstraint.WORLD)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - for (Task task : quest.getTasksOfType(super.getType())) { - if (!TaskUtils.validateWorld(player, task)) continue; + int tamesNeeded = (int) task.getConfigValue("amount"); - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); - - if (taskProgress.isCompleted()) { - continue; - } - - int tamesNeeded = (int) task.getConfigValue("amount"); - - int progress; - if (taskProgress.getProgress() == null) { - progress = 0; - } else { - progress = (int) taskProgress.getProgress(); - } + int progress; + if (taskProgress.getProgress() == null) { + progress = 0; + } else { + progress = (int) taskProgress.getProgress(); + } - progress += 1; - taskProgress.setProgress(progress); + progress += 1; + taskProgress.setProgress(progress); - if (progress >= tamesNeeded) { - taskProgress.setCompleted(true); - } - } + if (progress >= tamesNeeded) { + taskProgress.setCompleted(true); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/WalkingTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/WalkingTaskType.java index 8d227dfa..e2c47496 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/WalkingTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/WalkingTaskType.java @@ -52,73 +52,54 @@ public final class WalkingTaskType extends BukkitTaskType { return; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player.getPlayer(), qPlayer, this, TaskUtils.TaskConstraint.WORLD)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - for (Task task : quest.getTasksOfType(super.getType())) { - if (!TaskUtils.validateWorld(player, task)) continue; + super.debug("Player moved", quest.getId(), task.getId(), player.getUniqueId()); - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); - - if (taskProgress.isCompleted()) { - continue; - } - - if (task.getConfigValue("mode") != null - && !validateTransportMethod(player, task.getConfigValue("mode").toString())) { - continue; - } - - int distanceNeeded = (int) task.getConfigValue("distance"); + if (task.getConfigValue("mode") != null + && !validateTransportMethod(player, task.getConfigValue("mode").toString())) { + super.debug("Player's mode does not match required mode, continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; + } - int progressDistance; - if (taskProgress.getProgress() == null) { - progressDistance = 0; - } else { - progressDistance = (int) taskProgress.getProgress(); - } + int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress); + super.debug("Incrementing task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId()); - taskProgress.setProgress(progressDistance + 1); + int distanceNeeded = (int) task.getConfigValue("distance"); - if (((int) taskProgress.getProgress()) >= distanceNeeded) { - taskProgress.setCompleted(true); - } - } + if (progress >= distanceNeeded) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setCompleted(true); } } } private boolean validateTransportMethod(Player player, String mode) { - switch (mode.toLowerCase()) { - case "boat": - return player.getVehicle() != null && player.getVehicle().getType() == EntityType.BOAT; - case "horse": - return player.getVehicle() != null && player.getVehicle().getType() == EntityType.HORSE; - case "pig": - return player.getVehicle() != null && player.getVehicle().getType() == EntityType.PIG; - case "minecart": - return player.getVehicle() != null && player.getVehicle().getType() == EntityType.MINECART; - case "strider": - return plugin.getVersionSpecificHandler().isPlayerOnStrider(player); - case "sneaking": // sprinting does not matter - return player.isSneaking() && !player.isSwimming() && !player.isFlying() - && !plugin.getVersionSpecificHandler().isPlayerGliding(player); - case "walking": - return !player.isSneaking() && !player.isSwimming() && !player.isSprinting() && !player.isFlying() - && !plugin.getVersionSpecificHandler().isPlayerGliding(player); - case "running": - return !player.isSneaking() && !player.isSwimming() && player.isSprinting() && !player.isFlying() - && !plugin.getVersionSpecificHandler().isPlayerGliding(player); - case "swimming": // sprinting and sneaking do not matter, flying is not possible - return player.isSwimming() && !plugin.getVersionSpecificHandler().isPlayerGliding(player); - case "flying": // if the player is flying then the player is flying - return player.isFlying(); - case "elytra": // if the player is gliding then the player is gliding - return plugin.getVersionSpecificHandler().isPlayerGliding(player); - default: - return false; - } + return switch (mode.toLowerCase()) { + case "boat" -> player.getVehicle() != null && player.getVehicle().getType() == EntityType.BOAT; + case "horse" -> player.getVehicle() != null && player.getVehicle().getType() == EntityType.HORSE; + case "pig" -> player.getVehicle() != null && player.getVehicle().getType() == EntityType.PIG; + case "minecart" -> player.getVehicle() != null && player.getVehicle().getType() == EntityType.MINECART; + case "strider" -> plugin.getVersionSpecificHandler().isPlayerOnStrider(player); + case "sneaking" -> // sprinting does not matter + player.isSneaking() && !player.isSwimming() && !player.isFlying() + && !plugin.getVersionSpecificHandler().isPlayerGliding(player); + case "walking" -> + !player.isSneaking() && !player.isSwimming() && !player.isSprinting() && !player.isFlying() + && !plugin.getVersionSpecificHandler().isPlayerGliding(player); + case "running" -> !player.isSneaking() && !player.isSwimming() && player.isSprinting() && !player.isFlying() + && !plugin.getVersionSpecificHandler().isPlayerGliding(player); + case "swimming" -> // sprinting and sneaking do not matter, flying is not possible + player.isSwimming() && !plugin.getVersionSpecificHandler().isPlayerGliding(player); + case "flying" -> // if the player is flying then the player is flying + player.isFlying(); + case "elytra" -> // if the player is gliding then the player is gliding + plugin.getVersionSpecificHandler().isPlayerGliding(player); + default -> false; + }; } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/ASkyBlockLevelTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/ASkyBlockLevelTaskType.java index d8ed67da..9520a29d 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/ASkyBlockLevelTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/ASkyBlockLevelTaskType.java @@ -10,6 +10,8 @@ import com.leonardobishop.quests.common.player.questprogressfile.TaskProgress; import com.leonardobishop.quests.common.quest.Quest; import com.leonardobishop.quests.common.quest.Task; import com.wasteofplastic.askyblock.events.IslandPostLevelEvent; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.jetbrains.annotations.NotNull; @@ -42,25 +44,24 @@ public final class ASkyBlockLevelTaskType extends BukkitTaskType { return; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + Player player = Bukkit.getPlayer(event.getPlayer()); - for (Task task : quest.getTasksOfType(super.getType())) { - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - if (taskProgress.isCompleted()) { - continue; - } + super.debug("Player island level updated to " + event.getLongLevel(), quest.getId(), task.getId(), event.getPlayer()); - long islandLevelNeeded = (long) (int) task.getConfigValue("level"); + long islandLevelNeeded = (long) (int) task.getConfigValue("level"); - taskProgress.setProgress(event.getLongLevel()); + taskProgress.setProgress(event.getLongLevel()); + super.debug("Updating task progress (now " + event.getLongLevel() + ")", quest.getId(), task.getId(), event.getPlayer()); - if (((long) taskProgress.getProgress()) >= islandLevelNeeded) { - taskProgress.setCompleted(true); - } - } + if ((int) taskProgress.getProgress() >= islandLevelNeeded) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setProgress(islandLevelNeeded); + taskProgress.setCompleted(true); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/BentoBoxLevelTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/BentoBoxLevelTaskType.java index 3880d3e5..580aea6e 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/BentoBoxLevelTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/BentoBoxLevelTaskType.java @@ -10,6 +10,8 @@ import com.leonardobishop.quests.common.player.questprogressfile.TaskProgress; import com.leonardobishop.quests.common.quest.Quest; import com.leonardobishop.quests.common.quest.Task; import com.leonardobishop.quests.common.tasktype.TaskTypeManager; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.jetbrains.annotations.NotNull; import world.bentobox.bentobox.BentoBox; @@ -57,26 +59,29 @@ public final class BentoBoxLevelTaskType extends BukkitTaskType { continue; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + Player player = Bukkit.getPlayer(member); - for (Task task : quest.getTasksOfType(super.getType())) { - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); + if (player == null) { + continue; + } + + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - if (taskProgress.isCompleted()) { - continue; - } + long islandLevelNeeded = (long) (int) task.getConfigValue("level"); + long newLevel = (long) event.getKeyValues().get("level"); - long islandLevelNeeded = (long) (int) task.getConfigValue("level"); - long newLevel = (long) event.getKeyValues().get("level"); + super.debug("Player island level updated to " + newLevel, quest.getId(), task.getId(), member); - taskProgress.setProgress(event.getKeyValues().get("level")); + taskProgress.setProgress(newLevel); + super.debug("Updating task progress (now " + newLevel + ")", quest.getId(), task.getId(), player.getUniqueId()); - if (newLevel >= islandLevelNeeded) { - taskProgress.setCompleted(true); - } - } + if (newLevel >= islandLevelNeeded) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setProgress(newLevel); + taskProgress.setCompleted(true); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/CitizensDeliverTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/CitizensDeliverTaskType.java index b51b1265..6bab1df8 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/CitizensDeliverTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/CitizensDeliverTaskType.java @@ -76,125 +76,78 @@ public final class CitizensDeliverTaskType extends BukkitTaskType { return; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - for (Task task : quest.getTasksOfType(super.getType())) { - if (task.getConfigValue("npc-name") != null) { - if (!Chat.legacyStrip(Chat.legacyColor(String.valueOf(task.getConfigValue("npc-name")))) - .equals(Chat.legacyStrip(Chat.legacyColor(npc.getName())))) { - continue; - } - } else if (!task.getConfigValue("npc-id").equals(npc.getId())) { - continue; - } - if (!TaskUtils.validateWorld(player, task)) continue; - - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); + super.debug("Player clicked NPC", quest.getId(), task.getId(), player.getUniqueId()); - if (taskProgress.isCompleted()) { - continue; - } + if (task.getConfigValue("npc-name") != null) { + String npcName = Chat.legacyStrip(Chat.legacyColor(npc.getName())); + super.debug("NPC name is required, current name = '" + npcName + "'", quest.getId(), task.getId(), player.getUniqueId()); + if (!Chat.legacyStrip(Chat.legacyColor(String.valueOf(task.getConfigValue("npc-name")))) + .equals(npcName)) { + super.debug("NPC name does not match required name, continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; + } + } else if (!task.getConfigValue("npc-id").equals(npc.getId())) { + super.debug("NPC id ('" + npc.getId() + "') does not match required id, continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; + } - Material material; - int itemsNeeded = (int) task.getConfigValue("amount"); - Object configBlock = task.getConfigValue("item"); - Object configData = task.getConfigValue("data"); - boolean remove = (boolean) task.getConfigValue("remove-items-when-complete", false); - boolean allowPartial = (boolean) task.getConfigValue("allow-partial-completion", false); + int itemsNeeded = (int) task.getConfigValue("amount"); + boolean remove = TaskUtils.getConfigBoolean(task, "remove-items-when-complete"); + boolean allowPartial = TaskUtils.getConfigBoolean(task, "allow-partial-completion", true); - QuestItem qi; - if ((qi = fixedQuestItemCache.get(quest.getId(), task.getId())) == null) { - if (configBlock instanceof ConfigurationSection) { - qi = plugin.getConfiguredQuestItem("", (ConfigurationSection) configBlock); - } else { - material = Material.getMaterial(String.valueOf(configBlock)); - ItemStack is; - if (material == null) { - continue; - } - if (configData != null) { - is = new ItemStack(material, 1, ((Integer) configData).shortValue()); - } else { - is = new ItemStack(material, 1); - } - qi = new ParsedQuestItem("parsed", null, is); - } - fixedQuestItemCache.put(quest.getId(), task.getId(), qi); - } + QuestItem qi; + if ((qi = fixedQuestItemCache.get(quest.getId(), task.getId())) == null) { + QuestItem fetchedItem = TaskUtils.getConfigQuestItem(task, "item", "data"); + fixedQuestItemCache.put(quest.getId(), task.getId(), fetchedItem); + qi = fetchedItem; + } - int progress; - if (taskProgress.getProgress() == null) { - progress = 0; - } else { - progress = (int) taskProgress.getProgress(); - } + int progress = TaskUtils.getIntegerTaskProgress(taskProgress); - int total; - int[] amountPerSlot = getAmountsPerSlot(player, qi); + int total; + int[] amountPerSlot = TaskUtils.getAmountsPerSlot(player, qi); + super.debug("Player has " + amountPerSlot[36] + " of the required item", quest.getId(), task.getId(), player.getUniqueId()); - if (allowPartial) { - total = Math.min(amountPerSlot[36], itemsNeeded - progress); + if (allowPartial) { + total = Math.min(amountPerSlot[36], itemsNeeded - progress); - if (total == 0) { - continue; - } + if (total == 0) { + continue; + } - // We must ALWAYS remove items if partial completion is allowed - // https://github.com/LMBishop/Quests/issues/375 - removeItemsInSlots(player, amountPerSlot, total); + // We must ALWAYS remove items if partial completion is allowed + // https://github.com/LMBishop/Quests/issues/375 + TaskUtils.removeItemsInSlots(player, amountPerSlot, total); + super.debug("Removing items from inventory", quest.getId(), task.getId(), player.getUniqueId()); - progress += total; - taskProgress.setProgress(progress); + progress += total; + taskProgress.setProgress(progress); + super.debug("Updating task progress (now " + (progress) + ")", quest.getId(), task.getId(), player.getUniqueId()); - if (progress >= itemsNeeded) { - taskProgress.setCompleted(true); - } - } else { - total = Math.min(amountPerSlot[36], itemsNeeded); + if (progress >= itemsNeeded) { + taskProgress.setCompleted(true); + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + } + } else { + total = Math.min(amountPerSlot[36], itemsNeeded); - taskProgress.setProgress(total); - if (total >= itemsNeeded) { - taskProgress.setCompleted(true); + taskProgress.setProgress(total); + if (total >= itemsNeeded) { + taskProgress.setCompleted(true); + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); - if (remove) { - removeItemsInSlots(player, amountPerSlot, total); - } - } + if (remove) { + TaskUtils.removeItemsInSlots(player, amountPerSlot, total); + super.debug("Removing items from inventory", quest.getId(), task.getId(), player.getUniqueId()); } } } } } - - private int[] getAmountsPerSlot(Player player, QuestItem qi) { - int[] slotToAmount = new int[37]; - // idx 36 = total - for (int i = 0; i < 36; i++) { - ItemStack slot = player.getInventory().getItem(i); - if (slot == null || !qi.compareItemStack(slot)) - continue; - slotToAmount[36] = slotToAmount[36] + slot.getAmount(); - slotToAmount[i] = slot.getAmount(); - } - return slotToAmount; - } - - private void removeItemsInSlots(Player player, int[] amountPerSlot, int amountToRemove) { - for (int i = 0; i < 36; i++) { - if (amountPerSlot[i] == 0) continue; - - ItemStack slot = player.getInventory().getItem(i); - if (slot == null) continue; - - int amountInStack = slot.getAmount(); - int min = Math.max(0, amountInStack - amountToRemove); - slot.setAmount(min); - amountToRemove = amountToRemove - amountInStack; - if (amountToRemove <= 0) break; - } - } - } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/CitizensInteractTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/CitizensInteractTaskType.java index 31f9af67..a5df0d23 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/CitizensInteractTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/CitizensInteractTaskType.java @@ -11,6 +11,7 @@ import com.leonardobishop.quests.common.player.questprogressfile.TaskProgress; import com.leonardobishop.quests.common.quest.Quest; import com.leonardobishop.quests.common.quest.Task; import net.citizensnpcs.api.event.NPCRightClickEvent; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.jetbrains.annotations.NotNull; @@ -46,30 +47,30 @@ public final class CitizensInteractTaskType extends BukkitTaskType { return; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + Player player = event.getClicker(); - for (Task task : quest.getTasksOfType(super.getType())) { - if (!TaskUtils.validateWorld(event.getClicker(), task)) continue; + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - if (task.getConfigValue("npc-name") != null) { - if (!Chat.legacyStrip(Chat.legacyColor(String.valueOf(task.getConfigValue("npc-name")))) - .equals(Chat.legacyStrip(Chat.legacyColor(event.getNPC().getName())))) { - continue; - } - } else if (!task.getConfigValue("npc-id").equals(event.getNPC().getId())) { - continue; - } - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); + super.debug("Player clicked NPC", quest.getId(), task.getId(), player.getUniqueId()); - if (taskProgress.isCompleted()) { - continue; - } - - taskProgress.setCompleted(true); + if (task.getConfigValue("npc-name") != null) { + String npcName = Chat.legacyStrip(Chat.legacyColor(event.getNPC().getName())); + super.debug("NPC name is required, current name = '" + npcName + "'", quest.getId(), task.getId(), player.getUniqueId()); + if (!Chat.legacyStrip(Chat.legacyColor(String.valueOf(task.getConfigValue("npc-name")))) + .equals(npcName)) { + super.debug("NPC name does not match required name, continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; } + } else if (!task.getConfigValue("npc-id").equals(event.getNPC().getId())) { + super.debug("NPC id ('" + event.getNPC().getId() + "') does not match required id, continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; } + + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setCompleted(true); } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/EssentialsBalanceTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/EssentialsBalanceTaskType.java index b1a12206..9b259237 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/EssentialsBalanceTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/EssentialsBalanceTaskType.java @@ -70,25 +70,23 @@ public final class EssentialsBalanceTaskType extends BukkitTaskType { return; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + Player player = event.getPlayer(); - for (Task task : quest.getTasksOfType(super.getType())) { - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - if (taskProgress.isCompleted()) { - continue; - } + super.debug("Player balance updated to " + event.getNewBalance(), quest.getId(), task.getId(), player.getUniqueId()); - int earningsNeeded = (int) task.getConfigValue("amount"); + int earningsNeeded = (int) task.getConfigValue("amount"); - taskProgress.setProgress(event.getNewBalance()); + taskProgress.setProgress(event.getNewBalance()); + super.debug("Updating task progress (now " + event.getNewBalance() + ")", quest.getId(), task.getId(), player.getUniqueId()); - if (event.getNewBalance().compareTo(BigDecimal.valueOf(earningsNeeded)) > 0) { - taskProgress.setCompleted(true); - } - } + if (event.getNewBalance().compareTo(BigDecimal.valueOf(earningsNeeded)) > 0) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setCompleted(true); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/EssentialsMoneyEarnTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/EssentialsMoneyEarnTaskType.java index 6feb1a94..8c884e15 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/EssentialsMoneyEarnTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/EssentialsMoneyEarnTaskType.java @@ -10,6 +10,7 @@ import com.leonardobishop.quests.common.player.questprogressfile.TaskProgress; import com.leonardobishop.quests.common.quest.Quest; import com.leonardobishop.quests.common.quest.Task; import net.ess3.api.events.UserBalanceUpdateEvent; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.jetbrains.annotations.NotNull; @@ -43,30 +44,28 @@ public final class EssentialsMoneyEarnTaskType extends BukkitTaskType { return; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + Player player = event.getPlayer(); - for (Task task : quest.getTasksOfType(super.getType())) { - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - if (taskProgress.isCompleted()) { - continue; - } + super.debug("Player balance updated to " + event.getNewBalance(), quest.getId(), task.getId(), player.getUniqueId()); - int earningsNeeded = (int) task.getConfigValue("amount"); + int earningsNeeded = (int) task.getConfigValue("amount"); - BigDecimal current = (BigDecimal) taskProgress.getProgress(); - if (current == null) { - current = new BigDecimal(0); - } - BigDecimal newProgress = current.add(event.getNewBalance().subtract(event.getOldBalance())); - taskProgress.setProgress(newProgress); + BigDecimal current = (BigDecimal) taskProgress.getProgress(); + if (current == null) { + current = new BigDecimal(0); + } + BigDecimal newProgress = current.add(event.getNewBalance().subtract(event.getOldBalance())); + taskProgress.setProgress(newProgress); + super.debug("Updating task progress (now " + newProgress + ")", quest.getId(), task.getId(), player.getUniqueId()); - if (newProgress.compareTo(BigDecimal.valueOf(earningsNeeded)) > 0) { - taskProgress.setCompleted(true); - } - } + if (newProgress.compareTo(BigDecimal.valueOf(earningsNeeded)) > 0) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setCompleted(true); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/FabledSkyblockLevelTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/FabledSkyblockLevelTaskType.java index 953e8279..4a462024 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/FabledSkyblockLevelTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/FabledSkyblockLevelTaskType.java @@ -10,6 +10,8 @@ import com.leonardobishop.quests.common.player.questprogressfile.TaskProgress; import com.leonardobishop.quests.common.quest.Quest; import com.leonardobishop.quests.common.quest.Task; import com.songoda.skyblock.api.event.island.IslandLevelChangeEvent; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.jetbrains.annotations.NotNull; @@ -48,29 +50,31 @@ public final class FabledSkyblockLevelTaskType extends BukkitTaskType { continue; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + Player player = Bukkit.getPlayer(member); - for (Task task : quest.getTasksOfType(super.getType())) { - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); + if (player == null) { + continue; + } - if (taskProgress.isCompleted()) { - continue; - } + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - int islandValueNeeded = (int) task.getConfigValue("level"); + int islandLevelNeeded = (int) task.getConfigValue("level"); - taskProgress.setProgress(event.getLevel().getLevel()); + super.debug("Player island level updated to " + event.getLevel().getLevel(), quest.getId(), task.getId(), member); - if (((double) taskProgress.getProgress()) >= islandValueNeeded) { - taskProgress.setCompleted(true); - } - } + taskProgress.setProgress(event.getLevel().getLevel()); + super.debug("Updating task progress (now " + event.getLevel().getLevel() + ")", quest.getId(), task.getId(), member); + + if (event.getLevel().getLevel() >= islandLevelNeeded) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setProgress(islandLevelNeeded); + taskProgress.setCompleted(true); } } } - } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/IridiumSkyblockValueTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/IridiumSkyblockValueTaskType.java index 086224b5..9272bda2 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/IridiumSkyblockValueTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/IridiumSkyblockValueTaskType.java @@ -11,6 +11,8 @@ import com.leonardobishop.quests.common.player.questprogressfile.QuestProgress; import com.leonardobishop.quests.common.player.questprogressfile.TaskProgress; import com.leonardobishop.quests.common.quest.Quest; import com.leonardobishop.quests.common.quest.Task; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.jetbrains.annotations.NotNull; @@ -41,10 +43,10 @@ public final class IridiumSkyblockValueTaskType extends BukkitTaskType { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onIslandLevel(IslandWorthCalculatedEvent event) { Island island = event.getIsland(); - for (String player : island.members) { + for (String member : island.members) { UUID uuid; try { - uuid = UUID.fromString(player); + uuid = UUID.fromString(member); } catch (Exception e) { continue; } @@ -53,25 +55,28 @@ public final class IridiumSkyblockValueTaskType extends BukkitTaskType { continue; } - for (Quest quest : IridiumSkyblockValueTaskType.super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + Player player = Bukkit.getPlayer(member); - for (Task task : quest.getTasksOfType(IridiumSkyblockValueTaskType.super.getType())) { - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); + if (player == null) { + continue; + } + + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - if (taskProgress.isCompleted()) { - continue; - } + int islandValueNeeded = (int) task.getConfigValue("value"); - int islandValueNeeded = (int) task.getConfigValue("value"); + super.debug("Player island level updated to " + event.getIslandWorth(), quest.getId(), task.getId(), uuid); - taskProgress.setProgress(event.getIslandWorth()); + taskProgress.setProgress(event.getIslandWorth()); + super.debug("Updating task progress (now " + event.getIslandWorth() + ")", quest.getId(), task.getId(), uuid); - if (((double) taskProgress.getProgress()) >= islandValueNeeded) { - taskProgress.setCompleted(true); - } - } + if (((double) taskProgress.getProgress()) >= islandValueNeeded) { + super.debug("Marking task as complete", quest.getId(), task.getId(), uuid); + taskProgress.setProgress(islandValueNeeded); + taskProgress.setCompleted(true); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/MythicMobsKillingType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/MythicMobsKillingTaskType.java index 2087b69a..f13e51eb 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/MythicMobsKillingType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/MythicMobsKillingTaskType.java @@ -22,11 +22,11 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; -public final class MythicMobsKillingType extends BukkitTaskType { +public final class MythicMobsKillingTaskType extends BukkitTaskType { private final BukkitQuestsPlugin plugin; - public MythicMobsKillingType(BukkitQuestsPlugin plugin, String mythicMobsVersion) { + public MythicMobsKillingTaskType(BukkitQuestsPlugin plugin, String mythicMobsVersion) { super("mythicmobs_killing", TaskUtils.TASK_ATTRIBUTION_STRING, "Kill a set amount of a MythicMobs entity."); this.plugin = plugin; @@ -92,7 +92,7 @@ public final class MythicMobsKillingType extends BukkitTaskType { } private void handle(LivingEntity killer, Entity mob, String mobName, double level) { - if (killer == null) { + if (!(killer instanceof Player player)) { return; } @@ -105,46 +105,40 @@ public final class MythicMobsKillingType extends BukkitTaskType { return; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - for (Task task : quest.getTasksOfType(super.getType())) { - if (!TaskUtils.validateWorld(killer.getWorld().getName(), task)) continue; + String configName = (String) task.getConfigValue("name"); + int minMobLevel = (int) task.getConfigValue("min-level", -1); + int requiredLevel = (int) task.getConfigValue("level", -1); - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); + super.debug("Player killed mythic mob '" + mobName + "' (level = " + level + ")", quest.getId(), task.getId(), player.getUniqueId()); - if (taskProgress.isCompleted()) { - continue; - } - - String configName = (String) task.getConfigValue("name"); - int minMobLevel = (int) task.getConfigValue("min-level", -1); - int requiredLevel = (int) task.getConfigValue("level", -1); - - if (!mobName.equals(configName) || level < minMobLevel) { - continue; - } + if (!mobName.equals(configName)) { + super.debug("Name does not match required name, continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; + } - if (requiredLevel != -1 && level != requiredLevel) { - continue; - } + if (level < minMobLevel) { + super.debug("Minimum level is required and it is not high enough, continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; + } - int mobKillsNeeded = (int) task.getConfigValue("amount"); + if (requiredLevel != -1 && level != requiredLevel) { + super.debug("Specific level is required and it does not match, continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; + } - int progressKills; - if (taskProgress.getProgress() == null) { - progressKills = 0; - } else { - progressKills = (int) taskProgress.getProgress(); - } + int mobKillsNeeded = (int) task.getConfigValue("amount"); - taskProgress.setProgress(progressKills + 1); + int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress); + super.debug("Incrementing task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId()); - if (((int) taskProgress.getProgress()) >= mobKillsNeeded) { - taskProgress.setCompleted(true); - } - } + if (progress >= mobKillsNeeded) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setCompleted(true); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/NuVotifierVoteType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/NuVotifierVoteTaskType.java index ad8a3d98..9eb11e4d 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/NuVotifierVoteType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/NuVotifierVoteTaskType.java @@ -20,11 +20,11 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; -public final class NuVotifierVoteType extends BukkitTaskType { +public final class NuVotifierVoteTaskType extends BukkitTaskType { private final BukkitQuestsPlugin plugin; - public NuVotifierVoteType(BukkitQuestsPlugin plugin) { + public NuVotifierVoteTaskType(BukkitQuestsPlugin plugin) { super("nuvotifier_vote", TaskUtils.TASK_ATTRIBUTION_STRING, "Vote a set amount of times."); this.plugin = plugin; } @@ -51,32 +51,21 @@ public final class NuVotifierVoteType extends BukkitTaskType { return; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - for (Task task : quest.getTasksOfType(super.getType())) { - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); + super.debug("Player voted", quest.getId(), task.getId(), player.getUniqueId()); - if (taskProgress.isCompleted()) { - continue; - } + int votesNeeded = (int) task.getConfigValue("amount"); - int votesNeeded = (int) task.getConfigValue("amount"); + int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress); + super.debug("Incrementing task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId()); - int progressVotes; - if (taskProgress.getProgress() == null) { - progressVotes = 0; - } else { - progressVotes = (int) taskProgress.getProgress(); - } - - taskProgress.setProgress(progressVotes + 1); - - if (((int) taskProgress.getProgress()) >= votesNeeded) { - taskProgress.setCompleted(true); - } - } + if (progress >= votesNeeded) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setCompleted(true); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/PlaceholderAPIEvaluateTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/PlaceholderAPIEvaluateTaskType.java index 999beacf..344f2bf9 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/PlaceholderAPIEvaluateTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/PlaceholderAPIEvaluateTaskType.java @@ -75,62 +75,72 @@ public final class PlaceholderAPIEvaluateTaskType extends BukkitTaskType { continue; } - for (Quest quest : PlaceholderAPIEvaluateTaskType.super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); - for (Task task : quest.getTasksOfType(PlaceholderAPIEvaluateTaskType.super.getType())) { - if (!TaskUtils.validateWorld(player, task)) continue; - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); - if (taskProgress.isCompleted()) { + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, PlaceholderAPIEvaluateTaskType.this)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); + + PlaceholderAPIEvaluateTaskType.super.debug("Polling PAPI for player", quest.getId(), task.getId(), player.getUniqueId()); + + String placeholder = (String) task.getConfigValue("placeholder"); + String evaluates = String.valueOf(task.getConfigValue("evaluates")); + String configOperator = (String) task.getConfigValue("operator"); + Operator operator = null; + if (configOperator != null) { + try { + operator = Operator.valueOf(configOperator); + } catch (IllegalArgumentException ignored) { } + } + if (placeholder != null && evaluates != null) { + double numericEvaluates = 0; + if (operator != null) { + try { + numericEvaluates = Double.parseDouble(evaluates); + } catch (NumberFormatException ex) { + PlaceholderAPIEvaluateTaskType.super.debug("Numeric operator was specified but configured string to evaluate to cannot be parsed into a double, continuing...", quest.getId(), task.getId(), player.getUniqueId()); continue; } - String placeholder = (String) task.getConfigValue("placeholder"); - String evaluates = String.valueOf(task.getConfigValue("evaluates")); - String configOperator = (String) task.getConfigValue("operator"); - Operator operator = null; - if (configOperator != null) { - try { - operator = Operator.valueOf(configOperator); - } catch (IllegalArgumentException ignored) { } + } + + + String evaluated = PlaceholderAPI.setPlaceholders(player, placeholder); + PlaceholderAPIEvaluateTaskType.super.debug("Evaluation = '" + evaluated + "'", quest.getId(), task.getId(), player.getUniqueId()); + if (operator == null && evaluated.equals(evaluates)) { + PlaceholderAPIEvaluateTaskType.super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setCompleted(true); + } else if (operator != null) { + double numericEvaluated; + try { + numericEvaluated = Double.parseDouble(evaluated); + } catch (NumberFormatException ex) { + PlaceholderAPIEvaluateTaskType.super.debug("Numeric operator was specified but evaluated string cannot be parsed into a double, continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; } - if (placeholder != null && evaluates != null) { - double numericEvaluates = 0; - if (operator != null) { - try { - numericEvaluates = Double.parseDouble(evaluates); - } catch (NumberFormatException ex) { - continue; + PlaceholderAPIEvaluateTaskType.super.debug("Operator = " + operator, quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setProgress(numericEvaluated); + switch (operator) { + case GREATER_THAN -> { + if (numericEvaluated > numericEvaluates) { + PlaceholderAPIEvaluateTaskType.super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setCompleted(true); } } - - String evaluated = PlaceholderAPI.setPlaceholders(player, placeholder); - if (operator == null && evaluated.equals(evaluates)) { - taskProgress.setCompleted(true); - } else if (operator != null) { - double numericEvaluated; - try { - numericEvaluated = Double.parseDouble(evaluated); - } catch (NumberFormatException ex) { - continue; + case LESS_THAN -> { + if (numericEvaluated < numericEvaluates) { + PlaceholderAPIEvaluateTaskType.super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setCompleted(true); } - taskProgress.setProgress(numericEvaluated); - switch (operator) { - case GREATER_THAN: - if (numericEvaluated > numericEvaluates) - taskProgress.setCompleted(true); - continue; - case LESS_THAN: - if (numericEvaluated < numericEvaluates) - taskProgress.setCompleted(true); - continue; - case GREATER_THAN_OR_EQUAL_TO: - if (numericEvaluated >= numericEvaluates) - taskProgress.setCompleted(true); - continue; - case LESS_THAN_OR_EQUAL_TO: - if (numericEvaluated <= numericEvaluates) - taskProgress.setCompleted(true); - continue; + } + case GREATER_THAN_OR_EQUAL_TO -> { + if (numericEvaluated >= numericEvaluates) { + PlaceholderAPIEvaluateTaskType.super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setCompleted(true); + } + } + case LESS_THAN_OR_EQUAL_TO -> { + if (numericEvaluated <= numericEvaluates) { + PlaceholderAPIEvaluateTaskType.super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setCompleted(true); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/ShopGUIPlusBuyCertainTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/ShopGUIPlusBuyCertainTaskType.java index 25618a27..aaa1562d 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/ShopGUIPlusBuyCertainTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/ShopGUIPlusBuyCertainTaskType.java @@ -58,68 +58,48 @@ public final class ShopGUIPlusBuyCertainTaskType extends BukkitTaskType { Player player = result.getPlayer(); QPlayerManager playerManager = this.plugin.getPlayerManager(); - QPlayer qplayer = playerManager.getPlayer(player.getUniqueId()); - if (qplayer == null) { + QPlayer qPlayer = playerManager.getPlayer(player.getUniqueId()); + if (qPlayer == null) { return; } - World world = player.getWorld(); - String worldName = world.getName(); - ShopItem shopItem = result.getShopItem(); Shop shop = shopItem.getShop(); String shopId = shop.getId(); String itemId = shopItem.getId(); int amountBought = result.getAmount(); - - List<Quest> registeredQuests = super.getRegisteredQuests(); - for (Quest quest : registeredQuests) { - if (!qplayer.hasStartedQuest(quest)) { + + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); + + super.debug("Player bought item (shop = " + shopId + ", item id = " + itemId + ")", quest.getId(), task.getId(), player.getUniqueId()); + + String taskShopId = (String) task.getConfigValue("shop-id"); + if (taskShopId == null || !taskShopId.equals(shopId)) { + super.debug("Shop id does not match required id, continuing...", quest.getId(), task.getId(), player.getUniqueId()); continue; } - - QuestProgressFile questProgressFile = qplayer.getQuestProgressFile(); - QuestProgress questProgress = questProgressFile.getQuestProgress(quest); - - String questTypeName = super.getType(); - List<Task> taskList = quest.getTasksOfType(questTypeName); - for (Task task : taskList) { - if (!TaskUtils.validateWorld(worldName, task)) { - continue; - } - - String taskId = task.getId(); - TaskProgress taskProgress = questProgress.getTaskProgress(taskId); - if (taskProgress.isCompleted()) { - continue; - } - - String taskShopId = (String) task.getConfigValue("shop-id"); - if (taskShopId == null || !taskShopId.equals(shopId)) { - continue; - } - - String taskItemId = (String) task.getConfigValue("item-id"); - if (taskItemId == null || !taskItemId.equals(itemId)) { - continue; - } - - int amountNeeded = (int) task.getConfigValue("amount"); - - int progressAmount; - Object progress = taskProgress.getProgress(); - if (progress == null) { - progressAmount = 0; - } else { - progressAmount = (int) progress; - } - - int newProgress = (progressAmount + amountBought); - taskProgress.setProgress(newProgress); - - if (newProgress >= amountNeeded) { - taskProgress.setCompleted(true); - } + + String taskItemId = (String) task.getConfigValue("item-id"); + if (taskItemId == null || !taskItemId.equals(itemId)) { + super.debug("Item id does not match required id, continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; + } + + int amountNeeded = (int) task.getConfigValue("amount"); + + int progress = TaskUtils.getIntegerTaskProgress(taskProgress); + int newProgress = progress + amountBought; + taskProgress.setProgress(newProgress); + + super.debug("Updating task progress (now " + newProgress + ")", quest.getId(), task.getId(), player.getUniqueId()); + + if (newProgress >= amountNeeded) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setProgress(amountNeeded); + taskProgress.setCompleted(true); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/ShopGUIPlusSellCertainTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/ShopGUIPlusSellCertainTaskType.java index d5bc096c..0317277e 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/ShopGUIPlusSellCertainTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/ShopGUIPlusSellCertainTaskType.java @@ -55,71 +55,51 @@ public final class ShopGUIPlusSellCertainTaskType extends BukkitTaskType { if (shopAction != ShopAction.SELL && shopAction != ShopAction.SELL_ALL) { return; } - + Player player = result.getPlayer(); QPlayerManager playerManager = this.plugin.getPlayerManager(); - QPlayer qplayer = playerManager.getPlayer(player.getUniqueId()); - if (qplayer == null) { + QPlayer qPlayer = playerManager.getPlayer(player.getUniqueId()); + if (qPlayer == null) { return; } - - World world = player.getWorld(); - String worldName = world.getName(); - + ShopItem shopItem = result.getShopItem(); Shop shop = shopItem.getShop(); String shopId = shop.getId(); String itemId = shopItem.getId(); - int amountSold = result.getAmount(); - - List<Quest> registeredQuests = super.getRegisteredQuests(); - for (Quest quest : registeredQuests) { - if (!qplayer.hasStartedQuest(quest)) { + int amountBought = result.getAmount(); + + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); + + super.debug("Player bought item (shop = " + shopId + ", item id = " + itemId + ")", quest.getId(), task.getId(), player.getUniqueId()); + + String taskShopId = (String) task.getConfigValue("shop-id"); + if (taskShopId == null || !taskShopId.equals(shopId)) { + super.debug("Shop id does not match required id, continuing...", quest.getId(), task.getId(), player.getUniqueId()); continue; } - - QuestProgressFile questProgressFile = qplayer.getQuestProgressFile(); - QuestProgress questProgress = questProgressFile.getQuestProgress(quest); - - String questTypeName = super.getType(); - List<Task> taskList = quest.getTasksOfType(questTypeName); - for (Task task : taskList) { - if (!TaskUtils.validateWorld(worldName, task)) { - continue; - } - - String taskId = task.getId(); - TaskProgress taskProgress = questProgress.getTaskProgress(taskId); - if (taskProgress.isCompleted()) { - continue; - } - - String taskShopId = (String) task.getConfigValue("shop-id"); - if (taskShopId == null || !taskShopId.equals(shopId)) { - continue; - } - - String taskItemId = (String) task.getConfigValue("item-id"); - if (taskItemId == null || !taskItemId.equals(itemId)) { - continue; - } - - int amountNeeded = (int) task.getConfigValue("amount"); - - int progressAmount; - Object progress = taskProgress.getProgress(); - if (progress == null) { - progressAmount = 0; - } else { - progressAmount = (int) progress; - } - - int newProgress = (progressAmount + amountSold); - taskProgress.setProgress(newProgress); - - if (newProgress >= amountNeeded) { - taskProgress.setCompleted(true); - } + + String taskItemId = (String) task.getConfigValue("item-id"); + if (taskItemId == null || !taskItemId.equals(itemId)) { + super.debug("Item id does not match required id, continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; + } + + int amountNeeded = (int) task.getConfigValue("amount"); + + int progress = TaskUtils.getIntegerTaskProgress(taskProgress); + int newProgress = progress + amountBought; + taskProgress.setProgress(newProgress); + + super.debug("Updating task progress (now " + newProgress + ")", quest.getId(), task.getId(), player.getUniqueId()); + + if (newProgress >= amountNeeded) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setProgress(amountNeeded); + taskProgress.setCompleted(true); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/SuperiorSkyblockLevelType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/SuperiorSkyblockLevelType.java index 82808144..d652e2e6 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/SuperiorSkyblockLevelType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/SuperiorSkyblockLevelType.java @@ -11,6 +11,8 @@ import com.leonardobishop.quests.common.player.questprogressfile.QuestProgress; import com.leonardobishop.quests.common.player.questprogressfile.TaskProgress; import com.leonardobishop.quests.common.quest.Quest; import com.leonardobishop.quests.common.quest.Task; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.jetbrains.annotations.NotNull; @@ -39,32 +41,34 @@ public final class SuperiorSkyblockLevelType extends BukkitTaskType { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onIslandLevel(IslandWorthUpdateEvent event) { - for (SuperiorPlayer player : event.getIsland().getIslandMembers(true)) { - QPlayer qPlayer = plugin.getPlayerManager().getPlayer(player.getUniqueId()); + for (SuperiorPlayer superiorPlayer : event.getIsland().getIslandMembers(true)) { + QPlayer qPlayer = plugin.getPlayerManager().getPlayer(superiorPlayer.getUniqueId()); if (qPlayer == null) { continue; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + Player player = Bukkit.getPlayer(superiorPlayer.getUniqueId()); - for (Task task : quest.getTasksOfType(super.getType())) { - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); + if (player == null) { + continue; + } + + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - if (taskProgress.isCompleted()) { - continue; - } + super.debug("Player island level updated", quest.getId(), task.getId(), player.getUniqueId()); - double islandLevelNeeded = Double.parseDouble(String.valueOf(task.getConfigValue("level"))); - BigDecimal bd = new BigDecimal(islandLevelNeeded); + double islandLevelNeeded = Double.parseDouble(String.valueOf(task.getConfigValue("level"))); + BigDecimal bd = new BigDecimal(islandLevelNeeded); - taskProgress.setProgress(event.getNewLevel().doubleValue()); + taskProgress.setProgress(event.getNewLevel().doubleValue()); + super.debug("Updating task progress (now " + event.getNewLevel().doubleValue() + ")", quest.getId(), task.getId(), player.getUniqueId()); - if (event.getNewLevel().compareTo(bd) > 0) { - taskProgress.setCompleted(true); - } - } + if (event.getNewLevel().compareTo(bd) > 0) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setCompleted(true); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/SuperiorSkyblockWorthType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/SuperiorSkyblockWorthType.java index fdc72a6f..cc04aada 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/SuperiorSkyblockWorthType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/SuperiorSkyblockWorthType.java @@ -11,6 +11,8 @@ import com.leonardobishop.quests.common.player.questprogressfile.QuestProgress; import com.leonardobishop.quests.common.player.questprogressfile.TaskProgress; import com.leonardobishop.quests.common.quest.Quest; import com.leonardobishop.quests.common.quest.Task; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.jetbrains.annotations.NotNull; @@ -39,32 +41,34 @@ public final class SuperiorSkyblockWorthType extends BukkitTaskType { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onIslandLevel(IslandWorthUpdateEvent event) { - for (SuperiorPlayer player : event.getIsland().getIslandMembers(true)) { - QPlayer qPlayer = plugin.getPlayerManager().getPlayer(player.getUniqueId()); + for (SuperiorPlayer superiorPlayer : event.getIsland().getIslandMembers(true)) { + QPlayer qPlayer = plugin.getPlayerManager().getPlayer(superiorPlayer.getUniqueId()); if (qPlayer == null) { continue; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + Player player = Bukkit.getPlayer(superiorPlayer.getUniqueId()); - for (Task task : quest.getTasksOfType(super.getType())) { - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); + if (player == null) { + continue; + } + + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - if (taskProgress.isCompleted()) { - continue; - } + super.debug("Player island worth updated", quest.getId(), task.getId(), player.getUniqueId()); - double islandWorthNeeded = Double.parseDouble(String.valueOf(task.getConfigValue("worth"))); - BigDecimal bd = new BigDecimal(islandWorthNeeded); + double islandLevelNeeded = Double.parseDouble(String.valueOf(task.getConfigValue("worth"))); + BigDecimal bd = new BigDecimal(islandLevelNeeded); - taskProgress.setProgress(event.getNewWorth().doubleValue()); + taskProgress.setProgress(event.getNewLevel().doubleValue()); + super.debug("Updating task progress (now " + event.getNewLevel().doubleValue() + ")", quest.getId(), task.getId(), player.getUniqueId()); - if (event.getNewWorth().compareTo(bd) > 0) { - taskProgress.setCompleted(true); - } - } + if (event.getNewLevel().compareTo(bd) > 0) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setCompleted(true); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/VotingPluginVoteType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/VotingPluginVoteType.java index 7cc4ace8..02f2fbcb 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/VotingPluginVoteType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/VotingPluginVoteType.java @@ -51,32 +51,21 @@ public final class VotingPluginVoteType extends BukkitTaskType { return; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - for (Task task : quest.getTasksOfType(super.getType())) { - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); + super.debug("Player voted", quest.getId(), task.getId(), player.getUniqueId()); - if (taskProgress.isCompleted()) { - continue; - } + int votesNeeded = (int) task.getConfigValue("amount"); - int votesNeeded = (int) task.getConfigValue("amount"); + int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress); + super.debug("Incrementing task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId()); - int progressVotes; - if (taskProgress.getProgress() == null) { - progressVotes = 0; - } else { - progressVotes = (int) taskProgress.getProgress(); - } - - taskProgress.setProgress(progressVotes + 1); - - if (((int) taskProgress.getProgress()) >= votesNeeded) { - taskProgress.setCompleted(true); - } - } + if (progress >= votesNeeded) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setCompleted(true); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/uSkyBlockLevelTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/uSkyBlockLevelTaskType.java index 7c42ca9e..02d54156 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/uSkyBlockLevelTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/uSkyBlockLevelTaskType.java @@ -9,6 +9,8 @@ import com.leonardobishop.quests.common.player.questprogressfile.QuestProgress; import com.leonardobishop.quests.common.player.questprogressfile.TaskProgress; import com.leonardobishop.quests.common.quest.Quest; import com.leonardobishop.quests.common.quest.Task; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.jetbrains.annotations.NotNull; @@ -42,25 +44,24 @@ public final class uSkyBlockLevelTaskType extends BukkitTaskType { return; } - for (Quest quest : super.getRegisteredQuests()) { - if (qPlayer.hasStartedQuest(quest)) { - QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + Player player = event.getPlayer(); - for (Task task : quest.getTasksOfType(super.getType())) { - TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); - if (taskProgress.isCompleted()) { - continue; - } + super.debug("Player island level updated to " + event.getScore().getScore(), quest.getId(), task.getId(), player.getUniqueId()); - double islandLevelNeeded = (double) (int) task.getConfigValue("level"); + long islandLevelNeeded = (long) (int) task.getConfigValue("level"); - taskProgress.setProgress(event.getScore().getScore()); + taskProgress.setProgress(event.getScore().getScore()); + super.debug("Updating task progress (now " + event.getScore().getScore() + ")", quest.getId(), task.getId(), player.getUniqueId()); - if (((double) taskProgress.getProgress()) >= islandLevelNeeded) { - taskProgress.setCompleted(true); - } - } + if (event.getScore().getScore() >= islandLevelNeeded) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setProgress(islandLevelNeeded); + taskProgress.setCompleted(true); } } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/TaskUtils.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/TaskUtils.java index 66d804b6..a29233cb 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/TaskUtils.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/TaskUtils.java @@ -1,14 +1,27 @@ package com.leonardobishop.quests.bukkit.util; import com.leonardobishop.quests.bukkit.BukkitQuestsPlugin; +import com.leonardobishop.quests.bukkit.item.ParsedQuestItem; +import com.leonardobishop.quests.bukkit.item.QuestItem; +import com.leonardobishop.quests.bukkit.tasktype.BukkitTaskType; import com.leonardobishop.quests.common.config.ConfigProblem; import com.leonardobishop.quests.common.config.ConfigProblemDescriptions; +import com.leonardobishop.quests.common.player.QPlayer; +import com.leonardobishop.quests.common.player.questprogressfile.QuestProgress; +import com.leonardobishop.quests.common.player.questprogressfile.TaskProgress; +import com.leonardobishop.quests.common.quest.Quest; import com.leonardobishop.quests.common.quest.Task; +import com.leonardobishop.quests.common.tasktype.TaskType; import org.bukkit.Material; +import org.bukkit.block.Block; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.UUID; public class TaskUtils { @@ -49,6 +62,186 @@ public class TaskUtils { return true; } + public static List<String> getConfigStringList(Task task, String key) { + Object configObject = task.getConfigValue(key); + + List<String> strings = new ArrayList<>(); + if (configObject instanceof List) { + strings.addAll((List) configObject); + } else { + strings.add(String.valueOf(configObject)); + } + return strings; + } + + public static boolean getConfigBoolean(Task task, String key) { + return getConfigBoolean(task, key, false); + } + + public static boolean getConfigBoolean(Task task, String key, boolean def) { + Object configObject = task.getConfigValue(key); + + boolean bool = def; + if (configObject != null) { + bool = (boolean) task.getConfigValue(key, def); + } + return bool; + } + + public static QuestItem getConfigQuestItem(Task task, String itemKey, String dataKey) { + Object configBlock = task.getConfigValue(itemKey); + Object configData = task.getConfigValue(dataKey); + + QuestItem questItem; + if (configBlock instanceof ConfigurationSection) { + questItem = plugin.getConfiguredQuestItem("", (ConfigurationSection) configBlock); + } else { + Material material = Material.getMaterial(String.valueOf(configBlock)); + ItemStack is; + if (material == null) { + material = Material.STONE; + } + if (configData != null) { + is = new ItemStack(material, 1, ((Integer) configData).shortValue()); + } else { + is = new ItemStack(material, 1); + } + questItem = new ParsedQuestItem("parsed", null, is); + } + + return questItem; + } + + public static double getDecimalTaskProgress(TaskProgress taskProgress) { + double progress; + if (taskProgress.getProgress() == null) { + progress = 0.0; + } else { + progress = (double) taskProgress.getProgress(); + } + return progress; + } + + public static int getIntegerTaskProgress(TaskProgress taskProgress) { + int progress; + if (taskProgress.getProgress() == null) { + progress = 0; + } else { + progress = (int) taskProgress.getProgress(); + } + return progress; + } + + public static int incrementIntegerTaskProgress(TaskProgress taskProgress) { + int progress = getIntegerTaskProgress(taskProgress); + taskProgress.setProgress(++progress); + return progress; + } + + public static List<PendingTask> getApplicableTasks(Player player, QPlayer qPlayer, TaskType type, TaskConstraint... constraints) { + List<PendingTask> tasks = new ArrayList<>(); + List<TaskConstraint> taskConstraints = Arrays.asList(constraints); + + for (Quest quest : type.getRegisteredQuests()) { + if (qPlayer.hasStartedQuest(quest)) { + QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest); + + for (Task task : quest.getTasksOfType(type.getType())) { + if (taskConstraints.contains(TaskConstraint.WORLD)) { + if (!TaskUtils.validateWorld(player, task)) { + continue; + } + } + + TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); + + if (taskProgress.isCompleted()) { + continue; + } + + tasks.add(new PendingTask(quest, task, questProgress, taskProgress)); + } + } + } + + return tasks; + } + + public record PendingTask(Quest quest, Task task, QuestProgress questProgress, TaskProgress taskProgress) { } + + public enum TaskConstraint { + WORLD + } + + public static boolean matchBlock(BukkitTaskType type, PendingTask pendingTask, Block block, UUID player) { + Task task = pendingTask.task; + + Material material; + + Object configData = task.getConfigValue("data"); + + List<String> checkBlocks = TaskUtils.getConfigStringList(task, task.getConfigValues().containsKey("block") ? "block" : "blocks"); + + for (String materialName : checkBlocks) { + // LOG:1 LOG:2 LOG should all be supported with this + String[] split = materialName.split(":"); + int comparableData = 0; + if (configData != null) { + comparableData = (int) configData; + } + if (split.length > 1) { + comparableData = Integer.parseInt(split[1]); + } + + material = Material.getMaterial(String.valueOf(split[0])); + Material blockType = block.getType(); + + short blockData = block.getData(); + + type.debug("Checking against block " + material, pendingTask.quest.getId(), task.getId(), player); + + if (blockType == material) { + if (((split.length == 1 && configData == null) || ((int) blockData) == comparableData)) { + type.debug("Block match", pendingTask.quest.getId(), task.getId(), player); + return true; + } else { + type.debug("Data mismatch", pendingTask.quest.getId(), task.getId(), player); + } + } else { + type.debug("Type mismatch", pendingTask.quest.getId(), task.getId(), player); + } + } + return false; + } + + public static int[] getAmountsPerSlot(Player player, QuestItem qi) { + int[] slotToAmount = new int[37]; + // idx 36 = total + for (int i = 0; i < 36; i++) { + ItemStack slot = player.getInventory().getItem(i); + if (slot == null || !qi.compareItemStack(slot)) + continue; + slotToAmount[36] = slotToAmount[36] + slot.getAmount(); + slotToAmount[i] = slot.getAmount(); + } + return slotToAmount; + } + + public static void removeItemsInSlots(Player player, int[] amountPerSlot, int amountToRemove) { + for (int i = 0; i < 36; i++) { + if (amountPerSlot[i] == 0) continue; + + ItemStack slot = player.getInventory().getItem(i); + if (slot == null) continue; + + int amountInStack = slot.getAmount(); + int min = Math.max(0, amountInStack - amountToRemove); + slot.setAmount(min); + amountToRemove = amountToRemove - amountInStack; + if (amountToRemove <= 0) break; + } + } + public static void configValidateNumber(String path, Object object, List<ConfigProblem> problems, boolean allowNull, boolean greaterThanZero, String... args) { if (object == null) { if (!allowNull) { |
