Skip to content

Commit 1d1e423

Browse files
committed
fix config load
Signed-off-by: illyrius666 <[email protected]>
1 parent 8d57839 commit 1d1e423

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.xodium.illyriaplus.data
2+
3+
import kotlinx.serialization.Serializable
4+
import kotlin.uuid.ExperimentalUuidApi
5+
6+
/**
7+
* Wrapper for serializing a collection of kingdoms.
8+
*
9+
* @property kingdoms List of all kingdoms.
10+
*/
11+
@OptIn(ExperimentalUuidApi::class)
12+
@Serializable
13+
internal data class KingdomsData(
14+
val kingdoms: List<KingdomData> = emptyList(),
15+
)

IllyriaKingdoms/src/managers/KingdomManager.kt

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,54 @@
11
package org.xodium.illyriaplus.managers
22

33
import org.xodium.illyriaplus.data.KingdomData
4+
import org.xodium.illyriaplus.data.KingdomsData
45
import kotlin.uuid.ExperimentalUuidApi
56
import kotlin.uuid.Uuid
67

78
/**
89
* Manages kingdom data storage and retrieval.
9-
* Provides in-memory storage for kingdoms with get-or-create semantics.
10+
* Provides in-memory storage for kingdoms with get-or-create semantics and JSON persistence.
1011
*/
1112
@OptIn(ExperimentalUuidApi::class)
1213
internal object KingdomManager {
14+
private val configManager = ConfigManager()
1315
private val kingdoms = mutableMapOf<Uuid, KingdomData>()
1416

17+
init {
18+
load()
19+
}
20+
21+
/**
22+
* Loads kingdoms from disk into memory.
23+
*/
24+
fun load() {
25+
val data = configManager.load("kingdoms", KingdomsData())
26+
27+
kingdoms.clear()
28+
kingdoms.putAll(data.kingdoms.associateBy { it.id })
29+
}
30+
31+
/**
32+
* Saves all kingdoms to disk.
33+
*/
34+
fun save() {
35+
configManager.save("kingdoms", KingdomsData(kingdoms.values.toList()))
36+
}
37+
1538
/**
1639
* Gets an existing kingdom by ID, or creates a new one if it doesn't exist.
40+
* Automatically saves to disk when creating a new kingdom.
1741
*
1842
* @param id The UUID of the kingdom to get or create.
1943
* @return The existing or newly created [KingdomData].
2044
*/
2145
fun getOrCreate(id: Uuid): KingdomData =
2246
kingdoms.getOrPut(id) {
23-
KingdomData(id = id, name = "<gradient:#FFA751:#FFE259>Kingdom</gradient>", members = emptyList())
47+
KingdomData(
48+
id = id,
49+
name = "<gradient:#FFA751:#FFE259>Kingdom</gradient>",
50+
members = emptyList(),
51+
).also { save() }
2452
}
2553

2654
/**

0 commit comments

Comments
 (0)