aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/io/github/moulberry/notenoughupdates/commands/help/SettingsCommand.kt
blob: 0e6fb1ffddf29f75408ae38003bb17ac1669a55d (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
134
135
136
137
138
139
140
141
142
143
144
/*
 * Copyright (C) 2023 NotEnoughUpdates contributors
 *
 * This file is part of NotEnoughUpdates.
 *
 * NotEnoughUpdates is free software: you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * NotEnoughUpdates is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
 */

package io.github.moulberry.notenoughupdates.commands.help

import io.github.moulberry.moulconfig.GuiTextures
import io.github.moulberry.moulconfig.annotations.ConfigOption
import io.github.moulberry.moulconfig.common.MyResourceLocation
import io.github.moulberry.moulconfig.gui.GuiOptionEditor
import io.github.moulberry.moulconfig.gui.GuiScreenElementWrapper
import io.github.moulberry.moulconfig.gui.MoulConfigEditor
import io.github.moulberry.moulconfig.processor.*
import io.github.moulberry.notenoughupdates.NotEnoughUpdates
import io.github.moulberry.notenoughupdates.autosubscribe.NEUAutoSubscribe
import io.github.moulberry.notenoughupdates.core.config.GuiOptionEditorBlocked
import io.github.moulberry.notenoughupdates.events.RegisterBrigadierCommandEvent
import io.github.moulberry.notenoughupdates.miscfeatures.EnforcedConfigValues
import io.github.moulberry.notenoughupdates.miscfeatures.IQTest
import io.github.moulberry.notenoughupdates.miscfeatures.updater.ConfigVersionGuiOption
import io.github.moulberry.notenoughupdates.options.NEUConfig
import io.github.moulberry.notenoughupdates.options.customtypes.ConfigVersionDisplay
import io.github.moulberry.notenoughupdates.util.brigadier.*
import net.minecraft.client.gui.GuiScreen
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import java.lang.reflect.Field

@NEUAutoSubscribe
object SettingsCommand {
    @SubscribeEvent
    fun onCommands(event: RegisterBrigadierCommandEvent) {
        event.command("neu", "neusettings") {
            thenArgument("search", RestArgumentType) { search ->
                val searchSpace = mutableListOf<String>()
                ConfigProcessorDriver.processConfig(
                    NotEnoughUpdates.INSTANCE.config.javaClass,
                    NotEnoughUpdates.INSTANCE.config,
                    object : ConfigStructureReader {
                        override fun beginCategory(p0: Any?, p1: Field?, p2: String, p3: String) {
                            searchSpace.add(p2)
                            searchSpace.add(p3)
                        }

                        override fun endCategory() {
                        }

                        override fun beginAccordion(p0: Any?, p1: Field?, p2: ConfigOption, p3: Int) {
                            searchSpace.add(p2.name)
                            searchSpace.add(p2.desc)
                        }

                        override fun endAccordion() {
                        }

                        override fun emitOption(p0: Any?, p1: Field?, p2: ConfigOption) {
                            searchSpace.add(p2.name)
                            searchSpace.add(p2.desc)
                        }

                        override fun emitGuiOverlay(p0: Any?, p1: Field?, p2: ConfigOption?) {
                        }

                    }
                )
                suggestsList(searchSpace)
                thenExecute {
                    NotEnoughUpdates.INSTANCE.openGui = createConfigScreen(this[search])
                }
            }.withHelp("Search the NEU settings")
            thenExecute {
                NotEnoughUpdates.INSTANCE.openGui = createConfigScreen("")
            }
        }.withHelp("Open the NEU settings")
    }

    class BlockingMoulConfigProcessor : MoulConfigProcessor<NEUConfig>(NotEnoughUpdates.INSTANCE.config) {
        override fun createOptionGui(
            processedOption: ProcessedOption,
            field: Field,
            option: ConfigOption
        ): GuiOptionEditor? {
            val default = super.createOptionGui(processedOption, field, option) ?: return null
            if (EnforcedConfigValues.isBlockedFromEditing(processedOption.path)) {
                return GuiOptionEditorBlocked(default)
            }
            return default
        }

        var iqTestCopy: LinkedHashMap<String, ProcessedCategory>? = null
        override fun getAllCategories(): LinkedHashMap<String, ProcessedCategory> {
            val s = super.getAllCategories()
            if (iqTestCopy == null) {
                iqTestCopy = s.clone() as LinkedHashMap<String, ProcessedCategory>
            }
            iqTestCopy!!["apiData"] = IQTest.options
            if (NotEnoughUpdates.INSTANCE.config.apiData.apiDataUnlocked) {
                return s
            }
            return iqTestCopy!!
        }
    }

    var lastEditor = null as MoulConfigEditor<NEUConfig>?
    fun createConfigScreen(search: String): GuiScreen {
        return object : GuiScreenElementWrapper(createConfigElement(search)) {
        }
    }

    fun createConfigElement(search: String): MoulConfigEditor<NEUConfig> {
        val processor = BlockingMoulConfigProcessor()
        BuiltinMoulConfigGuis.addProcessors(processor)
        processor.registerConfigEditor(ConfigVersionDisplay::class.java) { option, annotation ->
            ConfigVersionGuiOption(option)
        }
        ConfigProcessorDriver.processConfig(
            NotEnoughUpdates.INSTANCE.config.javaClass,
            NotEnoughUpdates.INSTANCE.config,
            processor
        )
        val editor = MoulConfigEditor(processor)
        editor.search(search)
        lastEditor = editor
        return editor
    }

    init {
        GuiTextures.setTextureRoot(MyResourceLocation("notenoughupdates", "core"))
    }
}