Skip to content

Commit b67f5ab

Browse files
DSL: add custom item impl for simplicity
1 parent 6e6304d commit b67f5ab

File tree

1 file changed

+31
-6
lines changed
  • invui-kotlin/src/main/kotlin/xyz/xenondevs/invui/dsl

1 file changed

+31
-6
lines changed

invui-kotlin/src/main/kotlin/xyz/xenondevs/invui/dsl/ItemDsl.kt

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
package xyz.xenondevs.invui.dsl
44

55
import org.bukkit.entity.Player
6+
import org.bukkit.event.inventory.ClickType
7+
import xyz.xenondevs.commons.provider.Provider
68
import xyz.xenondevs.invui.Click
79
import xyz.xenondevs.invui.ExperimentalReactiveApi
810
import xyz.xenondevs.invui.dsl.property.ItemProviderDslProperty
11+
import xyz.xenondevs.invui.item.AbstractItem
912
import xyz.xenondevs.invui.item.Item
10-
import xyz.xenondevs.invui.item.setItemProvider
13+
import xyz.xenondevs.invui.item.ItemProvider
1114

1215
@ExperimentalDslApi
1316
fun item(item: ItemDsl.() -> Unit): Item =
@@ -43,10 +46,32 @@ internal class ItemDslImpl : ItemDsl {
4346
bundleSelectHandlers += handler
4447
}
4548

46-
fun build() = Item.builder().apply {
47-
setItemProvider(itemProvider)
48-
clickHandlers.forEach { addClickHandler(it) }
49-
bundleSelectHandlers.forEach { handler -> addBundleSelectHandler { player, bundleSlot -> BundleSelect(player, bundleSlot).handler() }}
50-
}.build()
49+
fun build(): Item =
50+
DslItemImpl(itemProvider, clickHandlers.toList(), bundleSelectHandlers.toList())
51+
52+
}
53+
54+
@ExperimentalDslApi
55+
private class DslItemImpl(
56+
private val itemProvider: Provider<ItemProvider>,
57+
private val clickHandlers: List<Click.() -> Unit>,
58+
private val bundleSelectHandlers: List<BundleSelect.() -> Unit>
59+
) : AbstractItem() {
60+
61+
init {
62+
itemProvider.observeWeak(this) { thisRef -> thisRef.notifyWindows() }
63+
}
64+
65+
override fun getItemProvider(viewer: Player) = itemProvider.get()
66+
67+
override fun handleClick(clickType: ClickType, player: Player, click: Click) {
68+
clickHandlers.forEach { it(click) }
69+
}
70+
71+
override fun handleBundleSelect(player: Player, bundleSlot: Int) {
72+
val bs = BundleSelect(player, bundleSlot)
73+
bundleSelectHandlers.forEach { it(bs) }
74+
}
75+
5176

5277
}

0 commit comments

Comments
 (0)