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 d3e0c44..8565521 100644 --- a/common/src/main/java/com/axolotlmaid/optionsprofiles/gui/EditProfileScreen.java +++ b/common/src/main/java/com/axolotlmaid/optionsprofiles/gui/EditProfileScreen.java @@ -1,6 +1,5 @@ package com.axolotlmaid.optionsprofiles.gui; -import com.axolotlmaid.optionsprofiles.profiles.OldProfiles; import com.axolotlmaid.optionsprofiles.profiles.Profiles; import net.minecraft.ChatFormatting; import net.minecraft.client.gui.GuiGraphics; diff --git a/common/src/main/java/com/axolotlmaid/optionsprofiles/gui/ProfilesScreen.java b/common/src/main/java/com/axolotlmaid/optionsprofiles/gui/ProfilesScreen.java index 23a0e1a..6cd1ad1 100644 --- a/common/src/main/java/com/axolotlmaid/optionsprofiles/gui/ProfilesScreen.java +++ b/common/src/main/java/com/axolotlmaid/optionsprofiles/gui/ProfilesScreen.java @@ -1,6 +1,5 @@ package com.axolotlmaid.optionsprofiles.gui; -import com.axolotlmaid.optionsprofiles.profiles.OldProfiles; import com.axolotlmaid.optionsprofiles.profiles.Profiles; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.components.Button; 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 31671e8..10314ab 100644 --- a/common/src/main/java/com/axolotlmaid/optionsprofiles/profiles/Profiles.java +++ b/common/src/main/java/com/axolotlmaid/optionsprofiles/profiles/Profiles.java @@ -3,15 +3,15 @@ package com.axolotlmaid.optionsprofiles.profiles; import com.axolotlmaid.optionsprofiles.OptionsProfilesMod; import org.apache.commons.io.FileUtils; -import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; -import java.util.Comparator; +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"); @@ -24,6 +24,7 @@ public class Profiles { String profileName = "Profile 1"; Path profile = PROFILES_DIRECTORY.resolve(profileName); + // Increases the number in 'Profile 1' if it already exists for (int i = 1; Files.exists(profile); i++) { profileName = "Profile " + i; profile = Paths.get(PROFILES_DIRECTORY.toString(), profileName); @@ -33,13 +34,13 @@ public class Profiles { Files.createDirectory(profile); if (Files.exists(profile)) { - OptionsProfilesMod.LOGGER.info("[Profile '" + profileName + "']: created"); + OptionsProfilesMod.LOGGER.info("[Profile '{}']: created", profileName); writeProfile(profileName); } else { - OptionsProfilesMod.LOGGER.warn("[Profile '" + profileName + "']: Profile already exists?"); + OptionsProfilesMod.LOGGER.warn("[Profile '{}']: Profile already exists?", profileName); } } catch (IOException e) { - OptionsProfilesMod.LOGGER.error("[Profile '" + profileName + "']: An error occurred when creating a profile", e); + OptionsProfilesMod.LOGGER.error("[Profile '{}']: An error occurred when creating a profile", profileName, e); } } @@ -50,7 +51,7 @@ public class Profiles { try { Files.copy(options, profileOptions); } catch (IOException e) { - OptionsProfilesMod.LOGGER.error("[Profile '" + profile.getFileName().toString() + "']: Unable to copy '" + options.getFileName().toString() + "'", e); + OptionsProfilesMod.LOGGER.error("[Profile '{}']: Unable to copy '{}'", profile.getFileName().toString(), options.getFileName().toString(), e); } } } @@ -59,9 +60,10 @@ public class Profiles { Path profile = PROFILES_DIRECTORY.resolve(profileName); try { + // Removes old option files FileUtils.cleanDirectory(profile.toFile()); } catch (IOException e) { - OptionsProfilesMod.LOGGER.error("[Profile '" + profileName + "']: An error occurred when clearing old options files", e); + OptionsProfilesMod.LOGGER.error("[Profile '{}']: An error occurred when clearing old options files", profileName, e); } copyOptionFile(profile, OPTIONS_FILE); @@ -70,25 +72,36 @@ public class Profiles { copyOptionFile(profile, SODIUM_EXTRA_OPTIONS_FILE); } - private static boolean compareFileContent(Path file1, Path file2) throws IOException { - return - Files.exists(file1) - && Files.exists(file2) - && Files.readAllLines(file1).equals(Files.readAllLines(file2)); - } - public static boolean isProfileLoaded(String profileName) { + boolean profileLoaded = false; + Path profile = PROFILES_DIRECTORY.resolve(profileName); + List optionFiles = new ArrayList<>(); + optionFiles.add(OPTIONS_FILE); + + // The next few lines check if the specified file exists. If so, it adds it to the optionFiles ArrayList. + Optional.of(OPTIFINE_OPTIONS_FILE).filter(Files::exists).ifPresent(optionFiles::add); + Optional.of(SODIUM_OPTIONS_FILE).filter(Files::exists).ifPresent(optionFiles::add); + Optional.of(SODIUM_EXTRA_OPTIONS_FILE).filter(Files::exists).ifPresent(optionFiles::add); + + // Check if the original option file and the profile option file have the same content try { - return compareFileContent(OPTIONS_FILE, profile.resolve(OPTIONS_FILE)) - || compareFileContent(OPTIFINE_OPTIONS_FILE, profile.resolve(OPTIFINE_OPTIONS_FILE)) - || compareFileContent(SODIUM_OPTIONS_FILE, profile.resolve(SODIUM_OPTIONS_FILE)) - || compareFileContent(SODIUM_EXTRA_OPTIONS_FILE, profile.resolve(SODIUM_EXTRA_OPTIONS_FILE)); + for (Path optionFile : optionFiles) { + Path profileOptions = profile.resolve(optionFile.getFileName()); + + if (FileUtils.contentEquals(optionFile.toFile(), profileOptions.toFile())) { + profileLoaded = true; + } else { + return false; + } + } } catch (IOException e) { - OptionsProfilesMod.LOGGER.error("[Profile '" + profileName + "']: An error occurred when checking if the profile is loaded", e); + OptionsProfilesMod.LOGGER.error("[Profile '{}']: An error occurred when checking if the profile is loaded", profileName, e); return false; } + + return profileLoaded; } private static void loadOptionFile(String profileName, Path options) { @@ -97,10 +110,11 @@ public class Profiles { if (Files.exists(profileOptions)) { try { + // Replaces the original option file with the profile option file Files.copy(profileOptions, options, StandardCopyOption.REPLACE_EXISTING); - OptionsProfilesMod.LOGGER.info("[Profile '" + profileName + "']: loaded"); + OptionsProfilesMod.LOGGER.info("[Profile '{}']: '{}' loaded", profileName, options.getFileName()); } catch (IOException e) { - OptionsProfilesMod.LOGGER.error("[Profile '" + profileName + "']: An error occurred when loading the profile", e); + OptionsProfilesMod.LOGGER.error("[Profile '{}']: An error occurred when loading the profile", profileName, e); } } } @@ -111,7 +125,7 @@ public class Profiles { if (Files.exists(profileOptions)) { loader.accept(profileOptions); - OptionsProfilesMod.LOGGER.info("[Profile '" + profileName + "']: loaded"); + OptionsProfilesMod.LOGGER.info("[Profile '{}']: '{}' loaded", profileName, options.getFileName()); } } @@ -119,7 +133,7 @@ public class Profiles { loadOptionFile(profileName, OPTIONS_FILE); loadOptionFile(profileName, OPTIFINE_OPTIONS_FILE); loadOptionFile(profileName, SODIUM_OPTIONS_FILE, SodiumConfigLoader::load); -// loadOptionFile(profileName, SODIUM_EXTRA_OPTIONS_FILE, SodiumExtraConfigLoader::load); + loadOptionFile(profileName, SODIUM_EXTRA_OPTIONS_FILE, SodiumExtraConfigLoader::load); } public static void renameProfile(String profileName, String newProfileName) { @@ -127,30 +141,26 @@ public class Profiles { Path newProfile = PROFILES_DIRECTORY.resolve(newProfileName); if (Files.exists(newProfile)) { - OptionsProfilesMod.LOGGER.warn("[Profile '" + profileName + "']: A profile with that name already exists!"); + OptionsProfilesMod.LOGGER.warn("[Profile '{}']: A profile with that name already exists!", profileName); return; } try { Files.move(profile, newProfile); - OptionsProfilesMod.LOGGER.info("[Profile '" + newProfileName + "']: renamed. Old name: " + profileName); + OptionsProfilesMod.LOGGER.info("[Profile '{}']: renamed. Old name: {}", newProfileName, profileName); } catch (IOException e) { - OptionsProfilesMod.LOGGER.error("[Profile '" + profileName + "']: An error occurred when renaming the profile", e); + OptionsProfilesMod.LOGGER.error("[Profile '{}']: An error occurred when renaming the profile", profileName, e); } } public static void deleteProfile(String profileName) { Path profile = PROFILES_DIRECTORY.resolve(profileName); - try (Stream files = Files.walk(profile)) { - files - .sorted(Comparator.reverseOrder()) - .map(Path::toFile) - .forEach(File::delete); - - OptionsProfilesMod.LOGGER.info("[Profile '" + profileName + "']: deleted"); + try { + FileUtils.deleteDirectory(profile.toFile()); + OptionsProfilesMod.LOGGER.info("[Profile '{}']: deleted", profileName); } catch (IOException e) { - OptionsProfilesMod.LOGGER.error("[Profile '" + profileName + "']: Profile was not deleted", e); + OptionsProfilesMod.LOGGER.error("[Profile '{}']: Profile was not deleted", profileName, e); } } }