将数组的每一行拆分为一个字符串

Splitting each line of an array into a String

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

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

所以如果我只是输入 minecraft:gold_ore 并删除 minecraft:diamond_ore 世界加载正常并且生成工作,这是配置 class:

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

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

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