Reworked ProcessorRecipe system to allow salvaging

This commit is contained in:
DarkGuardsman 2013-09-17 06:10:48 -04:00
parent bca6cc0262
commit 509a5c661b
3 changed files with 178 additions and 27 deletions

View file

@ -1,35 +1,37 @@
package dark.api;
import java.util.HashMap;
import java.util.Random;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import dark.core.prefab.helpers.AutoCraftingManager;
import dark.core.prefab.helpers.Pair;
/** Recipes for ore processor machines
*
*
* @author DarkGuardsman */
public class ProcessorRecipes
{
private static Random random = new Random();
public static enum ProcessorType
{
CRUSHER("crusher"),
GRINDER("grinder"),
PRESS("press");
public String unlocalizedContainerName;
CRUSHER(),
GRINDER(),
PRESS();
public HashMap<Pair<Integer, Integer>, ItemStack> recipes = new HashMap();
public HashMap<Pair<Integer, Integer>, Pair<ItemStack, Float>> recipesChance = new HashMap();
public HashMap<Pair<Integer, Integer>, Float> recipesChanceSalvage = new HashMap();
private ProcessorType(String name)
{
this.unlocalizedContainerName = "tile." + name + ".name";
}
}
static
{
createRecipe(ProcessorType.CRUSHER, new ItemStack(Block.stone.blockID, 1, 0), new ItemStack(Block.cobblestone.blockID, 1, 0));
createRecipe(ProcessorType.GRINDER, new ItemStack(Block.cobblestone.blockID, 1, 0), new ItemStack(Block.sand.blockID, 1, 0));
createSalvageRecipe(ProcessorType.CRUSHER, new ItemStack(Block.chest, 1), .8f);
}
@ -50,6 +52,39 @@ public class ProcessorRecipes
}
}
public static void createRecipeWithChance(ProcessorType type, Object in, Object out, float chance)
{
if (in != null && out != null && type != null)
{
ItemStack input = convert(in);
ItemStack output = convert(out);
if (input != null && output != null)
{
HashMap<Pair<Integer, Integer>, Pair<ItemStack, Float>> map = type.recipesChance;
if (map != null && !map.containsKey(input))
{
map.put(new Pair<Integer, Integer>(input.itemID, input.getItemDamage()), new Pair<ItemStack, Float>(output, chance));
}
}
}
}
public static void createSalvageRecipe(ProcessorType type, Object in, float chance)
{
if (in != null && type != null)
{
ItemStack input = convert(in);
if (input != null && input != null)
{
HashMap<Pair<Integer, Integer>, Float> map = type.recipesChanceSalvage;
if (map != null && !map.containsKey(input))
{
map.put(new Pair<Integer, Integer>(input.itemID, input.getItemDamage()), chance);
}
}
}
}
private static ItemStack convert(Object object)
{
if (object instanceof ItemStack)
@ -67,15 +102,16 @@ public class ProcessorRecipes
return null;
}
public static ItemStack getOuput(ProcessorType type, ItemStack stack)
public static ItemStack[] getOuput(ProcessorType type, ItemStack stack)
{
if (stack == null || type == null)
if (stack == null || type == null || stack.getItem() == null)
{
return null;
}
HashMap<Pair<Integer, Integer>, ItemStack> map = type.recipes;
ItemStack testStack = stack.copy();
testStack.stackSize = 1;
HashMap<Pair<Integer, Integer>, Pair<ItemStack, Float>> mapChance = type.recipesChance;
HashMap<Pair<Integer, Integer>, Float> mapSalvage = type.recipesChanceSalvage;
Pair<Integer, Integer> blockSet = new Pair<Integer, Integer>(stack.itemID, stack.getItemDamage());
if (map == null)
{
return null;
@ -83,8 +119,52 @@ public class ProcessorRecipes
ItemStack re = map.get(new Pair<Integer, Integer>(stack.itemID, -1));
if (re != null)
{
return re;
return new ItemStack[] { re };
}
return map.get(new Pair<Integer, Integer>(stack.itemID, stack.getItemDamage()));
re = map.get(blockSet);
if (re != null)
{
return new ItemStack[] { re };
}
Pair<ItemStack, Float> ree = mapChance.get(blockSet);
if (ree != null && random.nextFloat() >= ree.getValue())
{
return new ItemStack[] { ree.getKey() };
}
float chance = 0;
try
{
chance = mapSalvage != null ? mapSalvage.get(blockSet) : 0;
}
catch (Exception e)
{
}
if (chance == 0)
{
chance = .1f;
}
ItemStack[] recipeList = AutoCraftingManager.getReverseRecipe(stack.copy());
ItemStack[] reList = null;
if (recipeList != null)
{
reList = new ItemStack[recipeList.length];
for (int i = 0; i < recipeList.length; i++)
{
if (recipeList[i] != null && random.nextFloat() >= chance)
{
if (recipeList[i].getItemDamage() == 32767)
{
recipeList[i] = new ItemStack(recipeList[i].itemID, recipeList[i].stackSize, recipeList[i].getItemDamage());
}
reList[i] = recipeList[i];
}
}
}
return reList;
}
public static void parseOreNames()
{
}
}

View file

@ -30,6 +30,7 @@ import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.event.FMLServerStoppingEvent;
import cpw.mods.fml.common.network.NetworkMod;
import dark.api.ProcessorRecipes;
import dark.core.common.BlockRegistry.BlockData;
import dark.core.common.blocks.BlockBasalt;
import dark.core.common.blocks.BlockColorGlass;
@ -126,7 +127,7 @@ public class DarkMain extends ModPrefab
BlockRegistry.registerAllBlocks();
ExternalModHandler.init();
super.init(event);
ProcessorRecipes.parseOreNames();
if (CoreRecipeLoader.blockOre != null)
{
for (int i = 0; i < EnumMeterials.values().length; i++)

View file

@ -15,12 +15,14 @@ import net.minecraft.item.crafting.ShapelessRecipes;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerDestroyItemEvent;
import net.minecraftforge.oredict.OreDictionary;
import net.minecraftforge.oredict.ShapedOreRecipe;
import net.minecraftforge.oredict.ShapelessOreRecipe;
import cpw.mods.fml.relauncher.ReflectionHelper;
import dark.core.prefab.TileEntityMachine;
/** Rewrite of the imprinter crafting system into its own manageable class
*
*
* @author DarkGuardsman */
public class AutoCraftingManager
{
@ -57,7 +59,7 @@ public class AutoCraftingManager
}
/** Does this player's inventory contain the required resources to craft this item?
*
*
* @return Required items to make the desired item. */
public Pair<ItemStack, ItemStack[]> getIdealRecipe(ItemStack outputItem)
{
@ -122,8 +124,76 @@ public class AutoCraftingManager
return null;
}
/** Gets a basic array containing all items that were used to craft the given item. Doesn't sort
* threw the recipes and will return the first possible recipe */
public static ItemStack[] getReverseRecipe(ItemStack outputItem)
{
for (Object object : CraftingManager.getInstance().getRecipeList())
{
if (object instanceof IRecipe)
{
if (((IRecipe) object).getRecipeOutput() != null)
{
if (((IRecipe) object).getRecipeOutput().isItemEqual(outputItem))
{
if (object instanceof ShapedRecipes)
{
return ((ShapedRecipes) object).recipeItems.clone();
}
else if (object instanceof ShapelessRecipes)
{
return (ItemStack[]) ((ShapelessRecipes) object).recipeItems.toArray(new ItemStack[1]).clone();
}
else if (object instanceof ShapedOreRecipe)
{
ShapedOreRecipe oreRecipe = (ShapedOreRecipe) object;
Object[] recipeItems = (Object[]) ReflectionHelper.getPrivateValue(ShapedOreRecipe.class, oreRecipe, "input");
List<ItemStack> actualResources = new ArrayList<ItemStack>();
if (recipeItems != null)
{
for (Object obj : recipeItems)
{
if (obj instanceof ItemStack)
{
ItemStack recipeItem = (ItemStack) obj;
actualResources.add(recipeItem.copy());
}
else if (obj instanceof ArrayList)
{
Object[] ingredientsArray = ((ArrayList) obj).toArray();
for (int x = 0; x < ingredientsArray.length; x++)
{
if (ingredientsArray[x] != null && ingredientsArray[x] instanceof ItemStack)
{
ItemStack recipeItem = (ItemStack) ingredientsArray[x];
actualResources.add(recipeItem.copy());
}
}
}
}
return actualResources.toArray(new ItemStack[1]);
}
}
else if (object instanceof ShapelessOreRecipe)
{
ShapelessOreRecipe oreRecipe = (ShapelessOreRecipe) object;
return (ItemStack[]) ((ArrayList) ReflectionHelper.getPrivateValue(ShapelessOreRecipe.class, oreRecipe, "input")).toArray(new ItemStack[1]).clone();
}
}
}
}
}
return null;
}
/** Gets the itemStacks in the inv based on slots
*
*
* @param inv - @IInventory instance
* @param slots - slot # to be used
* @return array of itemStack the same size as the slots input array */
@ -143,7 +213,7 @@ public class AutoCraftingManager
}
/** Returns if the following inventory has the following resource required.
*
*
* @param recipeItems - The items to be checked for the recipes. */
public ArrayList<ItemStack> hasResource(Object[] recipeItems)
{
@ -231,7 +301,7 @@ public class AutoCraftingManager
}
/** Decreases the stack by a set amount
*
*
* @param stack - starting stack
* @param amount - amount of items
* @return the edited stack */
@ -262,7 +332,7 @@ public class AutoCraftingManager
}
/** Checks if an item exist within the inv array
*
*
* @param recipeItem - itemstack being searched for
* @param containingItems - inv array containing the search bounds
* @return the point in the array the item was found -1 = the item was null or not valid -2 =
@ -294,14 +364,14 @@ public class AutoCraftingManager
/** Checks if itemstack are equal based on crafting result rather than normal itemstack this is
* done so that if the itemstack returns with
*
*
* @param recipeItem - itemstack being compared
* @param checkStack - itemstack being comparted
* @return true if the items are a match for each other
*
*
* If the item can't be stack and is able to take damage the item will be check on damaged
* status
*
*
* If the item's meta data is not normal or in other words equals 32767 the meta data will be
* ignored */
public static boolean areStacksEqual(ItemStack recipeItem, ItemStack checkStack)
@ -322,7 +392,7 @@ public class AutoCraftingManager
}
/** Consumes an item checking for extra conditions like container items
*
*
* @param stack - starting itemStack
* @param ammount - amount to consume
* @return what is left of the itemStack if any */
@ -367,7 +437,7 @@ public class AutoCraftingManager
}
/** Used to automatically remove selected items from crafting inv
*
*
* @param requiredItems - items that are to be removed */
public void consumeItems(ItemStack... requiredItems)
{