2017-04-20 182 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

相關問題