From be498796c047205dfa5d547e9c6fb42eeed2b9fa Mon Sep 17 00:00:00 2001 From: BenWoodworth Date: Sat, 2 Apr 2016 11:37:19 -0400 Subject: [PATCH] Updated Item Maker Pro compatibility to reflect API changes. Fixed items being crafted with ingredients persisting in their lore. Prevent PrepareItemCraftEvent from being called with null parameters. Prevent CraftItemEvent from being called without a filled recipe matrix. --- pom.xml | 2 +- .../fastcraftplus/compat/Compat_Bukkit.java | 42 +++++++------ .../compat/Compat_ItemMakerPro.java | 6 +- .../craftgui/GUIButtonRecipe.java | 2 +- .../fastcraftplus/recipes/RecipeUtil.java | 59 ++++++++++++++----- 5 files changed, 72 insertions(+), 39 deletions(-) diff --git a/pom.xml b/pom.xml index 05f8b1bc..1f2eb511 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ co.kepler.fastcraftplus fastcraftplus - 0.6 + 0.7 diff --git a/src/main/java/co/kepler/fastcraftplus/compat/Compat_Bukkit.java b/src/main/java/co/kepler/fastcraftplus/compat/Compat_Bukkit.java index 6911b086..e7a4566c 100644 --- a/src/main/java/co/kepler/fastcraftplus/compat/Compat_Bukkit.java +++ b/src/main/java/co/kepler/fastcraftplus/compat/Compat_Bukkit.java @@ -34,35 +34,42 @@ public Set getRecipes(Player player) { // Loop through the server's recipes for (Iterator iter = Bukkit.recipeIterator(); iter.hasNext();) { Recipe recipe = iter.next(); - FastRecipe newRecipe = null; - if (recipe instanceof ShapedRecipe) { // Create FastRecipe from a ShapedRecipe ShapedRecipe sr = (ShapedRecipe) recipe; + + // Get the result when crafting in a workbench ItemStack craftResult = RecipeUtil.getCraftingResult(sr, player); - boolean notCancelled = RecipeUtil.callCraftItemEvent(player, sr); - if (notCancelled && craftResult != null && craftResult.equals(sr.getResult())) { - newRecipe = new FastRecipeCompat(sr); + if (sr.getResult() != null && craftResult != null && !sr.getResult().equals(craftResult)) continue; + + // Get the matrix of items needed to craft this recipe + ItemStack[] matrix = RecipeUtil.getRecipeMatrix(sr); + if (matrix == null) continue; + + // If crafting the recipe isn't cancelled, create a new recipe + if (RecipeUtil.callCraftItemEvent(player, sr, matrix, sr.getResult())) { + result.add(new FastRecipeCompat(sr)); } } else if (recipe instanceof ShapelessRecipe) { // Create FastRecipe from a ShapelessRecipe ShapelessRecipe sr = (ShapelessRecipe) recipe; + + // Get the result when crafting in a workbench ItemStack craftResult = RecipeUtil.getCraftingResult(sr, player); - boolean notCancelled = RecipeUtil.callCraftItemEvent(player, sr); - if (notCancelled && craftResult != null && craftResult.equals(sr.getResult())) { - newRecipe = new FastRecipeCompat(sr); - } - } else { - continue; - } + if (sr.getResult() != null && craftResult != null && !sr.getResult().equals(craftResult)) continue; - // Add the new recipe to the result - if (newRecipe != null) { - result.add(newRecipe); + // Get the matrix of items needed to craft this recipe + ItemStack[] matrix = RecipeUtil.getRecipeMatrix(sr); + if (matrix == null) continue; + + // If crafting the recipe isn't cancelled, create a new recipe + if (RecipeUtil.callCraftItemEvent(player, sr, matrix, sr.getResult())) { + result.add(new FastRecipeCompat(sr)); + } } } - // Return a list of FastRecipes + // Return a set of FastRecipes return result; } @@ -72,7 +79,6 @@ public Set getRecipes(Player player) { public static class FastRecipeCompat extends FastRecipe { private final Map ingredients = new HashMap<>(); private final List result; - private final Recipe recipe; /** * Create a new FastRecipeCompat from a ShapedRecipe. @@ -80,7 +86,6 @@ public static class FastRecipeCompat extends FastRecipe { * @param recipe The Recipe this FastRecipe is based off of. */ public FastRecipeCompat(ShapedRecipe recipe) { - this.recipe = recipe; result = Collections.singletonList(recipe.getResult()); // Fill map of ingredients @@ -101,7 +106,6 @@ public FastRecipeCompat(ShapedRecipe recipe) { * @param recipe The Recipe this FastRecipe is based off of. */ public FastRecipeCompat(ShapelessRecipe recipe) { - this.recipe = recipe; result = Collections.singletonList(recipe.getResult()); // Fill map of ingredients diff --git a/src/main/java/co/kepler/fastcraftplus/compat/Compat_ItemMakerPro.java b/src/main/java/co/kepler/fastcraftplus/compat/Compat_ItemMakerPro.java index 2ba8d6b1..4a238c6f 100644 --- a/src/main/java/co/kepler/fastcraftplus/compat/Compat_ItemMakerPro.java +++ b/src/main/java/co/kepler/fastcraftplus/compat/Compat_ItemMakerPro.java @@ -4,7 +4,7 @@ import co.kepler.fastcraftplus.recipes.Ingredient; import com.kirelcodes.ItemMaker.API.RecipeGetter; import com.kirelcodes.ItemMaker.Recipes.Perfect.PerfectShapedRecipe; -import com.kirelcodes.ItemMaker.Recipes.Perfect.PerfefectShapelessRecipe; +import com.kirelcodes.ItemMaker.Recipes.Perfect.PerfectShapelessRecipe; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -40,7 +40,7 @@ public Set getRecipes(Player player) { } // Add shapeless recipes - for (PerfefectShapelessRecipe recipe : RecipeGetter.getShapelessRecipe()) { + for (PerfectShapelessRecipe recipe : RecipeGetter.getShapelessRecipe()) { if (!recipe.hasPermission() || player.hasPermission(recipe.getPermission())) { // If player has permission to craft recipes.add(new FastRecipeCompat(recipe)); @@ -67,7 +67,7 @@ public FastRecipeCompat(PerfectShapedRecipe recipe) { } } - public FastRecipeCompat(PerfefectShapelessRecipe recipe) { + public FastRecipeCompat(PerfectShapelessRecipe recipe) { results = Collections.singletonList(recipe.getResult()); // Add ingredients diff --git a/src/main/java/co/kepler/fastcraftplus/craftgui/GUIButtonRecipe.java b/src/main/java/co/kepler/fastcraftplus/craftgui/GUIButtonRecipe.java index 64267a69..07ca02f2 100644 --- a/src/main/java/co/kepler/fastcraftplus/craftgui/GUIButtonRecipe.java +++ b/src/main/java/co/kepler/fastcraftplus/craftgui/GUIButtonRecipe.java @@ -41,7 +41,7 @@ public GUIButtonRecipe(GUIFastCraft gui, FastRecipe recipe) { @Override public ItemStack getItem() { // Add the ingredients to the lore of the item - ItemStack item = recipe.getDisplayResult(); + ItemStack item = recipe.getDisplayResult().clone(); List results = recipe.getResults(); ItemMeta meta = item.getItemMeta(); LinkedList lore = new LinkedList<>(); diff --git a/src/main/java/co/kepler/fastcraftplus/recipes/RecipeUtil.java b/src/main/java/co/kepler/fastcraftplus/recipes/RecipeUtil.java index 9b874693..8d82edfe 100644 --- a/src/main/java/co/kepler/fastcraftplus/recipes/RecipeUtil.java +++ b/src/main/java/co/kepler/fastcraftplus/recipes/RecipeUtil.java @@ -170,6 +170,35 @@ public static void awardAchievement(Player player, ItemStack craftedItem) { * @return Returns the item from the crafting table. */ public static ItemStack getCraftingResult(ShapedRecipe recipe, Player player) { + ItemStack[] matrix = getRecipeMatrix(recipe); + if (matrix == null) return null; + + // Return the item in the result slot of the inventory + return callPrepareItemCraftEvent(player, recipe, matrix, recipe.getResult()).getInventory().getResult(); + } + + /** + * See if a recipe is consistent. A recipe is consistent if its result is the same as + * the resulting item when crafting in a crafting table. + * + * @param recipe The recipe to check. + * @return Returns true if the recipe is consistent. + */ + public static ItemStack getCraftingResult(ShapelessRecipe recipe, Player player) { + ItemStack[] matrix = getRecipeMatrix(recipe); + if (matrix == null) return null; + + // Return the item in the result slot of the inventory + return callPrepareItemCraftEvent(player, recipe, matrix, recipe.getResult()).getInventory().getResult(); + } + + /** + * Get a recipe's matrix of ingredients in the crafting table. + * + * @param recipe The recipe to get the matrix of. + * @return Return a matrix, or null if the recipe's shape has a dimension greater than 3. + */ + public static ItemStack[] getRecipeMatrix(ShapedRecipe recipe) { Map ingredients = recipe.getIngredientMap(); String[] shape = recipe.getShape(); ItemStack[] matrix = new ItemStack[9]; @@ -184,18 +213,10 @@ public static ItemStack getCraftingResult(ShapedRecipe recipe, Player player) { } } - // Return the item in the result slot of the inventory - return callPrepareItemCraftEvent(player, matrix, recipe.getResult()).getInventory().getResult(); + return matrix; } - /** - * See if a recipe is consistent. A recipe is consistent if its result is the same as - * the resulting item when crafting in a crafting table. - * - * @param recipe The recipe to check. - * @return Returns true if the recipe is consistent. - */ - public static ItemStack getCraftingResult(ShapelessRecipe recipe, Player player) { + public static ItemStack[] getRecipeMatrix(ShapelessRecipe recipe) { ItemStack[] matrix = new ItemStack[9]; int matIndex = 0; @@ -208,9 +229,7 @@ public static ItemStack getCraftingResult(ShapelessRecipe recipe, Player player) matrix[matIndex++] = curStack; } } - - // Return the item in the result slot of the inventory - return callPrepareItemCraftEvent(player, matrix, recipe.getResult()).getInventory().getResult(); + return matrix; } /** @@ -221,8 +240,14 @@ public static ItemStack getCraftingResult(ShapelessRecipe recipe, Player player) * @param result The item in the result slot of the crafting table. * @return Returns the called event. */ - public static PrepareItemCraftEvent callPrepareItemCraftEvent(Player player, ItemStack[] matrix, ItemStack result) { + public static PrepareItemCraftEvent callPrepareItemCraftEvent(Player player, Recipe recipe, + ItemStack[] matrix, ItemStack result) { + assert player != null : "Player must not be null"; + assert recipe != null : "Recipe must not be null"; + assert matrix != null : "Matrix must not be null"; + CraftingInvWrapper inv = new CraftingInvWrapper(player); + inv.setRecipe(recipe); inv.setMatrix(matrix); inv.setResult(result); @@ -237,7 +262,11 @@ public static PrepareItemCraftEvent callPrepareItemCraftEvent(Player player, Ite * * @return Returns false if the event was cancelled. */ - public static boolean callCraftItemEvent(Player player, Recipe recipe) { + public static boolean callCraftItemEvent(Player player, Recipe recipe, ItemStack[] matrix, ItemStack result) { + assert player != null : "Player must not be null"; + assert recipe != null : "Recipe must not be null"; + assert matrix != null : "Matrix must not be null"; + CraftingInvWrapper inv = new CraftingInvWrapper(player); inv.setResult(recipe.getResult());