diff --git a/src/com/jaquadro/minecraft/storagedrawers/StorageDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/StorageDrawers.java index f1829bd..8df01dc 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/StorageDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/StorageDrawers.java @@ -3,6 +3,7 @@ package com.jaquadro.minecraft.storagedrawers; import com.jaquadro.minecraft.storagedrawers.config.CompTierRegistry; import com.jaquadro.minecraft.storagedrawers.config.ConfigManager; import com.jaquadro.minecraft.storagedrawers.config.OreDictRegistry; +import com.jaquadro.minecraft.storagedrawers.config.RecipeHandlerRegistry; import com.jaquadro.minecraft.storagedrawers.core.*; import com.jaquadro.minecraft.storagedrawers.integration.IntegrationRegistry; import com.jaquadro.minecraft.storagedrawers.network.BlockClickMessage; @@ -38,6 +39,7 @@ public class StorageDrawers public static ConfigManager config; public static CompTierRegistry compRegistry; public static OreDictRegistry oreDictRegistry; + public static RecipeHandlerRegistry recipeHandlerRegistry; @Mod.Instance(MOD_ID) public static StorageDrawers instance; @@ -57,6 +59,7 @@ public class StorageDrawers compRegistry = new CompTierRegistry(); oreDictRegistry = new OreDictRegistry(); + recipeHandlerRegistry = new RecipeHandlerRegistry(); blocks.init(); items.init(); diff --git a/src/com/jaquadro/minecraft/storagedrawers/api/registry/IIngredientHandler.java b/src/com/jaquadro/minecraft/storagedrawers/api/registry/IIngredientHandler.java new file mode 100644 index 0000000..5d30c05 --- /dev/null +++ b/src/com/jaquadro/minecraft/storagedrawers/api/registry/IIngredientHandler.java @@ -0,0 +1,8 @@ +package com.jaquadro.minecraft.storagedrawers.api.registry; + +import net.minecraft.item.ItemStack; + +public interface IIngredientHandler +{ + ItemStack getItemStack (Object object); +} diff --git a/src/com/jaquadro/minecraft/storagedrawers/api/registry/IRecipeHandler.java b/src/com/jaquadro/minecraft/storagedrawers/api/registry/IRecipeHandler.java new file mode 100644 index 0000000..87f2953 --- /dev/null +++ b/src/com/jaquadro/minecraft/storagedrawers/api/registry/IRecipeHandler.java @@ -0,0 +1,12 @@ +package com.jaquadro.minecraft.storagedrawers.api.registry; + +import net.minecraft.item.crafting.IRecipe; + +import java.util.List; + +public interface IRecipeHandler +{ + Object[] getInputAsArray (IRecipe recipe); + + List getInputAsList (IRecipe recipe); +} diff --git a/src/com/jaquadro/minecraft/storagedrawers/api/registry/IRecipeHandlerRegistry.java b/src/com/jaquadro/minecraft/storagedrawers/api/registry/IRecipeHandlerRegistry.java new file mode 100644 index 0000000..b83d534 --- /dev/null +++ b/src/com/jaquadro/minecraft/storagedrawers/api/registry/IRecipeHandlerRegistry.java @@ -0,0 +1,12 @@ +package com.jaquadro.minecraft.storagedrawers.api.registry; + +public interface IRecipeHandlerRegistry +{ + void registerRecipeHandler (Class clazz, IRecipeHandler handler); + + void registerIngredientHandler (Class clazz, IIngredientHandler handler); + + IRecipeHandler getRecipeHandler (Class clazz); + + IIngredientHandler getIngredientHandler (Class clazz); +} diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java index f7099d8..ea82353 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java @@ -1,25 +1,20 @@ package com.jaquadro.minecraft.storagedrawers.block.tile; import com.jaquadro.minecraft.storagedrawers.StorageDrawers; +import com.jaquadro.minecraft.storagedrawers.api.registry.IIngredientHandler; +import com.jaquadro.minecraft.storagedrawers.api.registry.IRecipeHandler; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; import com.jaquadro.minecraft.storagedrawers.config.CompTierRegistry; import com.jaquadro.minecraft.storagedrawers.config.ConfigManager; import com.jaquadro.minecraft.storagedrawers.network.CountUpdateMessage; import com.jaquadro.minecraft.storagedrawers.storage.*; -import com.jaquadro.minecraft.storagedrawers.storage.IStorageProvider; -import cpw.mods.fml.common.FMLLog; import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.common.network.simpleimpl.IMessage; import net.minecraft.inventory.InventoryCrafting; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.CraftingManager; import net.minecraft.item.crafting.IRecipe; -import net.minecraft.item.crafting.ShapedRecipes; -import net.minecraft.item.crafting.ShapelessRecipes; import net.minecraft.nbt.NBTTagCompound; -import net.minecraftforge.oredict.ShapedOreRecipe; -import net.minecraftforge.oredict.ShapelessOreRecipe; -import org.apache.logging.log4j.Level; import java.util.ArrayList; import java.util.List; @@ -236,14 +231,22 @@ public class TileEntityDrawersComp extends TileEntityDrawers if (!DrawerData.areItemsEqual(stack, output)) continue; - if (recipe instanceof ShapelessOreRecipe) - match = tryMatch(stack, ((ShapelessOreRecipe) recipe).getInput()); - else if (recipe instanceof ShapelessRecipes) - match = tryMatch(stack, ((ShapelessRecipes) recipe).recipeItems); - else if (recipe instanceof ShapedOreRecipe) - match = tryMatch(stack, ((ShapedOreRecipe) recipe).getInput()); - else if (recipe instanceof ShapedRecipes) - match = tryMatch(stack, ((ShapedRecipes) recipe).recipeItems); + IRecipeHandler handler = StorageDrawers.recipeHandlerRegistry.getRecipeHandler(recipe.getClass()); + while (handler != null) { + Object[] itemArr = handler.getInputAsArray(recipe); + if (itemArr != null) { + match = tryMatch(stack, itemArr); + break; + } + + List itemList = handler.getInputAsList(recipe); + if (itemList != null) { + match = tryMatch(stack, itemList); + break; + } + + break; + } if (match != null) { setupLookup(lookup1, stack); @@ -296,6 +299,9 @@ public class TileEntityDrawersComp extends TileEntityDrawers return null; Object item = list[0]; + if (item == null) + return null; + if (item instanceof ItemStack) { ItemStack item1 = (ItemStack)item; for (int i = 1, n = list.length; i < n; i++) { @@ -320,6 +326,29 @@ public class TileEntityDrawersComp extends TileEntityDrawers return (ItemStack)item1; } } + else { + IIngredientHandler handler = StorageDrawers.recipeHandlerRegistry.getIngredientHandler(item.getClass()); + if (handler == null) + return null; + + ItemStack item1 = handler.getItemStack(item); + if (item1 == null) + return null; + + for (int i = 1, n = list.length; i < n; i++) { + Object item2 = list[i]; + if (item2 == null || item.getClass() != item2.getClass()) + return null; + + item2 = handler.getItemStack(item2); + if (item2 == null || item2.getClass() != ItemStack.class) + return null; + if (!item1.isItemEqual((ItemStack)item2)) + return null; + } + + return item1; + } return null; } @@ -465,43 +494,6 @@ public class TileEntityDrawersComp extends TileEntityDrawers } } - /*private class CompStorageProvider extends DefaultStorageProvider - { - public CompStorageProvider () { - super(TileEntityDrawersComp.this, TileEntityDrawersComp.this); - } - - @Override - public boolean isCentrallyManaged () { - return true; - } - - @Override - public int getSlotStackCapacity (int slot) { - if (convRate == null || convRate[slot] == 0) - return 0; - - int stackLimit = convRate[0] * TileEntityDrawersComp.this.getDrawerCapacity(); - return stackLimit / convRate[slot]; - } - - @Override - public int getSlotCount (int slot) { - if (convRate == null || convRate[slot] == 0) - return 0; - - return pooledCount / convRate[slot]; - } - - @Override - public void setSlotCount (int slot, int amount) { - if (convRate == null || convRate[slot] == 0) - return; - - pooledCount = (pooledCount % convRate[slot]) + convRate[slot] * amount; - } - }*/ - private static class InventoryLookup extends InventoryCrafting { private ItemStack[] stackList; diff --git a/src/com/jaquadro/minecraft/storagedrawers/config/RecipeHandlerRegistry.java b/src/com/jaquadro/minecraft/storagedrawers/config/RecipeHandlerRegistry.java new file mode 100644 index 0000000..c85259e --- /dev/null +++ b/src/com/jaquadro/minecraft/storagedrawers/config/RecipeHandlerRegistry.java @@ -0,0 +1,120 @@ +package com.jaquadro.minecraft.storagedrawers.config; + +import com.jaquadro.minecraft.storagedrawers.api.registry.IIngredientHandler; +import com.jaquadro.minecraft.storagedrawers.api.registry.IRecipeHandler; +import com.jaquadro.minecraft.storagedrawers.api.registry.IRecipeHandlerRegistry; +import net.minecraft.item.crafting.IRecipe; +import net.minecraft.item.crafting.ShapedRecipes; +import net.minecraft.item.crafting.ShapelessRecipes; +import net.minecraftforge.oredict.ShapedOreRecipe; +import net.minecraftforge.oredict.ShapelessOreRecipe; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class RecipeHandlerRegistry implements IRecipeHandlerRegistry +{ + private Map recipeRegistry = new HashMap(); + private Map ingredientRegistry = new HashMap(); + + public RecipeHandlerRegistry () { + registerRecipeHandler(ShapedOreRecipe.class, new ShapedOreRecipeHandler()); + registerRecipeHandler(ShapedRecipes.class, new ShapedRecipeHandler()); + registerRecipeHandler(ShapelessOreRecipe.class, new ShapelessOreRecipeHandler()); + registerRecipeHandler(ShapelessRecipes.class, new ShapelessRecipeHandler()); + } + + @Override + public void registerRecipeHandler (Class clazz, IRecipeHandler handler) { + if (!recipeRegistry.containsKey(clazz)) + recipeRegistry.put(clazz, handler); + } + + @Override + public void registerIngredientHandler (Class clazz, IIngredientHandler handler) { + if (!ingredientRegistry.containsKey(clazz)) + ingredientRegistry.put(clazz, handler); + } + + @Override + public IRecipeHandler getRecipeHandler (Class clazz) { + while (clazz != null) { + if (recipeRegistry.containsKey(clazz)) + return recipeRegistry.get(clazz); + + clazz = clazz.getSuperclass(); + } + + return null; + } + + @Override + public IIngredientHandler getIngredientHandler (Class clazz) { + while (clazz != null) { + if (ingredientRegistry.containsKey(clazz)) + return ingredientRegistry.get(clazz); + + for (Class inter : clazz.getInterfaces()) { + if (ingredientRegistry.containsKey(inter)) + return ingredientRegistry.get(inter); + } + + clazz = clazz.getSuperclass(); + } + + return null; + } + + private static class ShapedOreRecipeHandler implements IRecipeHandler + { + @Override + public Object[] getInputAsArray (IRecipe recipe) { + return ((ShapedOreRecipe) recipe).getInput(); + } + + @Override + public List getInputAsList (IRecipe recipe) { + return null; + } + } + + private static class ShapedRecipeHandler implements IRecipeHandler + { + @Override + public Object[] getInputAsArray (IRecipe recipe) { + return ((ShapedRecipes) recipe).recipeItems; + } + + @Override + public List getInputAsList (IRecipe recipe) { + return null; + } + } + + private static class ShapelessOreRecipeHandler implements IRecipeHandler + { + @Override + public Object[] getInputAsArray (IRecipe recipe) { + return null; + } + + @Override + public List getInputAsList (IRecipe recipe) { + return ((ShapelessOreRecipe) recipe).getInput(); + } + } + + private static class ShapelessRecipeHandler implements IRecipeHandler + { + @Override + public Object[] getInputAsArray (IRecipe recipe) { + return null; + } + + @Override + public List getInputAsList (IRecipe recipe) { + return ((ShapelessRecipes) recipe).recipeItems; + } + } +} diff --git a/src/com/jaquadro/minecraft/storagedrawers/integration/AppliedEnergistics.java b/src/com/jaquadro/minecraft/storagedrawers/integration/AppliedEnergistics.java index e9d7253..ca92f18 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/integration/AppliedEnergistics.java +++ b/src/com/jaquadro/minecraft/storagedrawers/integration/AppliedEnergistics.java @@ -2,11 +2,12 @@ package com.jaquadro.minecraft.storagedrawers.integration; import appeng.api.AEApi; import appeng.api.networking.security.BaseActionSource; +import appeng.api.recipes.IIngredient; import appeng.api.storage.IMEInventory; import appeng.api.storage.IMEMonitor; import appeng.api.storage.data.IAEItemStack; -import com.jaquadro.minecraft.storagedrawers.integration.ae2.DrawerExternalStorageHandler; -import com.jaquadro.minecraft.storagedrawers.integration.ae2.IStorageBusMonitorFactory; +import com.jaquadro.minecraft.storagedrawers.StorageDrawers; +import com.jaquadro.minecraft.storagedrawers.integration.ae2.*; import java.lang.reflect.Constructor; @@ -68,6 +69,16 @@ public class AppliedEnergistics extends IntegrationModule @Override public void init () throws Throwable { + ShapedRecipeHandler shapedHandler = new ShapedRecipeHandler(); + if (shapedHandler.isValid()) + StorageDrawers.recipeHandlerRegistry.registerRecipeHandler(shapedHandler.getRecipeClass(), shapedHandler); + + ShapelessRecipeHandler shapelessHandler = new ShapelessRecipeHandler(); + if (shapelessHandler.isValid()) + StorageDrawers.recipeHandlerRegistry.registerRecipeHandler(shapelessHandler.getRecipeClass(), shapelessHandler); + + StorageDrawers.recipeHandlerRegistry.registerIngredientHandler(IIngredient.class, new IngredientHandler()); + ReflectionFactory rfactory = new ReflectionFactory(); if (!rfactory.init()) throw new Exception("No valid Storage Bus Monitor factory"); diff --git a/src/com/jaquadro/minecraft/storagedrawers/integration/ae2/IngredientHandler.java b/src/com/jaquadro/minecraft/storagedrawers/integration/ae2/IngredientHandler.java new file mode 100644 index 0000000..267416e --- /dev/null +++ b/src/com/jaquadro/minecraft/storagedrawers/integration/ae2/IngredientHandler.java @@ -0,0 +1,26 @@ +package com.jaquadro.minecraft.storagedrawers.integration.ae2; + +import appeng.api.recipes.IIngredient; +import com.jaquadro.minecraft.storagedrawers.api.registry.IIngredientHandler; +import net.minecraft.item.ItemStack; + +public class IngredientHandler implements IIngredientHandler +{ + @Override + public ItemStack getItemStack (Object object) { + if (!(object instanceof IIngredient)) + return null; + + try { + IIngredient ingredient = (IIngredient) object; + ItemStack[] set = ingredient.getItemStackSet(); + if (set == null || set.length == 0) + return null; + + return set[0]; + } + catch (Throwable t) { + return null; + } + } +} diff --git a/src/com/jaquadro/minecraft/storagedrawers/integration/ae2/ShapedRecipeHandler.java b/src/com/jaquadro/minecraft/storagedrawers/integration/ae2/ShapedRecipeHandler.java new file mode 100644 index 0000000..791f278 --- /dev/null +++ b/src/com/jaquadro/minecraft/storagedrawers/integration/ae2/ShapedRecipeHandler.java @@ -0,0 +1,51 @@ +package com.jaquadro.minecraft.storagedrawers.integration.ae2; + +import com.jaquadro.minecraft.storagedrawers.api.registry.IRecipeHandler; +import net.minecraft.item.crafting.IRecipe; + +import java.lang.reflect.Method; +import java.util.List; + +public class ShapedRecipeHandler implements IRecipeHandler +{ + private Class classShapedRecipe; + private Method methodGetInput; + + private boolean valid = true; + + public ShapedRecipeHandler () { + try { + classShapedRecipe = Class.forName("appeng.recipes.game.ShapedRecipe"); + methodGetInput = classShapedRecipe.getMethod("getInput"); + + if (!methodGetInput.getReturnType().isArray()) + valid = false; + } + catch (Throwable t) { + valid = false; + } + } + + public Class getRecipeClass () { + return classShapedRecipe; + } + + public boolean isValid () { + return valid; + } + + @Override + public Object[] getInputAsArray (IRecipe recipe) { + try { + return (Object[]) methodGetInput.invoke(recipe); + } + catch (Throwable t) { + return null; + } + } + + @Override + public List getInputAsList (IRecipe recipe) { + return null; + } +} diff --git a/src/com/jaquadro/minecraft/storagedrawers/integration/ae2/ShapelessRecipeHandler.java b/src/com/jaquadro/minecraft/storagedrawers/integration/ae2/ShapelessRecipeHandler.java new file mode 100644 index 0000000..8270dda --- /dev/null +++ b/src/com/jaquadro/minecraft/storagedrawers/integration/ae2/ShapelessRecipeHandler.java @@ -0,0 +1,51 @@ +package com.jaquadro.minecraft.storagedrawers.integration.ae2; + +import com.jaquadro.minecraft.storagedrawers.api.registry.IRecipeHandler; +import net.minecraft.item.crafting.IRecipe; + +import java.lang.reflect.Method; +import java.util.List; + +public class ShapelessRecipeHandler implements IRecipeHandler +{ + private Class classShapelessRecipe; + private Method methodGetInput; + + private boolean valid = true; + + public ShapelessRecipeHandler () { + try { + classShapelessRecipe = Class.forName("appeng.recipes.game.ShapelessRecipe"); + methodGetInput = classShapelessRecipe.getMethod("getInput"); + + if (!List.class.isAssignableFrom(methodGetInput.getReturnType())) + valid = false; + } + catch (Throwable t) { + valid = false; + } + } + + public Class getRecipeClass () { + return classShapelessRecipe; + } + + public boolean isValid () { + return valid; + } + + @Override + public Object[] getInputAsArray (IRecipe recipe) { + return null; + } + + @Override + public List getInputAsList (IRecipe recipe) { + try { + return (List) methodGetInput.invoke(recipe); + } + catch (Throwable t) { + return null; + } + } +}