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,10 +1,12 @@
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
@ -12,24 +14,24 @@ import dark.core.prefab.helpers.Pair;
* @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,9 +15,11 @@ 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
*
@ -122,6 +124,74 @@ 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