/** Find recipes excluding one or more recipe types */
  public static ItemStack findMatchingRecipeExcluding(
      InventoryCrafting inv, World world, Class... excluding) {
    int i = 0;
    ItemStack itemstack = null;
    ItemStack itemstack1 = null;
    int j;
    List recipes = CraftingManager.getInstance().getRecipeList();

    for (j = 0; j < inv.getSizeInventory(); ++j) {
      ItemStack itemstack2 = inv.getStackInSlot(j);

      if (itemstack2 != null) {
        if (i == 0) {
          itemstack = itemstack2;
        }

        if (i == 1) {
          itemstack1 = itemstack2;
        }

        ++i;
      }
    }

    if (i == 2
        && itemstack.getItem() == itemstack1.getItem()
        && itemstack.stackSize == 1
        && itemstack1.stackSize == 1
        && itemstack.getItem().isRepairable()) {
      Item item = itemstack.getItem();
      int j1 = item.getMaxDamage() - itemstack.getItemDamage(); // getItemDamageForDisplay
      int k = item.getMaxDamage() - itemstack1.getItemDamage();
      int l = j1 + k + item.getMaxDamage() * 5 / 100;
      int i1 = item.getMaxDamage() - l;

      if (i1 < 0) {
        i1 = 0;
      }

      return new ItemStack(itemstack.getItem(), 1, i1);
    } else {
      for (j = 0; j < recipes.size(); ++j) {
        IRecipe irecipe = (IRecipe) recipes.get(j);

        // 1.1.1 Botania fix (try catch)
        try {
          if ((!Arrays.asList(excluding).contains(irecipe.getClass()))
              && irecipe.matches(inv, world)) {
            return irecipe.getCraftingResult(inv);
          }
        } catch (Exception e) {
        }
      }

      return null;
    }
  }
  public ItemStack findMatchingRecipe(InventoryCrafting par1InventoryCrafting, World par2World) {
    int var3 = 0;
    ItemStack var4 = null;
    ItemStack var5 = null;

    for (int 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 var13 = Item.itemsList[var4.itemID];
      int var14 = var13.getMaxDamage() - var4.getItemDamageForDisplay();
      int var8 = var13.getMaxDamage() - var5.getItemDamageForDisplay();
      int var9 = var14 + var8 + var13.getMaxDamage() * 5 / 100;
      int var10 = var13.getMaxDamage() - var9;

      if (var10 < 0) {
        var10 = 0;
      }

      return new ItemStack(var4.itemID, 1, var10);
    } else {
      Iterator var11 = this.recipes.iterator();
      IRecipe var12;

      do {
        if (!var11.hasNext()) {
          return null;
        }

        var12 = (IRecipe) var11.next();
      } while (!var12.matches(par1InventoryCrafting, par2World));

      return var12.getCraftingResult(par1InventoryCrafting);
    }
  }
  public ItemStack getCraftingResult(TileSoulWorkBench soulworkbench) {
    InventoryCrafting InventoryCraftingSoulWorkBench = soulworkbench.getCraftingInventory();

    Iterator var11 = this.recipes.iterator();
    IRecipe var12;

    do {
      if (!var11.hasNext()) {
        // this.nSoulEnergyRequired = 0;
        return null;
      }

      var12 = (IRecipe) var11.next();
    } while (!var12.matches(InventoryCraftingSoulWorkBench, null));

    this.nSoulEnergyRequired = ((SoulWorkBenchShapedRecipes) var12).getCraftingResultSoulEnergy();

    return var12.getCraftingResult(InventoryCraftingSoulWorkBench);
  }
 private ItemStack getRecipeOutput() {
   if (internalInventoryCrafting == null || currentRecipe == null) {
     return null;
   }
   return currentRecipe.getCraftingResult(internalInventoryCrafting);
 }
  public void craftOrGetItem(final PacketPatternSlot packetPatternSlot) {
    if (packetPatternSlot.slotItem != null && this.getCellInventory() != null) {
      final IAEItemStack out = packetPatternSlot.slotItem.copy();
      InventoryAdaptor inv = new AdaptorPlayerHand(this.getPlayerInv().player);
      final InventoryAdaptor playerInv =
          InventoryAdaptor.getAdaptor(this.getPlayerInv().player, ForgeDirection.UNKNOWN);

      if (packetPatternSlot.shift) {
        inv = playerInv;
      }

      if (inv.simulateAdd(out.getItemStack()) != null) {
        return;
      }

      final IAEItemStack extracted =
          Platform.poweredExtraction(
              this.getPowerSource(), this.getCellInventory(), out, this.getActionSource());
      final EntityPlayer p = this.getPlayerInv().player;

      if (extracted != null) {
        inv.addItems(extracted.getItemStack());
        if (p instanceof EntityPlayerMP) {
          this.updateHeld((EntityPlayerMP) p);
        }
        this.detectAndSendChanges();
        return;
      }

      final InventoryCrafting ic = new InventoryCrafting(new ContainerNull(), 3, 3);
      final InventoryCrafting real = new InventoryCrafting(new ContainerNull(), 3, 3);

      for (int x = 0; x < 9; x++) {
        ic.setInventorySlotContents(
            x,
            packetPatternSlot.pattern[x] == null
                ? null
                : packetPatternSlot.pattern[x].getItemStack());
      }

      final IRecipe r = Platform.findMatchingRecipe(ic, p.worldObj);

      if (r == null) {
        return;
      }

      final IMEMonitor<IAEItemStack> storage = this.getPatternTerminal().getItemInventory();
      final IItemList<IAEItemStack> all = storage.getStorageList();

      final ItemStack is = r.getCraftingResult(ic);

      for (int x = 0; x < ic.getSizeInventory(); x++) {
        if (ic.getStackInSlot(x) != null) {
          final ItemStack pulled =
              Platform.extractItemsByRecipe(
                  this.getPowerSource(),
                  this.getActionSource(),
                  storage,
                  p.worldObj,
                  r,
                  is,
                  ic,
                  ic.getStackInSlot(x),
                  x,
                  all,
                  Actionable.MODULATE,
                  ItemViewCell.createFilter(this.getViewCells()));
          real.setInventorySlotContents(x, pulled);
        }
      }

      final IRecipe rr = Platform.findMatchingRecipe(real, p.worldObj);

      if (rr == r && Platform.isSameItemPrecise(rr.getCraftingResult(real), is)) {
        final SlotCrafting sc = new SlotCrafting(p, real, this.cOut, 0, 0, 0);
        sc.onPickupFromSlot(p, is);

        for (int x = 0; x < real.getSizeInventory(); x++) {
          final ItemStack failed = playerInv.addItems(real.getStackInSlot(x));

          if (failed != null) {
            p.dropPlayerItemWithRandomChoice(failed, false);
          }
        }

        inv.addItems(is);
        if (p instanceof EntityPlayerMP) {
          this.updateHeld((EntityPlayerMP) p);
        }
        this.detectAndSendChanges();
      } else {
        for (int x = 0; x < real.getSizeInventory(); x++) {
          final ItemStack failed = real.getStackInSlot(x);
          if (failed != null) {
            this.getCellInventory()
                .injectItems(
                    AEItemStack.create(failed),
                    Actionable.MODULATE,
                    new MachineSource(this.getPatternTerminal()));
          }
        }
      }
    }
  }
  @Override
  public void updateEntity() {
    super.updateEntity();

    if (getEnergy() >= getRequiredEnergy() && getEnergy() > 0) {
      ItemStack input = this.getStackInSlot(0);

      if (input == null) {
        return;
      }

      EntityPlayer internalPlayer = getInternalPlayer().get();

      if (craftSlot == null) {
        craftSlot = new SlotCrafting(internalPlayer, crafting, this, 1, 0, 0);
      }

      if (input.getItem() instanceof ItemPackage) {
        // Try a recipe made out of the package's contents
        NBTTagCompound tag = NBTUtils.getItemData(input);
        for (int i = 0; i < 9; i++) {
          if (tag.hasKey("item" + i)) {
            ItemStack is = ItemStack.loadItemStackFromNBT(tag.getCompoundTag("item" + i));
            if (is != null) {
              crafting.setInventorySlotContents(i, is);
            } else {
              return;
            }
          } else {
            crafting.setInventorySlotContents(i, null);
          }
        }
      } else {
        // Try a shapeless recipe made from just that item
        ItemStack input2 = input.copy();
        input2.stackSize = 1;
        crafting.setInventorySlotContents(0, input2);
        for (int i = 1; i < 9; i++) {
          crafting.setInventorySlotContents(i, null);
        }
      }

      IRecipe recipe = crafting.findRecipe();
      ItemStack result = recipe != null ? recipe.getCraftingResult(crafting).copy() : null;

      addEnergy(-getRequiredEnergy());

      if (result != null) {
        craftSlot.onPickupFromSlot(internalPlayer, result);
        handleLeftoverItems(crafting);
        handleLeftoverItems(internalPlayer.inventory);

        for (int i = 1; i <= 4; i++) {
          ItemStack inside = inv.getStackInSlot(i);

          if (inside == null || inside.stackSize <= 0) {
            inv.setInventorySlotContents(i, result.copy());
            result.stackSize = 0;
            break;
          } else if (StackHelper.canStacksMerge(inside, result)) {
            result.stackSize -= StackHelper.mergeStacks(result, inside, true);

            if (result.stackSize == 0) {
              break;
            }
          }
        }

        if (result.stackSize > 0) {
          EntityItem entityitem =
              new EntityItem(worldObj, xCoord + 0.5, yCoord + 0.7, zCoord + 0.5, result.copy());

          worldObj.spawnEntityInWorld(entityitem);
          result.stackSize = 0;
        }
        decrStackSize(0, 1);
      } else {
        ItemStack outputSlot = getStackInSlot(1);
        if (outputSlot == null) {
          setInventorySlotContents(1, getStackInSlot(0));
          setInventorySlotContents(0, null);
        }
      }
    }
  }
  @Override
  public void serverPacketData(INetworkInfo manager, AppEngPacket packet, EntityPlayer player) {
    EntityPlayerMP pmp = (EntityPlayerMP) player;
    Container con = pmp.openContainer;

    if (con instanceof IContainerCraftingPacket) {
      IContainerCraftingPacket cct = (IContainerCraftingPacket) con;
      IGridNode node = cct.getNetworkNode();
      if (node != null) {
        IGrid grid = node.getGrid();
        if (grid == null) {
          return;
        }

        IStorageGrid inv = grid.getCache(IStorageGrid.class);
        IEnergyGrid energy = grid.getCache(IEnergyGrid.class);
        ISecurityGrid security = grid.getCache(ISecurityGrid.class);
        IInventory craftMatrix = cct.getInventoryByName("crafting");
        IInventory playerInventory = cct.getInventoryByName("player");

        Actionable realForFake = cct.useRealItems() ? Actionable.MODULATE : Actionable.SIMULATE;

        if (inv != null && this.recipe != null && security != null) {
          InventoryCrafting testInv = new InventoryCrafting(new ContainerNull(), 3, 3);
          for (int x = 0; x < 9; x++) {
            if (this.recipe[x] != null && this.recipe[x].length > 0) {
              testInv.setInventorySlotContents(x, this.recipe[x][0]);
            }
          }

          IRecipe r = Platform.findMatchingRecipe(testInv, pmp.worldObj);

          if (r != null && security.hasPermission(player, SecurityPermissions.EXTRACT)) {
            ItemStack is = r.getCraftingResult(testInv);

            if (is != null) {
              IMEMonitor<IAEItemStack> storage = inv.getItemInventory();
              IItemList all = storage.getStorageList();
              IPartitionList<IAEItemStack> filter = ItemViewCell.createFilter(cct.getViewCells());

              for (int x = 0; x < craftMatrix.getSizeInventory(); x++) {
                ItemStack patternItem = testInv.getStackInSlot(x);

                ItemStack currentItem = craftMatrix.getStackInSlot(x);
                if (currentItem != null) {
                  testInv.setInventorySlotContents(x, currentItem);
                  ItemStack newItemStack =
                      r.matches(testInv, pmp.worldObj) ? r.getCraftingResult(testInv) : null;
                  testInv.setInventorySlotContents(x, patternItem);

                  if (newItemStack == null || !Platform.isSameItemPrecise(newItemStack, is)) {
                    IAEItemStack in = AEItemStack.create(currentItem);
                    if (in != null) {
                      IAEItemStack out =
                          realForFake == Actionable.SIMULATE
                              ? null
                              : Platform.poweredInsert(energy, storage, in, cct.getSource());
                      if (out != null) {
                        craftMatrix.setInventorySlotContents(x, out.getItemStack());
                      } else {
                        craftMatrix.setInventorySlotContents(x, null);
                      }

                      currentItem = craftMatrix.getStackInSlot(x);
                    }
                  }
                }

                // True if we need to fetch an item for the recipe
                if (patternItem != null && currentItem == null) {
                  // Grab from network by recipe
                  ItemStack whichItem =
                      Platform.extractItemsByRecipe(
                          energy,
                          cct.getSource(),
                          storage,
                          player.worldObj,
                          r,
                          is,
                          testInv,
                          patternItem,
                          x,
                          all,
                          realForFake,
                          filter);

                  // If that doesn't get it, grab exact items from network (?)
                  // TODO see if this code is necessary
                  if (whichItem == null) {
                    for (int y = 0; y < this.recipe[x].length; y++) {
                      IAEItemStack request = AEItemStack.create(this.recipe[x][y]);
                      if (request != null) {
                        if (filter == null || filter.isListed(request)) {
                          request.setStackSize(1);
                          IAEItemStack out =
                              Platform.poweredExtraction(energy, storage, request, cct.getSource());
                          if (out != null) {
                            whichItem = out.getItemStack();
                            break;
                          }
                        }
                      }
                    }
                  }

                  // If that doesn't work, grab from the player's inventory
                  if (whichItem == null && playerInventory != null) {
                    whichItem = extractItemFromPlayerInventory(player, realForFake, patternItem);
                  }

                  craftMatrix.setInventorySlotContents(x, whichItem);
                }
              }
              con.onCraftMatrixChanged(craftMatrix);
            }
          }
        }
      }
    }
  }