Skip to content

Commit

Permalink
Add mode option to trading task type
Browse files Browse the repository at this point in the history
  • Loading branch information
Krakenied committed Sep 7, 2024
1 parent 3a74f78 commit a06618b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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
Expand All @@ -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<ItemStack> 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();
Expand All @@ -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;
}
Expand All @@ -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)) {
Expand All @@ -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)) {
Expand All @@ -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());

Expand All @@ -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, TradingTaskType.Mode> STRING_MODE_MAP = new HashMap<>() {{
for (final TradingTaskType.Mode mode : TradingTaskType.Mode.values()) {
this.put(mode.name().toLowerCase(Locale.ROOT), mode);
}
}};
}
}
1 change: 0 additions & 1 deletion docs/Gemfile
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 0 additions & 2 deletions docs/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -90,7 +89,6 @@ DEPENDENCIES
jekyll (~> 4.3.3)
jekyll-default-layout
just-the-docs
wdm (>= 0.1.0)

BUNDLED WITH
2.4.13
1 change: 1 addition & 0 deletions docs/task-types/trading-(task-type).md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a06618b

Please sign in to comment.