package com.yolp900.itsjustacharm.api.constructionTable;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary;
import javax.annotation.Nonnull;
import java.util.List;
public abstract class RecipeBaseConstructionTable {
private ItemStack output;
private Object[] gridInputs;
private Object[] secInputs;
public RecipeBaseConstructionTable(@Nonnull Object output, Object[] gridInputs, Object[] secInputs) {
if (output instanceof ItemStack) {
this.output = (ItemStack)output;
} else if (output instanceof Item) {
this.output = new ItemStack((Item)output);
} else if (output instanceof Block) {
this.output = new ItemStack((Block)output);
}
this.gridInputs = gridInputs;
this.secInputs = secInputs;
}
public boolean matches(ItemStack[] grid, ItemStack[] sec) {
return gridMatches(grid) && secMatches(sec);
}
public boolean gridMatches(ItemStack[] grid) {
for (int i = 0; i < getGridInputs().length; i++) {
if (grid[i] == null) {
if (getGridInputs()[i] != null) {
return false;
}
} else {
if (getGridInputs()[i] == null) {
return false;
} else {
if (getGridInputs()[i] instanceof ItemStack) {
if (grid[i].getItem() != ((ItemStack)getGridInputs()[i]).getItem()) {
return false;
}
} else if (getGridInputs()[i] instanceof Item) {
if (grid[i].getItem() != getGridInputs()[i]) {
return false;
}
} else if (getGridInputs()[i] instanceof Block) {
if (grid[i].getItem() != Item.getItemFromBlock((Block)getGridInputs()[i])) {
return false;
}
} else if (getGridInputs()[i] instanceof String) {
List<ItemStack> oreInput = OreDictionary.getOres((String)getGridInputs()[i]);
for (ItemStack stack : oreInput) {
if (stack.getItem() != grid[i].getItem()) {
return false;
}
}
}
}
}
}
return true;
}
public boolean secMatches(ItemStack[] sec) {
if (getSecInputs() == null) {
return sec == null || sec.length == 0;
} else {
if (getSecInputs().length != sec.length) {
return false;
}
Object[] recipeSecGrid = getSecInputs().clone();
ItemStack[] currSecGrid = sec.clone();
for (int i = 0; i < currSecGrid.length; i++) {
ItemStack currStack = currSecGrid[i];
RecipeChecker:
{
for (int j = 0; j < recipeSecGrid.length; j++) {
Object recipeObj = recipeSecGrid[j];
if (recipeObj != null) {
if (recipeObj instanceof ItemStack) {
if (currStack.getItem() == ((ItemStack)recipeObj).getItem()) {
currSecGrid[i] = null;
recipeSecGrid[j] = null;
break RecipeChecker;
}
} else if (recipeObj instanceof Item) {
if (currStack.getItem() == recipeObj) {
currSecGrid[i] = null;
recipeSecGrid[j] = null;
break RecipeChecker;
}
} else if (recipeObj instanceof Block) {
if (currStack.getItem() == Item.getItemFromBlock((Block)recipeObj)) {
currSecGrid[i] = null;
recipeSecGrid[j] = null;
break RecipeChecker;
}
} else if (recipeObj instanceof String) {
int oreDictIndex = OreDictionary.getOreID((String)recipeObj);
int[] currItemOreDictValues = OreDictionary.getOreIDs(currStack);
for (int k : currItemOreDictValues) {
if (oreDictIndex == k) {
currSecGrid[i] = null;
recipeSecGrid[j] = null;
break RecipeChecker;
}
}
}
}
}
}
}
for (ItemStack stack : currSecGrid) {
if (stack != null) {
return false;
}
}
for (Object obj : recipeSecGrid) {
if (obj != null) {
return false;
}
}
return true;
}
}
@Nonnull
public ItemStack getOutput() {
return output;
}
public Object[] getGridInputs() {
return gridInputs;
}
public Object[] getSecInputs() {
return secInputs;
}
}