2015-12-28 19:09:51 -05:00
|
|
|
package WayofTime.bloodmagic.compress;
|
|
|
|
|
2016-03-17 13:00:44 -07:00
|
|
|
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;
|
2017-08-14 20:53:42 -07:00
|
|
|
import net.minecraft.item.crafting.*;
|
2015-12-28 19:09:51 -05:00
|
|
|
import net.minecraft.world.World;
|
2017-08-14 20:53:42 -07:00
|
|
|
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;
|
2016-03-17 13:00:44 -07:00
|
|
|
|
|
|
|
import java.util.*;
|
2015-12-28 19:09:51 -05:00
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
public class StorageBlockCraftingRecipeAssimilator
|
|
|
|
{
|
2016-04-24 08:30:59 -07:00
|
|
|
public static final List<Class> ignore = new ArrayList<Class>();
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
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>();
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
for (IRecipe recipe : getCraftingRecipes())
|
|
|
|
{
|
2016-04-24 08:30:59 -07:00
|
|
|
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())
|
2015-12-30 15:34:40 -05:00
|
|
|
continue;
|
2015-12-28 19:09:51 -05:00
|
|
|
|
2016-12-12 19:56:36 -08:00
|
|
|
if (output.getCount() == 1)
|
2015-12-30 15:34:40 -05:00
|
|
|
{
|
2015-12-28 19:09:51 -05:00
|
|
|
PackingRecipe packingRecipe = getPackingRecipe(recipe);
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
if (packingRecipe != null)
|
|
|
|
{
|
2015-12-28 19:09:51 -05:00
|
|
|
packingRecipes.add(packingRecipe);
|
|
|
|
}
|
2017-08-14 20:53:42 -07:00
|
|
|
} else if ((output.getCount() == 4 || output.getCount() == 9) && recipe.canFit(1, 1))
|
2015-12-30 15:34:40 -05:00
|
|
|
{
|
2015-12-28 19:09:51 -05:00
|
|
|
unpackingRecipes.add(recipe);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
// 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>();
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
for (IRecipe recipeUnpack : unpackingRecipes)
|
|
|
|
{
|
2016-04-24 08:30:59 -07:00
|
|
|
if (ignore.contains(recipeUnpack.getClass()))
|
|
|
|
continue;
|
|
|
|
|
2015-12-28 19:09:51 -05:00
|
|
|
ItemStack unpacked = recipeUnpack.getRecipeOutput();
|
|
|
|
InventoryCrafting inventory = null;
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
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;
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
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())
|
2015-12-30 15:34:40 -05:00
|
|
|
continue;
|
2015-12-28 19:09:51 -05:00
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
// check if any of the input options matches the unpacked
|
|
|
|
// item stack
|
2015-12-28 19:09:51 -05:00
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
for (ItemStack stack : recipePack.possibleInputs)
|
|
|
|
{
|
|
|
|
if (areInputsIdentical(unpacked, stack))
|
|
|
|
{
|
2015-12-28 19:09:51 -05:00
|
|
|
matched = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-12-30 15:34:40 -05:00
|
|
|
} else
|
2016-01-02 17:56:37 -05:00
|
|
|
{
|
|
|
|
// unknown IRecipe, check through the recipe conventionally verify recipe size for 3x3 to skip anything smaller quickly
|
|
|
|
|
2017-08-14 20:53:42 -07:00
|
|
|
if (unpacked.getCount() == 9 && recipePack.recipe.getIngredients().size() < 9)
|
2015-12-30 15:34:40 -05:00
|
|
|
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
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
if (inventory == null)
|
|
|
|
{
|
2016-12-12 19:56:36 -08:00
|
|
|
if (unpacked.getCount() == 4)
|
2015-12-30 15:34:40 -05:00
|
|
|
{
|
2015-12-28 19:09:51 -05:00
|
|
|
inventory = inventory2x2;
|
2015-12-30 15:34:40 -05:00
|
|
|
} 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-30 15:34:40 -05:00
|
|
|
{
|
2015-12-28 19:09:51 -05:00
|
|
|
inventory.setInventorySlotContents(i, unpacked.copy());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
// check if the packing recipe accepts the unpacked item
|
|
|
|
// stack
|
2015-12-28 19:09:51 -05:00
|
|
|
|
|
|
|
matched = recipePack.recipe.matches(inventory, world);
|
|
|
|
}
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
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());
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
if (recipeUnpack.matches(inventoryUnpack, world))
|
|
|
|
{
|
2015-12-28 19:09:51 -05:00
|
|
|
ret.add(recipePack.recipe);
|
|
|
|
it.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
2015-12-30 15:34:40 -05:00
|
|
|
private List<IRecipe> getCraftingRecipes()
|
|
|
|
{
|
2017-08-14 20:53:42 -07:00
|
|
|
return ForgeRegistries.RECIPES.getValues();
|
2015-12-28 19:09:51 -05:00
|
|
|
}
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
private Container makeDummyContainer()
|
|
|
|
{
|
|
|
|
return new Container()
|
|
|
|
{
|
2015-12-28 19:09:51 -05:00
|
|
|
@Override
|
2015-12-30 15:34:40 -05:00
|
|
|
public boolean canInteractWith(EntityPlayer player)
|
|
|
|
{
|
2015-12-28 19:09:51 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
private PackingRecipe getPackingRecipe(IRecipe recipe)
|
|
|
|
{
|
2017-08-14 20:53:42 -07:00
|
|
|
if (recipe.getIngredients().size() < 4)
|
2015-12-30 15:34:40 -05:00
|
|
|
return null;
|
2015-12-28 19:09:51 -05:00
|
|
|
|
2017-08-14 20:53:42 -07: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;
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
for (Object o : inputs)
|
|
|
|
{
|
|
|
|
if (o != null)
|
|
|
|
count++;
|
2015-12-28 19:09:51 -05:00
|
|
|
}
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
if (count != 4 && count != 9)
|
|
|
|
return null;
|
2015-12-28 19:09:51 -05:00
|
|
|
|
|
|
|
// grab identical inputs
|
|
|
|
|
2017-08-14 20:53:42 -07:00
|
|
|
List<Ingredient> identicalInputs = getIdenticalInputs(inputs);
|
2015-12-30 15:34:40 -05:00
|
|
|
if (identicalInputs == null)
|
|
|
|
return null;
|
2015-12-28 19:09:51 -05:00
|
|
|
|
|
|
|
return new PackingRecipe(recipe, identicalInputs, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-12-30 15:34:40 -05:00
|
|
|
* Determine the item stacks from the provided inputs which are suitable for
|
|
|
|
* every input element.
|
|
|
|
*
|
|
|
|
* @param inputs
|
2016-01-02 17:56:37 -05:00
|
|
|
* 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")
|
2017-08-14 20:53:42 -07:00
|
|
|
private List<Ingredient> getIdenticalInputs(List<Ingredient> inputs)
|
2015-12-30 15:34:40 -05:00
|
|
|
{
|
2017-08-14 20:53:42 -07:00
|
|
|
List<Ingredient> options = null;
|
2015-12-28 19:09:51 -05:00
|
|
|
|
2017-08-14 20:53:42 -07:00
|
|
|
for (Ingredient input : inputs)
|
2015-12-30 15:34:40 -05:00
|
|
|
{
|
|
|
|
if (input == null)
|
|
|
|
continue;
|
2015-12-28 19:09:51 -05:00
|
|
|
|
|
|
|
List<ItemStack> offers;
|
|
|
|
|
2017-08-14 20:53:42 -07:00
|
|
|
if (input. instanceof ItemStack)
|
2015-12-30 15:34:40 -05:00
|
|
|
{
|
2015-12-28 19:09:51 -05:00
|
|
|
offers = Collections.singletonList((ItemStack) input);
|
2015-12-30 15:34:40 -05:00
|
|
|
} else if (input instanceof List)
|
|
|
|
{
|
2015-12-28 19:09:51 -05:00
|
|
|
offers = (List<ItemStack>) input;
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
if (offers.isEmpty())
|
|
|
|
return null;
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
throw new RuntimeException("invalid input: " + input.getClass());
|
2015-12-28 19:09:51 -05:00
|
|
|
}
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
if (options == null)
|
|
|
|
{
|
2015-12-28 19:09:51 -05:00
|
|
|
options = new ArrayList<ItemStack>(offers);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
for (Iterator<ItemStack> it = options.iterator(); it.hasNext();)
|
|
|
|
{
|
2015-12-28 19:09:51 -05:00
|
|
|
ItemStack stackReq = it.next();
|
|
|
|
boolean found = false;
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
for (ItemStack stackCmp : offers)
|
|
|
|
{
|
|
|
|
if (areInputsIdentical(stackReq, stackCmp))
|
|
|
|
{
|
2015-12-28 19:09:51 -05:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
if (!found)
|
|
|
|
{
|
2015-12-28 19:09:51 -05:00
|
|
|
it.remove();
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
if (options.isEmpty())
|
|
|
|
return null;
|
2015-12-28 19:09:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
private boolean areInputsIdentical(ItemStack a, ItemStack b)
|
|
|
|
{
|
2015-12-28 19:09:51 -05:00
|
|
|
|
2015-12-30 15:34:40 -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;
|
2015-12-30 15:34:40 -05:00
|
|
|
} 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:");
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|