Merge pull request #305 from babbaj/master

real gamers do it like this
This commit is contained in:
Leijurv 2019-01-07 08:45:14 -08:00
commit 58bb209158
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A

View File

@ -18,15 +18,18 @@
package baritone.api.utils; package baritone.api.utils;
import baritone.api.Settings; import baritone.api.Settings;
import net.minecraft.client.Minecraft; import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
import java.awt.*; import java.awt.*;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
@ -35,44 +38,57 @@ import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import static net.minecraft.client.Minecraft.getMinecraft;
public class SettingsUtil { public class SettingsUtil {
private static final File settingsFile = new File(new File(Minecraft.getMinecraft().gameDir, "baritone"), "settings.txt"); private static final Path settingsFile = getMinecraft().gameDir.toPath().resolve("baritone").resolve("settings.txt");
private static final Pattern SETTING_PATTERN = Pattern.compile("^(?<setting>[^ ]+) +(?<value>[^ ]+)");// 2 words separated by spaces
private static final Map<Class<?>, SettingsIO> map; private static final Map<Class<?>, SettingsIO> map;
public static void readAndApply(Settings settings) {
try (BufferedReader scan = new BufferedReader(new FileReader(settingsFile))) { private static boolean isComment(String line) {
return line.startsWith("#") || line.startsWith("//");
}
private static void forEachLine(Path file, Consumer<String> consumer) throws IOException {
try (BufferedReader scan = Files.newBufferedReader(file)) {
String line; String line;
while ((line = scan.readLine()) != null) { while ((line = scan.readLine()) != null) {
if (line.isEmpty()) { if (line.isEmpty() || isComment(line)) continue;
continue;
consumer.accept(line);
} }
if (line.startsWith("#") || line.startsWith("//")) {
continue;
} }
int space = line.indexOf(" ");
if (space == -1) {
System.out.println("Skipping invalid line with no space: " + line);
continue;
} }
String settingName = line.substring(0, space).trim().toLowerCase();
String settingValue = line.substring(space).trim(); public static void readAndApply(Settings settings) {
try {
forEachLine(settingsFile, line -> {
Matcher matcher = SETTING_PATTERN.matcher(line);
if (!matcher.matches()) {
System.out.println("Invalid syntax in setting file: " + line);
return;
}
String settingName = matcher.group("setting").toLowerCase();
String settingValue = matcher.group("value");
try { try {
parseAndApply(settings, settingName, settingValue); parseAndApply(settings, settingName, settingValue);
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace();
System.out.println("Unable to parse line " + line); System.out.println("Unable to parse line " + line);
}
}
} catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
}
});
} catch (Exception ex) {
System.out.println("Exception while reading Baritone settings, some settings may be reset to default values!"); System.out.println("Exception while reading Baritone settings, some settings may be reset to default values!");
ex.printStackTrace();
} }
} }
public static synchronized void save(Settings settings) { public static synchronized void save(Settings settings) {
try (FileOutputStream out = new FileOutputStream(settingsFile)) { try (BufferedWriter out = Files.newBufferedWriter(settingsFile)) {
for (Settings.Setting setting : settings.allSettings) { for (Settings.Setting setting : settings.allSettings) {
if (setting.get() == null) { if (setting.get() == null) {
System.out.println("NULL SETTING?" + setting.getName()); System.out.println("NULL SETTING?" + setting.getName());
@ -88,11 +104,11 @@ public class SettingsUtil {
if (io == null) { if (io == null) {
throw new IllegalStateException("Missing " + setting.getValueClass() + " " + setting + " " + setting.getName()); throw new IllegalStateException("Missing " + setting.getValueClass() + " " + setting + " " + setting.getName());
} }
out.write((setting.getName() + " " + io.toString.apply(setting.get()) + "\n").getBytes()); out.write(setting.getName() + " " + io.toString.apply(setting.get()) + "\n");
} }
} catch (Exception ex) { } catch (Exception ex) {
System.out.println("Exception thrown while saving Baritone settings!");
ex.printStackTrace(); ex.printStackTrace();
System.out.println("Exception while saving Baritone settings!");
} }
} }