Make Profiles.java functions static

This commit is contained in:
axolotlmaid 2024-05-12 11:25:23 +01:00
parent 358eba97b9
commit 3c43614e27

View file

@ -1,7 +1,5 @@
package com.axolotlmaid.optionsprofiles.profiles; package com.axolotlmaid.optionsprofiles.profiles;
import dev.architectury.platform.Platform;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
@ -13,7 +11,7 @@ import java.util.List;
import java.util.stream.Stream; import java.util.stream.Stream;
public class Profiles { public class Profiles {
public void createProfile() { public static void createProfile() {
String profileName = "Profile 1"; String profileName = "Profile 1";
Path profile = Paths.get("options-profiles/" + profileName); Path profile = Paths.get("options-profiles/" + profileName);
@ -38,7 +36,7 @@ public class Profiles {
} }
} }
public void writeOptionsFilesIntoProfile(String profileName) { public static void writeOptionsFilesIntoProfile(String profileName) {
Path profile = Paths.get("options-profiles/" + profileName); Path profile = Paths.get("options-profiles/" + profileName);
// options.txt // options.txt
@ -64,33 +62,33 @@ public class Profiles {
} }
// sodium-options.json // sodium-options.json
if (Platform.isFabric()) { // if (Platform.isFabric()) {
if (Platform.isModLoaded("sodium")) { // if (Platform.isModLoaded("sodium")) {
Path sodiumConfiguration = Paths.get("config/sodium-options.json"); // Path sodiumConfiguration = Paths.get("config/sodium-options.json");
Path sodiumConfigurationProfile = Paths.get(profile.toAbsolutePath() + "/sodium-options.json"); // Path sodiumConfigurationProfile = Paths.get(profile.toAbsolutePath() + "/sodium-options.json");
//
try (Stream<String> paths = Files.lines(sodiumConfiguration)) { // try (Stream<String> paths = Files.lines(sodiumConfiguration)) {
if (Files.exists(sodiumConfigurationProfile)) // if (Files.exists(sodiumConfigurationProfile))
Files.newBufferedWriter(sodiumConfigurationProfile, StandardOpenOption.TRUNCATE_EXISTING); // Files.newBufferedWriter(sodiumConfigurationProfile, StandardOpenOption.TRUNCATE_EXISTING);
//
paths.forEach(line -> { // paths.forEach(line -> {
try { // try {
Files.write(sodiumConfigurationProfile, line.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND); // Files.write(sodiumConfigurationProfile, line.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND);
Files.write(sodiumConfigurationProfile, "\n".getBytes(), StandardOpenOption.APPEND); // Files.write(sodiumConfigurationProfile, "\n".getBytes(), StandardOpenOption.APPEND);
} catch (IOException e) { // } catch (IOException e) {
System.out.println("An error occurred when writing a profile."); // System.out.println("An error occurred when writing a profile.");
e.printStackTrace(); // e.printStackTrace();
} // }
}); // });
} catch (IOException e) { // } catch (IOException e) {
System.out.println("An error occurred when reading options.txt."); // System.out.println("An error occurred when reading options.txt.");
e.printStackTrace(); // e.printStackTrace();
} // }
} // }
} // }
} }
public boolean isProfileLoaded(String profileName) { public static boolean isProfileLoaded(String profileName) {
Path profile = Paths.get("options-profiles/" + profileName); Path profile = Paths.get("options-profiles/" + profileName);
Path options = Paths.get("options.txt"); Path options = Paths.get("options.txt");
@ -100,19 +98,19 @@ public class Profiles {
List<String> linesOptions = Files.readAllLines(options); List<String> linesOptions = Files.readAllLines(options);
List<String> linesProfileOptions = Files.readAllLines(profileOptions); List<String> linesProfileOptions = Files.readAllLines(profileOptions);
if (Platform.isFabric()) { // if (Platform.isFabric()) {
if (Platform.isModLoaded("sodium")) { // if (Platform.isModLoaded("sodium")) {
Path sodiumConfiguration = Paths.get("config/sodium-options.json"); // Path sodiumConfiguration = Paths.get("config/sodium-options.json");
Path sodiumConfigurationProfile = Paths.get(profile.toAbsolutePath() + "/sodium-options.json"); // Path sodiumConfigurationProfile = Paths.get(profile.toAbsolutePath() + "/sodium-options.json");
//
if (Files.exists(sodiumConfigurationProfile)) { // if (Files.exists(sodiumConfigurationProfile)) {
List<String> linesSodiumConfig = Files.readAllLines(sodiumConfiguration); // List<String> linesSodiumConfig = Files.readAllLines(sodiumConfiguration);
List<String> linesSodiumConfigProfile = Files.readAllLines(sodiumConfigurationProfile); // List<String> linesSodiumConfigProfile = Files.readAllLines(sodiumConfigurationProfile);
//
return linesOptions.equals(linesProfileOptions) && linesSodiumConfig.equals(linesSodiumConfigProfile); // return linesOptions.equals(linesProfileOptions) && linesSodiumConfig.equals(linesSodiumConfigProfile);
} // }
} // }
} // }
return linesOptions.equals(linesProfileOptions); return linesOptions.equals(linesProfileOptions);
} catch (IOException e) { } catch (IOException e) {
@ -122,7 +120,7 @@ public class Profiles {
return false; return false;
} }
public void loadProfile(String profileName) { public static void loadProfile(String profileName) {
Path profile = Paths.get("options-profiles/" + profileName); Path profile = Paths.get("options-profiles/" + profileName);
// options.txt // options.txt
@ -147,18 +145,18 @@ public class Profiles {
} }
// sodium-options.json // sodium-options.json
if (Platform.isFabric()) { // if (Platform.isFabric()) {
if (Platform.isModLoaded("sodium")) { // if (Platform.isModLoaded("sodium")) {
Path sodiumConfigurationProfile = Paths.get(profile.toAbsolutePath() + "/sodium-options.json"); // Path sodiumConfigurationProfile = Paths.get(profile.toAbsolutePath() + "/sodium-options.json");
//
if (Files.exists(sodiumConfigurationProfile)) { // if (Files.exists(sodiumConfigurationProfile)) {
SodiumConfigLoader.load(sodiumConfigurationProfile); // SodiumConfigLoader.load(sodiumConfigurationProfile);
} // }
} // }
} // }
} }
public void renameProfile(String profileName, String newProfileName) { public static void renameProfile(String profileName, String newProfileName) {
Path profile = Paths.get("options-profiles/" + profileName); Path profile = Paths.get("options-profiles/" + profileName);
Path newProfile = Paths.get("options-profiles/" + newProfileName); Path newProfile = Paths.get("options-profiles/" + newProfileName);
@ -179,7 +177,7 @@ public class Profiles {
} }
} }
public void deleteProfile(String profileName) { public static void deleteProfile(String profileName) {
Path profile = Paths.get("options-profiles/" + profileName); Path profile = Paths.get("options-profiles/" + profileName);
try (Stream<Path> files = Files.walk(profile)) { try (Stream<Path> files = Files.walk(profile)) {