Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
crafts item event: amount context for shaped recipes
- Loading branch information
1 parent
a8f034e
commit d0cb83d
Showing
2 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
plugin/src/main/java/com/denizenscript/denizen/utilities/inventory/RecipeHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.denizenscript.denizen.utilities.inventory; | ||
|
||
import org.bukkit.inventory.*; | ||
|
||
import java.util.Arrays; | ||
import java.util.Map; | ||
|
||
public class RecipeHelper { | ||
|
||
public static class ShapeHelper { | ||
public ShapeHelper(ShapedRecipe recipe) { | ||
choiceMap = recipe.getChoiceMap(); | ||
shapeText = recipe.getShape(); | ||
int len = shapeText.length; | ||
while (len > 0 && shapeText[len - 1].isEmpty()) { | ||
len--; | ||
} | ||
while (len > 0 && shapeText[0].isEmpty()) { | ||
shapeText = Arrays.copyOfRange(shapeText, 1, len); | ||
len--; | ||
} | ||
height = len; | ||
width = 0; | ||
for (int i = 0; i < len; i++) { | ||
width = Math.max(width, shapeText[i].length()); | ||
} | ||
if (height <= 0 || width <= 0) { | ||
throw new RuntimeException("ShapedRecipe malformed."); | ||
} | ||
} | ||
|
||
public int width; | ||
public int height; | ||
public String[] shapeText; | ||
public Map<Character, RecipeChoice> choiceMap; | ||
} | ||
|
||
public static int tryRecipeMatch(ItemStack[] matrix, int matrixWidth, ShapeHelper shape, int offsetX, int offsetY) { | ||
int max = 64; | ||
for (int shapeX = 0; shapeX < shape.width; shapeX++) { | ||
for (int shapeY = 0; shapeY < shape.height; shapeY++) { | ||
if (shape.shapeText[shapeY].length() <= shapeX) { | ||
continue; | ||
} | ||
int x = offsetX + shapeX; | ||
int y = offsetY + shapeY; | ||
int matrixIndex = x + y * matrixWidth; | ||
ItemStack matrixItem = matrix[matrixIndex]; | ||
RecipeChoice choices = shape.choiceMap.get(shape.shapeText[shapeY].charAt(shapeX)); | ||
if (choices != null) { | ||
if (matrixItem == null || !choices.test(matrixItem)) { | ||
return 0; | ||
} | ||
max = Math.min(max, matrixItem.getAmount()); | ||
} | ||
} | ||
} | ||
return max; | ||
} | ||
|
||
public static int getShapedQuantity(CraftingInventory inventory, ShapeHelper shape) { | ||
ItemStack[] matrix = inventory.getMatrix(); | ||
int matrixWidth = matrix.length == 9 ? 3 : 2; | ||
int canMoveX = matrixWidth - shape.width; | ||
int canMoveY = matrixWidth - shape.height; | ||
for (int offsetX = 0; offsetX < canMoveX; offsetX++) { | ||
for (int offsetY = 0; offsetY <= canMoveY; offsetY++) { | ||
int result = tryRecipeMatch(matrix, matrixWidth, shape, offsetX, offsetY); | ||
if (result > 0) { | ||
return result; | ||
} | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
public static int getMaximumOutputQuantity(Recipe recipe, CraftingInventory inventory) { | ||
if (recipe instanceof ShapedRecipe) { | ||
return getShapedQuantity(inventory, new ShapeHelper((ShapedRecipe) recipe)); | ||
} | ||
else if (recipe instanceof ShapelessRecipe) { | ||
// TODO | ||
return 1; | ||
} | ||
else { | ||
return 1; | ||
} | ||
} | ||
} |