From a9eaaae9e90372b6cb89dbe6506b9f169ac02e63 Mon Sep 17 00:00:00 2001 From: axolotlmaid <124442837+axolotlmaid@users.noreply.github.com> Date: Tue, 21 May 2024 21:31:13 +0100 Subject: [PATCH] Add version 1.2 features --- .../gui/EditProfileScreen.java | 8 +- .../optionsprofiles/profiles/Profiles.java | 153 +++++++++--------- 2 files changed, 83 insertions(+), 78 deletions(-) diff --git a/common/src/main/java/com/axolotlmaid/optionsprofiles/gui/EditProfileScreen.java b/common/src/main/java/com/axolotlmaid/optionsprofiles/gui/EditProfileScreen.java index 92c1f67..c89a04e 100644 --- a/common/src/main/java/com/axolotlmaid/optionsprofiles/gui/EditProfileScreen.java +++ b/common/src/main/java/com/axolotlmaid/optionsprofiles/gui/EditProfileScreen.java @@ -22,19 +22,19 @@ public class EditProfileScreen extends Screen { } protected void init() { - this.profileNameEdit = new EditBox(this.font, this.width / 2 - 102, this.height - 130, 204, 20, Component.empty()); + this.profileNameEdit = new EditBox(this.font, this.width / 2 - 102, 116, 204, 20, Component.empty()); this.profileNameEdit.setValue(profileName.getString()); this.addWidget(this.profileNameEdit); this.addRenderableWidget(Button.builder(Component.translatable("gui.optionsprofiles.overwrite-options"), (button) -> { new Profiles().writeOptionsFilesIntoProfile(profileName.getString()); this.minecraft.setScreen(this.lastScreen); - }).size(100, 20).pos(this.width / 2 - 50, this.height - 85).build()); + }).size(100, 20).pos(this.width / 2 - 50, 145).build()); this.addRenderableWidget(Button.builder(Component.translatable("gui.optionsprofiles.rename-profile"), (button) -> { new Profiles().renameProfile(profileName.getString(), this.profileNameEdit.getValue()); this.minecraft.setScreen(new EditProfileScreen(lastScreen, Component.literal(this.profileNameEdit.getValue()))); - }).size(100, 20).pos(this.width / 2 - 50, this.height - 65).build()); + }).size(100, 20).pos(this.width / 2 - 50, 166).build()); this.addRenderableWidget(Button.builder(Component.translatable("gui.optionsprofiles.delete-profile").withStyle(ChatFormatting.RED), (button) -> { new Profiles().deleteProfile(profileName.getString()); @@ -50,6 +50,6 @@ public class EditProfileScreen extends Screen { super.render(guiGraphics, mouseX, mouseY, delta); this.profileNameEdit.render(guiGraphics, mouseX, mouseY, delta); guiGraphics.drawCenteredString(this.font, this.title, this.width / 2, 8, 16777215); - guiGraphics.drawCenteredString(this.font, Component.translatable("gui.optionsprofiles.profile-name-text"), this.width / 2, this.height - 145, 16777215); + guiGraphics.drawCenteredString(this.font, Component.translatable("gui.optionsprofiles.profile-name-text"), this.width / 2, 100, 16777215); } } \ No newline at end of file diff --git a/common/src/main/java/com/axolotlmaid/optionsprofiles/profiles/Profiles.java b/common/src/main/java/com/axolotlmaid/optionsprofiles/profiles/Profiles.java index 9c438a1..7eeeedb 100644 --- a/common/src/main/java/com/axolotlmaid/optionsprofiles/profiles/Profiles.java +++ b/common/src/main/java/com/axolotlmaid/optionsprofiles/profiles/Profiles.java @@ -1,7 +1,5 @@ package com.axolotlmaid.optionsprofiles.profiles; -import dev.architectury.platform.Platform; - import java.io.File; import java.io.IOException; import java.nio.file.Files; @@ -13,7 +11,7 @@ import java.util.List; import java.util.stream.Stream; public class Profiles { - public void createProfile() { + public static void createProfile() { String profileName = "Profile 1"; Path profile = Paths.get("options-profiles/" + profileName); @@ -26,24 +24,29 @@ public class Profiles { Files.createDirectory(profile); if (Files.exists(profile)) { - System.out.println("Profile created."); + System.out.println("[Options Profiles] Profile created."); writeOptionsFilesIntoProfile(profileName); } else { - System.out.println("Profile was not created successfully."); + System.out.println("[Options Profiles] Profile was not created successfully."); } } catch (IOException e) { - System.out.println("An error occurred when creating a profile."); + System.out.println("[Options Profiles] An error occurred when creating a profile."); e.printStackTrace(); } } - public void writeOptionsFilesIntoProfile(String profileName) { + private static void writeOptionFile(String profileName, String optionsFile, boolean isSodium) { Path profile = Paths.get("options-profiles/" + profileName); - // options.txt - Path options = Paths.get("options.txt"); - Path profileOptions = Paths.get(profile.toAbsolutePath() + "/options.txt"); + Path options; + Path profileOptions = Paths.get(profile.toAbsolutePath() + "/" + optionsFile); + + if (isSodium) { + options = Paths.get("config/" + optionsFile); + } else { + options = Paths.get(optionsFile); + } try (Stream paths = Files.lines(options)) { if (Files.exists(profileOptions)) @@ -54,43 +57,32 @@ public class Profiles { Files.write(profileOptions, line.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND); Files.write(profileOptions, "\n".getBytes(), StandardOpenOption.APPEND); } catch (IOException e) { - System.out.println("An error occurred when writing a profile."); + System.out.println("[Options Profiles] An error occurred when writing a profile."); e.printStackTrace(); } }); } catch (IOException e) { - System.out.println("An error occurred when reading options.txt."); + System.out.println("[Options Profiles] An error occurred when reading an options file."); e.printStackTrace(); } - - // sodium-options.json - if (Platform.isFabric()) { - if (Platform.isModLoaded("sodium")) { - Path sodiumConfiguration = Paths.get("config/sodium-options.json"); - Path sodiumConfigurationProfile = Paths.get(profile.toAbsolutePath() + "/sodium-options.json"); - - try (Stream paths = Files.lines(sodiumConfiguration)) { - if (Files.exists(sodiumConfigurationProfile)) - Files.newBufferedWriter(sodiumConfigurationProfile, StandardOpenOption.TRUNCATE_EXISTING); - - paths.forEach(line -> { - try { - Files.write(sodiumConfigurationProfile, line.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND); - Files.write(sodiumConfigurationProfile, "\n".getBytes(), StandardOpenOption.APPEND); - } catch (IOException e) { - System.out.println("An error occurred when writing a profile."); - e.printStackTrace(); - } - }); - } catch (IOException e) { - System.out.println("An error occurred when reading options.txt."); - e.printStackTrace(); - } - } - } } - public boolean isProfileLoaded(String profileName) { + public static void writeOptionsFilesIntoProfile(String profileName) { + // options.txt + writeOptionFile(profileName, "options.txt", false); + + // sodium-options.json + if (Files.exists(Paths.get("config/sodium-options.json"))) { + writeOptionFile(profileName, "sodium-options.json", true); + } + + // optionsof.txt + if (Files.exists(Paths.get("optionsof.txt"))) { + writeOptionFile(profileName, "optionsof.txt", false); + } + } + + public static boolean isProfileLoaded(String profileName) { Path profile = Paths.get("options-profiles/" + profileName); Path options = Paths.get("options.txt"); @@ -100,18 +92,26 @@ public class Profiles { List linesOptions = Files.readAllLines(options); List linesProfileOptions = Files.readAllLines(profileOptions); - if (Platform.isFabric()) { - if (Platform.isModLoaded("sodium")) { - Path sodiumConfiguration = Paths.get("config/sodium-options.json"); - Path sodiumConfigurationProfile = Paths.get(profile.toAbsolutePath() + "/sodium-options.json"); + // sodium-options.json + Path sodiumConfiguration = Paths.get("config/sodium-options.json"); + Path sodiumConfigurationProfile = Paths.get(profile.toAbsolutePath() + "/sodium-options.json"); - if (Files.exists(sodiumConfigurationProfile)) { - List linesSodiumConfig = Files.readAllLines(sodiumConfiguration); - List linesSodiumConfigProfile = Files.readAllLines(sodiumConfigurationProfile); + if (Files.exists(sodiumConfigurationProfile)) { + List linesSodiumConfig = Files.readAllLines(sodiumConfiguration); + List linesSodiumConfigProfile = Files.readAllLines(sodiumConfigurationProfile); - return linesOptions.equals(linesProfileOptions) && linesSodiumConfig.equals(linesSodiumConfigProfile); - } - } + return linesOptions.equals(linesProfileOptions) && linesSodiumConfig.equals(linesSodiumConfigProfile); + } + + // optionsof.txt + Path optifineConfiguration = Paths.get("optionsof.txt"); + Path optifineConfigurationProfile = Paths.get(profile.toAbsolutePath() + "/optionsof.txt"); + + if (Files.exists(optifineConfigurationProfile)) { + List linesOptifineConfig = Files.readAllLines(optifineConfiguration); + List linesOptifineConfigProfile = Files.readAllLines(optifineConfigurationProfile); + + return linesOptions.equals(linesProfileOptions) && linesOptifineConfig.equals(linesOptifineConfigProfile); } return linesOptions.equals(linesProfileOptions); @@ -122,12 +122,11 @@ public class Profiles { return false; } - public void loadProfile(String profileName) { + private static void loadOptionFile(String profileName, String optionsFile) { Path profile = Paths.get("options-profiles/" + profileName); - // options.txt - Path options = Paths.get("options.txt"); - Path profileOptions = Paths.get(profile.toAbsolutePath() + "/options.txt"); + Path options = Paths.get(optionsFile); + Path profileOptions = Paths.get(profile.toAbsolutePath() + "/" + optionsFile); try (Stream paths = Files.lines(profileOptions)) { Files.newBufferedWriter(options, StandardOpenOption.TRUNCATE_EXISTING); @@ -137,49 +136,55 @@ public class Profiles { Files.write(options, line.getBytes(), StandardOpenOption.APPEND); Files.write(options, "\n".getBytes(), StandardOpenOption.APPEND); } catch (IOException e) { - System.out.println("An error occurred when loading a profile."); + System.out.println("[Options Profiles] An error occurred when loading a profile."); e.printStackTrace(); } }); } catch (IOException e) { - System.out.println("An error occurred when loading a profile."); + System.out.println("[Options Profiles] An error occurred when loading a profile."); e.printStackTrace(); } - - // sodium-options.json - if (Platform.isFabric()) { - if (Platform.isModLoaded("sodium")) { - Path sodiumConfigurationProfile = Paths.get(profile.toAbsolutePath() + "/sodium-options.json"); - - if (Files.exists(sodiumConfigurationProfile)) { - SodiumConfigLoader.load(sodiumConfigurationProfile); - } - } - } } - public void renameProfile(String profileName, String newProfileName) { + public static void loadProfile(String profileName) { + Path profile = Paths.get("options-profiles/" + profileName); + + // options.txt + loadOptionFile(profileName, "options.txt"); + + // sodium-options.json + Path sodiumConfigurationProfile = Paths.get(profile.toAbsolutePath() + "/sodium-options.json"); + + if (Files.exists(Paths.get(profile.toAbsolutePath() + "/sodium-options.json"))) + SodiumConfigLoader.load(sodiumConfigurationProfile); + + // optionsof.txt + if (Files.exists(Paths.get(profile.toAbsolutePath() + "/optionsof.txt"))) + loadOptionFile(profileName, "optionsof.txt"); + } + + public static void renameProfile(String profileName, String newProfileName) { Path profile = Paths.get("options-profiles/" + profileName); Path newProfile = Paths.get("options-profiles/" + newProfileName); if (Files.exists(newProfile)) - System.out.println("New profile already exists!"); + System.out.println("[Options Profiles] New profile already exists!"); try { Files.move(profile, newProfile); if (Files.exists(newProfile)) { - System.out.println("Profile renamed."); + System.out.println("[Options Profiles] Profile renamed."); } else { - System.out.println("Profile was not renamed successfully."); + System.out.println("[Options Profiles] Profile was not renamed successfully."); } } catch (IOException e) { - System.out.println("Profile was not renamed successfully."); + System.out.println("[Options Profiles] Profile was not renamed successfully."); e.printStackTrace(); } } - public void deleteProfile(String profileName) { + public static void deleteProfile(String profileName) { Path profile = Paths.get("options-profiles/" + profileName); try (Stream files = Files.walk(profile)) { @@ -188,10 +193,10 @@ public class Profiles { .map(Path::toFile) .forEach(File::delete); } catch (IOException e) { - System.out.println("Profile was not deleted."); + System.out.println("[Options Profiles] Profile was not deleted."); e.printStackTrace(); } - System.out.println("Profile deleted."); + System.out.println("[Options Profiles] Profile deleted."); } } \ No newline at end of file