From c1a214f34e8f580f768897cb8f996487379c5524 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Thu, 14 Jan 2021 11:31:05 +0000 Subject: [PATCH] Fixed crafting --- .../eco/util/recipes/EcoShapedRecipe.java | 11 ++++++++ .../eco/util/recipes/RecipeListener.java | 28 ++++++++++--------- .../eco/util/recipes/RecipeManager.java | 12 ++++++++ 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/willfp/eco/util/recipes/EcoShapedRecipe.java b/src/main/java/com/willfp/eco/util/recipes/EcoShapedRecipe.java index 203151561..95a8056c3 100644 --- a/src/main/java/com/willfp/eco/util/recipes/EcoShapedRecipe.java +++ b/src/main/java/com/willfp/eco/util/recipes/EcoShapedRecipe.java @@ -11,6 +11,8 @@ import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; +import java.util.Arrays; + public final class EcoShapedRecipe extends PluginDependent implements Registerable { /** * Recipe parts. @@ -85,6 +87,15 @@ public void register() { this.getPlugin().getRecipeManager().register(this); } + @Override + public String toString() { + return "EcoShapedRecipe{" + + "parts=" + Arrays.toString(parts) + + ", key='" + key + '\'' + + ", output=" + output + + '}'; + } + /** * Create a new recipe builder. * diff --git a/src/main/java/com/willfp/eco/util/recipes/RecipeListener.java b/src/main/java/com/willfp/eco/util/recipes/RecipeListener.java index 7d8e1c084..2301fd75a 100644 --- a/src/main/java/com/willfp/eco/util/recipes/RecipeListener.java +++ b/src/main/java/com/willfp/eco/util/recipes/RecipeListener.java @@ -2,6 +2,7 @@ import com.willfp.eco.util.internal.PluginDependent; import com.willfp.eco.util.plugin.AbstractEcoPlugin; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -38,16 +39,16 @@ public void prepareCraftListener(@NotNull final PrepareItemCraftEvent event) { return; } - EcoShapedRecipe internalRecipe = this.getPlugin().getRecipeManager().getShapedRecipe(recipe.getKey().getKey()); + ItemStack[] matrix = event.getInventory().getMatrix(); + EcoShapedRecipe matched = this.getPlugin().getRecipeManager().getMatch(matrix); - if (internalRecipe == null) { + if (matched == null) { + event.getInventory().setResult(new ItemStack(Material.AIR)); return; } - ItemStack[] matrix = event.getInventory().getMatrix(); - - if (internalRecipe.test(matrix)) { - event.getInventory().setResult(internalRecipe.getOutput()); + if (matched.test(matrix)) { + event.getInventory().setResult(matched.getOutput()); } else { event.getInventory().setResult(new ItemStack(Material.AIR)); } @@ -70,19 +71,20 @@ public void craftListener(@NotNull final CraftItemEvent event) { return; } - EcoShapedRecipe internalRecipe = this.getPlugin().getRecipeManager().getShapedRecipe(recipe.getKey().getKey()); + ItemStack[] matrix = event.getInventory().getMatrix(); + EcoShapedRecipe matched = this.getPlugin().getRecipeManager().getMatch(matrix); - if (internalRecipe == null) { + if (matched == null) { + event.getInventory().setResult(new ItemStack(Material.AIR)); + event.setCancelled(true); return; } - ItemStack[] matrix = event.getInventory().getMatrix(); - - if (internalRecipe.test(matrix)) { - event.getInventory().setResult(internalRecipe.getOutput()); - event.setCancelled(true); + if (matched.test(matrix)) { + event.getInventory().setResult(matched.getOutput()); } else { event.getInventory().setResult(new ItemStack(Material.AIR)); + event.setCancelled(true); } } } diff --git a/src/main/java/com/willfp/eco/util/recipes/RecipeManager.java b/src/main/java/com/willfp/eco/util/recipes/RecipeManager.java index bed905867..b60a38639 100644 --- a/src/main/java/com/willfp/eco/util/recipes/RecipeManager.java +++ b/src/main/java/com/willfp/eco/util/recipes/RecipeManager.java @@ -6,6 +6,7 @@ import com.willfp.eco.util.plugin.AbstractEcoPlugin; import org.bukkit.Bukkit; import org.bukkit.NamespacedKey; +import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.RecipeChoice; import org.bukkit.inventory.ShapedRecipe; import org.jetbrains.annotations.NotNull; @@ -55,6 +56,17 @@ void register(@NotNull final EcoShapedRecipe recipe) { Bukkit.getServer().addRecipe(displayedRecipe); } + /** + * Get recipe matching matrix. + * + * @param matrix The matrix to test. + * @return The match, or null if not found. + */ + @Nullable + public EcoShapedRecipe getMatch(@NotNull final ItemStack[] matrix) { + return registry.values().stream().filter(recipe -> recipe.test(matrix)).findFirst().orElse(null); + } + /** * Get shaped recipe by key. *