aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/com/dulkirfabric/config/DulkirConfig.kt
blob: 2855a175bdc76114fc7164f3381623c1723201bf (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
 *   This Source Code Form is subject to the terms of the Mozilla Public
 *   License, v. 2.0. If a copy of the MPL was not distributed with this
 *   file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * If it is not possible or desirable to put the notice in a particular
 * file, then You may include the notice in a location (such as a LICENSE
 * file in a relevant directory) where a recipient would be likely to look
 * for such a notice.
 *
 * You may add additional accurate notices of copyright ownership.
 */

package com.dulkirfabric.config

import com.dulkirfabric.DulkirModFabric.mc
import com.google.gson.Gson
import me.shedaniel.cloth.clothconfig.shadowed.blue.endless.jankson.annotation.SerializedName
import me.shedaniel.clothconfig2.api.ConfigBuilder
import net.minecraft.client.gui.screen.Screen
import net.minecraft.text.LiteralTextContent
import net.minecraft.text.MutableText
import net.minecraft.text.Text
import net.minecraft.util.Formatting
import net.minecraft.util.Identifier
import java.io.File
import java.util.*
import java.util.function.Consumer
import java.util.function.Supplier

class DulkirConfig {

    private val buttonText: Text =
        MutableText.of(LiteralTextContent("Dulkir")).formatted(Formatting.BOLD, Formatting.YELLOW)
    var screen: Screen

    init {
        val builder = ConfigBuilder.create().setTitle(buttonText)
        builder.setDefaultBackgroundTexture(Identifier("minecraft:textures/block/oak_planks.png"))
        builder.setGlobalized(true)
        builder.setGlobalizedExpanded(true)
        builder.setParentScreen(mc.currentScreen)
        builder.setSavingRunnable(::saveConfig)
        val entryBuilder = builder.entryBuilder()
        val general = builder.getOrCreateCategory(Text.literal("General"))
        general.addEntry(
            entryBuilder.startBooleanToggle(Text.literal("Custom Inventory Scale Toggle"), invScaleBool)
                .setTooltip(Text.literal("WAHOO!"))
                .setSaveConsumer { newValue -> invScaleBool = newValue }
                .build()
        )
        general.addEntry(
            entryBuilder.startFloatField(Text.literal("Inventory Scale"), inventoryScale)
                .setTooltip(Text.literal("Size of GUI whenever you're in an inventory screen"))
                .setSaveConsumer { newValue -> inventoryScale = newValue }
                .build()
        )
        val shortcuts = builder.getOrCreateCategory(Text.literal("Key Shortcuts"))
        /** public NestedListListEntry(Text fieldName,
         * List<T> value,
         * boolean defaultExpanded,
         * Supplier<Optional<Text[]>> tooltipSupplier,
         * Consumer<List<T>> saveConsumer,
         * Supplier<List<T>> defaultValue,
         * Text resetButtonKey,
         * boolean deleteButtonEnabled,
         * boolean insertInFront,
         * BiFunction<T, NestedListListEntry<T, INNER>, INNER> createNewCell) {
        */
        val fieldName = Text.literal("Chat Macros")
        val defaultExpanded = false
        val tooltipSupplier: Optional<MutableText> = Optional.empty<MutableText>()
        val saveConsumer: Consumer<List<Pair<Int,Int>>> = Consumer { list -> value = list }
        val defaultValue: Supplier<List<Pair<Int, Int>>> = Supplier { listOf(Pair(1, 2), Pair(3, 4)) }
        val resetButtonKey = Text.literal("Reset Macros")
        val deleteButtonEnabled = true
        val insertInFront = true
        //val createNewCell: BiFunction<Pair<Int,Int>, NestedListListEntry<Pair<Int,Int>, INNER>, INNER>>
        //TODO

        builder.transparentBackground()
        screen = builder.build()
    }


    data class ConfigOptions(
        @SerializedName("testOption")
        val invScaleBool: Boolean,

        @SerializedName("inventoryScale")
        val inventoryScale: Float
    )

    /**
     * Object for storing all the actual config values that will be used in doing useful stuff with the config
     */
    companion object ConfigVars {
        var invScaleBool: Boolean = true
        var inventoryScale: Float = 1f
        var value: List<Pair<Int, Int>> = listOf(Pair(1, 2), Pair(3, 4))


        private fun saveConfig() {
            val gson = Gson()
            val configOptions = ConfigOptions(
                invScaleBool,
                inventoryScale)
            val json = gson.toJson(configOptions)

            val configDirectory = File(mc.runDirectory, "config")
            if (!configDirectory.exists()) {
                configDirectory.mkdir()
            }
            val configFile = File(configDirectory, "dulkirConfig.json")
            configFile.writeText(json)
        }

        fun loadConfig() {
            val gson = Gson()
            val configDir = File(mc.runDirectory, "config")
            if (!configDir.exists()) return
            val configFile = File(configDir, "dulkirConfig.json")
            if (configFile.exists()) {
                val json = configFile.readText()
                val configOptions = gson.fromJson(json, ConfigOptions::class.java)

                invScaleBool = configOptions.invScaleBool
                inventoryScale = configOptions.inventoryScale
            }
        }
    }

}