Add preserveNBT option to IDescriptiveRecipe, simplify IFabricatorRecipe

This commit is contained in:
mezz committed 2015-10-24 19:59:59 -07:00
1 parent 7ad2e2e94d
commit 888047cad8
9 files changed
+131 -163

No files matched your search

@@ -98,17 +98,10 @@ public abstract class RecipeUtil {
return craftingNbt;
}
public static Object[] getCraftingRecipeAsArray(Object rec) {
public static Object[] getCraftingRecipeAsArray(IDescriptiveRecipe recipe) {
try {
if (rec instanceof IDescriptiveRecipe) {
IDescriptiveRecipe recipe = (IDescriptiveRecipe) rec;
return getShapedRecipeAsArray(recipe.getWidth(), recipe.getHeight(), recipe.getIngredients(), recipe.getRecipeOutput());
}
return getShapedRecipeAsArray(recipe.getWidth(), recipe.getHeight(), recipe.getIngredients(), recipe.getRecipeOutput());
} catch (Exception ex) {
Log.warning("Exception while trying to parse an ItemStack[10] from an IRecipe:");
Log.warning(ex.getMessage());
@@ -268,4 +261,109 @@ public abstract class RecipeUtil {
}
GameRegistry.addSmelting(res, prod, xp);
}
public static ItemStack getCraftingResult(IDescriptiveRecipe recipe, IInventory inventoryCrafting) {
return getCraftingResult(recipe.getRecipeOutput(), recipe.preserveNBT(), inventoryCrafting);
}
public static ItemStack getCraftingResult(ItemStack result, boolean preserveNBT, IInventory inventoryCrafting) {
result = result.copy();
if (preserveNBT) {
NBTTagCompound craftingNbt = getCraftingNbt(inventoryCrafting);
if (craftingNbt == null) {
return null;
}
result.setTagCompound(craftingNbt);
}
return result;
}
public static boolean matches(IDescriptiveRecipe recipe, IInventory inventoryCrafting) {
if (!matches(recipe.getIngredients(), recipe.getWidth(), recipe.getHeight(), inventoryCrafting)) {
return false;
}
if (recipe.preserveNBT()) {
if (getCraftingNbt(inventoryCrafting) == null) {
return false;
}
}
return true;
}
public static boolean matches(Object[] ingredients, int width, int height, IInventory inventoryCrafting) {
ItemStack[][] resources = new ItemStack[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int k = i + j * 3;
resources[i][j] = inventoryCrafting.getStackInSlot(k);
}
}
return matches(ingredients, width, height, resources);
}
public static boolean matches(Object[] ingredients, int width, int height, ItemStack[][] resources) {
for (int i = 0; i <= 3 - width; i++) {
for (int j = 0; j <= 3 - height; j++) {
if (checkMatch(ingredients, width, height, resources, i, j, true)) {
return true;
}
if (checkMatch(ingredients, width, height, resources, i, j, false)) {
return true;
}
}
}
return false;
}
private static boolean checkMatch(Object[] ingredients, int width, int height, ItemStack[][] resources, int xInGrid, int yInGrid, boolean flag) {
for (int k = 0; k < 3; k++) {
for (int l = 0; l < 3; l++) {
int widthIt = k - xInGrid;
int heightIt = l - yInGrid;
Object compare = null;
if (widthIt >= 0 && heightIt >= 0 && widthIt < width && heightIt < height) {
if (flag) {
compare = ingredients[(width - widthIt - 1) + heightIt * width];
} else {
compare = ingredients[widthIt + heightIt * width];
}
}
ItemStack resource = resources[k][l];
if (compare instanceof ItemStack) {
if (!ItemStackUtil.isIdenticalItem((ItemStack) compare, resource)) {
return false;
}
} else if (compare instanceof Iterable) {
boolean matched = false;
for (Object item : (Iterable) compare) {
if (item instanceof ItemStack) {
matched = matched || ItemStackUtil.isIdenticalItem((ItemStack) item, resource);
}
}
if (!matched) {
return false;
}
} else if (compare == null && resource != null) {
return false;
}
}
}
return true;
}
}
@@ -10,7 +10,6 @@
******************************************************************************/
package forestry.core.recipes;
import java.util.ArrayList;
import java.util.HashMap;
import net.minecraft.block.Block;
@@ -19,7 +18,6 @@ import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraftforge.oredict.OreDictionary;
@@ -50,7 +48,8 @@ public class ShapedRecipeCustom implements IDescriptiveRecipe {
return this;
}
public boolean preservesNbt() {
@Override
public boolean preserveNBT() {
return preserveNBT;
}
@@ -76,130 +75,18 @@ public class ShapedRecipeCustom implements IDescriptiveRecipe {
@Override
public boolean matches(InventoryCrafting inventoryCrafting, World world) {
if (!matches((IInventory) inventoryCrafting, world)) {
return false;
}
if (preserveNBT) {
if (RecipeUtil.getCraftingNbt(inventoryCrafting) == null) {
return false;
}
}
return true;
return RecipeUtil.matches(this, inventoryCrafting);
}
@Override
@Deprecated
public boolean matches(IInventory inventoryCrafting, World world) {
ItemStack[][] resources = new ItemStack[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int k = i + j * 3;
resources[i][j] = inventoryCrafting.getStackInSlot(k);
}
}
return matches(resources);
}
public boolean matches(ItemStack[][] resources) {
for (int i = 0; i <= 3 - width; i++) {
for (int j = 0; j <= 3 - height; j++) {
if (checkMatch(resources, i, j, true)) {
return true;
}
if (checkMatch(resources, i, j, false)) {
return true;
}
}
}
return false;
}
@SuppressWarnings("unchecked")
private boolean checkMatch(ItemStack[][] resources, int xInGrid, int yInGrid, boolean flag) {
for (int k = 0; k < 3; k++) {
for (int l = 0; l < 3; l++) {
int widthIt = k - xInGrid;
int heightIt = l - yInGrid;
Object compare = null;
if (widthIt >= 0 && heightIt >= 0 && widthIt < width && heightIt < height) {
if (flag) {
compare = ingredients[(width - widthIt - 1) + heightIt * width];
} else {
compare = ingredients[widthIt + heightIt * width];
}
}
ItemStack resource = resources[k][l];
if (compare instanceof ItemStack) {
if (!checkItemMatch((ItemStack) compare, resource)) {
return false;
}
} else if (compare instanceof ArrayList) {
boolean matched = false;
for (ItemStack item : (ArrayList<ItemStack>) compare) {
matched = matched || checkItemMatch(item, resource);
}
if (!matched) {
return false;
}
} else if (compare == null && resource != null) {
return false;
}
}
}
return true;
}
private static boolean checkItemMatch(ItemStack compare, ItemStack resource) {
if (resource == null && compare == null) {
return true;
}
if (resource == null || compare == null) {
return false;
}
if (compare.getItem() != resource.getItem()) {
return false;
}
if (compare.getItemDamage() != OreDictionary.WILDCARD_VALUE && compare.getItemDamage() != resource.getItemDamage()) {
return false;
}
return true;
return RecipeUtil.matches(this, inventoryCrafting);
}
@Override
public ItemStack getCraftingResult(InventoryCrafting inventoryCrafting) {
return getCraftingResult((IInventory) inventoryCrafting);
}
public ItemStack getCraftingResult(IInventory inventoryCrafting) {
ItemStack result = product.copy();
if (preserveNBT) {
NBTTagCompound craftingNbt = RecipeUtil.getCraftingNbt(inventoryCrafting);
if (craftingNbt == null) {
return null;
}
result.setTagCompound(craftingNbt);
}
return result;
return RecipeUtil.getCraftingResult(this, inventoryCrafting);
}
@Override
@@ -207,31 +94,6 @@ public class ShapedRecipeCustom implements IDescriptiveRecipe {
return width * height;
}
/**
* @return true if resource is a valid ingredient in this recipe.
*/
@SuppressWarnings("unchecked")
public boolean isIngredient(ItemStack resource) {
for (Object ingredient : ingredients) {
if (ingredient instanceof ItemStack) {
if (checkItemMatch((ItemStack) ingredient, resource)) {
return true;
}
} else if (ingredient instanceof ArrayList) {
for (ItemStack item : (ArrayList<ItemStack>) ingredient) {
if (checkItemMatch(item, resource)) {
return true;
}
}
}
}
return false;
}
public static ShapedRecipeCustom createShapedRecipe(ItemStack product, Object... materials) {
String s = "";
@@ -49,7 +49,7 @@ public class CraftGuideCustomRecipes implements RecipeProvider {
Object[] array = new Object[10];
Object[] flattened = RecipeUtil.getCraftingRecipeAsArray(rec);
Object[] flattened = RecipeUtil.getCraftingRecipeAsArray((IDescriptiveRecipe) rec);
if (flattened == null) {
continue;
}
@@ -37,7 +37,7 @@ public class NEIHandlerShapedCustom extends RecipeHandlerBase {
ItemStack output = recipe.getRecipeOutput();
this.output = new PositionedStack(output, 119, 24);
}
this.preservesNbt = recipe.preservesNbt();
this.preservesNbt = recipe.preserveNBT();
}
public void setIngredients(int width, int height, Object[] items) {
@@ -26,6 +26,7 @@ import net.minecraftforge.fluids.FluidStack;
import forestry.api.recipes.ICarpenterManager;
import forestry.api.recipes.ICarpenterRecipe;
import forestry.api.recipes.IDescriptiveRecipe;
import forestry.core.recipes.RecipeUtil;
import forestry.core.recipes.ShapedRecipeCustom;
import forestry.core.utils.ItemStackUtil;
@@ -73,7 +74,7 @@ public class CarpenterRecipeManager implements ICarpenterManager {
}
IDescriptiveRecipe internal = recipe.getCraftingGridRecipe();
return internal.matches(inventoryCrafting, world);
return RecipeUtil.matches(internal, inventoryCrafting);
}
public static boolean isBox(ItemStack resource) {
@@ -18,6 +18,7 @@ import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;
import forestry.api.recipes.IFabricatorRecipe;
import forestry.core.recipes.RecipeUtil;
import forestry.core.recipes.ShapedRecipeCustom;
import forestry.core.utils.ItemStackUtil;
@@ -44,7 +45,7 @@ public class FabricatorRecipe implements IFabricatorRecipe {
return false;
}
return internal.matches(resources);
return RecipeUtil.matches(internal.getIngredients(), internal.getWidth(), internal.getHeight(), resources);
}
@Override
@@ -64,7 +65,7 @@ public class FabricatorRecipe implements IFabricatorRecipe {
@Override
public boolean preservesNbt() {
return internal.preservesNbt();
return internal.preserveNBT();
}
@Override
@@ -80,7 +81,7 @@ public class FabricatorRecipe implements IFabricatorRecipe {
@Override
public ItemStack getCraftingResult(IInventory craftingInventory) {
return internal.getCraftingResult(craftingInventory);
return RecipeUtil.getCraftingResult(internal, craftingInventory);
}
@Override
@@ -23,6 +23,7 @@ import net.minecraftforge.fluids.FluidStack;
import forestry.api.recipes.IFabricatorManager;
import forestry.api.recipes.IFabricatorRecipe;
import forestry.api.recipes.RecipeManagers;
import forestry.core.recipes.RecipeUtil;
import forestry.core.recipes.ShapedRecipeCustom;
import forestry.core.utils.ItemStackUtil;
@@ -50,7 +51,11 @@ public class FabricatorRecipeManager implements IFabricatorManager {
}
for (IFabricatorRecipe recipe : recipes) {
if (recipe.matches(plan, gridResources)) {
if (recipe.getPlan() != null && !ItemStackUtil.isCraftingEquivalent(recipe.getPlan(), plan)) {
continue;
}
if (RecipeUtil.matches(recipe.getIngredients(), recipe.getWidth(), recipe.getHeight(), gridResources)) {
if (liquid == null || liquid.containsFluid(recipe.getLiquid())) {
return recipe;
}
@@ -41,6 +41,7 @@ import forestry.core.items.ICraftingPlan;
import forestry.core.network.DataInputStreamForestry;
import forestry.core.network.DataOutputStreamForestry;
import forestry.core.network.GuiId;
import forestry.core.recipes.RecipeUtil;
import forestry.core.tiles.ICrafter;
import forestry.core.tiles.ILiquidTankTile;
import forestry.core.tiles.TilePowered;
@@ -228,7 +229,7 @@ public class TileFabricator extends TilePowered implements ICrafter, ILiquidTank
return null;
}
return myRecipe.getCraftingResult(craftingInventory);
return RecipeUtil.getCraftingResult(myRecipe.getRecipeOutput(), myRecipe.preservesNbt(), craftingInventory);
}
@Override