Skip to content

Instantly share code, notes, and snippets.

@tterrag1098
Created May 3, 2014 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tterrag1098/5d2d9dde72bf9ead67cf to your computer and use it in GitHub Desktop.
Save tterrag1098/5d2d9dde72bf9ead67cf to your computer and use it in GitHub Desktop.
public static void checkRecipes()
{
Set<ShapedRecipes> shaped = new HashSet<ShapedRecipes>();
Set<ShapelessRecipes> shapeless = new HashSet<ShapelessRecipes>();
Set<ShapedOreRecipe> shapedOre = new HashSet<ShapedOreRecipe>();
Set<ShapelessOreRecipe> shapelessOre = new HashSet<ShapelessOreRecipe>();
for (Object o : CraftingManager.getInstance().getRecipeList())
{
IRecipe r = (IRecipe) o;
if (r instanceof ShapedRecipes)
{
shaped.add((ShapedRecipes) r);
}
else if (r instanceof ShapelessRecipes)
{
shapeless.add((ShapelessRecipes) r);
}
else if (r instanceof ShapedOreRecipe)
{
shapedOre.add((ShapedOreRecipe) r);
}
else if (r instanceof ShapelessOreRecipe)
{
shapelessOre.add((ShapelessOreRecipe) r);
}
}
for (ShapedRecipes sr1 : shaped)
{
for (ShapedRecipes sr2 : shaped)
{
if (sr1 != sr2 && baseCheckShaped(sr1, sr2) && areItemStackArraysEquals(sr1.recipeItems, sr2.recipeItems))
recipeErr(sr1, sr2);
}
}
for (ShapelessRecipes sh1 : shapeless)
{
for (ShapelessRecipes sh2 : shapeless)
{
if (sh1 != sh2 && !sh1.getRecipeOutput().isItemEqual(sh2.getRecipeOutput()) && areItemStackArraysEqualsCast(sh1.recipeItems.toArray(), sh2.recipeItems.toArray()))
recipeErr(sh1, sh2);
}
}
for (ShapedOreRecipe sro1 : shapedOre)
{
for (ShapedOreRecipe sro2 : shapedOre)
{
if (sro1 != sro2 && !sro1.getRecipeOutput().isItemEqual(sro2.getRecipeOutput()) && checkOreDictArray(sro1.getInput(), sro2.getInput()))
recipeErr(sro1, sro2);
}
}
for (ShapelessOreRecipe sho1 : shapelessOre)
{
for (ShapelessOreRecipe sho2 : shapelessOre)
{
if (sho1 != sho2 && !sho1.getRecipeOutput().isItemEqual(sho2.getRecipeOutput()) && checkOreDictArray(sho1.getInput().toArray(), sho2.getInput().toArray()))
recipeErr(sho1, sho2);
}
}
}
private static boolean baseCheckShaped(ShapedRecipes s1, ShapedRecipes s2)
{
return !(!s1.getRecipeOutput().isItemEqual(s2.getRecipeOutput()) || s1.recipeHeight != s2.recipeHeight || s1.recipeWidth != s2.recipeWidth);
}
private static boolean checkOreDictArray(Object[] a1, Object[] a2)
{
if (a1.length != a2.length)
return false;
for (int i = 0; i < a1.length; i++)
{
if (a1[i] instanceof String)
{
if (!a1[i].equals(a2[i]))
return false;
}
else if (a1[i] instanceof ItemStack)
{
if (!(a2[i] instanceof ItemStack))
return false;
if (!ItemStack.areItemStacksEqual((ItemStack) a1[i], (ItemStack) a2[i]))
return false;
}
else
{
if (a1[i].getClass() != a2[i].getClass())
return false;
}
}
return true;
}
private static boolean areItemStackArraysEqualsCast(Object[] a1, Object[] a2)
{
return areItemStackArraysEquals((ItemStack[]) a1, (ItemStack[]) a2);
}
private static class IdenticalRecipeException extends RuntimeException
{
private static final long serialVersionUID = 4575791419745830235L;
private IRecipe i1, i2;
public IdenticalRecipeException(IRecipe i1, IRecipe i2)
{
this.i1 = i1;
this.i2 = i2;
}
@Override
public String getMessage()
{
return i1.toString() + " was identical to " + i2.toString() + "\n" + "Outputs were: " + i1.getRecipeOutput() + ", " + i2.getRecipeOutput();
}
}
private static void recipeErr(IRecipe i1, IRecipe i2)
{
System.out.println(i1.getRecipeOutput().isItemEqual(i2.getRecipeOutput()) + "************************************");
throw new RuntimeException(new IdenticalRecipeException(i1, i2));
}
private static boolean areItemStackArraysEquals(ItemStack[] a1, ItemStack[] a2)
{
if (a1.length != a2.length)
return false;
System.out.println(Arrays.deepToString(a1) + "\n" + Arrays.deepToString(a2));
for (int i = 0; i < a1.length; i++)
{
if (!ItemStack.areItemStacksEqual(a1[i], a2[i]))
return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment