Skip to content

Commit 7646e65

Browse files
DSL: change receivers of onXY functions to dsl-marked interfaces
1 parent 9e4ec76 commit 7646e65

File tree

4 files changed

+125
-35
lines changed

4 files changed

+125
-35
lines changed

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,21 @@ import xyz.xenondevs.invui.window.CraftingWindow
1212
fun craftingWindow(viewer: Player, craftingWindow: CraftingWindowDsl.() -> Unit): CraftingWindow =
1313
CraftingWindowDslImpl(viewer).apply(craftingWindow).build()
1414

15+
@WindowDslMarker
1516
@ExperimentalDslApi
16-
class RecipeClick internal constructor(val recipeKey: Key)
17+
interface RecipeClickDsl {
18+
19+
val recipeKey: Key
20+
21+
}
1722

1823
@ExperimentalDslApi
1924
sealed interface CraftingWindowDsl : SplitWindowDsl {
2025

2126
val craftingGui: GuiDslProperty
2227
val resultGui: GuiDslProperty
2328

24-
fun onRecipeClick(handler: RecipeClick.() -> Unit)
29+
fun onRecipeClick(handler: RecipeClickDsl.() -> Unit)
2530

2631
}
2732

@@ -32,9 +37,9 @@ internal class CraftingWindowDslImpl(
3237

3338
override val craftingGui = GuiDslProperty(3, 3)
3439
override val resultGui = GuiDslProperty(1, 1)
35-
private val recipeClickHandlers = mutableListOf<RecipeClick.() -> Unit>()
40+
private val recipeClickHandlers = mutableListOf<RecipeClickDsl.() -> Unit>()
3641

37-
override fun onRecipeClick(handler: RecipeClick.() -> Unit) {
42+
override fun onRecipeClick(handler: RecipeClickDsl.() -> Unit) {
3843
recipeClickHandlers += handler
3944
}
4045

@@ -45,8 +50,13 @@ internal class CraftingWindowDslImpl(
4550
builder.apply {
4651
setCraftingGui(craftingGui.value)
4752
setResultGui(resultGui.value)
48-
recipeClickHandlers.forEach { handler -> addRecipeClickHandler { RecipeClick(it).handler() } }
53+
for (handler in recipeClickHandlers) {
54+
addRecipeClickHandler { RecipeClickDslImpl(it).handler() }
55+
}
4956
}
5057
}
5158

52-
}
59+
}
60+
61+
@ExperimentalDslApi
62+
internal class RecipeClickDslImpl(override val recipeKey: Key) : RecipeClickDsl

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ sealed interface FurnaceWindowDsl : SplitWindowDsl {
2222
val cookProgress: ProviderDslProperty<Double>
2323
val burnProgress: ProviderDslProperty<Double>
2424

25-
fun onRecipeClick(handler: RecipeClick.() -> Unit)
25+
fun onRecipeClick(handler: RecipeClickDsl.() -> Unit)
2626

2727
}
2828

@@ -35,9 +35,9 @@ internal class FurnaceWindowDslImpl(
3535
override val resultGui = GuiDslProperty(1, 1)
3636
override val cookProgress = ProviderDslProperty(0.0)
3737
override val burnProgress = ProviderDslProperty(0.0)
38-
private val recipeClickHandlers = mutableListOf<RecipeClick.() -> Unit>()
38+
private val recipeClickHandlers = mutableListOf<RecipeClickDsl.() -> Unit>()
3939

40-
override fun onRecipeClick(handler: RecipeClick.() -> Unit) {
40+
override fun onRecipeClick(handler: RecipeClickDsl.() -> Unit) {
4141
recipeClickHandlers += handler
4242
}
4343

@@ -50,7 +50,9 @@ internal class FurnaceWindowDslImpl(
5050
setResultGui(resultGui.value)
5151
setCookProgress(cookProgress)
5252
setBurnProgress(burnProgress)
53-
recipeClickHandlers.forEach { handler -> addRecipeClickHandler { RecipeClick(it).handler() } }
53+
for (handler in recipeClickHandlers) {
54+
addRecipeClickHandler { RecipeClickDslImpl(it).handler() }
55+
}
5456
}
5557
}
5658

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

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,49 @@ import xyz.xenondevs.invui.item.ItemProvider
1616
fun item(item: ItemDsl.() -> Unit): Item =
1717
ItemDslImpl().apply(item).build()
1818

19+
@ItemDslMarker
20+
@ExperimentalDslApi
21+
sealed interface ClickDsl {
22+
23+
val clickType: ClickType
24+
val player: Player
25+
val hotbarButton: Int
26+
27+
}
28+
29+
@ItemDslMarker
1930
@ExperimentalDslApi
20-
class BundleSelect internal constructor(val player: Player, val bundleSlot: Int)
31+
sealed interface BundleSelectDsl {
32+
33+
val player: Player
34+
val bundleSlot: Int
35+
36+
}
2137

2238
@ItemDslMarker
2339
@ExperimentalDslApi
2440
sealed interface ItemDsl {
2541

2642
val itemProvider: ItemProviderDslProperty
2743

28-
fun onClick(handler: Click.() -> Unit)
44+
fun onClick(handler: ClickDsl.() -> Unit)
2945

30-
fun onBundleSelect(handler: BundleSelect.() -> Unit)
46+
fun onBundleSelect(handler: BundleSelectDsl.() -> Unit)
3147

3248
}
3349

3450
@ExperimentalDslApi
3551
internal class ItemDslImpl : ItemDsl {
3652

3753
override val itemProvider = ItemProviderDslProperty()
38-
private val clickHandlers = mutableListOf<Click.() -> Unit>()
39-
private val bundleSelectHandlers = mutableListOf<BundleSelect.() -> Unit>()
54+
private val clickHandlers = mutableListOf<ClickDsl.() -> Unit>()
55+
private val bundleSelectHandlers = mutableListOf<BundleSelectDsl.() -> Unit>()
4056

41-
override fun onClick(handler: Click.() -> Unit) {
57+
override fun onClick(handler: ClickDsl.() -> Unit) {
4258
clickHandlers += handler
4359
}
4460

45-
override fun onBundleSelect(handler: BundleSelect.() -> Unit) {
61+
override fun onBundleSelect(handler: BundleSelectDsl.() -> Unit) {
4662
bundleSelectHandlers += handler
4763
}
4864

@@ -51,11 +67,24 @@ internal class ItemDslImpl : ItemDsl {
5167

5268
}
5369

70+
@ExperimentalDslApi
71+
internal class ClickDslImpl(
72+
override val clickType: ClickType,
73+
override val player: Player,
74+
override val hotbarButton: Int
75+
) : ClickDsl
76+
77+
@ExperimentalDslApi
78+
internal class BundleSelectDslImpl(
79+
override val player: Player,
80+
override val bundleSlot: Int
81+
) : BundleSelectDsl
82+
5483
@ExperimentalDslApi
5584
private class DslItemImpl(
5685
private val itemProvider: Provider<ItemProvider>,
57-
private val clickHandlers: List<Click.() -> Unit>,
58-
private val bundleSelectHandlers: List<BundleSelect.() -> Unit>
86+
private val clickHandlers: List<ClickDsl.() -> Unit>,
87+
private val bundleSelectHandlers: List<BundleSelectDsl.() -> Unit>
5988
) : AbstractItem() {
6089

6190
init {
@@ -65,12 +94,13 @@ private class DslItemImpl(
6594
override fun getItemProvider(viewer: Player) = itemProvider.get()
6695

6796
override fun handleClick(clickType: ClickType, player: Player, click: Click) {
68-
clickHandlers.forEach { it(click) }
97+
val cd = ClickDslImpl(clickType, player, click.hotbarButton)
98+
clickHandlers.forEach { it(cd) }
6999
}
70100

71101
override fun handleBundleSelect(player: Player, bundleSlot: Int) {
72-
val bs = BundleSelect(player, bundleSlot)
73-
bundleSelectHandlers.forEach { it(bs) }
102+
val bsd = BundleSelectDslImpl(player, bundleSlot)
103+
bundleSelectHandlers.forEach { it(bsd) }
74104
}
75105

76106

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

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

55
import org.bukkit.entity.Player
6+
import org.bukkit.event.inventory.ClickType
67
import org.bukkit.event.inventory.InventoryCloseEvent
78
import xyz.xenondevs.invui.ClickEvent
89
import xyz.xenondevs.invui.ExperimentalReactiveApi
@@ -23,8 +24,28 @@ fun window(viewer: Player, window: NormalSplitWindowDsl.() -> Unit): Window =
2324
fun mergedWindow(viewer: Player, window: NormalMergedWindowDsl.() -> Unit): Window =
2425
NormalMergedWindowDslImpl(viewer).apply(window).build()
2526

27+
@WindowDslMarker
2628
@ExperimentalDslApi
27-
class Close internal constructor(val reason: InventoryCloseEvent.Reason)
29+
interface WindowOpenDsl
30+
31+
@WindowDslMarker
32+
@ExperimentalDslApi
33+
interface WindowCloseDsl {
34+
35+
val reason: InventoryCloseEvent.Reason
36+
37+
}
38+
39+
@WindowDslMarker
40+
@ExperimentalDslApi
41+
interface WindowOutsideClickDsl {
42+
43+
val player: Player
44+
val clickType: ClickType
45+
val hotbarButton: Int
46+
var isCancelled: Boolean
47+
48+
}
2849

2950
@ExperimentalDslApi
3051
@WindowDslMarker
@@ -34,11 +55,11 @@ sealed interface WindowDsl {
3455
val closeable: ProviderDslProperty<Boolean>
3556
val fallbackWindow: ProviderDslProperty<Window?>
3657

37-
fun onOpen(handler: () -> Unit)
58+
fun onOpen(handler: WindowOpenDsl.() -> Unit)
3859

39-
fun onClose(handler: Close.() -> Unit)
60+
fun onClose(handler: WindowCloseDsl.() -> Unit)
4061

41-
fun onOutsideClick(handler: ClickEvent.() -> Unit)
62+
fun onOutsideClick(handler: WindowOutsideClickDsl.() -> Unit)
4263

4364
}
4465

@@ -71,19 +92,19 @@ internal abstract class AbstractWindowDsl<W : Window, B : Window.Builder<W, B>>(
7192
override val title = ComponentProviderDslProperty()
7293
override val closeable = ProviderDslProperty(true)
7394
override val fallbackWindow = ProviderDslProperty<Window?>(null)
74-
private val openHandlers = mutableListOf<() -> Unit>()
75-
private val closeHandlers = mutableListOf<Close.() -> Unit>()
76-
private val outsideClickHandlers = mutableListOf<ClickEvent.() -> Unit>()
95+
private val openHandlers = mutableListOf<WindowOpenDsl.() -> Unit>()
96+
private val closeHandlers = mutableListOf<WindowCloseDsl.() -> Unit>()
97+
private val outsideClickHandlers = mutableListOf<WindowOutsideClickDsl.() -> Unit>()
7798

78-
override fun onOpen(handler: () -> Unit) {
99+
override fun onOpen(handler: WindowOpenDsl.() -> Unit) {
79100
openHandlers += handler
80101
}
81102

82-
override fun onClose(handler: Close.() -> Unit) {
103+
override fun onClose(handler: WindowCloseDsl.() -> Unit) {
83104
closeHandlers += handler
84105
}
85106

86-
override fun onOutsideClick(handler: ClickEvent.() -> Unit) {
107+
override fun onOutsideClick(handler: WindowOutsideClickDsl.() -> Unit) {
87108
outsideClickHandlers += handler
88109
}
89110

@@ -95,9 +116,15 @@ internal abstract class AbstractWindowDsl<W : Window, B : Window.Builder<W, B>>(
95116
setTitle(title)
96117
setCloseable(closeable)
97118
setFallbackWindow(fallbackWindow)
98-
openHandlers.forEach { addOpenHandler(it) }
99-
closeHandlers.forEach { handler -> addCloseHandler { Close(it).handler() } }
100-
outsideClickHandlers.forEach { addOutsideClickHandler(it) }
119+
for (handler in openHandlers) {
120+
addOpenHandler { WindowOpenDslImpl().handler() }
121+
}
122+
for (handler in closeHandlers) {
123+
addCloseHandler { reason -> WindowCloseDslImpl(reason).handler() }
124+
}
125+
for (handler in outsideClickHandlers) {
126+
addOutsideClickHandler { event -> WindowOutsideClickDslImpl(event).handler() }
127+
}
101128
}
102129
}
103130

@@ -165,4 +192,25 @@ internal class NormalMergedWindowDslImpl(viewer: Player) : AbstractWindowDsl<Win
165192
builder.setGui(gui.value)
166193
}
167194

195+
}
196+
197+
@ExperimentalDslApi
198+
internal class WindowOpenDslImpl : WindowOpenDsl
199+
200+
@ExperimentalDslApi
201+
internal class WindowCloseDslImpl(override val reason: InventoryCloseEvent.Reason) : WindowCloseDsl
202+
203+
@ExperimentalDslApi
204+
internal class WindowOutsideClickDslImpl(private val event: ClickEvent) : WindowOutsideClickDsl {
205+
override val player: Player
206+
get() = event.player
207+
override val clickType: ClickType
208+
get() = event.clickType
209+
override val hotbarButton: Int
210+
get() = event.hotbarButton
211+
override var isCancelled: Boolean
212+
get() = event.isCancelled
213+
set(value) {
214+
event.isCancelled = value
215+
}
168216
}

0 commit comments

Comments
 (0)