Skip to content

Commit

Permalink
Updated Item Maker Pro compatibility to reflect API changes.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
BenWoodworth committed Apr 2, 2016
1 parent 37a6ec2 commit be49879
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 39 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>co.kepler.fastcraftplus</groupId>
<artifactId>fastcraftplus</artifactId>
<version>0.6</version>
<version>0.7</version>

<build>
<plugins>
Expand Down
42 changes: 23 additions & 19 deletions src/main/java/co/kepler/fastcraftplus/compat/Compat_Bukkit.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,35 +34,42 @@ public Set<FastRecipe> getRecipes(Player player) {
// Loop through the server's recipes
for (Iterator<Recipe> 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;
}

Expand All @@ -72,15 +79,13 @@ public Set<FastRecipe> getRecipes(Player player) {
public static class FastRecipeCompat extends FastRecipe {
private final Map<Ingredient, Integer> ingredients = new HashMap<>();
private final List<ItemStack> result;
private final Recipe recipe;

/**
* Create a new FastRecipeCompat from a ShapedRecipe.
*
* @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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -40,7 +40,7 @@ public Set<FastRecipe> 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));
Expand All @@ -67,7 +67,7 @@ public FastRecipeCompat(PerfectShapedRecipe recipe) {
}
}

public FastRecipeCompat(PerfefectShapelessRecipe recipe) {
public FastRecipeCompat(PerfectShapelessRecipe recipe) {
results = Collections.singletonList(recipe.getResult());

// Add ingredients
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ItemStack> results = recipe.getResults();
ItemMeta meta = item.getItemMeta();
LinkedList<String> lore = new LinkedList<>();
Expand Down
59 changes: 44 additions & 15 deletions src/main/java/co/kepler/fastcraftplus/recipes/RecipeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Character, ItemStack> ingredients = recipe.getIngredientMap();
String[] shape = recipe.getShape();
ItemStack[] matrix = new ItemStack[9];
Expand All @@ -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;

Expand All @@ -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;
}

/**
Expand All @@ -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);

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

Expand Down

0 comments on commit be49879

Please sign in to comment.