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) {
OptionsProfilesMod.LOGGER.error("An error occurred when listing profiles", e);
}
checkEntriesLoaded();
}
public void checkEntriesLoaded() {
this.children().forEach(ProfileEntry::checkLoaded);
}
protected int getScrollbarPosition() {
@ -98,6 +104,7 @@ public class ProfilesList extends ContainerObjectSelectionList<ProfilesList.Prof
minecraft.options.save();
ProfilesList.this.checkEntriesLoaded();
button.active = false;
}
);
@ -131,6 +138,10 @@ public class ProfilesList extends ContainerObjectSelectionList<ProfilesList.Prof
public List<? extends NarratableEntry> narratables() {
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.SodiumExtraLoader;
import com.axolotlmaid.optionsprofiles.profiles.loaders.SodiumLoader;
import com.seibel.distanthorizons.core.config.ConfigBase;
import org.apache.commons.io.FileUtils;
import java.io.IOException;
@ -94,6 +93,7 @@ public class Profiles {
try {
Files.copy(options, profileOptions);
OptionsProfilesMod.LOGGER.info("[Profile '{}']: Copied file '{}'", profile.getFileName().toString(), options.getFileName().toString());
} catch (IOException 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 profileOptions = profile.resolve("options.txt");
ProfileConfiguration profileConfiguration = ProfileConfiguration.get(profileName);
if (overwriting) {
try {
// Removes old option files
FileUtils.cleanDirectory(profile.toFile());
try (Stream<Path> files = Files.list(profile)) {
files.filter(file -> !file.getFileName().toString().equals("configuration.json"))
.forEach(file -> {
try {
Files.delete(file);
OptionsProfilesMod.LOGGER.info("[Profile '{}']: Deleted file '{}'", profileName, file.getFileName().toString());
} catch (IOException 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 clearing old options files", profileName, e);
OptionsProfilesMod.LOGGER.error("[Profile '{}']: An error occurred when deleting old options files.", profileName, e);
}
}
@ -122,18 +127,22 @@ public class Profiles {
copyOptionFile(profile, EMBEDDIUM_OPTIONS_FILE);
copyOptionFile(profile, DISTANT_HORIZONS_OPTIONS_FILE);
// Add every option value to configuration
try (Stream<String> lines = Files.lines(profileOptions)) {
List<String> optionsToLoad = profileConfiguration.getOptionsToLoad();
if (!overwriting) {
ProfileConfiguration profileConfiguration = ProfileConfiguration.get(profileName);
lines.forEach((line) -> {
String[] option = line.split(":");
optionsToLoad.add(option[0]);
});
// Add every option value to configuration
try (Stream<String> lines = Files.lines(profileOptions)) {
List<String> optionsToLoad = profileConfiguration.getOptionsToLoad();
profileConfiguration.save();
} catch (IOException e) {
OptionsProfilesMod.LOGGER.error("[Profile '{}']: An error occurred when adding options to the configuration file", profileName, e);
lines.forEach((line) -> {
String[] option = line.split(":");
optionsToLoad.add(option[0]);
});
profileConfiguration.save();
} catch (IOException e) {
OptionsProfilesMod.LOGGER.error("[Profile '{}']: An error occurred when adding options to the configuration file", profileName, e);
}
}
}