2012-01-21 56 views
0

我從塊中製作了一個工具,我在代碼中沒有犯錯。當我嘗試點擊構建時,它給了我這個終端錯誤: 我該如何解決這個問題?請。 下面是RecipesTools.addRecipes使用Eclipse的Minecraft的Mod在運行時出現錯誤

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

編輯 我也給的RAM 1024MB的Eclipe和刪除了我.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 

回答

1

recipeItems[0].length是6.但是recipeItems[2]和下面只具有五行。你的頂層循環在addRecipes因此是錯誤的。

您應該使用集合類型(向量,列表,Array,...)以及迭代器來使代碼更安全,更易讀(IMO)。

+0

我幾乎沒有編輯這個從原來的Minecraft代碼 – user1162004

+1

當我幾乎沒有編輯我的類來指代'Strrrrring'它不編譯... –

+0

我試圖解釋爲什麼我的集合類型是他們的方式 – user1162004

0

addRecipies中的外環正在迭代[0]陣列recipeItems。在第一個子數組中,有6個元素意味着recipeItems[0][5]將是一個有效的項目。但有一個不正確的假設,對於所有recipeItems陣列都是如此。後面的陣列中至少有一個少於6個元素。你應該遍歷子數組的大小而不是第一個數組的大小。

0

首先,你應該檢查什麼樣的數組中鍵入Object s爲,你投在他們面前,因此部分目前是

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 
} 

,因爲我敢肯定,你可以不投的ItemBlock。這不會解決這個問題,這只是一個提示,因爲您以前使用的代碼可能會在將來導致錯誤。

解決當前的問題是什麼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 
     } 
    } 
} 

希望這有助於!

相關問題