Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java minecraft forge似乎不叫postInit_Java_Minecraft Forge - Fatal编程技术网

Java minecraft forge似乎不叫postInit

Java minecraft forge似乎不叫postInit,java,minecraft-forge,Java,Minecraft Forge,我正在为minecraft mod Thaumcraft编写一个插件,特别是一个根据文件内容向块添加方面的插件。这是针对Minecraft 1.7.10的 代码运行preInit方法,一切正常。然而,游戏在postInit方法上崩溃了。我不明白它为什么会崩溃 以下是来自崩溃报告的stacktrace: ---- Minecraft Crash Report ---- // I'm sorry, Dave. Time: 6/10/15 5:40 PM Description: Initializ

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

代码运行preInit方法,一切正常。然而,游戏在postInit方法上崩溃了。我不明白它为什么会崩溃

以下是来自崩溃报告的stacktrace:

---- 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)
文件中有以下行:

布雷艇:海绵=水,水,水,空隙,空隙,庄稼

这将添加3个水相位、2个空相位和1个作物相位

这是我的密码:

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);
}
}
package polymer.aspectadder;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入java.util.Properties;
导入java.util.Set;
导入java.util.logging.Logger;
导入net.minecraft.item.item;
进口thaumcraft.api.ThaumcraftApi;
导入thaumcraft.api.Aspect.Aspect;
导入thaumcraft.api.aspects.AspectList;
导入cpw.mods.fml.common.Mod;
导入cpw.mods.fml.common.Mod.EventHandler;
导入cpw.mods.fml.common.event.fmlinitizationevent;
导入cpw.mods.fml.common.event.FMLPostInitializationEvent;
导入cpw.mods.fml.common.event.FMLPreInitializationEvent;
导入cpw.mods.fml.common.registry.GameRegistry;
@Mod(modid=“aspectadder”,name=“Aspect Adder”,
version=“1.0,minecraft 1.7.10”,
dependencies=“在:Thaumcraft之后需要”)
公共类AspectAdder{
/*
*一个允许将Thaumcraft方面添加到项目、块、,
*或实体(某天可以添加附魔:D)
*这是通过一个配置文件完成的。
* 
*在本例中,这是一个如何向某物添加方面的示例
*在修补匠的战斧构造中添加3个泰勒姆和2个工具
* 
*t建造:战斧=武器,武器,武器,工具,工具
*/
公共静态记录器log=Logger.getLogger(“方面加法器”);
静态字符串类=
AspectAdder.class.getProtectionDomain()
.getCodeSource().getLocation().getPath();
静态字符串pathOfInstall=pathOfClass.substring(0,
pathOfClass.indexOf(“mods”);//备份到安装文件夹
静态字符串路径=
pathOfInstall.concat(“config/aspectadder”).子字符串(6);
@事件处理程序
public void preInit(FMLPreInitializationEvent事件){
if(System.getProperty(“os.name”).startsWith(“Windows”)){
path.replaceAll(“/”,“\\\”);
}
log.info(“如果不存在文件,则在“+path+”处创建文件”);
试一试{
新文件(路径).mkdirs();
新文件(path+File.separator+“entries.txt”).createNewFile();
}捕获(IOE异常){
e、 printStackTrace();
}
}
@事件处理程序
public void init(fmlinitizationevent){
}
@事件处理程序
public void postInit(FMLPostInitializationEvent事件){
解码值(readFile());
}
静态属性readFile(){
属性p=新属性();
文件f=新文件(路径+文件.separator+“entries.txt”);
流内文件输入流;
试一试{
inStream=新文件输入流(f);
p、 负载(流内);
流内关闭();
}捕获(FileNotFoundException fnfe){
试一试{
f、 createNewFile();
}捕获(IOE异常){
e、 printStackTrace();
}
//什么也不做,因为我们在第39-44行处理了这个问题
//catch子句就在这里,因此代码可以运行
//另外,如果文件不在那里,它无论如何都是空白的
}捕获(ioe异常ioe){
ioe.printStackTrace();
}
返回p;
}
静态值(属性p){
设置键集=p.键集();
Object[]keys=新对象[p.size()];
int-dex=0;
用于(对象o:键集){
键[dex]=o;
dex++;
}
for(int i=0;i
因此,您不必计算所有这些行,崩溃似乎是由以下代码部分引起的:

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

    }
for(int i=0;i
它很可能在空行上崩溃,因为它找不到
字符。在创建项目的行上方添加此行:

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


另外,您不需要调用
key.toString()
:只要
key
就可以了。它已经是一个字符串。

您的
键之一在执行过程中不包含任何“:”,因此
key.toString().substring(0,key.indexOf(“:”)
计算为导致错误的
key.substring(0,-1)

检查是否存在“:”b
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));
  }
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
     }
}