2017-04-20 185 views
0

我正在做一个minecraft-forge mod,我的配置文件有问题。我使用了Configuration#getStringList,它允许其他人向配置文件添加一个String [],所以我使用它从数组中获得一个块,例如,如果有人写minecraft:gold_ore Block#getBlockFromName工作正常,因为在数组只有那个,没有别的,但是如果我在下一行放置了minecraft:diamond_ore,它会崩溃,因为Block#getBlockFromName读取了我发给他的字符串,所以他像这样读取它:我的世界:gold_oreminecraft:diamond_ore导致崩溃,因为名称以外的块不存在)而不是它读取我的世界:gold_ore和minecraft:diamond_ore。我基本上想要将该配置的每一行分割成单独的字符串或以某种方式单独读取每行。 这是配置文件的样子:将数组的每一行分割成一个字符串

# Configuration file 
"ore generation" { 
    S:ore_to_gen < 
     minecraft:gold_ore 
     minecraft:diamond_ore 
    > 

所以如果我只需要输入我的世界:gold_ore和删除的Minecraft:diamond_ore正常世界的负荷和发电工程,这是配置类:

public class ConfigCustomOreGen { 

     public static Configuration configCustomWorld; 
     private static File configCustomOreGenDir; 

     public static String oreToReplace; 

     public static final String WORLD = "Ore Generation"; 


     public static void init(File oreGenDir) { 
      oreGenDir = new File(oreGenDir, Constants.MODID + "/" + "world"); 
      oreGenDir.mkdir(); 
      ConfigCustomOreGen.configCustomOreGenDir = oreGenDir; 
      ConfigCustomOreGen.configCustomWorld = new Configuration(new File(oreGenDir, "Custom-Ore-Generation.cfg")); 

      String[] oreReplace = configCustomWorld.getStringList("ore_to_gen", WORLD, EMPTY_STRING, "ore which should have custom ore gen\n"); 

      StringBuilder strBuilder = new StringBuilder(); 
      for (int i = 0; i < oreReplace.length; i++) { 
       strBuilder.append(oreReplace[i]); 
      } 
      oreToReplace = strBuilder.toString(); 
      Constants.LOGGER.info(oreToReplace + " " + strBuilder.toString()); 

      if (configCustomWorld.hasChanged()) { 
       configCustomWorld.save(); 
      } 
     } 

     private final static String[] EMPTY_STRING = {}; 
} 

对于这一代,我只是使用我的自定义WorldGenMinable,它的工作原理是因为它与minecraft:gold_ore以及我在ore变量下给出的任何块一起工作,当Block#getBlockFromName读取不存在的块名称时会发生问题:

public class CustomOreGen implements IWorldGenerator { 

    @Override 
    public void generate(Random random, int chunkX, int chunkZ, World world, IChunkGenerator chunkGenerator, IChunkProvider chunkProvider) { 
     Block ore = Block.getBlockFromName(ConfigCustomOreGen.oreToReplace); 
     generateOre(ore, world, random, chunkX, chunkZ, 20, 180, 8, 24, 8); 
    } 

    private void generateOre(Block ore, World world, Random random, int chunkX, int chunkZ, int minY, int maxY, int minVeinSize, int maxVeinSize, int chancesToSpawn) { 
     int heightRange = maxY - minY; 
     BlockPos blockpos = new BlockPos((chunkX * 16) + random.nextInt(16), minY + random.nextInt(heightRange), (chunkZ * 16) + random.nextInt(16)); 

     if (world.provider.getDimension() == 0) { 
      for (int i = 0; i < chancesToSpawn; i++) { 
       WorldGenIngotterMinable generator = new WorldGenIngotterMinable(ore, minVeinSize, maxVeinSize, Blocks.AIR); 
       generator.generate(world, random, blockpos); 
      } 
     } 
    } 
} 
+0

在其包配置类? – alpert

回答

0

您可以简单地在每个换行符上分割字符串。只要确保使用该文件中使用的正确换行符。

它看起来是这样的:String[] oresToReplace = oreToReplace.split(System.getProperty("line.separator"))

+0

问题是我需要一个字符串,而不是在块#getBlockFromName中的字符串[],所以我应该怎么做?我总是得到java.lang.NullPointerException,因为该名称不存在,所以无法在世界中生成该块。但是,当我打印出行分隔符时,它会打印出[minecraft:gold_ore,minecraft:diamond_ore] – Terrails

+0

getBlockForName我猜想会返回一个Block,并将一个Name作为参数。通过一个接一个的名字来获得你需要的所有块。 – kalsowerus

+0

我应该如何通过它?我正在考虑进入逗号并将之前的所有内容复制到getBlockFromName中,但我应该怎么做? – Terrails

相关问题