Merge pull request #641 from AartBluestoke/upstreamMaster

cache the IRecipe
This commit is contained in:
SirSengir 2013-02-19 04:53:45 -08:00
commit c211af80ee
3 changed files with 102 additions and 5 deletions

View file

@ -0,0 +1,78 @@
package buildcraft.transport.utils;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.ShapelessRecipes;
import net.minecraft.world.World;
public class CraftingHelper {
public static IRecipe findMatchingRecipe(InventoryCrafting par1InventoryCrafting, World par2World)
{
int var3 = 0;
ItemStack var4 = null;
ItemStack var5 = null;
int var6;
for (var6 = 0; var6 < par1InventoryCrafting.getSizeInventory(); ++var6)
{
ItemStack var7 = par1InventoryCrafting.getStackInSlot(var6);
if (var7 != null)
{
if (var3 == 0)
{
var4 = var7;
}
if (var3 == 1)
{
var5 = var7;
}
++var3;
}
}
if (var3 == 2 && var4.itemID == var5.itemID && var4.stackSize == 1 && var5.stackSize == 1 && Item.itemsList[var4.itemID].isRepairable())
{
Item var11 = Item.itemsList[var4.itemID];
int var13 = var11.getMaxDamage() - var4.getItemDamageForDisplay();
int var8 = var11.getMaxDamage() - var5.getItemDamageForDisplay();
int var9 = var13 + var8 + var11.getMaxDamage() * 5 / 100;
int var10 = var11.getMaxDamage() - var9;
if (var10 < 0)
{
var10 = 0;
}
ArrayList ingredients = new ArrayList<ItemStack>(2);
ingredients.add(var4);
ingredients.add(var5);
return new ShapelessRecipes(new ItemStack(var4.itemID, 1, var10),ingredients);
}
else
{
List recipes = CraftingManager.getInstance().getRecipeList();
for (var6 = 0; var6 < recipes.size(); ++var6)
{
IRecipe var12 = (IRecipe) recipes.get(var6);
if (var12.matches(par1InventoryCrafting, par2World))
{
return var12;
}
}
return null;
}
}
}

View file

@ -17,6 +17,7 @@ import net.minecraft.inventory.IInventory;
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.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
@ -27,11 +28,13 @@ import buildcraft.api.inventory.ISpecialInventory;
import buildcraft.core.inventory.TransactorRoundRobin;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.core.utils.Utils;
import buildcraft.core.utils.CraftingHelper;
public class TileAutoWorkbench extends TileEntity implements ISpecialInventory {
private ItemStack stackList[] = new ItemStack[9];
private IRecipe currentRecipe = null;
class LocalInventoryCrafting extends InventoryCrafting {
public LocalInventoryCrafting() {
@ -53,6 +56,11 @@ public class TileAutoWorkbench extends TileEntity implements ISpecialInventory {
}
public IRecipe getCurrentRecipe() {
return currentRecipe ;
}
@Override
public int getSizeInventory() {
@ -137,9 +145,10 @@ public class TileAutoWorkbench extends TileEntity implements ISpecialInventory {
craftMatrix.setInventorySlotContents(i, stack);
}
ItemStack recipe = CraftingManager.getInstance().findMatchingRecipe(craftMatrix, worldObj);
if(this.currentRecipe == null || !this.currentRecipe.matches(craftMatrix, worldObj))
currentRecipe = CraftingHelper.findMatchingRecipe(craftMatrix, worldObj);
return recipe;
return currentRecipe.getCraftingResult(craftMatrix);
}
public ItemStack extractItem(boolean doRemove, boolean removeRecipe) {
@ -177,7 +186,10 @@ public class TileAutoWorkbench extends TileEntity implements ISpecialInventory {
craftMatrix.setInventorySlotContents(i, stack);
}
ItemStack resultStack = CraftingManager.getInstance().findMatchingRecipe(craftMatrix, worldObj);
if(this.currentRecipe == null || !this.currentRecipe.matches(craftMatrix, worldObj))
currentRecipe = CraftingHelper.findMatchingRecipe(craftMatrix, worldObj);
ItemStack resultStack = currentRecipe.getCraftingResult(craftMatrix);
if (resultStack == null || !doRemove) {
resetPointers(pointerList);

View file

@ -12,6 +12,7 @@ import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.inventory.SlotCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
@ -23,6 +24,7 @@ import buildcraft.core.network.PacketSlotChange;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.core.utils.SimpleInventory;
import buildcraft.core.utils.Utils;
import buildcraft.core.utils.CraftingHelper;
import com.google.common.collect.Lists;
@ -117,6 +119,7 @@ public class TileAssemblyAdvancedWorkbench extends TileEntity implements IInvent
private int tick;
private int recentEnergyAverage;
private InternalPlayer internalPlayer;
private IRecipe currentRecipe;
@Override
public int getSizeInventory() {
@ -354,7 +357,11 @@ public class TileAssemblyAdvancedWorkbench extends TileEntity implements IInvent
}
private void updateCraftingResults() {
craftResult.setInventorySlotContents(0, CraftingManager.getInstance().findMatchingRecipe(internalInventoryCrafting, worldObj));
if(this.currentRecipe == null || !this.currentRecipe.matches(internalInventoryCrafting, worldObj))
currentRecipe = CraftingHelper.findMatchingRecipe(internalInventoryCrafting, worldObj);
ItemStack resultStack = currentRecipe.getCraftingResult(internalInventoryCrafting);
craftResult.setInventorySlotContents(0, resultStack);
onInventoryChanged();
}