diff options
| -rw-r--r-- | bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/TradingTaskType.java | 45 | ||||
| -rw-r--r-- | docs/task-types/trading-(task-type).md | 16 |
2 files changed, 55 insertions, 6 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()); diff --git a/docs/task-types/trading-(task-type).md b/docs/task-types/trading-(task-type).md index 82bc8273..67bd2838 100644 --- a/docs/task-types/trading-(task-type).md +++ b/docs/task-types/trading-(task-type).md @@ -19,12 +19,16 @@ Trade with a Villager or Wandering Trader. ## Options -| Key | Description | Type | Required | Default | Notes | -|---------------|--------------------------------------------------------|------------------------|----------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| `amount` | The number of items to trade. | Integer | Yes | \- | \- | -| `item` | The specific item to trade. | Material, or ItemStack | No | \- | Accepts standard [item definition](../configuration/defining-items). Please see [this list](https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/Material.html) (1.13+) or [this list](https://helpch.at/docs/1.12.2/org/bukkit/Material.html) (1.8-1.12) for material names. | -| `exact-match` | Whether the item should exactly match what is defined. | Boolean | No | true | \- | -| `worlds` | Worlds which should count towards the progress. | List of world names | No | \- | \- | +| Key | Description | Type | Required | Default | Notes | +|---------------------------------|--------------------------------------------------------------------------|------------------------|----------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `amount` | The number of items to trade. | Integer | Yes | \- | \- | +| `item` | The specific item to trade. | Material, or ItemStack | No | \- | Accepts standard [item definition](../configuration/defining-items). Please see [this list](https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/Material.html) (1.13+) or [this list](https://helpch.at/docs/1.12.2/org/bukkit/Material.html) (1.8-1.12) for material names. | +| `exact-match` | Whether the item should exactly match what is defined. | Boolean | No | true | \- | +| `first-ingredient` | The specific item to be used as the first ingredient in a trade. | Material, or ItemStack | No | \- | Accepts standard [item definition](../configuration/defining-items). Please see [this list](https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/Material.html) (1.13+) or [this list](https://helpch.at/docs/1.12.2/org/bukkit/Material.html) (1.8-1.12) for material names. | +| `first-ingredient-exact-match` | Whether the first ingredient item should exactly match what is defined. | Boolean | No | true | \- | +| `second-ingredient` | The specific item to be used as the second ingredient in a trade. | Material, or ItemStack | No | \- | Accepts standard [item definition](../configuration/defining-items). Please see [this list](https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/Material.html) (1.13+) or [this list](https://helpch.at/docs/1.12.2/org/bukkit/Material.html) (1.8-1.12) for material names. | +| `second-ingredient-exact-match` | Whether the second ingredient item should exactly match what is defined. | Boolean | No | true | \- | +| `worlds` | Worlds which should count towards the progress. | List of world names | No | \- | \- | ## Examples |
