From c48e23a8949eaa89d06908201357b53fa7eaedbe Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Fri, 21 Jan 2022 15:50:24 +0200 Subject: Added config/screen for defaults --- .../defaults/config/CITResewnDefaultsConfig.java | 49 ++++++++++++++++++ .../CITResewnDefaultsConfigScreenFactory.java | 27 ++++++++++ .../defaults/config/CITResewnDefaultsModMenu.java | 25 +++++++++ .../main/java/shcm/shsupercm/util/logic/Loops.java | 60 ++++++++++++++++++++++ .../assets/citresewn-defaults/lang/en_us.json | 4 ++ defaults/src/main/resources/fabric.mod.json | 8 ++- .../config/CITResewnConfigScreenFactory.java | 21 +++++++- .../fabric/citresewn/config/CITResewnModMenu.java | 9 +--- src/main/java/shcm/shsupercm/util/logic/Loops.java | 60 ---------------------- .../resources/assets/citresewn/lang/en_us.json | 4 +- 10 files changed, 194 insertions(+), 73 deletions(-) create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsConfig.java create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsConfigScreenFactory.java create mode 100644 defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsModMenu.java create mode 100644 defaults/src/main/java/shcm/shsupercm/util/logic/Loops.java create mode 100644 defaults/src/main/resources/assets/citresewn-defaults/lang/en_us.json delete mode 100644 src/main/java/shcm/shsupercm/util/logic/Loops.java diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsConfig.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsConfig.java new file mode 100644 index 0000000..0787fba --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsConfig.java @@ -0,0 +1,49 @@ +package shcm.shsupercm.fabric.citresewn.defaults.config; + +import com.google.gson.Gson; +import com.google.gson.stream.JsonWriter; +import org.apache.commons.io.IOUtils; +import shcm.shsupercm.fabric.citresewn.CITResewn; + +import java.io.*; + +public class CITResewnDefaultsConfig { + + + private static final File FILE = new File("config/citresewn-defaults.json"); + + public static final CITResewnDefaultsConfig INSTANCE = read(); + + public static CITResewnDefaultsConfig read() { + if (!FILE.exists()) + return new CITResewnDefaultsConfig().write(); + + Reader reader = null; + try { + return new Gson().fromJson(reader = new FileReader(FILE), CITResewnDefaultsConfig.class); + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException(e); + } finally { + IOUtils.closeQuietly(reader); + } + } + + public CITResewnDefaultsConfig write() { + Gson gson = new Gson(); + JsonWriter writer = null; + try { + writer = gson.newJsonWriter(new FileWriter(FILE)); + writer.setIndent(" "); + + gson.toJson(gson.toJsonTree(this, CITResewnDefaultsConfig.class), writer); + } catch (Exception e) { + CITResewn.LOG.error("Couldn't save defaults config"); + e.printStackTrace(); + throw new RuntimeException(e); + } finally { + IOUtils.closeQuietly(writer); + } + return this; + } +} diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsConfigScreenFactory.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsConfigScreenFactory.java new file mode 100644 index 0000000..889c465 --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsConfigScreenFactory.java @@ -0,0 +1,27 @@ +package shcm.shsupercm.fabric.citresewn.defaults.config; + +import me.shedaniel.clothconfig2.api.ConfigBuilder; +import me.shedaniel.clothconfig2.api.ConfigCategory; +import me.shedaniel.clothconfig2.api.ConfigEntryBuilder; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.text.LiteralText; +import net.minecraft.text.TranslatableText; + +public class CITResewnDefaultsConfigScreenFactory { + public static Screen create(Screen parent) { + CITResewnDefaultsConfig currentConfig = CITResewnDefaultsConfig.INSTANCE, defaultConfig = new CITResewnDefaultsConfig(); + + ConfigBuilder builder = ConfigBuilder.create() + .setParentScreen(parent) + .setTitle(new TranslatableText("config.citresewn.defaults.title")) + .setSavingRunnable(currentConfig::write); + + ConfigCategory category = builder.getOrCreateCategory(new LiteralText("")); + ConfigEntryBuilder entryBuilder = builder.entryBuilder(); + + + + return builder.build(); + } +} diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsModMenu.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsModMenu.java new file mode 100644 index 0000000..cba1592 --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/config/CITResewnDefaultsModMenu.java @@ -0,0 +1,25 @@ +package shcm.shsupercm.fabric.citresewn.defaults.config; + +import com.terraformersmc.modmenu.api.ConfigScreenFactory; +import com.terraformersmc.modmenu.api.ModMenuApi; +import net.fabricmc.loader.api.FabricLoader; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.screen.NoticeScreen; +import net.minecraft.text.Text; + +public class CITResewnDefaultsModMenu implements ModMenuApi { + @Override + public ConfigScreenFactory getModConfigScreenFactory() { + if (FabricLoader.getInstance().isModLoaded("cloth-config2")) + return new ClothConfigOpenImpl().getModConfigScreenFactory(); + + return parent -> new NoticeScreen(() -> MinecraftClient.getInstance().setScreen(parent), Text.of("CIT Resewn: Defaults"), Text.of("CIT Resewn requires Cloth Config to be able to show the config.")); + } + + private static class ClothConfigOpenImpl implements ModMenuApi { + @Override + public ConfigScreenFactory getModConfigScreenFactory() { + return CITResewnDefaultsConfigScreenFactory::create; + } + } +} diff --git a/defaults/src/main/java/shcm/shsupercm/util/logic/Loops.java b/defaults/src/main/java/shcm/shsupercm/util/logic/Loops.java new file mode 100644 index 0000000..6c18244 --- /dev/null +++ b/defaults/src/main/java/shcm/shsupercm/util/logic/Loops.java @@ -0,0 +1,60 @@ +package shcm.shsupercm.util.logic; + +import java.util.*; + +/** + * This class(or class portion) is a part of SHCM's utilities. Feel free to use without credit. + */ +public class Loops { + /** + * Creates a loop of T with linked intensities allowing for fading between the elements. + * @param items list of items and pause durations(in time units) ordered as they are in the loop + * @param fade time in units to fade between each item + * @param ticks positive raising counter + * @param tpu the amount of ticks per time unit + * @param element type + * @return map of elements and their respective intensities(between 0.0f and 1.0f) + */ + public static Map statelessFadingLoop(List> items, float fade, int ticks, int tpu) { + Map itemValues = new HashMap<>(); + + if (items == null || items.size() == 0) + return itemValues; + + if (items.size() == 1) { + itemValues.put(items.get(0).getKey(), 1f); + return itemValues; + } + + float totalUnitsInLoop = 0f; + for (Map.Entry item : items) { + itemValues.put(item.getKey(), 0f); + totalUnitsInLoop += item.getValue() + fade; + } + + float unitInLoop = (ticks % (tpu * totalUnitsInLoop)) / tpu; + + for (int i = 0; i < items.size(); i++) { + Map.Entry item = items.get(i); + if (unitInLoop < item.getValue()) { + itemValues.put(item.getKey(), 1f); + break; + } else + unitInLoop -= item.getValue(); + + if (unitInLoop < fade) { + Map.Entry nextItem = items.get(i + 1 >= items.size() ? 0 : i + 1); + + unitInLoop /= fade; + + itemValues.put(item.getKey(), 1f - unitInLoop); + itemValues.put(nextItem.getKey(), unitInLoop); + + break; + } else + unitInLoop -= fade; + } + + return itemValues; + } +} \ No newline at end of file diff --git a/defaults/src/main/resources/assets/citresewn-defaults/lang/en_us.json b/defaults/src/main/resources/assets/citresewn-defaults/lang/en_us.json new file mode 100644 index 0000000..72784ed --- /dev/null +++ b/defaults/src/main/resources/assets/citresewn-defaults/lang/en_us.json @@ -0,0 +1,4 @@ +{ + "config.citresewn.defaults.title": "CIT Resewn: Defaults", + "config.citresewn.defaults.tooltip": "Go to the defaults' config menu" +} \ No newline at end of file diff --git a/defaults/src/main/resources/fabric.mod.json b/defaults/src/main/resources/fabric.mod.json index 004b7a8..0f8e566 100644 --- a/defaults/src/main/resources/fabric.mod.json +++ b/defaults/src/main/resources/fabric.mod.json @@ -2,7 +2,7 @@ "schemaVersion": 1, "id": "citresewn-defaults", "version": "${version}", - "name": "CIT Resewn - Defaults", + "name": "CIT Resewn: Defaults", "description": "Default types and conditions for CIT Resewn", "authors": [ "SHsuperCM" @@ -12,6 +12,12 @@ "environment": "client", "entrypoints": { + "modmenu": [ + "shcm.shsupercm.fabric.citresewn.defaults.config.CITResewnDefaultsModMenu" + ], + "citresewn-defaults:config_screen": [ + "shcm.shsupercm.fabric.citresewn.defaults.config.CITResewnDefaultsConfigScreenFactory::create" + ] }, "accessWidener" : "citresewn-defaults.accesswidener", "mixins": [ diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfigScreenFactory.java b/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfigScreenFactory.java index 33f4950..57ae2c3 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfigScreenFactory.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfigScreenFactory.java @@ -3,12 +3,16 @@ package shcm.shsupercm.fabric.citresewn.config; import me.shedaniel.clothconfig2.api.ConfigBuilder; import me.shedaniel.clothconfig2.api.ConfigCategory; import me.shedaniel.clothconfig2.api.ConfigEntryBuilder; +import net.fabricmc.loader.api.FabricLoader; +import net.fabricmc.loader.api.entrypoint.EntrypointContainer; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.Screen; import net.minecraft.text.LiteralText; import net.minecraft.text.TranslatableText; import net.minecraft.util.Formatting; +import java.util.function.Function; + public class CITResewnConfigScreenFactory { public static Screen create(Screen parent) { CITResewnConfig currentConfig = CITResewnConfig.INSTANCE, defaultConfig = new CITResewnConfig(); @@ -32,6 +36,21 @@ public class CITResewnConfigScreenFactory { .setDefaultValue(defaultConfig.enabled) .build()); + class CurrentScreen { Screen screen; boolean prevToggle = false; } final CurrentScreen currentScreen = new CurrentScreen(); + category.addEntry(entryBuilder.startBooleanToggle(new TranslatableText("config.citresewn.defaults.title"), false) + .setTooltip(new TranslatableText("config.citresewn.defaults.tooltip")) + .setYesNoTextSupplier((b) -> { + if (b != currentScreen.prevToggle) { + //noinspection unchecked + MinecraftClient.getInstance().setScreen((Screen) FabricLoader.getInstance().getEntrypoints("citresewn-defaults:config_screen", Function.class).stream().findAny().orElseThrow().apply(currentScreen.screen)); + + currentScreen.prevToggle = b; + } + + return new TranslatableText("config.citresewn.configure"); + }) + .build()); + category.addEntry(entryBuilder.startBooleanToggle(new TranslatableText("config.citresewn.mute_errors.title"), currentConfig.mute_errors) .setTooltip(new TranslatableText("config.citresewn.mute_errors.tooltip")) .setSaveConsumer(newConfig -> currentConfig.mute_errors = newConfig) @@ -70,6 +89,6 @@ public class CITResewnConfigScreenFactory { .requireRestart() .build()); - return builder.build(); + return currentScreen.screen = builder.build(); } } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnModMenu.java b/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnModMenu.java index cf6d9bd..378350a 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnModMenu.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnModMenu.java @@ -11,15 +11,8 @@ public class CITResewnModMenu implements ModMenuApi { @Override public ConfigScreenFactory getModConfigScreenFactory() { if (FabricLoader.getInstance().isModLoaded("cloth-config2")) - return new ClothConfigOpenImpl().getModConfigScreenFactory(); + return CITResewnConfigScreenFactory::create; return parent -> new NoticeScreen(() -> MinecraftClient.getInstance().setScreen(parent), Text.of("CIT Resewn"), Text.of("CIT Resewn requires Cloth Config to be able to show the config.")); } - - private static class ClothConfigOpenImpl implements ModMenuApi { - @Override - public ConfigScreenFactory getModConfigScreenFactory() { - return CITResewnConfigScreenFactory::create; - } - } } diff --git a/src/main/java/shcm/shsupercm/util/logic/Loops.java b/src/main/java/shcm/shsupercm/util/logic/Loops.java deleted file mode 100644 index 6c18244..0000000 --- a/src/main/java/shcm/shsupercm/util/logic/Loops.java +++ /dev/null @@ -1,60 +0,0 @@ -package shcm.shsupercm.util.logic; - -import java.util.*; - -/** - * This class(or class portion) is a part of SHCM's utilities. Feel free to use without credit. - */ -public class Loops { - /** - * Creates a loop of T with linked intensities allowing for fading between the elements. - * @param items list of items and pause durations(in time units) ordered as they are in the loop - * @param fade time in units to fade between each item - * @param ticks positive raising counter - * @param tpu the amount of ticks per time unit - * @param element type - * @return map of elements and their respective intensities(between 0.0f and 1.0f) - */ - public static Map statelessFadingLoop(List> items, float fade, int ticks, int tpu) { - Map itemValues = new HashMap<>(); - - if (items == null || items.size() == 0) - return itemValues; - - if (items.size() == 1) { - itemValues.put(items.get(0).getKey(), 1f); - return itemValues; - } - - float totalUnitsInLoop = 0f; - for (Map.Entry item : items) { - itemValues.put(item.getKey(), 0f); - totalUnitsInLoop += item.getValue() + fade; - } - - float unitInLoop = (ticks % (tpu * totalUnitsInLoop)) / tpu; - - for (int i = 0; i < items.size(); i++) { - Map.Entry item = items.get(i); - if (unitInLoop < item.getValue()) { - itemValues.put(item.getKey(), 1f); - break; - } else - unitInLoop -= item.getValue(); - - if (unitInLoop < fade) { - Map.Entry nextItem = items.get(i + 1 >= items.size() ? 0 : i + 1); - - unitInLoop /= fade; - - itemValues.put(item.getKey(), 1f - unitInLoop); - itemValues.put(nextItem.getKey(), unitInLoop); - - break; - } else - unitInLoop -= fade; - } - - return itemValues; - } -} \ No newline at end of file diff --git a/src/main/resources/assets/citresewn/lang/en_us.json b/src/main/resources/assets/citresewn/lang/en_us.json index 2f90a92..3164cb5 100644 --- a/src/main/resources/assets/citresewn/lang/en_us.json +++ b/src/main/resources/assets/citresewn/lang/en_us.json @@ -1,5 +1,6 @@ { "config.citresewn.title": "CIT Resewn Configuration", + "config.citresewn.configure": "Configure", "config.citresewn.enabled.title": "Enabled", "config.citresewn.enabled.tooltip": "Should CIT Resewn load and inject cits from resourcepacks", @@ -10,9 +11,6 @@ "config.citresewn.mute_warns.title": "Mute loading warnings", "config.citresewn.mute_warns.tooltip": "Should CIT Resewn not log cit warnings", - "config.citresewn.citenchantment_scroll_multiplier.title": "CIT Enchantment: Scroll Multiplier", - "config.citresewn.citenchantment_scroll_multiplier.tooltip": "General multiplier for the scroll speed of the glint textures added by enchantment CITs.", - "config.citresewn.broken_paths.title": "Allow broken paths in resourcepacks", "config.citresewn.broken_paths.tooltip": "Allows packs with illegal resource paths to load.\nMay cause issues!", -- cgit