Skip to content

Commit

Permalink
Add trading task type
Browse files Browse the repository at this point in the history
  • Loading branch information
Krakenied committed Jul 7, 2024
1 parent 77fe3ae commit 7bcf8f9
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
import com.leonardobishop.quests.bukkit.tasktype.type.SmeltingTaskType;
import com.leonardobishop.quests.bukkit.tasktype.type.SmithingTaskType;
import com.leonardobishop.quests.bukkit.tasktype.type.TamingTaskType;
import com.leonardobishop.quests.bukkit.tasktype.type.TradingTaskType;
import com.leonardobishop.quests.bukkit.tasktype.type.WalkingTaskType;
import com.leonardobishop.quests.bukkit.tasktype.type.dependent.ASkyBlockLevelTaskType;
import com.leonardobishop.quests.bukkit.tasktype.type.dependent.BentoBoxLevelTaskType;
Expand Down Expand Up @@ -462,6 +463,7 @@ public void onEnable() {
taskTypeManager.registerTaskType(() -> new ReplenishingTaskType(this), () -> CompatUtils.classExists("com.destroystokyo.paper.loottable.LootableInventoryReplenishEvent"));
taskTypeManager.registerTaskType(() -> new ResurrectingTaskType(this), () -> CompatUtils.classExists("org.bukkit.event.entity.EntityResurrectEvent"));
taskTypeManager.registerTaskType(() -> new SmithingTaskType(this), () -> CompatUtils.classExists("org.bukkit.event.inventory.SmithItemEvent"));
taskTypeManager.registerTaskType(() -> new TradingTaskType(this), () -> CompatUtils.classExists("io.papermc.paper.event.player.PlayerTradeEvent"));

// Register task types with enabled plugin compatibility requirement
taskTypeManager.registerTaskType(() -> new ASkyBlockLevelTaskType(this), () -> CompatUtils.isPluginEnabled("ASkyBlock"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
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 io.papermc.paper.event.player.PlayerTradeEvent;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.MerchantRecipe;
import org.jetbrains.annotations.NotNull;

public final class TradingTaskType extends BukkitTaskType {

private final BukkitQuestsPlugin plugin;
private final Table<String, String, QuestItem> fixedQuestItemCache = HashBasedTable.create();

public TradingTaskType(BukkitQuestsPlugin plugin) {
super("trading", TaskUtils.TASK_ATTRIBUTION_STRING, "Trade with a Villager or Wandering Trader.");
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"));
}

@Override
public void onReady() {
fixedQuestItemCache.clear();
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerTrade(final @NotNull PlayerTradeEvent event) {
Player player = event.getPlayer();
if (player.hasMetadata("NPC")) {
return;
}

QPlayer qPlayer = plugin.getPlayerManager().getPlayer(player.getUniqueId());
if (qPlayer == null) {
return;
}

MerchantRecipe recipe = event.getTrade();
ItemStack item = recipe.getResult();
int itemAmount = item.getAmount();

for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this, TaskConstraintSet.ALL)) {
Quest quest = pendingTask.quest();
Task task = pendingTask.task();
TaskProgress taskProgress = pendingTask.taskProgress();

// TODO: add villager-type and villager-profession options
// TODO: add option to set villager/wandering trader

if (task.hasConfigKey("item")) {
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;
}

super.debug("Player traded " + itemAmount + " item of type " + item.getType(), 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;
}
}

int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress, itemAmount);
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);
}
}
}

0 comments on commit 7bcf8f9

Please sign in to comment.