Posted May 20, 20169 yr I am having some trouble iterating over every furnace recipe and assigning a recipe to each recipe. It is meant to assign the output of the recipe as the output of the smelting recipe one of the inputs as the item being smelted. Here is the code I have. The print line is for debugging and only prints "null". Any suggestions are welcome. Map smeltList = FurnaceRecipes.instance().getSmeltingList(); for (int i = 0; i < smeltList.size(); i++) { System.out.println(smeltList.get(i)); } founder of the bacon foundation; for kids who cant read good you want to join?
May 20, 20169 yr FurnaceRecipes#getSmeltingList returns a Map<ItemStack, ItemStack> , i.e. a Map with ItemStack keys and ItemStack values. Calling Map#get on this with an int will always return null because it doesn't have int keys. You can't iterate over a Map directly, you must iterate through either its entry set, key set or values collection (obtained by calling the corresponding Map methods). All of these return an object that implements Iterable , so you can either iterate through them with an enhanced for/for-each loop or by using the Iterator . When iterating a collection with an enhanced for loop, you can't modify the collection at all. When iterating with an Iterator , you can remove the current value by calling Iterator#remove but you can't add new values. This is basic Java knowledge that you should already have before making a mod. Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.
May 20, 20169 yr Author When I learnt Java I didn't read anything about "enhanced for" loops. I will research about them and then tell you the results! founder of the bacon foundation; for kids who cant read good you want to join?
May 20, 20169 yr Author FurnaceRecipes#getSmeltingList returns a Map<ItemStack, ItemStack> , i.e. a Map with ItemStack keys and ItemStack values. Calling Map#get on this with an int will always return null because it doesn't have int keys. You can't iterate over a Map directly, you must iterate through either its entry set, key set or values collection (obtained by calling the corresponding Map methods). All of these return an object that implements Iterable , so you can either iterate through them with an enhanced for/for-each loop or by using the Iterator . When iterating a collection with an enhanced for loop, you can't modify the collection at all. When iterating with an Iterator , you can remove the current value by calling Iterator#remove but you can't add new values. This is basic Java knowledge that you should already have before making a mod. Thank you so much! It worked after I did a quick Google search. The whole problem was mainly the way that I couldn't work out how to iterate over a set properly, so I ignored it. This is how I did it after. Set smeltList = FurnaceRecipes.instance().getSmeltingList().keySet(); for (Object in: smeltList) { GameRegistry.addShapelessRecipe(FurnaceRecipes.instance().getSmeltingResult((ItemStack) in), furnace, (ItemStack) in); } founder of the bacon foundation; for kids who cant read good you want to join?
May 20, 20169 yr Don't use raw types (e.g. Set ). FurnaceRecipes.instance().getSmeltingList().keySet() returns a generic type ( Set<ItemStack> ), so refer to it as that type. Iterating over the keys and using FurnaceRecipes#getSmeltingResult to get the smelting result is extremely inefficient since you're now iterating through the smelting list again for every entry. Either look up the ItemStack key directly in the Map or iterate over the entries so you have direct access to the key and value. Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.
May 20, 20169 yr Author Don't use raw types (e.g. Set ). FurnaceRecipes.instance().getSmeltingList().keySet() returns a generic type ( Set<ItemStack> ), so refer to it as that type. Iterating over the keys and using FurnaceRecipes#getSmeltingResult to get the smelting result is extremely inefficient since you're now iterating through the smelting list again for every entry. Either look up the ItemStack key directly in the Map or iterate over the entries so you have direct access to the key and value. Is this more efficient? It might not be what you were telling me, but this seems to work too. Map smeltList = FurnaceRecipes.instance().getSmeltingList(); Set<ItemStack> smeltSet = smeltList.keySet(); for (ItemStack in: smeltSet) { GameRegistry.addShapelessRecipe((ItemStack) smeltList.get(in), furnace, in); } founder of the bacon foundation; for kids who cant read good you want to join?
May 20, 20169 yr That's the first alternative I suggested, it's more efficient than using FurnaceRecipes#getSmeltingResult . It's probably slightly less efficient than iterating through the entries rather than the keys (since it needs to look up the value of the key in the map), but not by a large amount. You're still using raw types: FurnaceRecipes#getSmeltingList() returns a Map<ItemStack, ItemStack> , not a Map . Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.
May 20, 20169 yr Author That's the first alternative I suggested, it's more efficient than using FurnaceRecipes#getSmeltingResult . It's probably slightly less efficient than iterating through the entries rather than the keys (since it needs to look up the value of the key in the map), but not by a large amount. You're still using raw types: FurnaceRecipes#getSmeltingList() returns a Map<ItemStack, ItemStack> , not a Map . *facepalm* I forgot to change the Map to be Map<ItemStack, ItemStack> . Nevermind, I have changed that now! Thanks for your advice. founder of the bacon foundation; for kids who cant read good you want to join?
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.