aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfigScreenFactory.java
blob: d00e9b044229f5178438ba67b248188c5db21b1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;

import java.util.function.Function;

/**
 * Cloth Config integration to CIT Resewn's config
 * @see CITResewnConfig
 */
public class CITResewnConfigScreenFactory {
    /**
     * Used to get CIT Resewn - Defaults's Cloth Config implementation.
     */
    public static final String DEFAULTS_CONFIG_ENTRYPOINT = "citresewn-defaults:config_screen";

    /**
     * Creates a Cloth Config screen for the current active config instance.
     * @param parent parent to return to from the config screen
     * @return the config screen
     * @throws NoClassDefFoundError if Cloth Config is not present
     */
    public static Screen create(Screen parent) {
        CITResewnConfig currentConfig = CITResewnConfig.INSTANCE, defaultConfig = new CITResewnConfig();

        ConfigBuilder builder = ConfigBuilder.create()
                .setParentScreen(parent)
                .setTitle(Text.translatable("config.citresewn.title"))
                .setSavingRunnable(currentConfig::write);

        ConfigCategory category = builder.getOrCreateCategory(Text.empty());
        ConfigEntryBuilder entryBuilder = builder.entryBuilder();

        category.addEntry(entryBuilder.startBooleanToggle(Text.translatable("config.citresewn.enabled.title"), currentConfig.enabled)
                .setTooltip(Text.translatable("config.citresewn.enabled.tooltip"))
                .setSaveConsumer(newConfig -> {
                    if (currentConfig.enabled != newConfig) {
                        currentConfig.enabled = newConfig;
                        MinecraftClient.getInstance().reloadResources();
                    }
                })
                .setDefaultValue(defaultConfig.enabled)
                .build());

        if (FabricLoader.getInstance().isModLoaded("citresewn-defaults")) {
            class CurrentScreen { boolean prevToggle = false; } final CurrentScreen currentScreen = new CurrentScreen();
            category.addEntry(entryBuilder.startBooleanToggle(Text.translatable("config.citresewn-defaults.title"), false)
                    .setTooltip(Text.translatable("config.citresewn-defaults.tooltip"))

                    .setYesNoTextSupplier((b) -> {
                        if (b != currentScreen.prevToggle) {
                            //noinspection unchecked
                            MinecraftClient.getInstance().setScreen((Screen) FabricLoader.getInstance().getEntrypoints(DEFAULTS_CONFIG_ENTRYPOINT, Function.class).stream().findAny().orElseThrow().apply(create(parent)));

                            currentScreen.prevToggle = b;
                        }

                        return Text.translatable("config.citresewn.configure");
                    })
                    .build());
        }

        category.addEntry(entryBuilder.startBooleanToggle(Text.translatable("config.citresewn.mute_errors.title"), currentConfig.mute_errors)
                .setTooltip(Text.translatable("config.citresewn.mute_errors.tooltip"))
                .setSaveConsumer(newConfig -> currentConfig.mute_errors = newConfig)
                .setDefaultValue(defaultConfig.mute_errors)
                .build());

        category.addEntry(entryBuilder.startBooleanToggle(Text.translatable("config.citresewn.mute_warns.title"), currentConfig.mute_warns)
                .setTooltip(Text.translatable("config.citresewn.mute_warns.tooltip"))
                .setSaveConsumer(newConfig -> currentConfig.mute_warns = newConfig)
                .setDefaultValue(defaultConfig.mute_warns)
                .build());

        category.addEntry(entryBuilder.startIntSlider(Text.translatable("config.citresewn.cache_ms.title"), currentConfig.cache_ms / 50, 0, 5 * 20)
                .setTooltip(Text.translatable("config.citresewn.cache_ms.tooltip"))
                .setSaveConsumer(newConfig -> currentConfig.cache_ms = newConfig * 50)
                .setDefaultValue(defaultConfig.cache_ms / 50)
                .setTextGetter(ticks -> {
                    if (ticks <= 1)
                        return Text.translatable("config.citresewn.cache_ms.ticks." + ticks).formatted(Formatting.AQUA);

                    Formatting color = Formatting.DARK_RED;

                    if (ticks <= 40) color = Formatting.RED;
                    if (ticks <= 20) color = Formatting.GOLD;
                    if (ticks <= 10) color = Formatting.DARK_GREEN;
                    if (ticks <= 5) color = Formatting.GREEN;

                    return Text.translatable("config.citresewn.cache_ms.ticks.any", ticks).formatted(color);
                })
                .build());

        category.addEntry(entryBuilder.startBooleanToggle(Text.translatable("config.citresewn.broken_paths.title"), currentConfig.broken_paths)
                .setTooltip(Text.translatable("config.citresewn.broken_paths.tooltip"))
                .setSaveConsumer(newConfig -> currentConfig.broken_paths = newConfig)
                .setDefaultValue(defaultConfig.broken_paths)
                .requireRestart()
                .build());

        return builder.build();
    }
}