BloodMagic/src/main/java/WayofTime/bloodmagic/compress/StorageBlockCraftingRecipeAssimilator.java

301 lines
9.1 KiB
Java
Raw Normal View History

2015-12-28 19:09:51 -05:00
package WayofTime.bloodmagic.compress;
import WayofTime.bloodmagic.BloodMagic;
2015-12-28 19:09:51 -05:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.*;
2015-12-28 19:09:51 -05:00
import net.minecraft.world.World;
import net.minecraftforge.fml.common.registry.ForgeRegistries;
2015-12-28 19:09:51 -05:00
import net.minecraftforge.oredict.OreDictionary;
import net.minecraftforge.oredict.ShapedOreRecipe;
import net.minecraftforge.oredict.ShapelessOreRecipe;
import java.util.*;
2015-12-28 19:09:51 -05:00
public class StorageBlockCraftingRecipeAssimilator
{
public static final List<Class> ignore = new ArrayList<Class>();
public List<IRecipe> getPackingRecipes()
{
2015-12-28 19:09:51 -05:00
// grab all recipes potentially suitable for packing or unpacking
List<PackingRecipe> packingRecipes = new LinkedList<PackingRecipe>();
List<IRecipe> unpackingRecipes = new ArrayList<IRecipe>();
for (IRecipe recipe : getCraftingRecipes())
{
if (ignore.contains(recipe.getClass()))
continue;
2015-12-28 19:09:51 -05:00
ItemStack output = recipe.getRecipeOutput();
2016-12-12 19:56:36 -08:00
if (output.isEmpty())
continue;
2015-12-28 19:09:51 -05:00
2016-12-12 19:56:36 -08:00
if (output.getCount() == 1)
{
2015-12-28 19:09:51 -05:00
PackingRecipe packingRecipe = getPackingRecipe(recipe);
if (packingRecipe != null)
{
2015-12-28 19:09:51 -05:00
packingRecipes.add(packingRecipe);
}
} else if ((output.getCount() == 4 || output.getCount() == 9) && recipe.canFit(1, 1))
{
2015-12-28 19:09:51 -05:00
unpackingRecipes.add(recipe);
}
}
// grab all packing recipes which accept the output of any of the
// unpacking recipes
2015-12-28 19:09:51 -05:00
Container container = makeDummyContainer();
InventoryCrafting inventoryUnpack = new InventoryCrafting(container, 2, 2);
InventoryCrafting inventory2x2 = new InventoryCrafting(container, 2, 2);
InventoryCrafting inventory3x3 = new InventoryCrafting(container, 3, 3);
World world = null; // TODO: use a proper dummy world?
List<IRecipe> ret = new ArrayList<IRecipe>();
for (IRecipe recipeUnpack : unpackingRecipes)
{
if (ignore.contains(recipeUnpack.getClass()))
continue;
2015-12-28 19:09:51 -05:00
ItemStack unpacked = recipeUnpack.getRecipeOutput();
InventoryCrafting inventory = null;
for (Iterator<PackingRecipe> it = packingRecipes.iterator(); it.hasNext();)
{
2015-12-28 19:09:51 -05:00
PackingRecipe recipePack = it.next();
2016-01-01 10:34:17 +01:00
// check if the packing recipe accepts the unpacking recipe's output
2015-12-28 19:09:51 -05:00
boolean matched = false;
if (recipePack.possibleInputs != null)
2016-01-01 10:34:17 +01:00
{
// the recipe could be parsed, use its inputs directly since that's faster verify recipe size
2015-12-28 19:09:51 -05:00
2016-12-12 19:56:36 -08:00
if (recipePack.inputCount != unpacked.getCount())
continue;
2015-12-28 19:09:51 -05:00
// check if any of the input options matches the unpacked
// item stack
2015-12-28 19:09:51 -05:00
for (ItemStack stack : recipePack.possibleInputs)
{
if (areInputsIdentical(unpacked, stack))
{
2015-12-28 19:09:51 -05:00
matched = true;
break;
}
}
} else
{
// unknown IRecipe, check through the recipe conventionally verify recipe size for 3x3 to skip anything smaller quickly
if (unpacked.getCount() == 9 && recipePack.recipe.getIngredients().size() < 9)
continue;
2015-12-28 19:09:51 -05:00
2016-01-01 10:34:17 +01:00
// initialize inventory late, but only once per unpack recipe
2015-12-28 19:09:51 -05:00
if (inventory == null)
{
2016-12-12 19:56:36 -08:00
if (unpacked.getCount() == 4)
{
2015-12-28 19:09:51 -05:00
inventory = inventory2x2;
} else
{
2015-12-28 19:09:51 -05:00
inventory = inventory3x3;
}
2016-12-12 19:56:36 -08:00
for (int i = 0; i < unpacked.getCount(); i++)
{
2015-12-28 19:09:51 -05:00
inventory.setInventorySlotContents(i, unpacked.copy());
}
}
// check if the packing recipe accepts the unpacked item
// stack
2015-12-28 19:09:51 -05:00
matched = recipePack.recipe.matches(inventory, world);
}
if (matched)
{
// check if the unpacking recipe accepts the packing
// recipe's output
2015-12-28 19:09:51 -05:00
ItemStack packOutput = recipePack.recipe.getRecipeOutput();
inventoryUnpack.setInventorySlotContents(0, packOutput.copy());
if (recipeUnpack.matches(inventoryUnpack, world))
{
2015-12-28 19:09:51 -05:00
ret.add(recipePack.recipe);
it.remove();
}
}
}
}
return ret;
}
@SuppressWarnings("unchecked")
private List<IRecipe> getCraftingRecipes()
{
return ForgeRegistries.RECIPES.getValues();
2015-12-28 19:09:51 -05:00
}
private Container makeDummyContainer()
{
return new Container()
{
2015-12-28 19:09:51 -05:00
@Override
public boolean canInteractWith(EntityPlayer player)
{
2015-12-28 19:09:51 -05:00
return true;
}
};
}
private PackingRecipe getPackingRecipe(IRecipe recipe)
{
if (recipe.getIngredients().size() < 4)
return null;
2015-12-28 19:09:51 -05:00
List<Ingredient> inputs = recipe.getIngredients();
2015-12-28 19:09:51 -05:00
// check if the recipe inputs are size 4 or 9
int count = 0;
for (Object o : inputs)
{
if (o != null)
count++;
2015-12-28 19:09:51 -05:00
}
if (count != 4 && count != 9)
return null;
2015-12-28 19:09:51 -05:00
// grab identical inputs
List<Ingredient> identicalInputs = getIdenticalInputs(inputs);
if (identicalInputs == null)
return null;
2015-12-28 19:09:51 -05:00
return new PackingRecipe(recipe, identicalInputs, count);
}
/**
* Determine the item stacks from the provided inputs which are suitable for
* every input element.
*
* @param inputs
* List of all inputs, null elements are being ignored.
2015-12-28 19:09:51 -05:00
* @return List List of all options.
*/
@SuppressWarnings("unchecked")
private List<Ingredient> getIdenticalInputs(List<Ingredient> inputs)
{
List<Ingredient> options = null;
2015-12-28 19:09:51 -05:00
for (Ingredient input : inputs)
{
if (input == null)
continue;
2015-12-28 19:09:51 -05:00
List<ItemStack> offers;
if (input. instanceof ItemStack)
{
2015-12-28 19:09:51 -05:00
offers = Collections.singletonList((ItemStack) input);
} else if (input instanceof List)
{
2015-12-28 19:09:51 -05:00
offers = (List<ItemStack>) input;
if (offers.isEmpty())
return null;
} else
{
throw new RuntimeException("invalid input: " + input.getClass());
2015-12-28 19:09:51 -05:00
}
if (options == null)
{
2015-12-28 19:09:51 -05:00
options = new ArrayList<ItemStack>(offers);
continue;
}
for (Iterator<ItemStack> it = options.iterator(); it.hasNext();)
{
2015-12-28 19:09:51 -05:00
ItemStack stackReq = it.next();
boolean found = false;
for (ItemStack stackCmp : offers)
{
if (areInputsIdentical(stackReq, stackCmp))
{
2015-12-28 19:09:51 -05:00
found = true;
break;
}
}
if (!found)
{
2015-12-28 19:09:51 -05:00
it.remove();
if (options.isEmpty())
return null;
2015-12-28 19:09:51 -05:00
}
}
}
return options;
}
private boolean areInputsIdentical(ItemStack a, ItemStack b)
{
2015-12-28 19:09:51 -05:00
try
{
2015-12-28 19:09:51 -05:00
if (a.getItem() != b.getItem())
return false;
int dmgA = a.getItemDamage();
int dmgB = b.getItemDamage();
return dmgA == dmgB || dmgA == OreDictionary.WILDCARD_VALUE || dmgB == OreDictionary.WILDCARD_VALUE;
} catch (NullPointerException e)
{
2015-12-28 19:09:51 -05:00
BloodMagic.instance.getLogger().error("A mod in this instance has registered an item with a null input. Known problem mods are:");
// String err = "";
// for (String problem : problemMods)
// err += (err.length() > 0 ? ", " : "") + problem;
// BloodMagic.instance.getLogger().error(err);
2015-12-28 19:09:51 -05:00
return false;
}
}
private static class PackingRecipe
{
PackingRecipe(IRecipe recipe, List<ItemStack> possibleInputs, int inputCount)
{
2015-12-28 19:09:51 -05:00
this.recipe = recipe;
this.possibleInputs = possibleInputs;
this.inputCount = inputCount;
}
final IRecipe recipe;
final List<ItemStack> possibleInputs;
final int inputCount;
}
}