Add profile configuration
This commit is contained in:
parent
6aa8d28d5a
commit
b6480365ad
4 changed files with 102 additions and 13 deletions
|
|
@ -1,5 +1,6 @@
|
|||
package com.axolotlmaid.optionsprofiles;
|
||||
|
||||
import com.axolotlmaid.optionsprofiles.profiles.Profiles;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
|
|
@ -22,5 +23,7 @@ public class OptionsProfilesMod {
|
|||
LOGGER.error("An error occurred when creating the 'options-profiles' directory.", e);
|
||||
}
|
||||
}
|
||||
|
||||
Profiles.updateProfiles();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ public class EditProfileScreen extends Screen {
|
|||
this.addWidget(this.profileNameEdit);
|
||||
|
||||
this.addRenderableWidget(Button.builder(Component.translatable("gui.optionsprofiles.overwrite-options"), (button) -> {
|
||||
Profiles.writeProfile(profileName.getString());
|
||||
Profiles.writeProfile(profileName.getString(), true);
|
||||
this.minecraft.setScreen(this.lastScreen);
|
||||
}).size(100, 20).pos(this.width / 2 - 50, 145).build());
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,60 @@
|
|||
package com.axolotlmaid.optionsprofiles.profiles;
|
||||
|
||||
import com.axolotlmaid.optionsprofiles.OptionsProfilesMod;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
public class ProfileConfiguration {
|
||||
private static final Path profilesDirectory = Profiles.PROFILES_DIRECTORY;
|
||||
|
||||
private boolean keybindingsOnly = false;
|
||||
|
||||
public ProfileConfiguration save(String profileName) {
|
||||
ProfileConfiguration configuration = new ProfileConfiguration();
|
||||
|
||||
Path profile = profilesDirectory.resolve(profileName);
|
||||
Path configurationFile = profile.resolve("configuration.json");
|
||||
|
||||
Gson gson = new GsonBuilder()
|
||||
.setPrettyPrinting()
|
||||
.create();
|
||||
|
||||
try (BufferedWriter writer = Files.newBufferedWriter(configurationFile)) {
|
||||
gson.toJson(this, writer);
|
||||
} catch (IOException e) {
|
||||
OptionsProfilesMod.LOGGER.error("Unable to write configuration.json to profile!", e);
|
||||
}
|
||||
|
||||
return configuration;
|
||||
}
|
||||
|
||||
public static ProfileConfiguration get(String profileName) {
|
||||
ProfileConfiguration configuration = new ProfileConfiguration();
|
||||
|
||||
Path profile = profilesDirectory.resolve(profileName);
|
||||
Path configurationFile = profile.resolve("configuration.json");
|
||||
|
||||
try (BufferedReader reader = Files.newBufferedReader(configurationFile)) {
|
||||
Gson gson = new Gson();
|
||||
configuration = gson.fromJson(reader, ProfileConfiguration.class);
|
||||
} catch (IOException e) {
|
||||
OptionsProfilesMod.LOGGER.error("[Profile '{}']: An error occurred when reading configuration.json", profileName, e);
|
||||
}
|
||||
|
||||
return configuration;
|
||||
}
|
||||
|
||||
public boolean isKeybindingsOnly() {
|
||||
return keybindingsOnly;
|
||||
}
|
||||
|
||||
public void setKeybindingsOnly(boolean keybindingsOnly) {
|
||||
this.keybindingsOnly = keybindingsOnly;
|
||||
}
|
||||
}
|
||||
|
|
@ -12,13 +12,34 @@ import java.util.ArrayList;
|
|||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class Profiles {
|
||||
private static final Path PROFILES_DIRECTORY = Paths.get("options-profiles");
|
||||
private static final Path OPTIONS_FILE = Paths.get("options.txt");
|
||||
private static final Path OPTIFINE_OPTIONS_FILE = Paths.get("optionsof.txt");
|
||||
private static final Path SODIUM_OPTIONS_FILE = Paths.get("config/sodium-options.json");
|
||||
private static final Path SODIUM_EXTRA_OPTIONS_FILE = Paths.get("config/sodium-extra-options.json");
|
||||
public static final Path PROFILES_DIRECTORY = Paths.get("options-profiles/");
|
||||
public static final Path OPTIONS_FILE = Paths.get("options.txt");
|
||||
public static final Path OPTIFINE_OPTIONS_FILE = Paths.get("optionsof.txt");
|
||||
public static final Path SODIUM_OPTIONS_FILE = Paths.get("config/sodium-options.json");
|
||||
public static final Path SODIUM_EXTRA_OPTIONS_FILE = Paths.get("config/sodium-extra-options.json");
|
||||
|
||||
// This function goes through every profile and adds a configuration file if it doesn't exist
|
||||
public static void updateProfiles() {
|
||||
try (Stream<Path> paths = Files.list(PROFILES_DIRECTORY)) {
|
||||
paths.filter(Files::isDirectory)
|
||||
.forEach(path -> {
|
||||
Path configurationFile = path.resolve("configuration.json");
|
||||
if (!Files.exists(configurationFile)) {
|
||||
String profileName = path.getFileName().toString();
|
||||
|
||||
// Create configuration.json
|
||||
new ProfileConfiguration().save(profileName);
|
||||
|
||||
OptionsProfilesMod.LOGGER.warn("[Profile '{}']: Profile updated", profileName);
|
||||
}
|
||||
});
|
||||
} catch (IOException e) {
|
||||
OptionsProfilesMod.LOGGER.error("An error occurred when updating profiles", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void createProfile() {
|
||||
String profileName = "Profile 1";
|
||||
|
|
@ -35,7 +56,7 @@ public class Profiles {
|
|||
|
||||
if (Files.exists(profile)) {
|
||||
OptionsProfilesMod.LOGGER.info("[Profile '{}']: created", profileName);
|
||||
writeProfile(profileName);
|
||||
writeProfile(profileName, false);
|
||||
} else {
|
||||
OptionsProfilesMod.LOGGER.warn("[Profile '{}']: Profile already exists?", profileName);
|
||||
}
|
||||
|
|
@ -56,14 +77,19 @@ public class Profiles {
|
|||
}
|
||||
}
|
||||
|
||||
public static void writeProfile(String profileName) {
|
||||
public static void writeProfile(String profileName, boolean overwriting) {
|
||||
Path profile = PROFILES_DIRECTORY.resolve(profileName);
|
||||
|
||||
try {
|
||||
// Removes old option files
|
||||
FileUtils.cleanDirectory(profile.toFile());
|
||||
} catch (IOException e) {
|
||||
OptionsProfilesMod.LOGGER.error("[Profile '{}']: An error occurred when clearing old options files", profileName, e);
|
||||
if (overwriting) {
|
||||
try {
|
||||
// Removes old option files
|
||||
FileUtils.cleanDirectory(profile.toFile());
|
||||
} catch (IOException e) {
|
||||
OptionsProfilesMod.LOGGER.error("[Profile '{}']: An error occurred when clearing old options files", profileName, e);
|
||||
}
|
||||
} else {
|
||||
// Create configuration.json
|
||||
new ProfileConfiguration().save(profileName);
|
||||
}
|
||||
|
||||
copyOptionFile(profile, OPTIONS_FILE);
|
||||
|
|
|
|||
Loading…
Reference in a new issue