Added two trigger list to crafting recipes to do things when the crafting recipe is used, canceled and claimed

This commit is contained in:
Jules 2022-03-01 15:51:42 +01:00
parent 6c9b02bc8a
commit 6ca9f19645
2 changed files with 44 additions and 30 deletions

View File

@ -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,7 +21,16 @@ public abstract class Recipe {
private final Set<Ingredient> ingredients = new LinkedHashSet<>();
private final Set<Condition> conditions = new LinkedHashSet<>();
private final Set<Trigger> triggers = new LinkedHashSet<>();
/**
* - <code>onUse</code> are called when USING the recipe (not claiming the item from the queue)
* - <code>onClaim</code> are called when claiming the item in the crafting queue
* - <code>onCancel</> are called when canceling the craft in the crafting queue
*
* For backwards compatibility, <code>onClaim</code> corresponds to the trigger list
* stored with the 'triggers' config key
*/
private final Set<Trigger> onUse = new LinkedHashSet<>(), onClaim = new LinkedHashSet<>(), onCancel = new LinkedHashSet<>();
/**
* Stores the information about a specific recipe setup in a crafting
@ -37,17 +43,13 @@ public abstract class Recipe {
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,16 +71,20 @@ public abstract class Recipe {
throw new IllegalArgumentException("No conditions or ingredients set.");
}
/*
* load triggers
*/
for (String format : config.getStringList("triggers"))
// 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<String> stringList, String triggerType, Set<Trigger> collection) {
for (String format : stringList)
try {
Trigger trigger = MMOItems.plugin.getCrafting().getTrigger(new MMOLineConfig(format));
Validate.notNull(trigger, "Could not match trigger");
triggers.add(trigger);
onUse.add(trigger);
} catch (IllegalArgumentException exception) {
throw new IllegalArgumentException("Could not load trigger '" + format + "': " + exception.getMessage());
throw new IllegalArgumentException("Could not load " + triggerType + " trigger '" + format + "': " + exception.getMessage());
}
}
@ -102,8 +106,16 @@ public abstract class Recipe {
return conditions;
}
public Set<Trigger> getTriggers() {
return triggers;
public Set<Trigger> whenUsed() {
return onUse;
}
public Set<Trigger> whenClaimed() {
return onClaim;
}
public Set<Trigger> whenCanceled() {
return onCancel;
}
public Condition getCondition(String format) {

View File

@ -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();
}