@@ -2,8 +2,8 @@ package settingdust.modsets.game
2
2
3
3
import dev.isxander.yacl3.api.*
4
4
import dev.isxander.yacl3.api.controller.StringControllerBuilder
5
- import kotlinx.coroutines.flow.MutableSharedFlow
6
- import kotlinx.coroutines.flow.asSharedFlow
5
+ import kotlinx.coroutines.delay
6
+ import kotlinx.coroutines.flow.*
7
7
import kotlinx.coroutines.runBlocking
8
8
import kotlinx.serialization.ExperimentalSerializationApi
9
9
import kotlinx.serialization.json.Json
@@ -16,22 +16,19 @@ import net.minecraft.network.chat.Component
16
16
import settingdust.kinecraft.serialization.ComponentSerializer
17
17
import settingdust.kinecraft.serialization.GsonElementSerializer
18
18
import settingdust.modsets.ModSets
19
- import settingdust.modsets.ModSetsConfig
20
19
import settingdust.modsets.PlatformHelper
21
20
import settingdust.modsets.config
22
21
import kotlin.io.path.*
23
22
24
23
@OptIn(ExperimentalSerializationApi ::class )
25
24
@Deprecated(" Use ModSets.rules instead" , ReplaceWith (" ModSets.rules" ))
26
- object Rules : MutableMap<String, RuleSet> by mutableMapOf () {
25
+ object Rules : MutableMap<String, RuleSet> by hashMapOf () {
27
26
private val configDir = PlatformHelper .configDir / " modsets"
28
27
29
- val modSets = mutableMapOf<String , ModSet >()
30
- private val _ModSetsRegisterCallback =
31
- MutableSharedFlow <Unit >()
32
- val ModSetsRegisterCallback = _ModSetsRegisterCallback .asSharedFlow()
28
+ val modSets = hashMapOf<String , ModSet >()
29
+ val ModSetsRegisterCallback = WaitedSharedFlow <Unit >()
33
30
34
- private val definedModSets = mutableMapOf <String , ModSet >()
31
+ private val definedModSets = hashMapOf <String , ModSet >()
35
32
private val modSetsPath = configDir / " modsets.json"
36
33
37
34
private val rulesDir = configDir / " rules"
@@ -51,29 +48,29 @@ object Rules : MutableMap<String, RuleSet> by mutableMapOf() {
51
48
52
49
private val config: YetAnotherConfigLib
53
50
get() {
54
- runBlocking { load() }
51
+ load()
55
52
val builder = YetAnotherConfigLib .createBuilder().title(Component .translatable(" modsets.name" ))
56
- if (ModSetsConfig .common.displayModSetsScreen && modSets.isNotEmpty()) {
53
+ if (ModSets .config .common.displayModSetsScreen && modSets.isNotEmpty()) {
57
54
builder.category(
58
55
ConfigCategory .createBuilder().apply {
59
56
name(Component .translatable(" modsets.name" ))
60
57
tooltip(Component .translatable(" modsets.description" ))
61
58
groups(
62
- modSets.map { modSet ->
59
+ modSets.map { (name, modSet) ->
63
60
ListOption .createBuilder<String >()
64
61
.apply {
65
- name(modSet.value. text)
66
- modSet.value. description?.let { description(OptionDescription .of(it)) }
62
+ name(modSet.text)
63
+ modSet.description?.let { description(OptionDescription .of(it)) }
67
64
initial(" " )
68
65
collapsed(true )
69
66
controller { StringControllerBuilder .create(it) }
70
67
binding(
71
- Binding .generic(modSet.value. mods.toMutableList(), {
72
- modSet.value. mods.toMutableList()
68
+ Binding .generic(modSet.mods.toMutableList(), {
69
+ modSet.mods.toMutableList()
73
70
}) {
74
- modSet.value. mods.clear()
75
- modSet.value. mods.addAll(it)
76
- definedModSets[modSet.key ] = modSet.value
71
+ modSet.mods.clear()
72
+ modSet.mods.addAll(it)
73
+ definedModSets[name ] = modSet
77
74
},
78
75
)
79
76
}
@@ -101,11 +98,9 @@ object Rules : MutableMap<String, RuleSet> by mutableMapOf() {
101
98
for (option in options) {
102
99
option.addListener { _, _ ->
103
100
var needSave = false
104
- for (currentOption in options.filter { it != option }) {
105
- if (currentOption.changed()) {
106
- needSave = true
107
- (currentOption as Option <Any >).requestSet(currentOption.binding().value)
108
- }
101
+ options.filter { it != option && it.changed() }.forEach {
102
+ needSave = true
103
+ (it as Option <Any >).requestSet(it.binding().value)
109
104
}
110
105
if (option.changed()) {
111
106
(option as Option <Any >).requestSet(option.binding().value)
@@ -126,10 +121,10 @@ object Rules : MutableMap<String, RuleSet> by mutableMapOf() {
126
121
}
127
122
128
123
init {
129
- runBlocking { load() }
124
+ load()
130
125
}
131
126
132
- private suspend fun load () {
127
+ private fun load () {
133
128
ModSets .config.load()
134
129
try {
135
130
configDir.createDirectories()
@@ -145,7 +140,7 @@ object Rules : MutableMap<String, RuleSet> by mutableMapOf() {
145
140
definedModSets.putAll(json.decodeFromStream(it))
146
141
}
147
142
modSets.putAll(definedModSets)
148
- _ModSetsRegisterCallback .emit(Unit )
143
+ runBlocking { ModSetsRegisterCallback .emit(Unit )}
149
144
150
145
clear()
151
146
rulesDir.listDirectoryEntries(" *.json" ).forEach {
0 commit comments