Update to proper configs, as well as add no power requirement to machines
This commit is contained in:
@@ -28,8 +28,16 @@ public final class JRConfigManager {
|
||||
// For now: keep it simple and non-fatal.
|
||||
try {
|
||||
String text = Files.readString(file, StandardCharsets.UTF_8);
|
||||
// TODO: parse JSON into config (Gson recommended)
|
||||
// config = parsed;
|
||||
JRConfig loaded = new JRConfig();
|
||||
|
||||
loaded.requirePower = readBoolean(text, "requirePower", loaded.requirePower);
|
||||
loaded.naturallySpawning = readBoolean(text, "naturallySpawning", loaded.naturallySpawning);
|
||||
loaded.fePerSecond = readPositiveInt(text, "fePerSecond", loaded.fePerSecond);
|
||||
loaded.itemsPerSecond = readPositiveInt(text, "itemsPerSecond", loaded.itemsPerSecond);
|
||||
loaded.milliBucketsPerSecond = readPositiveInt(text, "milliBucketsPerSecond", loaded.milliBucketsPerSecond);
|
||||
|
||||
config = loaded;
|
||||
save(configDir);
|
||||
} catch (IOException e) {
|
||||
// Keep defaults if load fails
|
||||
}
|
||||
@@ -40,10 +48,12 @@ public final class JRConfigManager {
|
||||
Files.createDirectories(configDir);
|
||||
Path file = configDir.resolve("jurassicrevived.json");
|
||||
|
||||
// TODO: write JSON (Gson recommended)
|
||||
String text = "{\n" +
|
||||
" \"enableDinosaurs\": " + config.enableDinosaurs + ",\n" +
|
||||
" \"spawnWeight\": " + config.spawnWeight + "\n" +
|
||||
" \"requirePower\": " + config.requirePower + ",\n" +
|
||||
" \"naturallySpawning\": " + config.naturallySpawning + ",\n" +
|
||||
" \"fePerSecond\": " + config.fePerSecond + ",\n" +
|
||||
" \"itemsPerSecond\": " + config.itemsPerSecond + ",\n" +
|
||||
" \"milliBucketsPerSecond\": " + config.milliBucketsPerSecond + "\n" +
|
||||
"}\n";
|
||||
|
||||
Files.writeString(file, text, StandardCharsets.UTF_8);
|
||||
@@ -51,4 +61,45 @@ public final class JRConfigManager {
|
||||
// ignore / log via your logger if desired
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean readBoolean(String text, String key, boolean fallback) {
|
||||
String value = readRawValue(text, key);
|
||||
if ("true".equalsIgnoreCase(value)) return true;
|
||||
if ("false".equalsIgnoreCase(value)) return false;
|
||||
return fallback;
|
||||
}
|
||||
|
||||
private static int readPositiveInt(String text, String key, int fallback) {
|
||||
String value = readRawValue(text, key);
|
||||
if (value == null) return fallback;
|
||||
|
||||
try {
|
||||
return Math.max(1, Integer.parseInt(value));
|
||||
} catch (NumberFormatException e) {
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
|
||||
private static String readRawValue(String text, String key) {
|
||||
String quotedKey = "\"" + key + "\"";
|
||||
int keyIndex = text.indexOf(quotedKey);
|
||||
if (keyIndex < 0) return null;
|
||||
|
||||
int colonIndex = text.indexOf(':', keyIndex + quotedKey.length());
|
||||
if (colonIndex < 0) return null;
|
||||
|
||||
int valueStart = colonIndex + 1;
|
||||
while (valueStart < text.length() && Character.isWhitespace(text.charAt(valueStart))) {
|
||||
valueStart++;
|
||||
}
|
||||
|
||||
int valueEnd = valueStart;
|
||||
while (valueEnd < text.length()) {
|
||||
char c = text.charAt(valueEnd);
|
||||
if (c == ',' || c == '\n' || c == '\r' || c == '}') break;
|
||||
valueEnd++;
|
||||
}
|
||||
|
||||
return text.substring(valueStart, valueEnd).trim();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user