我正在做一個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);
}
}
}
}
在其包配置類? – alpert