Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
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;
Expand Down Expand Up @@ -476,6 +477,7 @@ public void onEnable() {
// 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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public BlockItemdroppingTaskType(BukkitQuestsPlugin plugin) {
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
Expand All @@ -56,51 +57,68 @@ public void onBlockDropItem(BlockDropItemEvent event) {
}

BlockState state = event.getBlockState();

List<Item> 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();
TaskProgress taskProgress = pendingTask.taskProgress();

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())) {
super.debug("Continuing...", quest.getId(), task.getId(), player.getUniqueId());
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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String, QuestItem> 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<ItemStack> 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);
}
}
}