|
1 | 1 | package btw.lowercase.optiboxes.config; |
2 | 2 |
|
3 | | -import com.google.gson.FieldNamingPolicy; |
4 | | -import com.google.gson.Gson; |
5 | | -import com.google.gson.GsonBuilder; |
| 3 | +import btw.lowercase.optiboxes.OptiBoxesClient; |
| 4 | +import dev.isxander.yacl3.api.ConfigCategory; |
| 5 | +import dev.isxander.yacl3.api.Option; |
| 6 | +import dev.isxander.yacl3.api.OptionDescription; |
| 7 | +import dev.isxander.yacl3.api.YetAnotherConfigLib; |
| 8 | +import dev.isxander.yacl3.api.controller.TickBoxControllerBuilder; |
| 9 | +import dev.isxander.yacl3.config.v2.api.ConfigClassHandler; |
| 10 | +import dev.isxander.yacl3.config.v2.api.serializer.GsonConfigSerializerBuilder; |
6 | 11 | import net.fabricmc.loader.api.FabricLoader; |
7 | | -import org.slf4j.Logger; |
8 | | -import org.slf4j.LoggerFactory; |
9 | | - |
10 | | -import java.io.File; |
11 | | -import java.io.FileReader; |
12 | | -import java.io.FileWriter; |
13 | | -import java.io.IOException; |
14 | | -import java.lang.reflect.Modifier; |
| 12 | +import net.minecraft.client.Minecraft; |
| 13 | +import net.minecraft.client.gui.screens.Screen; |
| 14 | +import net.minecraft.network.chat.Component; |
15 | 15 |
|
16 | 16 | public class OptiBoxesConfig { |
17 | | - private static final Logger logger = LoggerFactory.getLogger("OptiBoxes Config"); |
18 | | - private static final Gson GSON = new GsonBuilder() |
19 | | - .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) |
20 | | - .setPrettyPrinting() |
21 | | - .excludeFieldsWithModifiers(Modifier.PRIVATE) |
22 | | - .create(); |
23 | | - |
24 | | - public static final OptiBoxesConfig INSTANCE = load(FabricLoader.getInstance().getConfigDir().resolve("optiboxes.json").toFile()); |
| 17 | + private static final ConfigClassHandler<OptiBoxesConfig> CONFIG = |
| 18 | + ConfigClassHandler.createBuilder(OptiBoxesConfig.class) |
| 19 | + .serializer(config -> |
| 20 | + GsonConfigSerializerBuilder.create(config) |
| 21 | + .setPath(FabricLoader.getInstance().getConfigDir().resolve(OptiBoxesClient.MOD_ID + ".json")) |
| 22 | + .build() |
| 23 | + ).build(); |
| 24 | + |
| 25 | + public boolean enabled = true; |
25 | 26 | public boolean processOptiFine = true; |
26 | 27 | public boolean processMCPatcher = false; |
27 | | - private File file; |
28 | | - |
29 | | - public static OptiBoxesConfig load(File file) { |
30 | | - OptiBoxesConfig config; |
31 | | - if (file.exists()) { |
32 | | - try (FileReader reader = new FileReader(file)) { |
33 | | - config = GSON.fromJson(reader, OptiBoxesConfig.class); |
34 | | - } catch (Exception e) { |
35 | | - logger.error("Could not parse config, falling back to defaults!", e); |
36 | | - config = new OptiBoxesConfig(); |
37 | | - } |
38 | | - } else { |
39 | | - config = new OptiBoxesConfig(); |
40 | | - } |
41 | | - |
42 | | - config.file = file; |
43 | | - config.writeChanges(); |
44 | | - return config; |
| 28 | + public boolean useNewSunriseRendering = false; |
| 29 | + |
| 30 | + public static Screen getConfigScreen(Screen parent) { |
| 31 | + return YetAnotherConfigLib.create(CONFIG, (defaults, config, builder) -> { |
| 32 | + builder.title(Component.translatable("options.optiboxes.title")); |
| 33 | + |
| 34 | + ConfigCategory.Builder category = ConfigCategory.createBuilder(); |
| 35 | + category.name(Component.translatable("options.optiboxes.title")); |
| 36 | + Minecraft minecraft = Minecraft.getInstance(); |
| 37 | + category.option(Option.<Boolean>createBuilder() |
| 38 | + .name(Component.translatable("options.optiboxes.enabled")) |
| 39 | + .description(OptionDescription.of(Component.translatable("options.optiboxes.enabled.tooltip"))) |
| 40 | + .binding(defaults.enabled, () -> config.enabled, (newVal) -> { |
| 41 | + config.enabled = newVal; |
| 42 | + minecraft.reloadResourcePacks(); |
| 43 | + }) |
| 44 | + .controller(TickBoxControllerBuilder::create) |
| 45 | + .build()); |
| 46 | + category.option(Option.<Boolean>createBuilder() |
| 47 | + .name(Component.translatable("options.optiboxes.process_optifine")) |
| 48 | + .description(OptionDescription.of(Component.translatable("options.optiboxes.process_optifine.tooltip"))) |
| 49 | + .binding(defaults.processOptiFine, () -> config.processOptiFine, (newVal) -> { |
| 50 | + config.processOptiFine = newVal; |
| 51 | + minecraft.reloadResourcePacks(); |
| 52 | + }) |
| 53 | + .controller(TickBoxControllerBuilder::create) |
| 54 | + .build()); |
| 55 | + category.option(Option.<Boolean>createBuilder() |
| 56 | + .name(Component.translatable("options.optiboxes.process_mcpatcher")) |
| 57 | + .description(OptionDescription.of(Component.translatable("options.optiboxes.process_mcpatcher.tooltip"))) |
| 58 | + .binding(defaults.processMCPatcher, () -> config.processMCPatcher, (newVal) -> { |
| 59 | + config.processMCPatcher = newVal; |
| 60 | + minecraft.reloadResourcePacks(); |
| 61 | + }) |
| 62 | + .controller(TickBoxControllerBuilder::create) |
| 63 | + .build()); |
| 64 | + category.option(Option.<Boolean>createBuilder() |
| 65 | + .name(Component.translatable("options.optiboxes.use_new_sunrise_rendering")) |
| 66 | + .description(OptionDescription.of(Component.translatable("options.optiboxes.use_new_sunrise_rendering.tooltip"))) |
| 67 | + .binding(defaults.useNewSunriseRendering, () -> config.useNewSunriseRendering, (newVal) -> config.useNewSunriseRendering = newVal) |
| 68 | + .controller(TickBoxControllerBuilder::create) |
| 69 | + .build()); |
| 70 | + builder.category(category.build()); |
| 71 | + |
| 72 | + return builder; |
| 73 | + }).generateScreen(parent); |
| 74 | + } |
| 75 | + |
| 76 | + public static void load() { |
| 77 | + CONFIG.load(); |
45 | 78 | } |
46 | 79 |
|
47 | | - public void writeChanges() { |
48 | | - File dir = this.file.getParentFile(); |
49 | | - if (!dir.exists()) { |
50 | | - if (!dir.mkdirs()) { |
51 | | - throw new RuntimeException("Could not create parent directories"); |
52 | | - } |
53 | | - } else if (!dir.isDirectory()) { |
54 | | - throw new RuntimeException("The parent file is not a directory"); |
55 | | - } |
56 | | - |
57 | | - try (FileWriter writer = new FileWriter(this.file)) { |
58 | | - GSON.toJson(this, writer); |
59 | | - } catch (IOException e) { |
60 | | - throw new RuntimeException("Could not save configuration file", e); |
61 | | - } |
| 80 | + public static OptiBoxesConfig instance() { |
| 81 | + return CONFIG.instance(); |
62 | 82 | } |
63 | 83 | } |
0 commit comments