Solved How I can get a ingredients of an Item to do uncraft?

Discussion in 'Plugin Development' started by lucasdidur, Dec 29, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    lucasdidur

    Hello,

    Could someone help me get the ingredients of an item.
    Would you like to make a uncraft plugin.
     
  2. what did you tried to acomplish this task already, maybe we can point out where it goes wrong
     
  3. Offline

    lucasdidur

    I have created this, but gets null:

    Code:
        @SuppressWarnings("rawtypes")
        public static ItemStack[] uncraft(ItemStack item)
        {
            net.minecraft.server.v1_4_6.ItemStack itemstackc = CraftItemStack.asNMSCopy(item);
           
            List list = CraftingManager.getInstance().getRecipes();
           
            Object recipe_tmp;
            Object ingredient_tmp;
            IRecipe recipe;
            net.minecraft.server.v1_4_6.ItemStack result;
           
            net.minecraft.server.v1_4_6.ItemStack back[] = new net.minecraft.server.v1_4_6.ItemStack[0];
           
            for (int i = 0; i < list.size(); i++)
            {
                recipe_tmp = list.get(i);
                if(!(recipe_tmp instanceof IRecipe))
                    continue;
                recipe = (IRecipe) recipe_tmp;
               
                result = recipe.b() == null ? null : recipe.b().cloneItemStack();
               
                if((result == null) || (result.id != itemstackc.id) || (result.getData() != itemstackc.getData()))
                    continue;
               
                if(recipe instanceof ShapedRecipes)
                {
                    try
                    {
                        Field field = ShapedRecipes.class.getDeclaredField("items");
                        field.setAccessible(true);
                       
                        ingredient_tmp = field.get(recipe);
                       
                        if(!(ingredient_tmp instanceof net.minecraft.server.v1_4_6.ItemStack[]))
                            break;
                       
                        net.minecraft.server.v1_4_6.ItemStack[] tmp3 = (net.minecraft.server.v1_4_6.ItemStack[]) ingredient_tmp;
                       
                        back = new net.minecraft.server.v1_4_6.ItemStack[tmp3.length];
                       
                        for (int j = 0; j < tmp3.length; j++)
                        {
                            back[j] = tmp3[j].cloneItemStack();
                        }
                    } catch (Throwable t)
                    {
                        t.printStackTrace();
                        break;
                    }
                }
                else if(recipe instanceof ShapelessRecipes)
                {
                    try
                    {
                        Field field = ShapelessRecipes.class.getDeclaredField("ingredients");
                        field.setAccessible(true);
                       
                        ingredient_tmp = field.get(recipe);
                       
                        if(!(ingredient_tmp instanceof List))
                            break;
                       
                        Object obj[] = ((List) ingredient_tmp).toArray();
                       
                        back = new net.minecraft.server.v1_4_6.ItemStack[obj.length];
                       
                        for (int j = 0; j < obj.length; j++)
                        {
                            back[j] = ((net.minecraft.server.v1_4_6.ItemStack) obj[j]).cloneItemStack();
                        }
                    } catch (Throwable t)
                    {
                        t.printStackTrace();
                        break;
                    }
                }
               
                for (int j = 0; j < back.length; j++)
                {
                    if((back[j] != null) && (back[j].getData() < 0))
                    {
                        back[j].setData(0);
                    }
                }
               
            }
           
            ItemStack[] item_back = new ItemStack[back.length];
           
            for(int i = 0; i >= back.length; i++)
            {
                item_back[i] = CraftItemStack.asBukkitCopy(back[i]);
            }
           
            return item_back ;
        }
    
     
  4. Offline

    fireblast709

    Something like this maybe (untested):
    Code:java
    1. private final none = new ItemStack(Material.AIR, 0);
    2.  
    3. public ItemStack[][] undoCrafting(ItemStack i)
    4. {
    5. List<Recipe> recipes = Bukkit.getRecipesFor(i);
    6. if(recipes.size() < 0)
    7. {
    8. return new ItemStack[]
    9. {
    10. {none, none, none},
    11. {none, none, none},
    12. {none, none, none}
    13. };
    14. }
    15. Recipe first = recipes.get(0);
    16. ItemStack returns = new ItemStack[3][3];
    17. if(first instanceof ShapedRecipe)
    18. {
    19. ShapedRecipe sr = (ShapedRecipe)first;
    20. Map<Char, ItemStack> chart = sr.getIngredientsMap();
    21. String[] shape = sr.getShape();
    22. char[] columns;
    23. for(int i = 0; i < shape.length; i++)
    24. {
    25. columns = row.toCharArray();
    26. for(int j = 0; j < columns.length; j++)
    27. {
    28. ItemStack x = chart.get(columns[j]);
    29. returns[i][j] = (x != null ? x : none);
    30. }
    31. }
    32. }
    33. else if(first instanceof ShapelessRecipe)
    34. {
    35. ShapelessRecipe sr = (ShapelessRecipe)first;
    36. List<ItemStack> x = sr.getIngredientList();
    37. int index;
    38. for(int i = 0; i < 3; i++)
    39. {
    40. for(int j = 0; j < 3; j++)
    41. {
    42. index = 3*i+j;
    43. if(index >= x.size())
    44. {
    45. returns[i][j] = none;
    46. }
    47. else
    48. {
    49. returns[i][j] = x.get(index);
    50. }
    51. }
    52. }
    53. }
    54. }[/i][/i][/i]
     
    ThunderWaffeMC likes this.
  5. Offline

    lucasdidur

    Thank you, I'll try.

    I found some problem at this part:

    What is: row.toCharArray();
    Code:
            if(first instanceof ShapedRecipe)
            {
                ShapedRecipe sr = (ShapedRecipe)first;
                Map<Character, ItemStack> chart = sr.getIngredientMap();
                String[] shape = sr.getShape();
                char[] columns;
               
                for(int i = 0; i < shape.length; i++)
                {
                    columns = row.toCharArray();
                   
                    for(int j = 0; j < columns.length; j++)
                    {
                        ItemStack x = chart.get(columns[j]);
                        returns[i][j] = (x != null ? x : none);
                    }
                }
            }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  6. Offline

    fireblast709

    it converts a String to a char array... So "Hello" would become {'H', 'e', 'l', 'l', 'o'}. I am sorry that my code is a bit incorrect, I was writing it in Notepad++ and changed somewhere in between from foreach to normal for loops
    Code:java
    1. if(first instanceof ShapedRecipe)
    2. {
    3. ShapedRecipe sr = (ShapedRecipe)first;
    4. Map<Character, ItemStack> chart = sr.getIngredientMap();
    5. String[] shape = sr.getShape();
    6. char[] columns;
    7.  
    8. for(int i = 0; i < shape.length; i++)
    9. {
    10. columns = shape[i ].toCharArray();
    11.  
    12. for(int j = 0; j < columns.length; j++)
    13. {
    14. ItemStack x = chart.get(columns[j]);
    15. returns[i ][j] = (x != null ? x : none);
    16. }
    17. }
    18. }
     
  7. Offline

    lucasdidur

    Thank you for the help, I created a new function based on their.

    Here go:

    Code:
    public List<ItemStack> undoCrafting(ItemStack item)
        {
            List<Recipe> recipes = Bukkit.getRecipesFor(item);
            if(recipes.size() < 0)
            {
                return new ArrayList<ItemStack>();
            }
            Recipe first = recipes.get(0);
            List<ItemStack> returns = new ArrayList<ItemStack>();
            
            if(first instanceof ShapedRecipe)
            {
                ShapedRecipe sr = (ShapedRecipe) first;
                Map<Character, ItemStack> chart = sr.getIngredientMap();
                
                for (ItemStack is : chart.values())
                {
                    returns.add(is);
                }
            }
            else if(first instanceof ShapelessRecipe)
            {
                ShapelessRecipe sr = (ShapelessRecipe) first;
                List<ItemStack> x = sr.getIngredientList();
                
                for (ItemStack is : x)
                {
                    returns.add(is);
                }
            }
            
            return returns;
        }
    
     
  8. Offline

    fireblast709

    Just note that your users won't get the same quantities :p
     
  9. Offline

    lucasdidur

    Yes, is not finished yet.
     
  10. Offline

    lucasdidur

    Here the final uncraft function:

    Code:
        public static List<ItemStack> uncraft(ItemStack item)
        {
            List<ItemStack> returns = new ArrayList<ItemStack>();
            List<Recipe> recipes = Bukkit.getRecipesFor(item);
            
            if(recipes.size() <= 0)
                return returns;
            
            Recipe recipe = null;
            
            for (Recipe r : recipes)
            {
                if(r.getResult().isSimilar(item))
                {
                    if(r.getResult().getAmount() != item.getAmount())
                        continue;
                    
                    recipe = r;
                    break;
                }
            }
            
            if(recipe == null)
                return returns;
            
            if(recipe instanceof ShapedRecipe)
            {
                ShapedRecipe sr = (ShapedRecipe) recipe;
                Map<Character, ItemStack> chart = sr.getIngredientMap();
                            
                for (ItemStack is : chart.values())
                {
                    if(is != null)
                        returns.add(is);
                }
            }
            else if(recipe instanceof ShapelessRecipe)
            {
                ShapelessRecipe sr = (ShapelessRecipe) recipe;
                List<ItemStack> x = sr.getIngredientList();
                
                for (ItemStack is : x)
                {
                    if(is != null)
                        returns.add(is);
                }
            }
            
            return returns;
        }
    
     
Thread Status:
Not open for further replies.

Share This Page