From 6ca9f1964574ef370db49aed80180284d4fba34c Mon Sep 17 00:00:00 2001 From: Jules Date: Tue, 1 Mar 2022 15:51:42 +0100 Subject: [PATCH] Added two trigger list to crafting recipes to do things when the crafting recipe is used, canceled and claimed --- .../mmoitems/api/crafting/recipe/Recipe.java | 70 +++++++++++-------- .../mmoitems/gui/CraftingStationView.java | 4 +- 2 files changed, 44 insertions(+), 30 deletions(-) diff --git a/src/main/java/net/Indyuce/mmoitems/api/crafting/recipe/Recipe.java b/src/main/java/net/Indyuce/mmoitems/api/crafting/recipe/Recipe.java index 5b044777..33963c05 100644 --- a/src/main/java/net/Indyuce/mmoitems/api/crafting/recipe/Recipe.java +++ b/src/main/java/net/Indyuce/mmoitems/api/crafting/recipe/Recipe.java @@ -12,10 +12,7 @@ import org.apache.commons.lang.Validate; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.inventory.ItemStack; -import java.util.HashMap; -import java.util.LinkedHashSet; -import java.util.Map; -import java.util.Set; +import java.util.*; @SuppressWarnings("unused") public abstract class Recipe { @@ -24,30 +21,35 @@ public abstract class Recipe { private final Set ingredients = new LinkedHashSet<>(); private final Set conditions = new LinkedHashSet<>(); - private final Set triggers = new LinkedHashSet<>(); + + /** + * - onUse are called when USING the recipe (not claiming the item from the queue) + * - onClaim are called when claiming the item in the crafting queue + * - onCancel are called when canceling the craft in the crafting queue + * + * For backwards compatibility, onClaim corresponds to the trigger list + * stored with the 'triggers' config key + */ + private final Set onUse = new LinkedHashSet<>(), onClaim = new LinkedHashSet<>(), onCancel = new LinkedHashSet<>(); /** * Stores the information about a specific recipe setup in a crafting * station. When a player opens a crafting station GUI, a CheckedRecipe * instance is created to evaluate the conditions which are not met/the * ingredients he is missing. - * + * * @param config Config section to load data from */ public Recipe(ConfigurationSection config) { this(config.getName()); - /* - * load recipe options - */ + // Load recipe options if (config.contains("options")) for (RecipeOption option : RecipeOption.values()) if (config.getConfigurationSection("options").contains(option.getConfigPath())) options.put(option, config.getBoolean("options." + option.getConfigPath())); - /* - * load ingredients - */ + // Load ingredients for (String format : config.getStringList("ingredients")) try { Ingredient ingredient = MMOItems.plugin.getCrafting().getIngredient(new MMOLineConfig(format)); @@ -56,9 +58,7 @@ public abstract class Recipe { throw new IllegalArgumentException("Could not load ingredient '" + format + "': " + exception.getMessage()); } - /* - * load conditions - */ + // Load conditions for (String format : config.getStringList("conditions")) try { Condition condition = MMOItems.plugin.getCrafting().getCondition(new MMOLineConfig(format)); @@ -71,19 +71,23 @@ public abstract class Recipe { throw new IllegalArgumentException("No conditions or ingredients set."); } - /* - * load triggers - */ - for (String format : config.getStringList("triggers")) - try { - Trigger trigger = MMOItems.plugin.getCrafting().getTrigger(new MMOLineConfig(format)); - Validate.notNull(trigger, "Could not match trigger"); - triggers.add(trigger); - } catch (IllegalArgumentException exception) { - throw new IllegalArgumentException("Could not load trigger '" + format + "': " + exception.getMessage()); - } + // Load triggers + loadTriggerList(config.getStringList("on-use"), "on-use", onUse); + loadTriggerList(config.getStringList("triggers"), "on-claim", onClaim); + loadTriggerList(config.getStringList("on-cancel"), "on-cancel", onCancel); } + private void loadTriggerList(List stringList, String triggerType, Set collection) { + for (String format : stringList) + try { + Trigger trigger = MMOItems.plugin.getCrafting().getTrigger(new MMOLineConfig(format)); + Validate.notNull(trigger, "Could not match trigger"); + onUse.add(trigger); + } catch (IllegalArgumentException exception) { + throw new IllegalArgumentException("Could not load " + triggerType + " trigger '" + format + "': " + exception.getMessage()); + } + } + private Recipe(String id) { Validate.notNull(id, "Recipe ID must not be null"); @@ -102,9 +106,17 @@ public abstract class Recipe { return conditions; } - public Set getTriggers() { - return triggers; - } + public Set whenUsed() { + return onUse; + } + + public Set whenClaimed() { + return onClaim; + } + + public Set whenCanceled() { + return onCancel; + } public Condition getCondition(String format) { for (Condition condition : conditions) diff --git a/src/main/java/net/Indyuce/mmoitems/gui/CraftingStationView.java b/src/main/java/net/Indyuce/mmoitems/gui/CraftingStationView.java index 3ed8c33d..ba623157 100644 --- a/src/main/java/net/Indyuce/mmoitems/gui/CraftingStationView.java +++ b/src/main/java/net/Indyuce/mmoitems/gui/CraftingStationView.java @@ -190,6 +190,7 @@ public class CraftingStationView extends PluginInventory { // Remove from crafting queue playerData.getCrafting().getQueue(station).remove(recipeInfo); + recipe.whenClaimed().forEach(trigger -> trigger.whenCrafting(playerData)); // Play sounds CustomSoundListener.stationCrafting(result, player); @@ -211,6 +212,7 @@ public class CraftingStationView extends PluginInventory { // Remove from crafting queue playerData.getCrafting().getQueue(station).remove(recipeInfo); + recipe.whenCanceled().forEach(trigger -> trigger.whenCrafting(playerData)); // Play sounds if (!recipe.hasOption(Recipe.RecipeOption.SILENT_CRAFT)) @@ -247,7 +249,7 @@ public class CraftingStationView extends PluginInventory { if (recipe.getRecipe().whenUsed(playerData, ingredients, recipe, station)) { recipe.getIngredients().forEach(ingredient -> ingredient.takeAway()); recipe.getConditions().forEach(condition -> condition.getCondition().whenCrafting(playerData)); - recipe.getRecipe().getTriggers().forEach(trigger -> trigger.whenCrafting(playerData)); + recipe.getRecipe().whenUsed().forEach(trigger -> trigger.whenCrafting(playerData)); updateData(); }