From c201f4b991eac547e22b29612139752487e4c3bd Mon Sep 17 00:00:00 2001 From: tildejustin Date: Fri, 16 Aug 2024 21:25:26 -0400 Subject: [PATCH] feature: toggle all option (#17) * upgradle: speedrunapi v1.0 * feature: option to toggle all standardsettings on or off --------- Co-authored-by: contaria <98704937+KingContaria@users.noreply.github.com> --- build.gradle | 2 +- .../StandardSettingsConfig.java | 49 ++++++++++++++++++- .../options/StandardSetting.java | 4 ++ .../assets/standardsettings/lang/en_us.json | 5 +- 4 files changed, 57 insertions(+), 3 deletions(-) diff --git a/build.gradle b/build.gradle index 8ee31dbf..9353ddcc 100644 --- a/build.gradle +++ b/build.gradle @@ -24,7 +24,7 @@ dependencies { // SpeedrunAPI // check for the latest versions at https://jitpack.io/#kingcontaria/speedrunapi - modImplementation "com.github.KingContaria:SpeedrunAPI:0fe52454e2" + modImplementation "com.github.KingContaria:SpeedrunAPI:1.0" // check for the latest versions at https://jitpack.io/#Minecraft-Java-Edition-Speedrunning/sodium modCompileOnly "com.github.Minecraft-Java-Edition-Speedrunning:sodium:7e4905f134" diff --git a/src/main/java/me/contaria/standardsettings/StandardSettingsConfig.java b/src/main/java/me/contaria/standardsettings/StandardSettingsConfig.java index c02b5b06..88f210b9 100644 --- a/src/main/java/me/contaria/standardsettings/StandardSettingsConfig.java +++ b/src/main/java/me/contaria/standardsettings/StandardSettingsConfig.java @@ -4,6 +4,9 @@ import com.mojang.blaze3d.platform.GlStateManager; import me.contaria.standardsettings.options.*; import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.screen.ConfirmScreen; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.gui.screen.ScreenTexts; import net.minecraft.client.gui.screen.options.LanguageOptionsScreen; import net.minecraft.client.gui.widget.AbstractButtonWidget; import net.minecraft.client.gui.widget.ButtonWidget; @@ -30,6 +33,7 @@ import java.io.File; import java.io.IOException; +import java.lang.reflect.Field; import java.nio.file.Files; import java.nio.file.Path; import java.util.*; @@ -56,6 +60,9 @@ public class StandardSettingsConfig implements SpeedrunConfig { public boolean toggleStandardSettings = true; + @SuppressWarnings("unused") + public boolean toggleAll = true; + @Config.Category("f3") public boolean autoF3Esc = false; @@ -261,7 +268,7 @@ private StringOptionStandardSetting register(String id, String category, Functio return this.register(new StringOptionStandardSetting(id, category, this.options, getter, setter, getText, createMainWidget)); } - private > T register(T standardSetting) { + private > T register(T standardSetting) { this.standardSettings.add(standardSetting); return standardSetting; } @@ -293,6 +300,46 @@ public Map> init() throws ReflectiveOperationException return options; } + private void toggleAll(boolean enabled) { + for (StandardSetting setting : StandardSettings.config.standardSettings) { + setting.setEnabled(enabled); + } + for (StandardSetting setting : StandardSettings.config.standardSettingsOnWorldJoin) { + setting.setEnabled(enabled); + } + } + + private void confirmToggleAll(ButtonWidget button) { + MinecraftClient client = MinecraftClient.getInstance(); + Screen screen = client.currentScreen; + client.openScreen(new ConfirmScreen( + confirmed -> { + if (confirmed) { + this.toggleAll = !this.toggleAll; + this.toggleAll(this.toggleAll); + } + client.openScreen(screen); + }, + new TranslatableText("speedrunapi.config.standardsettings.option.toggleAll"), + new TranslatableText("speedrunapi.config.standardsettings.option.toggleAll.description") + .append(" ") + .append(new TranslatableText("speedrunapi.config.standardsettings.option.toggleAll.confirm")), + ScreenTexts.PROCEED, + ScreenTexts.CANCEL + )); + } + + @Override + public @Nullable SpeedrunOption parseField(Field field, SpeedrunConfig config, String... idPrefix) { + if ("toggleAll".equals(field.getName())) { + return new SpeedrunConfigAPI.CustomOption.Builder(this, this, field, idPrefix) + .createWidget((option, innerConfig, configStorage, optionField) -> + new ButtonWidget(0, 0, 150, 20, ScreenTexts.getToggleText(!option.get()), this::confirmToggleAll) + ).build(); + } + return SpeedrunConfig.super.parseField(field, config, idPrefix); + } + @Override public void finishInitialization(SpeedrunConfigContainer container) { this.configContainer = container; diff --git a/src/main/java/me/contaria/standardsettings/options/StandardSetting.java b/src/main/java/me/contaria/standardsettings/options/StandardSetting.java index 5c222768..12c1d3e9 100644 --- a/src/main/java/me/contaria/standardsettings/options/StandardSetting.java +++ b/src/main/java/me/contaria/standardsettings/options/StandardSetting.java @@ -134,6 +134,10 @@ public boolean isEnabled() { return this.enabled; } + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + public boolean toggleEnabled() { return this.enabled = !this.enabled; } diff --git a/src/main/resources/assets/standardsettings/lang/en_us.json b/src/main/resources/assets/standardsettings/lang/en_us.json index 3852babc..26359bf4 100644 --- a/src/main/resources/assets/standardsettings/lang/en_us.json +++ b/src/main/resources/assets/standardsettings/lang/en_us.json @@ -7,7 +7,10 @@ "speedrunapi.config.standardsettings.option.language": "Language", "speedrunapi.config.standardsettings.option.pieDirectory": "Pie Directory", - "speedrunapi.config.standardsettings.option.toggleStandardSettings": "Toggle StandardSettings", + "speedrunapi.config.standardsettings.option.toggleStandardSettings": "Use StandardSettings", + "speedrunapi.config.standardsettings.option.toggleAll": "Toggle all", + "speedrunapi.config.standardsettings.option.toggleAll.description": "This will enable / disable all toggleable options.", + "speedrunapi.config.standardsettings.option.toggleAll.confirm": "If you don't want to do this or understand what this means, please press Cancel.", "speedrunapi.config.standardsettings.option.autoF3Esc": "F3 Pause on World Load", "speedrunapi.config.standardsettings.option.autoF3Esc.description": "Automatically opens the F3 Pause Screen on WorldPreview and when joining a world while the game is unfocused.\nMake sure Pause On Lost Focus is disabled for this option to work properly!", "speedrunapi.config.standardsettings.option.firstAutoF3EscDelay": "First World F3 Pause Delay",