feat: load profiles on startup (#17)
This commit is contained in:
parent
84b1eadf4a
commit
1b8e17e02b
6 changed files with 48 additions and 41 deletions
|
|
@ -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() {
|
||||
|
|
|
|||
|
|
@ -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<Boolean> 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();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<Boolean> showProfilesButtonButton = CycleButton.onOffBuilder(OptionsProfilesMod.config().shouldShowProfilesButton()).displayOnlyValue().create(this.width / 2 + 60, 100, 44, 20, Component.empty(), (button, boolean_) -> {
|
||||
CycleButton<Boolean> 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
|
||||
|
|
|
|||
|
|
@ -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<String> 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<String> getOptionsToLoad() {
|
||||
|
|
|
|||
|
|
@ -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<Path> 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<String> 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
Loading…
Reference in a new issue