Solved Get all furnace recipes with a given input

Discussion in 'Spigot Plugin Development' started by jbs1222, Aug 2, 2018.

  1. I've looked through the docs and I can't find a method that with return all furnace recipes with a given input. How would I go about creating such a method if it doesn't already exist? Thanks in advance for your help!

    The method I want would look like
    Code (Java):
    getFurnaceRecipesForInput(ItemStack input)
    // Returns all furnace recipes that have the specified input
     
  2. This is more or less a guess as I haven't done it yet, but a good place to start would be to use Bukkit.recipeIterator()

    By iterating over the Recipe objects, you can check if it's an instanceof a FurnaceRecipe. If it is, then cast it to FurnaceRecipe and use FurnaceRecipe#getResult() to get the resulting ItemStack, check if that equals the input ItemStack, and add it to the list or array that will be returned by the method.

    Edit: Sorry, realize I worded that and overlooked one of the aspects of the question. Instead of comparing the result, use FurnaceRecipe#getInput()
     
    #2 OffLuffy, Aug 2, 2018
    Last edited: Aug 2, 2018
    • Like Like x 1
  3. Thanks for your help! This worked great!
     
  4. Wait, never mind. The recipe iterator only has crafting recipes not furnace ones. So I get an error saying a can't cast CraftShapelessRecipe to FurnaceRecipe
     
  5. You're not trying to cast all recipes to FurnaceRecipes are you? Make sure to first check if it is one.
    Code (Java):
    Recipe r;
    if (recipe instanceof FurnaceRecipe) { /* cast and check result */ }
     
  6. Here's the part in my code where I go through the iterator:
    Code (Java):
                Iterator<Recipe> recipes = Bukkit.recipeIterator();
                while (recipes.hasNext()) {
                    if (recipes.next() instanceof FurnaceRecipe == false) continue;
                    FurnaceRecipe frecipe = (FurnaceRecipe) recipes.next();
     
  7. You're calling next() twice, which skips the FurnaceRecipe. Instead, store the next() to a variable, then check instanceof.

    Also, if it's any better for you, you can also do
    Code (Java):
    if (!(recipe instanceof FurnaceRecipe)) continue;
     
  8. That was it! Thanks again
     
    • Friendly Friendly x 1
  9. I think I opted out of that solution because it sounded like he wanted to find Recipes which require a certain component rather than ones that result in a certain item, but I did realize when you suggested that, I forgot that part by the time I typed my suggestion, lmao.

     
  10. I know you had probably already sorted out what you needed to from my earlier response despite my screw up, but I went back and edited it for the sake of others who may view the thread later.