feat: load profiles through keybinds (#33)
This commit is contained in:
parent
1b8e17e02b
commit
698e793586
7 changed files with 126 additions and 57 deletions
|
|
@ -0,0 +1,18 @@
|
|||
package net.trafficlunar.optionsprofiles;
|
||||
|
||||
import dev.architectury.event.events.common.CommandRegistrationEvent;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.trafficlunar.optionsprofiles.gui.ProfilesScreen;
|
||||
|
||||
public class Commands {
|
||||
public static void init() {
|
||||
CommandRegistrationEvent.EVENT.register(((dispatcher, buildContext, selection) -> dispatcher.register(
|
||||
net.minecraft.commands.Commands
|
||||
.literal("optionsprofiles")
|
||||
.executes(context -> {
|
||||
Minecraft.getInstance().execute(() -> Minecraft.getInstance().setScreen(new ProfilesScreen(null)));
|
||||
return 1;
|
||||
})
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
package net.trafficlunar.optionsprofiles;
|
||||
|
||||
import com.mojang.blaze3d.platform.InputConstants;
|
||||
import dev.architectury.event.events.client.ClientTickEvent;
|
||||
import dev.architectury.registry.client.keymappings.KeyMappingRegistry;
|
||||
import net.minecraft.client.KeyMapping;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.trafficlunar.optionsprofiles.profiles.ProfileConfiguration;
|
||||
import net.trafficlunar.optionsprofiles.profiles.Profiles;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class Keybinds {
|
||||
private static final KeyMapping[] PROFILE_KEYMAPPINGS = new KeyMapping[3];
|
||||
|
||||
public static void init() {
|
||||
for (int i = 0; i < PROFILE_KEYMAPPINGS.length; i++) {
|
||||
PROFILE_KEYMAPPINGS[i] = new KeyMapping(
|
||||
"key.optionsprofiles.profile_" + (i + 1),
|
||||
InputConstants.Type.KEYSYM,
|
||||
-1,
|
||||
"category.optionsprofiles.keys"
|
||||
);
|
||||
KeyMappingRegistry.register(PROFILE_KEYMAPPINGS[i]);
|
||||
}
|
||||
|
||||
ClientTickEvent.CLIENT_POST.register(minecraft -> {
|
||||
for (int i = 0; i < PROFILE_KEYMAPPINGS.length; i++) {
|
||||
while (PROFILE_KEYMAPPINGS[i].consumeClick()) {
|
||||
loadProfilesByKeybind(i + 1);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static void loadProfilesByKeybind(int keybindIndex) {
|
||||
try (Stream<Path> paths = Files.list(Profiles.PROFILES_DIRECTORY)) {
|
||||
paths.filter(Files::isDirectory)
|
||||
.forEach(path -> {
|
||||
String profileName = path.getFileName().toString();
|
||||
|
||||
ProfileConfiguration profileConfiguration = ProfileConfiguration.get(profileName);
|
||||
if (profileConfiguration.getKeybindIndex() == keybindIndex) {
|
||||
Minecraft minecraft = Minecraft.getInstance();
|
||||
|
||||
Profiles.loadProfile(profileName);
|
||||
minecraft.options.load();
|
||||
|
||||
if (ProfileConfiguration.get(profileName).getOptionsToLoad().contains("resourcePacks")) {
|
||||
minecraft.options.loadSelectedResourcePacks(minecraft.getResourcePackRepository());
|
||||
minecraft.reloadResourcePacks();
|
||||
}
|
||||
|
||||
minecraft.options.save();
|
||||
minecraft.levelRenderer.allChanged();
|
||||
|
||||
OptionsProfilesMod.LOGGER.warn("[Profile '{}']: Loaded through keybind", profileName);
|
||||
}
|
||||
});
|
||||
} catch (IOException e) {
|
||||
OptionsProfilesMod.LOGGER.error("An error occurred when loading profiles through keybinds", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,10 +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;
|
||||
import net.trafficlunar.optionsprofiles.gui.ProfilesScreen;
|
||||
import net.trafficlunar.optionsprofiles.profiles.Profiles;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
|
@ -20,6 +16,7 @@ public class OptionsProfilesMod {
|
|||
private static OptionsProfilesModConfiguration CONFIG;
|
||||
|
||||
public static void init() {
|
||||
// Create options-profiles directory
|
||||
Path profilesDirectory = Paths.get("options-profiles");
|
||||
|
||||
if (Files.notExists(profilesDirectory)) {
|
||||
|
|
@ -33,20 +30,13 @@ public class OptionsProfilesMod {
|
|||
// Load mod config
|
||||
CONFIG = OptionsProfilesModConfiguration.load();
|
||||
|
||||
// Add /optionsprofiles command
|
||||
CommandRegistrationEvent.EVENT.register(((dispatcher, buildContext, selection) -> dispatcher.register(
|
||||
Commands
|
||||
.literal("optionsprofiles")
|
||||
.executes(context -> {
|
||||
Minecraft.getInstance().execute(() -> Minecraft.getInstance().setScreen(new ProfilesScreen(null)));
|
||||
return 1;
|
||||
})
|
||||
)));
|
||||
|
||||
// Init profiles
|
||||
// Init profiles (for loading on startup)
|
||||
ClientLifecycleEvent.CLIENT_STARTED.register(client -> {
|
||||
Profiles.init();
|
||||
});
|
||||
|
||||
Keybinds.init();
|
||||
Commands.init();
|
||||
}
|
||||
|
||||
public static OptionsProfilesModConfiguration config() {
|
||||
|
|
|
|||
|
|
@ -40,7 +40,6 @@ public class EditProfileScreen extends Screen {
|
|||
linearLayoutEditBox.addChild(this.profileNameEdit);
|
||||
|
||||
LinearLayout linearLayoutButtons = linearLayoutContent.addChild(LinearLayout.vertical().spacing(1), LayoutSettings::alignHorizontallyCenter);
|
||||
|
||||
linearLayoutButtons.addChild(
|
||||
Button.builder(
|
||||
Component.translatable("gui.optionsprofiles.overwrite-options"),
|
||||
|
|
@ -77,27 +76,23 @@ 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
|
||||
}
|
||||
|
||||
LinearLayout linearLayoutSettings = linearLayoutContent.addChild(LinearLayout.vertical().spacing(1), LayoutSettings::alignHorizontallyCenter);
|
||||
linearLayoutSettings.addChild(
|
||||
CycleButton.<Integer>builder(value -> Component.literal(value.toString()))
|
||||
.withValues(0, 1, 2, 3)
|
||||
.withInitialValue(0)
|
||||
.create(0, 0, 150, 20, Component.translatable("gui.optionsprofiles.keybind-index"), (button, keybindIndex) -> {
|
||||
this.profileConfiguration.setKeybindIndex(keybindIndex);
|
||||
}),
|
||||
LayoutSettings::alignHorizontallyCenter
|
||||
);
|
||||
linearLayoutSettings.addChild(
|
||||
CycleButton.onOffBuilder(this.profileConfiguration.shouldLoadOnStartup())
|
||||
.create(0, 0, 150, 20, Component.translatable("gui.optionsprofiles.load-on-startup"), (button, boolean_) -> {
|
||||
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);
|
||||
}),
|
||||
LayoutSettings::alignHorizontallyCenter
|
||||
);
|
||||
|
||||
this.layout.addToFooter(
|
||||
Button.builder(
|
||||
|
|
|
|||
|
|
@ -26,29 +26,13 @@ public class SettingsScreen extends Screen {
|
|||
LinearLayout linearLayoutHeader = this.layout.addToHeader(LinearLayout.vertical());
|
||||
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(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
|
||||
}
|
||||
|
||||
CycleButton<Boolean> showProfilesButtonButton = CycleButton.onOffBuilder(OptionsProfilesMod.config().shouldShowProfilesButton())
|
||||
.create(0, 0, 150, 20, Component.translatable("gui.optionsprofiles.show-profiles-button"), (button, boolean_) -> {
|
||||
OptionsProfilesMod.config().setShowProfilesButton(boolean_);
|
||||
});
|
||||
|
||||
showProfilesButtonButton.setTooltip(Tooltip.create(Component.translatable("gui.optionsprofiles.show-profiles-button.tooltip")));
|
||||
|
||||
// Set color on first init
|
||||
if (OptionsProfilesMod.config().shouldShowProfilesButton()) {
|
||||
showProfilesButtonButton.setMessage(showProfilesButtonButton.getMessage().copy().withStyle(ChatFormatting.GREEN)); // Set the button's color to green
|
||||
} else {
|
||||
showProfilesButtonButton.setMessage(showProfilesButtonButton.getMessage().copy().withStyle(ChatFormatting.RED)); // Set the button's color to red
|
||||
}
|
||||
|
||||
linearLayoutContent.addChild(new StringWidget(Component.translatable("gui.optionsprofiles.show-profiles-button"), this.font), LayoutSettings::alignVerticallyMiddle);
|
||||
linearLayoutContent.addChild(showProfilesButtonButton);
|
||||
this.layout.addToContents(showProfilesButtonButton);
|
||||
|
||||
this.layout.addToFooter(
|
||||
Button.builder(
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ public class ProfileConfiguration {
|
|||
private static String profileName;
|
||||
|
||||
private boolean loadOnStartup = false;
|
||||
private int keybindIndex = 0;
|
||||
private List<String> optionsToLoad = new ArrayList<>();
|
||||
|
||||
public ProfileConfiguration save() {
|
||||
|
|
@ -63,6 +64,14 @@ public class ProfileConfiguration {
|
|||
this.loadOnStartup = loadOnStartup;
|
||||
}
|
||||
|
||||
public int getKeybindIndex() {
|
||||
return keybindIndex;
|
||||
}
|
||||
|
||||
public void setKeybindIndex(int keybindIndex) {
|
||||
this.keybindIndex = keybindIndex;
|
||||
}
|
||||
|
||||
public List<String> getOptionsToLoad() {
|
||||
return optionsToLoad;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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.keybind-index": "Keybind index",
|
||||
"gui.optionsprofiles.load-on-startup": "Load on startup",
|
||||
|
||||
"gui.optionsprofiles.options-toggle": "Select options to toggle",
|
||||
|
|
@ -20,5 +21,10 @@
|
|||
"gui.optionsprofiles.settings-button": "Settings",
|
||||
"gui.optionsprofiles.settings-menu": "Options Profiles Settings",
|
||||
"gui.optionsprofiles.show-profiles-button": "Show Profiles Button",
|
||||
"gui.optionsprofiles.show-profiles-button.tooltip": "Toggle to remove the profiles button in Options. Access profiles through /optionsprofiles."
|
||||
"gui.optionsprofiles.show-profiles-button.tooltip": "Toggle to remove the profiles button in Options. Access profiles through /optionsprofiles.",
|
||||
|
||||
"category.optionsprofiles.keys": "Options Profiles",
|
||||
"key.optionsprofiles.profile_1": "Profile 1",
|
||||
"key.optionsprofiles.profile_2": "Profile 2",
|
||||
"key.optionsprofiles.profile_3": "Profile 3"
|
||||
}
|
||||
Loading…
Reference in a new issue