diff --git a/common/src/main/java/net/trafficlunar/optionsprofiles/OptionsProfilesMod.java b/common/src/main/java/net/trafficlunar/optionsprofiles/OptionsProfilesMod.java index 14ca7a5..18bf08c 100644 --- a/common/src/main/java/net/trafficlunar/optionsprofiles/OptionsProfilesMod.java +++ b/common/src/main/java/net/trafficlunar/optionsprofiles/OptionsProfilesMod.java @@ -1,5 +1,6 @@ package net.trafficlunar.optionsprofiles; +import dev.architectury.event.events.client.ClientLifecycleEvent; import dev.architectury.event.events.common.CommandRegistrationEvent; import net.minecraft.client.Minecraft; import net.minecraft.commands.Commands; @@ -29,11 +30,9 @@ public class OptionsProfilesMod { } } + // Load mod config CONFIG = OptionsProfilesModConfiguration.load(); - // Update / add configuration for existing profiles - Profiles.updateProfiles(); - // Add /optionsprofiles command CommandRegistrationEvent.EVENT.register(((dispatcher, buildContext, selection) -> dispatcher.register( Commands @@ -43,6 +42,11 @@ public class OptionsProfilesMod { return 1; }) ))); + + // Init profiles + ClientLifecycleEvent.CLIENT_STARTED.register(client -> { + Profiles.init(); + }); } public static OptionsProfilesModConfiguration config() { diff --git a/common/src/main/java/net/trafficlunar/optionsprofiles/gui/EditProfileScreen.java b/common/src/main/java/net/trafficlunar/optionsprofiles/gui/EditProfileScreen.java index 96d6d06..d108e13 100644 --- a/common/src/main/java/net/trafficlunar/optionsprofiles/gui/EditProfileScreen.java +++ b/common/src/main/java/net/trafficlunar/optionsprofiles/gui/EditProfileScreen.java @@ -1,29 +1,29 @@ package net.trafficlunar.optionsprofiles.gui; -import net.trafficlunar.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; import net.minecraft.client.gui.screens.Screen; import net.minecraft.network.chat.CommonComponents; import net.minecraft.network.chat.Component; +import net.trafficlunar.optionsprofiles.profiles.ProfileConfiguration; +import net.trafficlunar.optionsprofiles.profiles.Profiles; public class EditProfileScreen extends Screen { private final ProfilesScreen profilesScreen; private final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this, 24, 33); private final Component profileName; + private final ProfileConfiguration profileConfiguration; private EditBox profileNameEdit; public EditProfileScreen(ProfilesScreen profilesScreen, Component profileName) { 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() { @@ -77,6 +77,28 @@ public class EditProfileScreen extends Screen { LayoutSettings::alignHorizontallyCenter ); + LinearLayout linearLayoutStartup = linearLayoutContent.addChild(LinearLayout.horizontal().spacing(12), LayoutSettings::alignHorizontallyCenter); + CycleButton loadOnStartupButton = CycleButton.onOffBuilder(this.profileConfiguration.shouldLoadOnStartup()).displayOnlyValue().create(0, 0, 44, 20, Component.empty(), (button, boolean_) -> { + // If toggled to true + if (boolean_) { + button.setMessage(button.getMessage().copy().withStyle(ChatFormatting.GREEN)); // Set the button's color to green + } else { + button.setMessage(button.getMessage().copy().withStyle(ChatFormatting.RED)); // Set the button's color to red + } + + this.profileConfiguration.setLoadOnStartup(boolean_); + }); + + // Set color on first init + if (this.profileConfiguration.shouldLoadOnStartup()) { + loadOnStartupButton.setMessage(loadOnStartupButton.getMessage().copy().withStyle(ChatFormatting.GREEN)); // Set the button's color to green + } else { + loadOnStartupButton.setMessage(loadOnStartupButton.getMessage().copy().withStyle(ChatFormatting.RED)); // Set the button's color to red + } + + linearLayoutStartup.addChild(new StringWidget(Component.translatable("gui.optionsprofiles.load-on-startup"), this.font), LayoutSettings::alignVerticallyMiddle); + linearLayoutStartup.addChild(loadOnStartupButton); + this.layout.addToFooter( Button.builder( CommonComponents.GUI_DONE, @@ -106,6 +128,7 @@ public class EditProfileScreen extends Screen { } public void onClose() { + this.profileConfiguration.save(); this.minecraft.setScreen(this.profilesScreen); this.profilesScreen.profilesList.refreshEntries(); } diff --git a/common/src/main/java/net/trafficlunar/optionsprofiles/gui/SettingsScreen.java b/common/src/main/java/net/trafficlunar/optionsprofiles/gui/SettingsScreen.java index 737a9c1..6a37093 100644 --- a/common/src/main/java/net/trafficlunar/optionsprofiles/gui/SettingsScreen.java +++ b/common/src/main/java/net/trafficlunar/optionsprofiles/gui/SettingsScreen.java @@ -27,7 +27,7 @@ public class SettingsScreen extends Screen { linearLayoutHeader.addChild(new StringWidget(this.title, this.font), LayoutSettings::alignHorizontallyCenter); LinearLayout linearLayoutContent = this.layout.addToContents(LinearLayout.horizontal().spacing(12), LayoutSettings::alignHorizontallyCenter); - CycleButton showProfilesButtonButton = CycleButton.onOffBuilder(OptionsProfilesMod.config().shouldShowProfilesButton()).displayOnlyValue().create(this.width / 2 + 60, 100, 44, 20, Component.empty(), (button, boolean_) -> { + CycleButton showProfilesButtonButton = CycleButton.onOffBuilder(OptionsProfilesMod.config().shouldShowProfilesButton()).displayOnlyValue().create(0, 0, 44, 20, Component.empty(), (button, boolean_) -> { // If toggled to true if (boolean_) { button.setMessage(button.getMessage().copy().withStyle(ChatFormatting.GREEN)); // Set the button's color to green diff --git a/common/src/main/java/net/trafficlunar/optionsprofiles/profiles/ProfileConfiguration.java b/common/src/main/java/net/trafficlunar/optionsprofiles/profiles/ProfileConfiguration.java index 5000e9f..75d9b1b 100644 --- a/common/src/main/java/net/trafficlunar/optionsprofiles/profiles/ProfileConfiguration.java +++ b/common/src/main/java/net/trafficlunar/optionsprofiles/profiles/ProfileConfiguration.java @@ -1,8 +1,8 @@ package net.trafficlunar.optionsprofiles.profiles; -import net.trafficlunar.optionsprofiles.OptionsProfilesMod; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import net.trafficlunar.optionsprofiles.OptionsProfilesMod; import java.io.BufferedReader; import java.io.BufferedWriter; @@ -16,8 +16,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 optionsToLoad = new ArrayList<>(); public ProfileConfiguration save() { @@ -56,12 +55,12 @@ public class ProfileConfiguration { return configuration; } - public int getVersion() { - return version; + public boolean shouldLoadOnStartup() { + return loadOnStartup; } - public void setVersion(int version) { - this.version = version; + public void setLoadOnStartup(boolean loadOnStartup) { + this.loadOnStartup = loadOnStartup; } public List getOptionsToLoad() { diff --git a/common/src/main/java/net/trafficlunar/optionsprofiles/profiles/Profiles.java b/common/src/main/java/net/trafficlunar/optionsprofiles/profiles/Profiles.java index 74a1d8f..ac5489d 100644 --- a/common/src/main/java/net/trafficlunar/optionsprofiles/profiles/Profiles.java +++ b/common/src/main/java/net/trafficlunar/optionsprofiles/profiles/Profiles.java @@ -27,8 +27,7 @@ public class Profiles { public static final Path IRIS_OPTIONS_FILE = Paths.get("config/iris.properties"); public static final Path DISTANT_HORIZONS_OPTIONS_FILE = Paths.get("config/DistantHorizons.toml"); - // This function goes through every profile and updates / adds the configuration file if it doesn't exist - public static void updateProfiles() { + public static void init() { try (Stream paths = Files.list(PROFILES_DIRECTORY)) { paths.filter(Files::isDirectory) .forEach(path -> { @@ -36,32 +35,13 @@ public class Profiles { // This gets the configuration but also creates the configuration file if it is not there ProfileConfiguration profileConfiguration = ProfileConfiguration.get(profileName); - List optionsToLoad = profileConfiguration.getOptionsToLoad(); - - // Checks for updates to the configuration - if (profileConfiguration.getVersion() != ProfileConfiguration.configurationVersion) { - Path configurationFile = path.resolve("configuration.json"); - - try { - Files.delete(configurationFile); - } catch (IOException e) { - OptionsProfilesMod.LOGGER.error("[Profile '{}']: Error deleting configuration file", profileName, e); - } - - // Create the configuration.json again thus updating it - profileConfiguration = ProfileConfiguration.get(profileName); - - // Add player's old configuration - profileConfiguration.setOptionsToLoad(optionsToLoad); - - // Save configuration - profileConfiguration.save(); + if (profileConfiguration.shouldLoadOnStartup()) { + loadProfile(profileName); + OptionsProfilesMod.LOGGER.warn("[Profile '{}']: Loaded on startup", profileName); } - - OptionsProfilesMod.LOGGER.warn("[Profile '{}']: Profile configuration updated / added", profileName); }); } catch (IOException e) { - OptionsProfilesMod.LOGGER.error("An error occurred when updating profiles", e); + OptionsProfilesMod.LOGGER.error("An error occurred when initializing", e); } } diff --git a/common/src/main/resources/assets/optionsprofiles/lang/en_us.json b/common/src/main/resources/assets/optionsprofiles/lang/en_us.json index 0a01b06..f712812 100644 --- a/common/src/main/resources/assets/optionsprofiles/lang/en_us.json +++ b/common/src/main/resources/assets/optionsprofiles/lang/en_us.json @@ -10,6 +10,7 @@ "gui.optionsprofiles.overwrite-options.tooltip": "Replaces the profile's options with your current options", "gui.optionsprofiles.rename-profile": "Rename", "gui.optionsprofiles.delete-profile": "Delete", + "gui.optionsprofiles.load-on-startup": "Load on startup", "gui.optionsprofiles.options-toggle": "Select options to toggle", "gui.optionsprofiles.options-toggle.tooltip": "Select the options you want to load in this profile",