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 ce74a327..170e8c4d 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 @@ -20,7 +20,10 @@ import org.bukkit.inventory.MerchantRecipe; import org.jetbrains.annotations.NotNull; +import java.util.HashMap; import java.util.List; +import java.util.Locale; +import java.util.Map; public final class TradingTaskType extends BukkitTaskType { @@ -43,6 +46,7 @@ public TradingTaskType(BukkitQuestsPlugin plugin) { 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")); + super.addConfigValidator(TaskUtils.useAcceptedValuesConfigValidator(this, Mode.STRING_MODE_MAP.keySet(), "mode")); } @Override @@ -67,12 +71,31 @@ public void onPlayerTrade(final @NotNull PlayerTradeEvent event) { AbstractVillager villager = event.getVillager(); MerchantRecipe recipe = event.getTrade(); - ItemStack item = recipe.getResult(); - int itemAmount = item.getAmount(); + ItemStack result = recipe.getResult(); + int resultAmount = result.getAmount(); List ingredients = recipe.getIngredients(); - ItemStack firstIngredient = ingredients.size() >= 1 ? ingredients.get(0) : null; - ItemStack secondIngredient = ingredients.size() >= 2 ? ingredients.get(1) : null; + ItemStack firstIngredient, secondIngredient; + int firstIngredientAmount, secondIngredientAmount; + + if (ingredients.size() >= 1) { + if (ingredients.size() >= 2) { + secondIngredient = ingredients.get(1); + secondIngredientAmount = secondIngredient.getAmount(); + } else { + secondIngredient = null; + secondIngredientAmount = 0; + } + + firstIngredient = ingredients.get(0); + firstIngredientAmount = firstIngredient.getAmount(); + } else { + firstIngredient = null; + firstIngredientAmount = 0; + + secondIngredient = null; + secondIngredientAmount = 0; + } for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this, TaskConstraintSet.ALL)) { Quest quest = pendingTask.quest(); @@ -95,10 +118,10 @@ public void onPlayerTrade(final @NotNull PlayerTradeEvent event) { qi = fetchedItem; } - super.debug("Player traded " + itemAmount + " item of type " + item.getType(), quest.getId(), task.getId(), player.getUniqueId()); + super.debug("Player traded " + resultAmount + " items of type " + result.getType(), quest.getId(), task.getId(), player.getUniqueId()); boolean exactMatch = TaskUtils.getConfigBoolean(task, "exact-match", true); - if (!qi.compareItemStack(item, exactMatch)) { + if (!qi.compareItemStack(result, exactMatch)) { super.debug("Item does not match required item, continuing...", quest.getId(), task.getId(), player.getUniqueId()); continue; } @@ -112,7 +135,7 @@ public void onPlayerTrade(final @NotNull PlayerTradeEvent event) { qi = fetchedItem; } - super.debug("First ingredient was of type " + (firstIngredient != null ? firstIngredient.getType() : null), quest.getId(), task.getId(), player.getUniqueId()); + super.debug("First ingredient was " + (firstIngredient != null ? (firstIngredient.getAmount() + " of type " + firstIngredient.getType()) : null), quest.getId(), task.getId(), player.getUniqueId()); boolean exactMatch = TaskUtils.getConfigBoolean(task, "first-ingredient-exact-match", true); if (firstIngredient == null || !qi.compareItemStack(firstIngredient, exactMatch)) { @@ -129,7 +152,7 @@ public void onPlayerTrade(final @NotNull PlayerTradeEvent event) { qi = fetchedItem; } - super.debug("Second ingredient was of type " + (secondIngredient != null ? secondIngredient.getType() : null), quest.getId(), task.getId(), player.getUniqueId()); + super.debug("Second ingredient was " + (secondIngredient != null ? (secondIngredient.getAmount() + " of type " + secondIngredient.getType()) : null), quest.getId(), task.getId(), player.getUniqueId()); boolean exactMatch = TaskUtils.getConfigBoolean(task, "second-ingredient-exact-match", true); if (secondIngredient == null || !qi.compareItemStack(secondIngredient, exactMatch)) { @@ -138,6 +161,18 @@ public void onPlayerTrade(final @NotNull PlayerTradeEvent event) { } } + Object modeObject = task.getConfigValue("mode"); + + // not suspicious at all ඞ + //noinspection SuspiciousMethodCalls + Mode mode = Mode.STRING_MODE_MAP.getOrDefault(modeObject, Mode.RESULT); + + int itemAmount = switch (mode) { + case RESULT -> resultAmount; + case FIRST_INGREDIENT -> firstIngredientAmount; + case SECOND_INGREDIENT -> secondIngredientAmount; + }; + int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress, itemAmount); super.debug("Incrementing task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId()); @@ -151,4 +186,16 @@ public void onPlayerTrade(final @NotNull PlayerTradeEvent event) { TaskUtils.sendTrackAdvancement(player, quest, task, pendingTask, amount); } } + + private enum Mode { + RESULT, + FIRST_INGREDIENT, + SECOND_INGREDIENT; + + private static final Map STRING_MODE_MAP = new HashMap<>() {{ + for (final TradingTaskType.Mode mode : TradingTaskType.Mode.values()) { + this.put(mode.name().toLowerCase(Locale.ROOT), mode); + } + }}; + } } diff --git a/docs/Gemfile b/docs/Gemfile index 893af1ec..73a2dcd1 100644 --- a/docs/Gemfile +++ b/docs/Gemfile @@ -1,6 +1,5 @@ source 'https://rubygems.org' -gem 'wdm', '>= 0.1.0' if Gem.win_platform? gem "jekyll", "~> 4.3.3" gem "jekyll-default-layout" gem "just-the-docs" diff --git a/docs/Gemfile.lock b/docs/Gemfile.lock index c961229e..07513fdd 100644 --- a/docs/Gemfile.lock +++ b/docs/Gemfile.lock @@ -79,7 +79,6 @@ GEM terminal-table (3.0.2) unicode-display_width (>= 1.1.1, < 3) unicode-display_width (2.5.0) - wdm (0.2.0) webrick (1.8.1) PLATFORMS @@ -90,7 +89,6 @@ DEPENDENCIES jekyll (~> 4.3.3) jekyll-default-layout just-the-docs - wdm (>= 0.1.0) BUNDLED WITH 2.4.13 diff --git a/docs/task-types/trading-(task-type).md b/docs/task-types/trading-(task-type).md index 3d9ce944..522a86e2 100644 --- a/docs/task-types/trading-(task-type).md +++ b/docs/task-types/trading-(task-type).md @@ -29,6 +29,7 @@ Trade with a Villager or Wandering Trader. | `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 | \- | +| `mode` | The specific mode of trading. | String | No | result | One of: `result`, `first_ingredient`, `second_ingredient`. | | `worlds` | Worlds which should count towards the progress. | List of world names | No | \- | \- | ## Examples