Add profile loading on startup (#17)
This commit is contained in:
parent
5ef62d174c
commit
81a4370297
3 changed files with 43 additions and 14 deletions
|
|
@ -1,13 +1,20 @@
|
|||
package com.axolotlmaid.optionsprofiles;
|
||||
|
||||
import com.axolotlmaid.optionsprofiles.gui.ProfilesList;
|
||||
import com.axolotlmaid.optionsprofiles.profiles.ProfileConfiguration;
|
||||
import com.axolotlmaid.optionsprofiles.profiles.Profiles;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.DirectoryStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
public class OptionsProfilesMod {
|
||||
public static final String MOD_ID = "optionsprofiles";
|
||||
|
|
@ -24,7 +31,21 @@ public class OptionsProfilesMod {
|
|||
}
|
||||
}
|
||||
|
||||
// Update / add configuration for existing profiles
|
||||
// Add configuration for existing profiles that were made before v1.3
|
||||
Profiles.updateProfiles();
|
||||
|
||||
// Loading profiles on startup
|
||||
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(Profiles.PROFILES_DIRECTORY)) {
|
||||
for (Path profile : directoryStream) {
|
||||
String profileName = profile.getFileName().toString();
|
||||
ProfileConfiguration profileConfiguration = ProfileConfiguration.get(profileName);
|
||||
|
||||
if (profileConfiguration.isLoadOnStartup()) {
|
||||
Profiles.loadProfile(profileName);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
OptionsProfilesMod.LOGGER.error("An error occurred when loading startup profiles", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,9 @@
|
|||
package com.axolotlmaid.optionsprofiles.gui;
|
||||
|
||||
import com.axolotlmaid.optionsprofiles.profiles.ProfileConfiguration;
|
||||
import com.axolotlmaid.optionsprofiles.profiles.Profiles;
|
||||
import net.minecraft.ChatFormatting;
|
||||
import net.minecraft.client.gui.components.Button;
|
||||
import net.minecraft.client.gui.components.EditBox;
|
||||
import net.minecraft.client.gui.components.StringWidget;
|
||||
import net.minecraft.client.gui.components.Tooltip;
|
||||
import net.minecraft.client.gui.components.*;
|
||||
import net.minecraft.client.gui.layouts.HeaderAndFooterLayout;
|
||||
import net.minecraft.client.gui.layouts.LayoutSettings;
|
||||
import net.minecraft.client.gui.layouts.LinearLayout;
|
||||
|
|
@ -13,10 +11,13 @@ import net.minecraft.client.gui.screens.Screen;
|
|||
import net.minecraft.network.chat.CommonComponents;
|
||||
import net.minecraft.network.chat.Component;
|
||||
|
||||
import java.nio.file.Files;
|
||||
|
||||
public class EditProfileScreen extends Screen {
|
||||
private final ProfilesScreen profilesScreen;
|
||||
private final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this, 24, 33);
|
||||
|
||||
private final ProfileConfiguration profileConfiguration;
|
||||
private final Component profileName;
|
||||
private EditBox profileNameEdit;
|
||||
|
||||
|
|
@ -24,6 +25,7 @@ public class EditProfileScreen extends Screen {
|
|||
super(Component.literal(Component.translatable("gui.optionsprofiles.editing-profile-title").getString() + profileName.getString()));
|
||||
this.profilesScreen = profilesScreen;
|
||||
this.profileName = profileName;
|
||||
this.profileConfiguration = ProfileConfiguration.get(profileName.getString());
|
||||
}
|
||||
|
||||
protected void init() {
|
||||
|
|
@ -49,7 +51,6 @@ public class EditProfileScreen extends Screen {
|
|||
this.onClose();
|
||||
})
|
||||
.size(150, 20)
|
||||
.pos(this.width / 2 - 75, 145)
|
||||
.tooltip(Tooltip.create(Component.translatable("gui.optionsprofiles.overwrite-options.tooltip")))
|
||||
.build(),
|
||||
LayoutSettings::alignHorizontallyCenter
|
||||
|
|
@ -62,7 +63,6 @@ public class EditProfileScreen extends Screen {
|
|||
this.minecraft.setScreen(new EditProfileScreen(profilesScreen, Component.literal(this.profileNameEdit.getValue())));
|
||||
})
|
||||
.size(150, 20)
|
||||
.pos(this.width / 2 - 75, 166)
|
||||
.build(),
|
||||
LayoutSettings::alignHorizontallyCenter
|
||||
);
|
||||
|
|
@ -71,11 +71,20 @@ public class EditProfileScreen extends Screen {
|
|||
Component.translatable("gui.optionsprofiles.options-toggle").append("..."),
|
||||
(button) -> this.minecraft.setScreen(new OptionsToggleScreen(this, profileName)))
|
||||
.size(150, 20)
|
||||
.pos(this.width / 2 - 75, 187)
|
||||
.tooltip(Tooltip.create(Component.translatable("gui.optionsprofiles.options-toggle.tooltip")))
|
||||
.build(),
|
||||
LayoutSettings::alignHorizontallyCenter
|
||||
);
|
||||
linearLayoutButtons.addChild(
|
||||
Checkbox.builder(
|
||||
Component.literal("Load on startup"),
|
||||
this.font
|
||||
)
|
||||
.selected(profileConfiguration.isLoadOnStartup())
|
||||
.onValueChange((checkbox, value) -> profileConfiguration.setLoadOnStartup(value))
|
||||
.build(),
|
||||
(layoutSettings) -> layoutSettings.alignHorizontallyCenter().paddingTop(5)
|
||||
);
|
||||
|
||||
this.layout.addToFooter(
|
||||
Button.builder(
|
||||
|
|
|
|||
|
|
@ -17,8 +17,7 @@ public class ProfileConfiguration {
|
|||
private static Path configurationFile;
|
||||
private static String profileName;
|
||||
|
||||
public static int configurationVersion = 1; // Used to update configuration in later revisions
|
||||
private int version = configurationVersion; // ^ same here - this variable is used to show it in the configuration.json file
|
||||
private boolean loadOnStartup = false;
|
||||
private List<String> optionsToLoad = new ArrayList<>();
|
||||
|
||||
public ProfileConfiguration save() {
|
||||
|
|
@ -59,12 +58,12 @@ public class ProfileConfiguration {
|
|||
return configuration;
|
||||
}
|
||||
|
||||
public int getVersion() {
|
||||
return version;
|
||||
public boolean isLoadOnStartup() {
|
||||
return loadOnStartup;
|
||||
}
|
||||
|
||||
public void setVersion(int version) {
|
||||
this.version = version;
|
||||
public void setLoadOnStartup(boolean loadOnStartup) {
|
||||
this.loadOnStartup = loadOnStartup;
|
||||
}
|
||||
|
||||
public List<String> getOptionsToLoad() {
|
||||
|
|
|
|||
Loading…
Reference in a new issue