minecraft forge似乎没有打电话给postInit

时间:2015-06-10 21:58:48

标签: java minecraft-forge

我正在为Minecraft mod Thaumcraft编写一个插件,特别是根据文件内容向块添加方面的插件。这是针对Minecraft 1.7.10

代码运行preInit方法,一切都很顺利。但是,游戏在postInit方法崩溃了。我无法弄清楚它崩溃的原因

以下是崩溃报告中的堆栈跟踪:

---- Minecraft Crash Report ----
// I'm sorry, Dave.

Time: 6/10/15 5:40 PM
Description: Initializing game

    java.lang.StringIndexOutOfBoundsException: String index out of range: 
-1
at java.lang.String.substring(Unknown Source)
at polymer.aspectadder.AspectAdder.decodeValues(AspectAdder.java:105)
at polymer.aspectadder.AspectAdder.postInit(AspectAdder.java:64)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at cpw.mods.fml.common.FMLModContainer.
handleModStateEvent(FMLModContainer.java:513)
at sun.reflect.GeneratedMethodAccessor3.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.common.eventbus.EventSubscriber.
handleEvent(EventSubscriber.java:74)
at com.google.common.eventbus.SynchronizedEventSubscriber.
handleEvent(SynchronizedEventSubscriber.java:47)
at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322)
at com.google.common.eventbus.EventBus.
dispatchQueuedEvents(EventBus.java:304)
at com.google.common.eventbus.EventBus.post(EventBus.java:275)
at cpw.mods.fml.common.LoadController.
sendEventToModContainer(LoadController.java:208)
at cpw.mods.fml.common.LoadController.
propogateStateMessage(LoadController.java:187)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.common.eventbus.EventSubscriber.
handleEvent(EventSubscriber.java:74)
at com.google.common.eventbus.SynchronizedEventSubscriber
.handleEvent(SynchronizedEventSubscriber.java:47)
at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322)
at com.google.common.eventbus.EventBus.
dispatchQueuedEvents(EventBus.java:304)
at com.google.common.eventbus.EventBus.post(EventBus.java:275)
at cpw.mods.fml.common.LoadController.
distributeStateMessage(LoadController.java:118)
at cpw.mods.fml.common.Loader.initializeMods(Loader.java:694)
at cpw.mods.fml.client.FMLClientHandler.
finishMinecraftLoading(FMLClientHandler.java:288)
at net.minecraft.client.Minecraft.func_71384_a(Minecraft.java:541)
at net.minecraft.client.Minecraft.func_99999_d(Minecraft.java:867)
at net.minecraft.client.main.Main.main(SourceFile:148)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)
at net.minecraft.launchwrapper.Launch.main(Launch.java:28)


A detailed walkthrough of the error, its code path and all known details  
is as follows:    
---------------------------------------------------------------------------
------------

-- Head --
Stacktrace:
at java.lang.String.substring(Unknown Source)
at polymer.aspectadder.AspectAdder.decodeValues(AspectAdder.java:105)
at polymer.aspectadder.AspectAdder.postInit(AspectAdder.java:64)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at cpw.mods.fml.common.FMLModContainer.
handleModStateEvent(FMLModContainer.java:513)
at sun.reflect.GeneratedMethodAccessor3.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.common.eventbus.EventSubscriber.
handleEvent(EventSubscriber.java:74)
at com.google.common.eventbus.SynchronizedEventSubscriber.
handleEvent(SynchronizedEventSubscriber.java:47)
at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322)
at com.google.common.eventbus.EventBus.
dispatchQueuedEvents(EventBus.java:304)
at com.google.common.eventbus.EventBus.post(EventBus.java:275)
at cpw.mods.fml.common.LoadController.
sendEventToModContainer(LoadController.java:208)
at cpw.mods.fml.common.LoadController.
propogateStateMessage(LoadController.java:187)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.common.eventbus.EventSubscriber.
handleEvent(EventSubscriber.java:74)
at com.google.common.eventbus.SynchronizedEventSubscriber.
handleEvent(SynchronizedEventSubscriber.java:47)
at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322)
at com.google.common.eventbus.EventBus.
dispatchQueuedEvents(EventBus.java:304)
at com.google.common.eventbus.EventBus.post(EventBus.java:275)
at cpw.mods.fml.common.LoadController.
distributeStateMessage(LoadController.java:118)
at cpw.mods.fml.common.Loader.initializeMods(Loader.java:694)
at cpw.mods.fml.client.FMLClientHandler.
finishMinecraftLoading(FMLClientHandler.java:288)
at net.minecraft.client.Minecraft.func_71384_a(Minecraft.java:541)

-- Initialization --
Details:
Stacktrace:
at net.minecraft.client.Minecraft.func_99999_d(Minecraft.java:867)
at net.minecraft.client.main.Main.main(SourceFile:148)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)
at net.minecraft.launchwrapper.Launch.main(Launch.java:28)

该文件中包含以下行:

的Minecraft:海绵=水,水,海水,VOID,VOID,CROP

这应该增加3个WATER方面,2个VOID方面和1个CROP方面。

这是我的代码:

package polymer.aspectadder;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Logger;

import net.minecraft.item.Item;
import thaumcraft.api.ThaumcraftApi;
import thaumcraft.api.aspects.Aspect;
import thaumcraft.api.aspects.AspectList;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.registry.GameRegistry;

@Mod(modid = "aspectadder", name = "Aspect Adder", 
version = "1.0,minecraft 1.7.10", 
dependencies="required-after:Thaumcraft")
public class AspectAdder {


/*
 * A mod that allows the adding of Thaumcraft aspects to items, blocks,
 * or entities (enchantments may be added one day :D) 
 * This is done through a config file.
 * 
 * This is an example of how to add an aspect to something, in this case
 * adding 3 telum and 2 instrumentum to a Tinker's Construct Battleaxe
 * 
 * tconstruct:battleaxe=WEAPON,WEAPON,WEAPON,TOOL,TOOL
 */
public static Logger log = Logger.getLogger("Aspect Adder");
static String pathOfClass =
AspectAdder.class.getProtectionDomain()
.getCodeSource().getLocation().getPath();

static String pathOfInstall = pathOfClass.substring(0, 
pathOfClass.indexOf("mods")); //back up to the install folder
static String path = 
pathOfInstall.concat("config/aspectadder").substring(6);


@EventHandler
public void preInit(FMLPreInitializationEvent event){
        if(System.getProperty("os.name").startsWith("Windows")){
            path.replaceAll("/", "\\\\");
        }
    log.info("Creating a file at " + path + " if one does not exist.");
    try {
        new File(path).mkdirs();
        new File(path + File.separator + "entries.txt").createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }


}

@EventHandler
public void init(FMLInitializationEvent event){

}

@EventHandler
public void postInit(FMLPostInitializationEvent event){
    decodeValues(readFile());
}

static Properties readFile(){
    Properties p = new Properties();
    File f = new File(path + File.separator + "entries.txt");

        FileInputStream inStream;
        try {
            inStream = new FileInputStream(f);
            p.load(inStream);
            inStream.close();
        } catch (FileNotFoundException fnfe) {
            try {
                f.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            // do nothing, since we handled this at lines 39-44
            // this catch clause is just here so the code runs
            // plus, the file is blank anyways if it wasn't there
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

    return p;
}


static void decodeValues(Properties p){
    Set<Object> keySet= p.keySet();
    Object[] keys = new Object[p.size()];
    int dex = 0;
    for(Object o : keySet){
        keys[dex] = o;
        dex++;
    }

    for(int i = 0; i < keys.length; i++){
        String key = keys[i].toString();
        Item item = GameRegistry.findItem(
                key.toString().substring(0, key.indexOf(":")),
                key.toString().substring(key.indexOf(":") + 1));
                //these are modID and item name, without the colon
        addAspects(item, p.getProperty(key));

    }
}


static void addAspects(Item item, String aspects) {
    log.info("Adding Aspects: " + aspects + " to " + 
item.getUnlocalizedName());
    AspectList list = new AspectList();
            int commaIndex = 0;
    int i = 0;
    do{
        String aspect = aspects.substring(commaIndex + 1, aspects.substring(commaIndex + 1).indexOf(","));
        commaIndex = aspects.substring(commaIndex + 1).indexOf(",");
        list.add(Aspect.getAspect(aspect), 1);
        i++;
        log.info("Attempting to add " + Aspect.getAspect(aspect).getTag() + " (" + aspect.toLowerCase() + ") to " + item.getUnlocalizedName());
    }while(commaIndex < aspects.lastIndexOf(","));
    ThaumcraftApi.registerObjectTag(item.getUnlocalizedName(), list);
}
}

所以你不必计算所有这些行,崩溃似乎是由代码的这一部分引起的:

    for(int i = 0; i < keys.length; i++){
        String key = keys[i].toString();
        Item item = GameRegistry.findItem(
                //the following line is the one in the crash report
                key.toString().substring(0, key.indexOf(":")), 
                key.toString().substring(key.indexOf(":") + 1));
                //these are modID and item name, without the colon
        addAspects(item, p.getProperty(key));

    }

3 个答案:

答案 0 :(得分:1)

很可能它在空行上崩溃,因为它无法在其中找到:个字符。将此行添加到创建项目的行上方:

if (key.length() == 0) continue;

此外,您无需致电key.toString():只需key即可。它已经是一个字符串。

答案 1 :(得分:1)

其中一个Sub LastRowofData() Dim ws As Worksheet Dim rng1 As Range Set ws = Sheets("ILS_IMPORT") Set rng1 = ws.Columns("A:AI").Find("*", ws.[a1], xlValues, , xlByRows, xlPrevious) If Not rng1 Is Nothing Then MsgBox "The last cell with data is " & rng1.Address(0) & " , therefore copy the current date in AG2 down to the row indicated." Else MsgBox ws.Name & " columns A:AI are empty", vbCritical End If End Sub 在执行过程中不包含任何“:”,所以 key被评估为导致错误的key.toString().substring(0, key.indexOf(":"))

在使用key.substring(0, -1)

之前检查是否存在“:”

答案 2 :(得分:0)

您正在项目注册表中查找minecraft:sponge。海绵是一个块,而不是一个项目。 BlockItem有一个完全不同的id,不会出现在minecraft:sponge下的项目注册表中。

所以而不是:

Item item = GameRegistry.findItem(
            //the following line is the one in the crash report
            key.toString().substring(0, key.indexOf(":")), 
            key.toString().substring(key.indexOf(":") + 1));
            //these are modID and item name, without the colon

你应该做

 Item item = GameRegistry.findItem(
            //the following line is the one in the crash report
            key.toString().substring(0, key.indexOf(":")), 
            key.toString().substring(key.indexOf(":") + 1));
            //these are modID and item name, without the colon
  // nothing found, assume it's a block
  if(item == null) {
      Block block = GameRegistry.findBlock(key.toString().substring(0,key.indexOf(":")),
                                           key.toString().substring(key.indexOf(":") + 1));
      if(block != null) {
          item = Block.getItemFromBlock(block);// You should probaly also do something with metadata here...
      }
  }
  if(item != null) {
      addAspects(item, p.getProperty(key));
  }

编辑我注意到没有任何检查,他从项目注册表中获得了一个简单的公然抓取,所以我添加了上面的内容。但是下面的内容也很重要。我没有在上面的代码中添加以下步骤。

您应该学习如何处理字符串。始终检查是否存在某些内容(索引不是-1,返回值不为null)然后继续。

如果那么简单。特别是用户自定义输入。你必须总是假设他们输错了。因为这只是在启动时完成,所以你可以在检查中使它“昂贵”。

int pos = str.indexOf(":");
if(pos > -1) {
     String[] contents = str.split(":");
     if(contents.length == 2) {
          String modname = contents[0];
          String itemid = contents[2];
          if(modname != null && itemid != null && modname.length() > 0 && itemid.length > 0
     }
}