Reputation: 1
I made a tool from a block I made no errors in code. When I try to click build it gives me this terminal error: How can i fix this? Please. Here is the code for the RecipesTools.addRecipes
package net.minecraft.src;
public class RecipesTools
{
private String recipePatterns[][] =
{
{
"XXX", " # ", " # "
}, {
"X", "#", "#"
}, {
"XX", "X#", " #"
}, {
"XX", " #", " #"
}
};
private Object recipeItems[][];
public RecipesTools()
{
recipeItems = (new Object[][]
{
new Object[] {
Block.planks, Block.cobblestone, Item.ingotIron, Item.diamond, Item.ingotGold, Block.RadiatedStone
}, new Object[] {
Item.pickaxeWood, Item.pickaxeStone, Item.pickaxeSteel, Item.pickaxeDiamond, Item.pickaxeGold, Item.pickaxeRadiated
}, new Object[] {
Item.shovelWood, Item.shovelStone, Item.shovelSteel, Item.shovelDiamond, Item.shovelGold
}, new Object[] {
Item.axeWood, Item.axeStone, Item.axeSteel, Item.axeDiamond, Item.axeGold
}, new Object[] {
Item.hoeWood, Item.hoeStone, Item.hoeSteel, Item.hoeDiamond, Item.hoeGold
}
});
}
public void addRecipes(CraftingManager craftingmanager)
{
for (int i = 0; i < recipeItems[0].length; i++)
{
Object obj = recipeItems[0][i];
for (int j = 0; j < recipeItems.length - 1; j++)
{
Item item = (Item)recipeItems[j + 1][i];
craftingmanager.addRecipe(new ItemStack(item), new Object[]
{
recipePatterns[j], Character.valueOf('#'), Item.stick, Character.valueOf('X'), obj
});
}
}
craftingmanager.addRecipe(new ItemStack(Item.shears), new Object[]
{
" #", "# ", Character.valueOf('#'), Item.ingotIron
});
}
}
EDIT I also have given Eclipe 1024mb of RAM and deleted my .Minecraft folder.
CONFLICT @ 22
27 achievements
Exception in thread "main" java.lang.ExceptionInInitializerError
at net.minecraft.src.StatList.initCraftableStats(StatList.java:74)
at net.minecraft.src.StatList.initBreakableStats(StatList.java:55)
at net.minecraft.src.Block.<clinit>(Block.java:975)
at net.minecraft.src.TextureWaterFX.<init>(TextureWaterFX.java:13)
at net.minecraft.client.Minecraft.<init>(Minecraft.java:205)
at net.minecraft.src.MinecraftImpl.<init>(MinecraftImpl.java:13)
at net.minecraft.client.Minecraft.startMainThread(Minecraft.java:1984)
at net.minecraft.client.Minecraft.startMainThread1(Minecraft.java:1970)
at net.minecraft.client.Minecraft.main(Minecraft.java:2032)
at Start.main(Start.java:25)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 5
at net.minecraft.src.RecipesTools.addRecipes(RecipesTools.java:44)
at net.minecraft.src.CraftingManager.<init>(CraftingManager.java:19)
at net.minecraft.src.CraftingManager.<clinit>(CraftingManager.java:8)
... 10 more
Upvotes: 0
Views: 769
Reputation: 3042
First of all, you should probably check of what type the Object
s in the array are, before you cast them, so the part which currently is
Item item = (Item)recipeItems[j + 1][i];
in the loop, should be replaced with something like this:
Object itemObj = recipeItems[j + 1][i];
if(itemObj instanceof Item)
{
// The current element is an Item
Item item = (Item)recipeItems[j + 1][i];
craftingmanager.addRecipe(new ItemStack(item), new Object[]
{
recipePatterns[j], Character.valueOf('#'), Item.stick, Character.valueOf('X'), obj
});
}
else if(itemObj instanceof Block)
{
// The current element is a Block
Block block = (Block)recipeItems[j + 1][i];
craftingmanager.addRecipe(new ItemStack(block), new Object[]
{
recipePatterns[j], Character.valueOf('#'), Item.stick, Character.valueOf('X'), obj
});
}
else
{
// The current element is none of the types above
// TODO Throw an exception, print a message or quit the game
}
because, I'm pretty sure you can't cast an Item
to a Block
. This will not fix this problem, that was just a tip, because the code you used there before may cause errors in the future.
The solution to your current problem is what Mat and nicholas.hauschild has already answered. The first two elements of the recipeItems
array (recipeItems[0]
and recipeItems[1]
) have 6 elements, but the rest of the elements have only 5 elements. In your loop, you only take the length of the first element and use it to loop through the rest of the elements too, but they are smaller than the first.
What happens if you try to acces element 6 in an array with 5 elements?
You could replace the loop with something like this:
for(int i = 0; i < recipeItems.length - 1; i++)
{
for(int j = 0; j < recipeItems[i].length; j++)
{
Object obj = recipeItems[0][j];
Object itemObj = recipeItems[i + 1][j];
if(itemObj instanceof Item)
{
// The current element is an Item
Item item = (Item)recipeItems[j + 1][i];
craftingmanager.addRecipe(new ItemStack(item), new Object[]
{
recipePatterns[j], Character.valueOf('#'), Item.stick, Character.valueOf('X'), obj
});
}
else if(itemObj instanceof Block)
{
// The current element is a Block
Block block = (Block)recipeItems[j + 1][i];
craftingmanager.addRecipe(new ItemStack(block), new Object[]
{
recipePatterns[j], Character.valueOf('#'), Item.stick, Character.valueOf('X'), obj
});
}
else
{
// The current element is none of the types above
// TODO Throw an exception, print a message or quit the game
}
}
}
Hope this helps!
Upvotes: 0
Reputation: 42849
Your outer loop in addRecipies
is iterating over the [0]
array of recipeItems
. In the first child array, there are 6 elements meaning that recipeItems[0][5]
will be a valid item. But there is an incorrect assumption that this is true for all recipeItems
arrays. At least one of the later arrays has fewer than 6 elements.
You should be iterating over the size of the child arrays rather than the size of the first array.
Upvotes: 0
Reputation: 206727
recipeItems[0].length
is 6. But recipeItems[2]
and the following only have five elements. Your toplevel loop in addRecipes
it therefore wrong.
You should probably be using collection types (vector, list, Array
, ...) and iterators for this, would make the code safer and more readable (IMO).
Upvotes: 1