使用Eclipse的Mod for Minecraft在运行时会出错

时间:2012-01-21 07:46:31

标签: java eclipse minecraft

我从一个块创建了一个工具,我在代码中没有出错。当我尝试单击构建它给我这个终端错误: 我怎样才能解决这个问题?请。 以下是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
                });
    }
}

修改的 我也给了Eclipe 1024mb的RAM并删除了我的.Minecraft文件夹。

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

3 个答案:

答案 0 :(得分:1)

recipeItems[0].length是6.但是recipeItems[2]以及以下只有五个元素。你在addRecipes中的顶层循环因此是错误的。

你应该使用集合类型(vector,list,Array,...)和迭代器,这样可以使代码更安全,更易读(IMO)。

答案 1 :(得分:0)

addRecipies中的外部循环遍历[0] recipeItems数组recipeItems[0][5]。在第一个子数组中,有6个元素意味着recipeItems将是有效项。但是对于所有{{1}}数组都有一个错误的假设。至少有一个后面的数组少于6个元素。

您应该迭代子数组的大小而不是第一个数组的大小。

答案 2 :(得分:0)

首先,您应该在投射之前检查数组中Object的类型是什么类型,因此当前的部分

Item item = (Item)recipeItems[j + 1][i];
循环中的

应该替换为这样的东西:

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
}

因为,我非常确定您无法将Item投放到Block。这不会解决这个问题,这只是一个提示,因为您之前使用的代码可能会在将来导致错误。

当前问题的解决方案是 Mat nicholas.hauschild 已经回答的问题。 recipeItems数组(recipeItems[0]recipeItems[1])的前两个元素有6个元素,但其余元素只有5个元素。在你的循环中,你只需要获取第一个元素的长度,并使用它来遍历其余元素,但它们比第一个元素小。
如果您尝试访问具有5个元素的数组中的元素6会发生什么?

您可以使用以下内容替换循环:

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
        }
    }
}

希望这有帮助!