Fix profiles not being checked if they are loaded every time

This commit is contained in:
axolotlmaid 2024-07-16 21:12:24 +01:00
parent 746ef7ad3c
commit a8fa28e834
2 changed files with 37 additions and 17 deletions

View file

@ -51,6 +51,12 @@ public class ProfilesList extends ContainerObjectSelectionList<ProfilesList.Prof
} catch (Exception e) { } catch (Exception e) {
OptionsProfilesMod.LOGGER.error("An error occurred when listing profiles", e); OptionsProfilesMod.LOGGER.error("An error occurred when listing profiles", e);
} }
checkEntriesLoaded();
}
public void checkEntriesLoaded() {
this.children().forEach(ProfileEntry::checkLoaded);
} }
protected int getScrollbarPosition() { protected int getScrollbarPosition() {
@ -98,6 +104,7 @@ public class ProfilesList extends ContainerObjectSelectionList<ProfilesList.Prof
minecraft.options.save(); minecraft.options.save();
ProfilesList.this.checkEntriesLoaded();
button.active = false; button.active = false;
} }
); );
@ -131,6 +138,10 @@ public class ProfilesList extends ContainerObjectSelectionList<ProfilesList.Prof
public List<? extends NarratableEntry> narratables() { public List<? extends NarratableEntry> narratables() {
return ImmutableList.of(this.editButton, this.loadButton); return ImmutableList.of(this.editButton, this.loadButton);
} }
protected void checkLoaded() {
this.loadButton.active = !Profiles.isProfileLoaded(profileName.getString());
}
} }
} }

View file

@ -5,7 +5,6 @@ import com.axolotlmaid.optionsprofiles.profiles.loaders.DistantHorizonsLoader;
import com.axolotlmaid.optionsprofiles.profiles.loaders.EmbeddiumLoader; import com.axolotlmaid.optionsprofiles.profiles.loaders.EmbeddiumLoader;
import com.axolotlmaid.optionsprofiles.profiles.loaders.SodiumExtraLoader; import com.axolotlmaid.optionsprofiles.profiles.loaders.SodiumExtraLoader;
import com.axolotlmaid.optionsprofiles.profiles.loaders.SodiumLoader; import com.axolotlmaid.optionsprofiles.profiles.loaders.SodiumLoader;
import com.seibel.distanthorizons.core.config.ConfigBase;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import java.io.IOException; import java.io.IOException;
@ -94,6 +93,7 @@ public class Profiles {
try { try {
Files.copy(options, profileOptions); Files.copy(options, profileOptions);
OptionsProfilesMod.LOGGER.info("[Profile '{}']: Copied file '{}'", profile.getFileName().toString(), options.getFileName().toString());
} catch (IOException e) { } catch (IOException e) {
OptionsProfilesMod.LOGGER.error("[Profile '{}']: Unable to copy '{}'", profile.getFileName().toString(), options.getFileName().toString(), e); OptionsProfilesMod.LOGGER.error("[Profile '{}']: Unable to copy '{}'", profile.getFileName().toString(), options.getFileName().toString(), e);
} }
@ -104,14 +104,19 @@ public class Profiles {
Path profile = PROFILES_DIRECTORY.resolve(profileName); Path profile = PROFILES_DIRECTORY.resolve(profileName);
Path profileOptions = profile.resolve("options.txt"); Path profileOptions = profile.resolve("options.txt");
ProfileConfiguration profileConfiguration = ProfileConfiguration.get(profileName);
if (overwriting) { if (overwriting) {
try (Stream<Path> files = Files.list(profile)) {
files.filter(file -> !file.getFileName().toString().equals("configuration.json"))
.forEach(file -> {
try { try {
// Removes old option files Files.delete(file);
FileUtils.cleanDirectory(profile.toFile()); OptionsProfilesMod.LOGGER.info("[Profile '{}']: Deleted file '{}'", profileName, file.getFileName().toString());
} 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 trying to delete the file '{}'", profileName, file.getFileName().toString(), e);
}
});
} catch (IOException e) {
OptionsProfilesMod.LOGGER.error("[Profile '{}']: An error occurred when deleting old options files.", profileName, e);
} }
} }
@ -122,6 +127,9 @@ public class Profiles {
copyOptionFile(profile, EMBEDDIUM_OPTIONS_FILE); copyOptionFile(profile, EMBEDDIUM_OPTIONS_FILE);
copyOptionFile(profile, DISTANT_HORIZONS_OPTIONS_FILE); copyOptionFile(profile, DISTANT_HORIZONS_OPTIONS_FILE);
if (!overwriting) {
ProfileConfiguration profileConfiguration = ProfileConfiguration.get(profileName);
// Add every option value to configuration // Add every option value to configuration
try (Stream<String> lines = Files.lines(profileOptions)) { try (Stream<String> lines = Files.lines(profileOptions)) {
List<String> optionsToLoad = profileConfiguration.getOptionsToLoad(); List<String> optionsToLoad = profileConfiguration.getOptionsToLoad();
@ -136,6 +144,7 @@ public class Profiles {
OptionsProfilesMod.LOGGER.error("[Profile '{}']: An error occurred when adding options to the configuration file", profileName, e); OptionsProfilesMod.LOGGER.error("[Profile '{}']: An error occurred when adding options to the configuration file", profileName, e);
} }
} }
}
public static boolean isProfileLoaded(String profileName) { public static boolean isProfileLoaded(String profileName) {
Path profile = PROFILES_DIRECTORY.resolve(profileName); Path profile = PROFILES_DIRECTORY.resolve(profileName);