From 03f899de659047448c5648fa8f75bbe8f8cd28f1 Mon Sep 17 00:00:00 2001 From: Krakenied Date: Sat, 14 Feb 2026 09:02:50 +0100 Subject: Add blocklootdispensing task type Allows for making trial chambers vaults tasks --- .../quests/bukkit/BukkitQuestsPlugin.java | 2 + .../tasktype/type/BlockItemdroppingTaskType.java | 62 ++++++---- .../tasktype/type/BlockLootdispensingTaskType.java | 133 +++++++++++++++++++++ 3 files changed, 175 insertions(+), 22 deletions(-) create mode 100644 bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/BlockLootdispensingTaskType.java (limited to 'bukkit') 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 fe361ebf..c6d9257d 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/BukkitQuestsPlugin.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/BukkitQuestsPlugin.java @@ -58,6 +58,7 @@ import com.leonardobishop.quests.bukkit.storage.ModernYAMLStorageProvider; import com.leonardobishop.quests.bukkit.tasktype.BukkitTaskTypeManager; import com.leonardobishop.quests.bukkit.tasktype.type.BarteringTaskType; import com.leonardobishop.quests.bukkit.tasktype.type.BlockItemdroppingTaskType; +import com.leonardobishop.quests.bukkit.tasktype.type.BlockLootdispensingTaskType; import com.leonardobishop.quests.bukkit.tasktype.type.BlockchangingTaskType; import com.leonardobishop.quests.bukkit.tasktype.type.BlockfertilizingTaskType; import com.leonardobishop.quests.bukkit.tasktype.type.BlockshearingTaskType; @@ -476,6 +477,7 @@ public class BukkitQuestsPlugin extends JavaPlugin implements Quests { // Register task types with class/method compatibility requirement taskTypeManager.registerTaskType(() -> new BarteringTaskType(this), () -> CompatUtils.classExists("org.bukkit.event.entity.PiglinBarterEvent")); taskTypeManager.registerTaskType(() -> new BlockItemdroppingTaskType(this), () -> CompatUtils.classExists("org.bukkit.event.block.BlockDropItemEvent")); + taskTypeManager.registerTaskType(() -> new BlockLootdispensingTaskType(this), () -> CompatUtils.classExists("org.bukkit.event.block.BlockDispenseLootEvent")); taskTypeManager.registerTaskType(() -> new BlockchangingTaskType(this), () -> CompatUtils.classWithMethodExists("org.bukkit.event.entity.EntityChangeBlockEvent", "getBlockData")); taskTypeManager.registerTaskType(() -> new BlockfertilizingTaskType(this), () -> CompatUtils.classExists("org.bukkit.event.block.BlockFertilizeEvent")); taskTypeManager.registerTaskType(() -> new BlockshearingTaskType(this), () -> CompatUtils.classExists("io.papermc.paper.event.block.PlayerShearBlockEvent")); diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/BlockItemdroppingTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/BlockItemdroppingTaskType.java index 21b9c0ea..e1c93acd 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/BlockItemdroppingTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/BlockItemdroppingTaskType.java @@ -36,6 +36,7 @@ public final class BlockItemdroppingTaskType extends BukkitTaskType { super.addConfigValidator(TaskUtils.useIntegerConfigValidator(this, "data")); super.addConfigValidator(TaskUtils.useBooleanConfigValidator(this, "exact-match")); super.addConfigValidator(TaskUtils.useMaterialListConfigValidator(this, TaskUtils.MaterialListConfigValidatorMode.BLOCK, "block", "blocks")); + super.addConfigValidator(TaskUtils.useBooleanConfigValidator(this, "count-drops")); } @Override @@ -56,14 +57,18 @@ public final class BlockItemdroppingTaskType extends BukkitTaskType { } BlockState state = event.getBlockState(); - List drops = event.getItems(); - for (Item drop : drops) { - handle(player, qPlayer, state, drop.getItemStack()); + + if (drops.isEmpty()) { + handle(player, qPlayer, state, null, 0); + } + + for (int i = 0; i < drops.size(); i++) { + handle(player, qPlayer, state, drops.get(i).getItemStack(), i); } } - private void handle(Player player, QPlayer qPlayer, BlockState state, ItemStack item) { + private void handle(Player player, QPlayer qPlayer, BlockState state, ItemStack item, int i) { for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this, TaskConstraintSet.ALL)) { Quest quest = pendingTask.quest(); Task task = pendingTask.task(); @@ -71,28 +76,41 @@ public final class BlockItemdroppingTaskType extends BukkitTaskType { super.debug("Player dropped item from block", quest.getId(), task.getId(), player.getUniqueId()); - if (task.hasConfigKey("item")) { - if (item == null) { - super.debug("Specific item is required, dropped item is null; continuing...", quest.getId(), task.getId(), player.getUniqueId()); + Boolean countDrops = (Boolean) task.getConfigValue("count-drops"); + final int amountToIncrease; + + if (Boolean.TRUE.equals(countDrops)) { + if (i != 0) { continue; } - super.debug("Specific item is required; dropped item is of type '" + item.getType() + "'", quest.getId(), task.getId(), player.getUniqueId()); - - 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; + amountToIncrease = 1; + } else { + if (task.hasConfigKey("item")) { + if (item == null) { + super.debug("Specific item is required, dropped item is null; continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; + } + + super.debug("Specific item is required; dropped item is of type '" + item.getType() + "'", quest.getId(), task.getId(), player.getUniqueId()); + + 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; + } + + boolean exactMatch = TaskUtils.getConfigBoolean(task, "exact-match", true); + if (!qi.compareItemStack(item, exactMatch)) { + super.debug("Item does not match required item, continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; + } else { + super.debug("Item matches required item", quest.getId(), task.getId(), player.getUniqueId()); + } } - boolean exactMatch = TaskUtils.getConfigBoolean(task, "exact-match", true); - if (!qi.compareItemStack(item, exactMatch)) { - super.debug("Item does not match required item, continuing...", quest.getId(), task.getId(), player.getUniqueId()); - continue; - } else { - super.debug("Item matches required item", quest.getId(), task.getId(), player.getUniqueId()); - } + amountToIncrease = item != null ? item.getAmount() : 0; } if (!TaskUtils.matchBlock(this, pendingTask, state, player.getUniqueId())) { @@ -100,7 +118,7 @@ public final class BlockItemdroppingTaskType extends BukkitTaskType { continue; } - int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress); + int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress, amountToIncrease); super.debug("Incrementing task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId()); int amount = (int) task.getConfigValue("amount"); diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/BlockLootdispensingTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/BlockLootdispensingTaskType.java new file mode 100644 index 00000000..dfab6231 --- /dev/null +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/BlockLootdispensingTaskType.java @@ -0,0 +1,133 @@ +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.bukkit.util.constraint.TaskConstraintSet; +import com.leonardobishop.quests.common.player.QPlayer; +import com.leonardobishop.quests.common.player.questprogressfile.TaskProgress; +import com.leonardobishop.quests.common.quest.Quest; +import com.leonardobishop.quests.common.quest.Task; +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.BlockDispenseLootEvent; +import org.bukkit.inventory.ItemStack; + +import java.util.List; + +public final class BlockLootdispensingTaskType extends BukkitTaskType { + + private final BukkitQuestsPlugin plugin; + private final Table fixedQuestItemCache = HashBasedTable.create(); + + public BlockLootdispensingTaskType(BukkitQuestsPlugin plugin) { + super("blocklootdispensing", TaskUtils.TASK_ATTRIBUTION_STRING, "Dispense certain amount of loot from a block."); + this.plugin = plugin; + + super.addConfigValidator(TaskUtils.useRequiredConfigValidator(this, "amount")); + super.addConfigValidator(TaskUtils.useIntegerConfigValidator(this, "amount")); + super.addConfigValidator(TaskUtils.useItemStackConfigValidator(this, "item")); + super.addConfigValidator(TaskUtils.useIntegerConfigValidator(this, "data")); + super.addConfigValidator(TaskUtils.useBooleanConfigValidator(this, "exact-match")); + super.addConfigValidator(TaskUtils.useMaterialListConfigValidator(this, TaskUtils.MaterialListConfigValidatorMode.BLOCK, "block", "blocks")); + super.addConfigValidator(TaskUtils.useBooleanConfigValidator(this, "count-dispenses")); + } + + @Override + public void onReady() { + fixedQuestItemCache.clear(); + } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onBlockDispenseLoot(BlockDispenseLootEvent event) { + Player player = event.getPlayer(); + if (player == null || player.hasMetadata("NPC")) { + return; + } + + QPlayer qPlayer = plugin.getPlayerManager().getPlayer(player.getUniqueId()); + if (qPlayer == null) { + return; + } + + Block block = event.getBlock(); + List loots = event.getDispensedLoot(); + + if (loots.isEmpty()) { + handle(player, qPlayer, block, null, 0); + } + + for (int i = 0; i < loots.size(); i++) { + handle(player, qPlayer, block, loots.get(i), i); + } + } + + private void handle(Player player, QPlayer qPlayer, Block block, ItemStack item, int i) { + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this, TaskConstraintSet.ALL)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); + + super.debug("Player dispensed loot from block", quest.getId(), task.getId(), player.getUniqueId()); + + Boolean countDispenses = (Boolean) task.getConfigValue("count-dispenses"); + final int amountToIncrease; + + if (Boolean.TRUE.equals(countDispenses)) { + if (i != 0) { + continue; + } + + amountToIncrease = 1; + } else { + if (task.hasConfigKey("item")) { + if (item == null) { + super.debug("Specific item is required, dropped item is null; continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; + } + + super.debug("Specific item is required; dropped item is of type '" + item.getType() + "'", quest.getId(), task.getId(), player.getUniqueId()); + + 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; + } + + boolean exactMatch = TaskUtils.getConfigBoolean(task, "exact-match", true); + if (!qi.compareItemStack(item, exactMatch)) { + super.debug("Item does not match required item, continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; + } else { + super.debug("Item matches required item", quest.getId(), task.getId(), player.getUniqueId()); + } + } + + amountToIncrease = item != null ? item.getAmount() : 0; + } + + if (!TaskUtils.matchBlock(this, pendingTask, block, player.getUniqueId())) { + super.debug("Continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; + } + + int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress, amountToIncrease); + super.debug("Incrementing task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId()); + + int amount = (int) task.getConfigValue("amount"); + + if (progress >= amount) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setCompleted(true); + } + + TaskUtils.sendTrackAdvancement(player, quest, task, pendingTask, amount); + } + } +} -- cgit v1.2.3-70-g09d2