From 0b0a93dca745a14e7fa75ac3d1d9385f8ce12b2a Mon Sep 17 00:00:00 2001 From: axolotlmaid Date: Sat, 29 Jun 2024 16:55:37 +0100 Subject: [PATCH] Add checkbox in edit profile screen --- .../gui/EditProfileScreen.java | 70 +++++++++++++++---- .../optionsprofiles/gui/ProfilesList.java | 28 +++++--- .../optionsprofiles/gui/ProfilesScreen.java | 25 ++++--- .../mixin/MixinOptionsScreen.java | 10 ++- .../profiles/ProfileConfiguration.java | 12 ++-- .../optionsprofiles/profiles/Profiles.java | 8 +-- .../assets/optionsprofiles/lang/en_us.json | 1 + .../assets/optionsprofiles/lang/ru_ru.json | 1 + .../assets/optionsprofiles/lang/tt_ru.json | 1 + .../assets/optionsprofiles/lang/zh_cn.json | 1 + 10 files changed, 111 insertions(+), 46 deletions(-) diff --git a/common/src/main/java/com/axolotlmaid/optionsprofiles/gui/EditProfileScreen.java b/common/src/main/java/com/axolotlmaid/optionsprofiles/gui/EditProfileScreen.java index 02707cb..f7b8ff8 100644 --- a/common/src/main/java/com/axolotlmaid/optionsprofiles/gui/EditProfileScreen.java +++ b/common/src/main/java/com/axolotlmaid/optionsprofiles/gui/EditProfileScreen.java @@ -1,9 +1,11 @@ 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.GuiGraphics; import net.minecraft.client.gui.components.Button; +import net.minecraft.client.gui.components.Checkbox; import net.minecraft.client.gui.components.EditBox; import net.minecraft.client.gui.screens.Screen; import net.minecraft.network.chat.CommonComponents; @@ -14,6 +16,7 @@ public class EditProfileScreen extends Screen { private final Component profileName; private EditBox profileNameEdit; + private Checkbox keybindingsOnlyCheckbox; public EditProfileScreen(Screen screen, Component profileName) { super(Component.literal(Component.translatable("gui.optionsprofiles.editing-profile-title").getString() + profileName.getString())); @@ -22,28 +25,65 @@ public class EditProfileScreen extends Screen { } protected void init() { + ProfileConfiguration profileConfiguration = ProfileConfiguration.get(profileName.getString()); + this.profileNameEdit = new EditBox(this.font, this.width / 2 - 102, 116, 204, 20, Component.empty()); this.profileNameEdit.setValue(profileName.getString()); this.addWidget(this.profileNameEdit); - this.addRenderableWidget(Button.builder(Component.translatable("gui.optionsprofiles.overwrite-options"), (button) -> { - Profiles.writeProfile(profileName.getString(), true); - this.minecraft.setScreen(this.lastScreen); - }).size(100, 20).pos(this.width / 2 - 50, 145).build()); + this.keybindingsOnlyCheckbox = Checkbox.builder( + Component.translatable("gui.optionsprofiles.keybindings-only"), + this.font) + .pos(5, this.height - 45) + .selected(profileConfiguration.isKeybindingsOnly()) + .build(); + this.addRenderableWidget(this.keybindingsOnlyCheckbox); - this.addRenderableWidget(Button.builder(Component.translatable("gui.optionsprofiles.rename-profile"), (button) -> { - Profiles.renameProfile(profileName.getString(), this.profileNameEdit.getValue()); - this.minecraft.setScreen(new EditProfileScreen(lastScreen, Component.literal(this.profileNameEdit.getValue()))); - }).size(100, 20).pos(this.width / 2 - 50, 166).build()); + this.addRenderableWidget( + Button.builder( + Component.translatable("gui.optionsprofiles.overwrite-options"), + (button) -> { + Profiles.writeProfile(profileName.getString(), true); + this.minecraft.setScreen(this.lastScreen); + }) + .size(100, 20) + .pos(this.width / 2 - 50, 145) + .build()); - this.addRenderableWidget(Button.builder(Component.translatable("gui.optionsprofiles.delete-profile").withStyle(ChatFormatting.RED), (button) -> { - Profiles.deleteProfile(profileName.getString()); - this.minecraft.setScreen(this.lastScreen); - }).size(100, 20).pos(5, this.height - 25).build()); + this.addRenderableWidget( + Button.builder( + Component.translatable("gui.optionsprofiles.rename-profile"), + (button) -> { + Profiles.renameProfile(profileName.getString(), this.profileNameEdit.getValue()); + this.minecraft.setScreen(new EditProfileScreen(lastScreen, Component.literal(this.profileNameEdit.getValue()))); + }) + .size(100, 20) + .pos(this.width / 2 - 50, 166) + .build()); - this.addRenderableWidget(Button.builder(CommonComponents.GUI_DONE, (button) -> { - this.minecraft.setScreen(this.lastScreen); - }).size(100, 20).pos(this.width / 2 - 50, this.height - 40).build()); + this.addRenderableWidget( + Button.builder( + Component.translatable("gui.optionsprofiles.delete-profile") + .withStyle(ChatFormatting.RED), + (button) -> { + Profiles.deleteProfile(profileName.getString()); + this.minecraft.setScreen(this.lastScreen); + }) + .size(100, 20) + .pos(5, this.height - 25) + .build()); + + this.addRenderableWidget( + Button.builder( + CommonComponents.GUI_DONE, + (button) -> { + profileConfiguration.setKeybindingsOnly(keybindingsOnlyCheckbox.selected()); + profileConfiguration.save(); + this.minecraft.setScreen(this.lastScreen); + }) + .size(100, 20) + .pos(this.width / 2 - 50, this.height - 40) + .build()); } public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float delta) { diff --git a/common/src/main/java/com/axolotlmaid/optionsprofiles/gui/ProfilesList.java b/common/src/main/java/com/axolotlmaid/optionsprofiles/gui/ProfilesList.java index e43c955..fedf12f 100644 --- a/common/src/main/java/com/axolotlmaid/optionsprofiles/gui/ProfilesList.java +++ b/common/src/main/java/com/axolotlmaid/optionsprofiles/gui/ProfilesList.java @@ -59,21 +59,27 @@ public class ProfilesList extends ContainerObjectSelectionList { - minecraft.setScreen(new EditProfileScreen(profilesScreen, profileName)); - }).size(75, 20).createNarration((supplier) -> Component.translatable("gui.optionsprofiles.edit-profile")).build(); + this.editButton = Button.builder( + Component.translatable("gui.optionsprofiles.edit-profile"), + (button) -> minecraft.setScreen(new EditProfileScreen(profilesScreen, profileName))) + .size(75, 20) + .build(); - this.loadButton = Button.builder(Component.translatable("gui.optionsprofiles.load-profile"), (button) -> { - Profiles.loadProfile(profileName.getString()); + this.loadButton = Button.builder( + Component.translatable("gui.optionsprofiles.load-profile"), + (button) -> { + Profiles.loadProfile(profileName.getString()); - minecraft.options.load(); - minecraft.options.loadSelectedResourcePacks(minecraft.getResourcePackRepository()); - minecraft.reloadResourcePacks(); + minecraft.options.load(); + minecraft.options.loadSelectedResourcePacks(minecraft.getResourcePackRepository()); + minecraft.reloadResourcePacks(); - minecraft.options.save(); + minecraft.options.save(); - button.active = false; - }).size(75, 20).createNarration((supplier) -> Component.translatable("gui.optionsprofiles.load-profile")).build(); + button.active = false; + }) + .size(75, 20) + .build(); this.loadButton.active = !Profiles.isProfileLoaded(profileName.getString()); } diff --git a/common/src/main/java/com/axolotlmaid/optionsprofiles/gui/ProfilesScreen.java b/common/src/main/java/com/axolotlmaid/optionsprofiles/gui/ProfilesScreen.java index 6cd1ad1..9162673 100644 --- a/common/src/main/java/com/axolotlmaid/optionsprofiles/gui/ProfilesScreen.java +++ b/common/src/main/java/com/axolotlmaid/optionsprofiles/gui/ProfilesScreen.java @@ -20,15 +20,24 @@ public class ProfilesScreen extends Screen { this.profilesList = new ProfilesList(this, this.minecraft); this.addWidget(this.profilesList); - // buttons - this.addRenderableWidget(Button.builder(Component.translatable("gui.optionsprofiles.save-current-options"), (button) -> { - Profiles.createProfile(); - this.profilesList.refreshEntries(); - }).size(150, 20).pos(this.width / 2 - 155, this.height - 29).build()); + this.addRenderableWidget( + Button.builder( + Component.translatable("gui.optionsprofiles.save-current-options"), + (button) -> { + Profiles.createProfile(); + this.profilesList.refreshEntries(); + }) + .size(150, 20) + .pos(this.width / 2 - 155, this.height - 29) + .build()); - this.addRenderableWidget(Button.builder(CommonComponents.GUI_DONE, (button) -> { - this.minecraft.setScreen(this.lastScreen); - }).size(150, 20).pos(this.width / 2 + 5, this.height - 29).build()); + this.addRenderableWidget( + Button.builder( + CommonComponents.GUI_DONE, + (button) -> this.minecraft.setScreen(this.lastScreen)) + .size(150, 20) + .pos(this.width / 2 + 5, this.height - 29) + .build()); } public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float delta) { diff --git a/common/src/main/java/com/axolotlmaid/optionsprofiles/mixin/MixinOptionsScreen.java b/common/src/main/java/com/axolotlmaid/optionsprofiles/mixin/MixinOptionsScreen.java index 3cc5669..9afa6a7 100644 --- a/common/src/main/java/com/axolotlmaid/optionsprofiles/mixin/MixinOptionsScreen.java +++ b/common/src/main/java/com/axolotlmaid/optionsprofiles/mixin/MixinOptionsScreen.java @@ -18,8 +18,12 @@ public class MixinOptionsScreen extends Screen { @Inject(at = @At("HEAD"), method = "init") private void init(CallbackInfo info) { - this.addRenderableWidget(Button.builder(Component.translatable("gui.optionsprofiles.profiles-menu"), (button) -> { - this.minecraft.setScreen(new ProfilesScreen(this)); - }).width(100).pos(5, 5).build()); + this.addRenderableWidget( + Button.builder( + Component.translatable("gui.optionsprofiles.profiles-menu"), + (button) -> this.minecraft.setScreen(new ProfilesScreen(this))) + .width(100) + .pos(5, 5) + .build()); } } \ No newline at end of file diff --git a/common/src/main/java/com/axolotlmaid/optionsprofiles/profiles/ProfileConfiguration.java b/common/src/main/java/com/axolotlmaid/optionsprofiles/profiles/ProfileConfiguration.java index 9573746..3279034 100644 --- a/common/src/main/java/com/axolotlmaid/optionsprofiles/profiles/ProfileConfiguration.java +++ b/common/src/main/java/com/axolotlmaid/optionsprofiles/profiles/ProfileConfiguration.java @@ -12,15 +12,13 @@ import java.nio.file.Path; public class ProfileConfiguration { private static final Path profilesDirectory = Profiles.PROFILES_DIRECTORY; + private static Path configurationFile; private boolean keybindingsOnly = false; - public ProfileConfiguration save(String profileName) { + public ProfileConfiguration save() { ProfileConfiguration configuration = new ProfileConfiguration(); - Path profile = profilesDirectory.resolve(profileName); - Path configurationFile = profile.resolve("configuration.json"); - Gson gson = new GsonBuilder() .setPrettyPrinting() .create(); @@ -38,7 +36,11 @@ public class ProfileConfiguration { ProfileConfiguration configuration = new ProfileConfiguration(); Path profile = profilesDirectory.resolve(profileName); - Path configurationFile = profile.resolve("configuration.json"); + configurationFile = profile.resolve("configuration.json"); + + if (Files.notExists(configurationFile)) { + configuration.save(); + } try (BufferedReader reader = Files.newBufferedReader(configurationFile)) { Gson gson = new Gson(); diff --git a/common/src/main/java/com/axolotlmaid/optionsprofiles/profiles/Profiles.java b/common/src/main/java/com/axolotlmaid/optionsprofiles/profiles/Profiles.java index b3e1a97..0b20ad6 100644 --- a/common/src/main/java/com/axolotlmaid/optionsprofiles/profiles/Profiles.java +++ b/common/src/main/java/com/axolotlmaid/optionsprofiles/profiles/Profiles.java @@ -27,13 +27,13 @@ public class Profiles { paths.filter(Files::isDirectory) .forEach(path -> { Path configurationFile = path.resolve("configuration.json"); - if (!Files.exists(configurationFile)) { + if (Files.notExists(configurationFile)) { String profileName = path.getFileName().toString(); // Create configuration.json - new ProfileConfiguration().save(profileName); + ProfileConfiguration.get(profileName); - OptionsProfilesMod.LOGGER.warn("[Profile '{}']: Profile updated", profileName); + OptionsProfilesMod.LOGGER.warn("[Profile '{}']: Profile configuration added", profileName); } }); } catch (IOException e) { @@ -89,7 +89,7 @@ public class Profiles { } } else { // Create configuration.json - new ProfileConfiguration().save(profileName); + ProfileConfiguration.get(profileName); } copyOptionFile(profile, OPTIONS_FILE); 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 76abbc3..f24917f 100644 --- a/common/src/main/resources/assets/optionsprofiles/lang/en_us.json +++ b/common/src/main/resources/assets/optionsprofiles/lang/en_us.json @@ -8,5 +8,6 @@ "gui.optionsprofiles.profile-name-text": "Profile Name", "gui.optionsprofiles.overwrite-options": "Overwrite", "gui.optionsprofiles.rename-profile": "Rename", + "gui.optionsprofiles.keybindings-only": "Keybindings Only", "gui.optionsprofiles.delete-profile": "Delete" } \ No newline at end of file diff --git a/common/src/main/resources/assets/optionsprofiles/lang/ru_ru.json b/common/src/main/resources/assets/optionsprofiles/lang/ru_ru.json index 4ffe547..fae0d60 100644 --- a/common/src/main/resources/assets/optionsprofiles/lang/ru_ru.json +++ b/common/src/main/resources/assets/optionsprofiles/lang/ru_ru.json @@ -8,6 +8,7 @@ "gui.optionsprofiles.profile-name-text": "Имя профиля", "gui.optionsprofiles.overwrite-options": "Перезаписать", "gui.optionsprofiles.rename-profile": "Переименовать", + "gui.optionsprofiles.keybindings-only": "Только элементы управления", "gui.optionsprofiles.delete-profile": "удалить", "modmenu.summaryTranslation.options-profiles": "Cохраняйте и загружайте профили настроек, не выходя из игры.", diff --git a/common/src/main/resources/assets/optionsprofiles/lang/tt_ru.json b/common/src/main/resources/assets/optionsprofiles/lang/tt_ru.json index 8314a93..7c1bec2 100644 --- a/common/src/main/resources/assets/optionsprofiles/lang/tt_ru.json +++ b/common/src/main/resources/assets/optionsprofiles/lang/tt_ru.json @@ -8,6 +8,7 @@ "gui.optionsprofiles.profile-name-text": "Профиль исеме", "gui.optionsprofiles.overwrite-options": "перезапись", "gui.optionsprofiles.rename-profile": "переименовывать", + "gui.optionsprofiles.keybindings-only": "Контрольләр генә", "gui.optionsprofiles.delete-profile": "бетерү", "modmenu.summaryTranslation.options-profiles": "Хәзерге көйләүләр профильләрен саклагыз һәм уеннан чыгусыз йөкләгез.", diff --git a/common/src/main/resources/assets/optionsprofiles/lang/zh_cn.json b/common/src/main/resources/assets/optionsprofiles/lang/zh_cn.json index d704b38..6387ec5 100644 --- a/common/src/main/resources/assets/optionsprofiles/lang/zh_cn.json +++ b/common/src/main/resources/assets/optionsprofiles/lang/zh_cn.json @@ -8,5 +8,6 @@ "gui.optionsprofiles.profile-name-text": "预设名", "gui.optionsprofiles.overwrite-options": "覆盖", "gui.optionsprofiles.rename-profile": "重命名", + "gui.optionsprofiles.keybindings-only": "仅按键绑定", "gui.optionsprofiles.delete-profile": "删除" }