Add profile configuration

This commit is contained in:
axolotlmaid 2024-06-29 14:38:18 +01:00
parent 6aa8d28d5a
commit b6480365ad
4 changed files with 102 additions and 13 deletions

View file

@ -1,5 +1,6 @@
package com.axolotlmaid.optionsprofiles; package com.axolotlmaid.optionsprofiles;
import com.axolotlmaid.optionsprofiles.profiles.Profiles;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; 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); LOGGER.error("An error occurred when creating the 'options-profiles' directory.", e);
} }
} }
Profiles.updateProfiles();
} }
} }

View file

@ -27,7 +27,7 @@ public class EditProfileScreen extends Screen {
this.addWidget(this.profileNameEdit); this.addWidget(this.profileNameEdit);
this.addRenderableWidget(Button.builder(Component.translatable("gui.optionsprofiles.overwrite-options"), (button) -> { 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); this.minecraft.setScreen(this.lastScreen);
}).size(100, 20).pos(this.width / 2 - 50, 145).build()); }).size(100, 20).pos(this.width / 2 - 50, 145).build());

View file

@ -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;
}
}

View file

@ -12,13 +12,34 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.stream.Stream;
public class Profiles { public class Profiles {
private static final Path PROFILES_DIRECTORY = Paths.get("options-profiles"); public static final Path PROFILES_DIRECTORY = Paths.get("options-profiles/");
private static final Path OPTIONS_FILE = Paths.get("options.txt"); public static final Path OPTIONS_FILE = Paths.get("options.txt");
private static final Path OPTIFINE_OPTIONS_FILE = Paths.get("optionsof.txt"); public static final Path OPTIFINE_OPTIONS_FILE = Paths.get("optionsof.txt");
private static final Path SODIUM_OPTIONS_FILE = Paths.get("config/sodium-options.json"); public 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 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() { public static void createProfile() {
String profileName = "Profile 1"; String profileName = "Profile 1";
@ -35,7 +56,7 @@ public class Profiles {
if (Files.exists(profile)) { if (Files.exists(profile)) {
OptionsProfilesMod.LOGGER.info("[Profile '{}']: created", profileName); OptionsProfilesMod.LOGGER.info("[Profile '{}']: created", profileName);
writeProfile(profileName); writeProfile(profileName, false);
} else { } else {
OptionsProfilesMod.LOGGER.warn("[Profile '{}']: Profile already exists?", profileName); OptionsProfilesMod.LOGGER.warn("[Profile '{}']: Profile already exists?", profileName);
} }
@ -56,15 +77,20 @@ public class Profiles {
} }
} }
public static void writeProfile(String profileName) { public static void writeProfile(String profileName, boolean overwriting) {
Path profile = PROFILES_DIRECTORY.resolve(profileName); Path profile = PROFILES_DIRECTORY.resolve(profileName);
if (overwriting) {
try { try {
// Removes old option files // Removes old option files
FileUtils.cleanDirectory(profile.toFile()); FileUtils.cleanDirectory(profile.toFile());
} catch (IOException e) { } catch (IOException e) {
OptionsProfilesMod.LOGGER.error("[Profile '{}']: An error occurred when clearing old options files", profileName, 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); copyOptionFile(profile, OPTIONS_FILE);
copyOptionFile(profile, OPTIFINE_OPTIONS_FILE); copyOptionFile(profile, OPTIFINE_OPTIONS_FILE);