将数组的每一行拆分为String

时间:2017-04-20 12:27:49

标签: java arrays string minecraft-forge

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

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

所以,如果我只是输入minecraft: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);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以简单地在每个换行符上拆分String。只需确保使用该文件中使用的正确换行符。

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