Skip to content
This repository was archived by the owner on Nov 27, 2025. It is now read-only.

Commit b25c7df

Browse files
committed
feat(menu): improve closing middleware
1 parent edcbc8a commit b25c7df

4 files changed

Lines changed: 23 additions & 5 deletions

File tree

common/src/main/kotlin/com/undefined/cassini/internal/listener/PacketHandler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import com.undefined.cassini.internal.info.PacketClickInformation
44
import com.undefined.cassini.internal.info.PacketCloseInformation
55

66
/**
7-
* Handles any Cassini menu related server bound packets and custom events (such as dialog click).
7+
* Handles any Cassini menu related serverbound packets and custom events (such as dialog click).
88
* This is overridden by the implementation of the core module.
99
*/
1010
interface PacketHandler {

core/src/main/kotlin/com/undefined/cassini/listener/PacketHandlerImpl.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,13 @@ object PacketHandlerImpl : PacketHandler, Listener {
4242
}
4343

4444
override fun onClose(closeInformation: PacketCloseInformation) {
45-
val menu = NMSManager.openMenus[closeInformation.player.uniqueId] as? ItemMenu<*> ?: return
45+
val menu = NMSManager.openMenus[closeInformation.player.uniqueId] ?: return
46+
menu.close(closeInformation.player)
4647
NMSManager.openMenus.remove(closeInformation.player.uniqueId)
47-
for (closeAction in menu.closeActions) closeAction(closeInformation.player)
48+
49+
if (menu is ItemMenu<*>) {
50+
for (closeAction in menu.closeActions) closeAction(closeInformation.player)
51+
}
4852
}
4953

5054
@Suppress("UnstableApiUsage")

core/src/main/kotlin/com/undefined/cassini/menu/Menu.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ abstract class Menu<T : Menu<T, *>, C : MenuSettings>(
3333
viewers.add(player.uniqueId)
3434
if (initialize) initialize(player)
3535
}
36-
val previousMenu = NMSManager.openMenus[player.uniqueId]
37-
previousMenu?.viewers?.remove(player.uniqueId)
3836

37+
NMSManager.openMenus[player.uniqueId]?.close(player) // previous menu
3938
NMSManager.openMenus[player.uniqueId] = this
4039
}
4140

@@ -50,6 +49,11 @@ abstract class Menu<T : Menu<T, *>, C : MenuSettings>(
5049
throw UnsupportedOperationException("Update method not available for ${this::class.simpleName}.")
5150
}
5251

52+
fun close(player: Player) {
53+
viewers.remove(player.uniqueId)
54+
onClose(player)
55+
}
56+
5357
/**
5458
* Apply a [MenuPattern] to this menu.
5559
*/
@@ -65,4 +69,7 @@ abstract class Menu<T : Menu<T, *>, C : MenuSettings>(
6569
@ApiStatus.OverrideOnly
6670
open fun initialize(player: Player) {}
6771

72+
@ApiStatus.OverrideOnly
73+
open fun onClose(player: Player) {}
74+
6875
}

server/src/main/kotlin/TestMenu.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ class TestMenu(parent: Menu<*, *>? = null) : PaginatedChestMenu(!"Change Lore",
2323
setElement(26, StaticItemElement(Material.PAPER) {
2424
next()
2525
})
26+
27+
onClose { player ->
28+
player.sendMessage("Closed action")
29+
}
2630
}
2731

32+
override fun onClose(player: Player) {
33+
player.sendMessage("onClose!")
34+
}
2835
}

0 commit comments

Comments
 (0)