mirror of https://github.com/cabaletta/baritone
command to load settings from a specific file
This commit is contained in:
parent
c9d7a5bea6
commit
790a4f6769
|
@ -35,7 +35,7 @@ public final class BaritoneAPI {
|
||||||
|
|
||||||
static {
|
static {
|
||||||
settings = new Settings();
|
settings = new Settings();
|
||||||
SettingsUtil.readAndApply(settings);
|
SettingsUtil.readAndApply(settings, SettingsUtil.SETTINGS_DEFAULT_NAME);
|
||||||
|
|
||||||
ServiceLoader<IBaritoneProvider> baritoneLoader = ServiceLoader.load(IBaritoneProvider.class);
|
ServiceLoader<IBaritoneProvider> baritoneLoader = ServiceLoader.load(IBaritoneProvider.class);
|
||||||
Iterator<IBaritoneProvider> instances = baritoneLoader.iterator();
|
Iterator<IBaritoneProvider> instances = baritoneLoader.iterator();
|
||||||
|
|
|
@ -48,7 +48,7 @@ import static net.minecraft.client.Minecraft.getMinecraft;
|
||||||
|
|
||||||
public class SettingsUtil {
|
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 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"};
|
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 {
|
try {
|
||||||
forEachLine(SETTINGS_PATH, line -> {
|
forEachLine(settingsByName(settingsName), line -> {
|
||||||
Matcher matcher = SETTING_PATTERN.matcher(line);
|
Matcher matcher = SETTING_PATTERN.matcher(line);
|
||||||
if (!matcher.matches()) {
|
if (!matcher.matches()) {
|
||||||
System.out.println("Invalid syntax in setting file: " + line);
|
Helper.HELPER.logDirect("Invalid syntax in setting file: " + line);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,29 +82,33 @@ public class SettingsUtil {
|
||||||
try {
|
try {
|
||||||
parseAndApply(settings, settingName, settingValue);
|
parseAndApply(settings, settingName, settingValue);
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
System.out.println("Unable to parse line " + line);
|
Helper.HELPER.logDirect("Unable to parse line " + line);
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (NoSuchFileException ignored) {
|
} catch (NoSuchFileException ignored) {
|
||||||
System.out.println("Baritone settings file not found, resetting.");
|
Helper.HELPER.logDirect("Baritone settings file not found, resetting.");
|
||||||
} catch (Exception ex) {
|
} 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();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static synchronized void save(Settings settings) {
|
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)) {
|
for (Settings.Setting setting : modifiedSettings(settings)) {
|
||||||
out.write(settingToString(setting) + "\n");
|
out.write(settingToString(setting) + "\n");
|
||||||
}
|
}
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
System.out.println("Exception thrown while saving Baritone settings!");
|
Helper.HELPER.logDirect("Exception thrown while saving Baritone settings!");
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Path settingsByName(String name) {
|
||||||
|
return getMinecraft().gameDir.toPath().resolve("baritone").resolve(name);
|
||||||
|
}
|
||||||
|
|
||||||
public static List<Settings.Setting> modifiedSettings(Settings settings) {
|
public static List<Settings.Setting> modifiedSettings(Settings settings) {
|
||||||
List<Settings.Setting> modified = new ArrayList<>();
|
List<Settings.Setting> modified = new ArrayList<>();
|
||||||
for (Settings.Setting setting : settings.allSettings) {
|
for (Settings.Setting setting : settings.allSettings) {
|
||||||
|
|
|
@ -57,6 +57,18 @@ public class SetCommand extends Command {
|
||||||
logDirect("Settings saved");
|
logDirect("Settings saved");
|
||||||
return;
|
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 viewModified = Arrays.asList("m", "mod", "modified").contains(arg);
|
||||||
boolean viewAll = Arrays.asList("all", "l", "list").contains(arg);
|
boolean viewAll = Arrays.asList("all", "l", "list").contains(arg);
|
||||||
boolean paginate = viewModified || viewAll;
|
boolean paginate = viewModified || viewAll;
|
||||||
|
@ -228,7 +240,7 @@ public class SetCommand extends Command {
|
||||||
return new TabCompleteHelper()
|
return new TabCompleteHelper()
|
||||||
.addSettings()
|
.addSettings()
|
||||||
.sortAlphabetically()
|
.sortAlphabetically()
|
||||||
.prepend("list", "modified", "reset", "toggle", "save")
|
.prepend("list", "modified", "reset", "toggle", "save", "load")
|
||||||
.filterPrefix(arg)
|
.filterPrefix(arg)
|
||||||
.stream();
|
.stream();
|
||||||
}
|
}
|
||||||
|
@ -255,7 +267,9 @@ public class SetCommand extends Command {
|
||||||
"> set reset all - Reset ALL SETTINGS to their defaults",
|
"> set reset all - Reset ALL SETTINGS to their defaults",
|
||||||
"> set reset <setting> - Reset a setting to its default",
|
"> set reset <setting> - Reset a setting to its default",
|
||||||
"> set toggle <setting> - Toggle a boolean setting",
|
"> 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"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue