Skip to content

Commit 53de3fe

Browse files
committed
finished rename handling
Signed-off-by: illyrius666 <[email protected]>
1 parent 550d852 commit 53de3fe

3 files changed

Lines changed: 116 additions & 3 deletions

File tree

IllyriaKingdoms/src/guis/MainGui.kt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@ package org.xodium.illyriaplus.guis
22

33
import org.bukkit.Material
44
import org.bukkit.inventory.ItemStack
5+
import org.xodium.illyriaplus.Utils.MM
56
import org.xodium.illyriaplus.interfaces.GuiInterface
7+
import org.xodium.illyriaplus.pdcs.PlayerPDC.kingdom
68
import xyz.xenondevs.invui.gui.Gui
79
import xyz.xenondevs.invui.item.Item
10+
import xyz.xenondevs.invui.window.AnvilWindow
811
import xyz.xenondevs.invui.window.Window
12+
import kotlin.uuid.ExperimentalUuidApi
913

1014
/** A demo GUI showing a basic InvUI setup with a clickable dragon breath item. */
15+
@OptIn(ExperimentalUuidApi::class)
1116
internal object MainGui : GuiInterface {
1217
/** The clickable item displayed in the GUI that prints "TEST" when clicked. */
1318
private val FILLER_ITEM =
@@ -21,8 +26,14 @@ internal object MainGui : GuiInterface {
2126
Item
2227
.builder()
2328
.setItemProvider(ItemStack.of(Material.NAME_TAG))
24-
.addClickHandler { TODO("trigger rename") }
25-
.build()
29+
.addClickHandler { _, click ->
30+
AnvilWindow
31+
.builder()
32+
.setTitle(MM.deserialize("Enter New Kingdom Name"))
33+
.setTextFieldAlwaysEnabled(true)
34+
.addRenameHandler { click.player.kingdom?.displayName(MM.deserialize(it)) }
35+
.open(click.player)
36+
}.build()
2637

2738
override val gui =
2839
Gui
@@ -35,6 +46,6 @@ internal object MainGui : GuiInterface {
3546
override val window =
3647
Window
3748
.builder()
38-
.setTitle("Kingdom Menu")
49+
.setTitle("")
3950
.setUpperGui(gui)
4051
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.xodium.illyriaplus.managers
2+
3+
import org.xodium.illyriaplus.data.KingdomData
4+
import kotlin.uuid.ExperimentalUuidApi
5+
import kotlin.uuid.Uuid
6+
7+
/**
8+
* Manages all kingdoms in memory and persists them to JSON.
9+
* Provides lookup by UUID and by player.
10+
*/
11+
@OptIn(ExperimentalUuidApi::class)
12+
internal object KingdomManager {
13+
private val configManager = ConfigManager()
14+
private val kingdoms = mutableMapOf<Uuid, KingdomData>()
15+
16+
/** All loaded kingdoms mapped by their UUID. */
17+
val allKingdoms: Map<Uuid, KingdomData> get() = kingdoms.toMap()
18+
19+
init {
20+
load()
21+
}
22+
23+
/**
24+
* Gets a kingdom by its UUID.
25+
* @param id The kingdom UUID.
26+
* @return The [KingdomData] or null if not found.
27+
*/
28+
operator fun get(id: Uuid): KingdomData? = kingdoms[id]
29+
30+
/**
31+
* Creates a new kingdom and saves it.
32+
* @param kingdom The kingdom to create.
33+
*/
34+
fun create(kingdom: KingdomData) {
35+
kingdoms[kingdom.id] = kingdom
36+
save()
37+
}
38+
39+
/**
40+
* Updates an existing kingdom and saves changes.
41+
* @param kingdom The updated kingdom data.
42+
*/
43+
fun update(kingdom: KingdomData) {
44+
kingdoms[kingdom.id] = kingdom
45+
save()
46+
}
47+
48+
/**
49+
* Deletes a kingdom by UUID.
50+
* @param id The kingdom UUID to delete.
51+
*/
52+
fun delete(id: Uuid) {
53+
kingdoms.remove(id)
54+
save()
55+
}
56+
57+
private fun load() {
58+
val loaded = configManager.load("kingdoms", emptyList<KingdomData>())
59+
kingdoms.clear()
60+
loaded.forEach { kingdoms[it.id] = it }
61+
}
62+
63+
private fun save() {
64+
configManager.save("kingdoms", kingdoms.values.toList())
65+
}
66+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.xodium.illyriaplus.pdcs
2+
3+
import org.bukkit.NamespacedKey
4+
import org.bukkit.entity.Player
5+
import org.bukkit.persistence.PersistentDataType
6+
import org.xodium.illyriaplus.IllyriaKingdoms.Companion.instance
7+
import org.xodium.illyriaplus.data.KingdomData
8+
import org.xodium.illyriaplus.managers.KingdomManager
9+
import kotlin.uuid.ExperimentalUuidApi
10+
import kotlin.uuid.Uuid
11+
12+
/** Provides access to [Player]-specific persistent data. */
13+
@OptIn(ExperimentalUuidApi::class)
14+
internal object PlayerPDC {
15+
/** The [NamespacedKey] used for storing kingdom UUID. */
16+
private val KINGDOM_KEY = NamespacedKey(instance, "kingdom")
17+
18+
/**
19+
* Gets or sets a [Player]'s kingdom in their persistent data container.
20+
* Stores the UUID in PDC and syncs with [KingdomManager].
21+
* @return The [Player]'s [KingdomData], or `null` if not in a kingdom.
22+
*/
23+
var Player.kingdom: KingdomData?
24+
get() =
25+
persistentDataContainer
26+
.get(KINGDOM_KEY, PersistentDataType.STRING)
27+
?.let { KingdomManager[Uuid.parse(it)] }
28+
set(value) {
29+
if (value == null) {
30+
persistentDataContainer.remove(KINGDOM_KEY)
31+
} else {
32+
persistentDataContainer.set(KINGDOM_KEY, PersistentDataType.STRING, value.id.toString())
33+
KingdomManager.update(value)
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)