diff options
Diffstat (limited to 'bukkit')
| -rw-r--r-- | bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/TradingTaskType.java | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/TradingTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/TradingTaskType.java index 71c85cad..484079a4 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/TradingTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/TradingTaskType.java @@ -19,6 +19,8 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.MerchantRecipe; import org.jetbrains.annotations.NotNull; +import java.util.List; + public final class TradingTaskType extends BukkitTaskType { private final BukkitQuestsPlugin plugin; @@ -31,8 +33,12 @@ public final class TradingTaskType extends BukkitTaskType { super.addConfigValidator(TaskUtils.useRequiredConfigValidator(this, "amount")); super.addConfigValidator(TaskUtils.useIntegerConfigValidator(this, "amount")); super.addConfigValidator(TaskUtils.useItemStackConfigValidator(this, "item")); + super.addConfigValidator(TaskUtils.useItemStackConfigValidator(this, "first-ingredient")); + super.addConfigValidator(TaskUtils.useItemStackConfigValidator(this, "second-ingredient")); super.addConfigValidator(TaskUtils.useIntegerConfigValidator(this, "data")); super.addConfigValidator(TaskUtils.useBooleanConfigValidator(this, "exact-match")); + super.addConfigValidator(TaskUtils.useBooleanConfigValidator(this, "first-ingredient-exact-match")); + super.addConfigValidator(TaskUtils.useBooleanConfigValidator(this, "second-ingredient-exact-match")); } @Override @@ -40,6 +46,7 @@ public final class TradingTaskType extends BukkitTaskType { fixedQuestItemCache.clear(); } + @SuppressWarnings({"SizeReplaceableByIsEmpty"}) // for readability @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onPlayerTrade(final @NotNull PlayerTradeEvent event) { Player player = event.getPlayer(); @@ -56,6 +63,10 @@ public final class TradingTaskType extends BukkitTaskType { ItemStack item = recipe.getResult(); int itemAmount = item.getAmount(); + List<ItemStack> ingredients = recipe.getIngredients(); + ItemStack firstIngredient = ingredients.size() >= 1 ? ingredients.get(0) : null; + ItemStack secondIngredient = ingredients.size() >= 2 ? ingredients.get(1) : null; + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this, TaskConstraintSet.ALL)) { Quest quest = pendingTask.quest(); Task task = pendingTask.task(); @@ -81,6 +92,40 @@ public final class TradingTaskType extends BukkitTaskType { } } + if (task.hasConfigKey("first-ingredient")) { + QuestItem qi; + if ((qi = fixedQuestItemCache.get(quest.getId(), task.getId())) == null) { + QuestItem fetchedItem = TaskUtils.getConfigQuestItem(task, "first-ingredient", "data"); + fixedQuestItemCache.put(quest.getId(), task.getId(), fetchedItem); + qi = fetchedItem; + } + + super.debug("First ingredient was of type " + (firstIngredient != null ? firstIngredient.getType() : null), quest.getId(), task.getId(), player.getUniqueId()); + + boolean exactMatch = TaskUtils.getConfigBoolean(task, "first-ingredient-exact-match", true); + if (!qi.compareItemStack(item, exactMatch)) { + super.debug("First ingredient does not match required item, continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; + } + } + + if (task.hasConfigKey("second-ingredient")) { + QuestItem qi; + if ((qi = fixedQuestItemCache.get(quest.getId(), task.getId())) == null) { + QuestItem fetchedItem = TaskUtils.getConfigQuestItem(task, "second-ingredient", "data"); + fixedQuestItemCache.put(quest.getId(), task.getId(), fetchedItem); + qi = fetchedItem; + } + + super.debug("Second ingredient was of type " + (secondIngredient != null ? secondIngredient.getType() : null), quest.getId(), task.getId(), player.getUniqueId()); + + boolean exactMatch = TaskUtils.getConfigBoolean(task, "second-ingredient-exact-match", true); + if (!qi.compareItemStack(item, exactMatch)) { + super.debug("Second ingredient 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()); |
