command to load settings from a specific file

This commit is contained in:
Leijurv 2023-03-01 00:25:30 -08:00
parent c9d7a5bea6
commit 790a4f6769
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A
3 changed files with 30 additions and 12 deletions

View File

@ -35,7 +35,7 @@ public final class BaritoneAPI {
static {
settings = new Settings();
SettingsUtil.readAndApply(settings);
SettingsUtil.readAndApply(settings, SettingsUtil.SETTINGS_DEFAULT_NAME);
ServiceLoader<IBaritoneProvider> baritoneLoader = ServiceLoader.load(IBaritoneProvider.class);
Iterator<IBaritoneProvider> instances = baritoneLoader.iterator();

View File

@ -48,7 +48,7 @@ import static net.minecraft.client.Minecraft.getMinecraft;
public class SettingsUtil {
private static final Path SETTINGS_PATH = getMinecraft().gameDir.toPath().resolve("baritone").resolve("settings.txt");
public static final String SETTINGS_DEFAULT_NAME = "settings.txt";
private static final Pattern SETTING_PATTERN = Pattern.compile("^(?<setting>[^ ]+) +(?<value>.+)"); // key and value split by the first space
private static final String[] JAVA_ONLY_SETTINGS = {"logger", "notifier", "toaster"};
@ -68,12 +68,12 @@ public class SettingsUtil {
}
}
public static void readAndApply(Settings settings) {
public static void readAndApply(Settings settings, String settingsName) {
try {
forEachLine(SETTINGS_PATH, line -> {
forEachLine(settingsByName(settingsName), line -> {
Matcher matcher = SETTING_PATTERN.matcher(line);
if (!matcher.matches()) {
System.out.println("Invalid syntax in setting file: " + line);
Helper.HELPER.logDirect("Invalid syntax in setting file: " + line);
return;
}
@ -82,29 +82,33 @@ public class SettingsUtil {
try {
parseAndApply(settings, settingName, settingValue);
} catch (Exception ex) {
System.out.println("Unable to parse line " + line);
Helper.HELPER.logDirect("Unable to parse line " + line);
ex.printStackTrace();
}
});
} catch (NoSuchFileException ignored) {
System.out.println("Baritone settings file not found, resetting.");
Helper.HELPER.logDirect("Baritone settings file not found, resetting.");
} catch (Exception ex) {
System.out.println("Exception while reading Baritone settings, some settings may be reset to default values!");
Helper.HELPER.logDirect("Exception while reading Baritone settings, some settings may be reset to default values!");
ex.printStackTrace();
}
}
public static synchronized void save(Settings settings) {
try (BufferedWriter out = Files.newBufferedWriter(SETTINGS_PATH)) {
try (BufferedWriter out = Files.newBufferedWriter(settingsByName(SETTINGS_DEFAULT_NAME))) {
for (Settings.Setting setting : modifiedSettings(settings)) {
out.write(settingToString(setting) + "\n");
}
} catch (Exception ex) {
System.out.println("Exception thrown while saving Baritone settings!");
Helper.HELPER.logDirect("Exception thrown while saving Baritone settings!");
ex.printStackTrace();
}
}
private static Path settingsByName(String name) {
return getMinecraft().gameDir.toPath().resolve("baritone").resolve(name);
}
public static List<Settings.Setting> modifiedSettings(Settings settings) {
List<Settings.Setting> modified = new ArrayList<>();
for (Settings.Setting setting : settings.allSettings) {

View File

@ -57,6 +57,18 @@ public class SetCommand extends Command {
logDirect("Settings saved");
return;
}
if (Arrays.asList("load", "ld").contains(arg)) {
String file = SETTINGS_DEFAULT_NAME;
if (args.hasAny()) {
file = args.getString();
}
// reset to defaults
SettingsUtil.modifiedSettings(Baritone.settings()).forEach(Settings.Setting::reset);
// then load from disk
SettingsUtil.readAndApply(Baritone.settings(), file);
logDirect("Settings reloaded from " + file);
return;
}
boolean viewModified = Arrays.asList("m", "mod", "modified").contains(arg);
boolean viewAll = Arrays.asList("all", "l", "list").contains(arg);
boolean paginate = viewModified || viewAll;
@ -228,7 +240,7 @@ public class SetCommand extends Command {
return new TabCompleteHelper()
.addSettings()
.sortAlphabetically()
.prepend("list", "modified", "reset", "toggle", "save")
.prepend("list", "modified", "reset", "toggle", "save", "load")
.filterPrefix(arg)
.stream();
}
@ -255,7 +267,9 @@ public class SetCommand extends Command {
"> set reset all - Reset ALL SETTINGS to their defaults",
"> set reset <setting> - Reset a setting to its default",
"> set toggle <setting> - Toggle a boolean setting",
"> set save - Save all settings (this is automatic tho)"
"> set save - Save all settings (this is automatic tho)",
"> set load - Load settings from settings.txt",
"> set load [filename] - Load settings from another file in your minecraft/baritone"
);
}
}