Skip to content

Commit d83c577

Browse files
Item / Gui / Window DSL
- Added DSL-style builders for reactive items / guis / windows to invui-kotlin - Guis now use properties for background, frozen, ignoreObscuredInventorySlots - Fixed page change / scroll / tab change handlers not called when updated through property
1 parent 18e7c81 commit d83c577

File tree

67 files changed

+1997
-457
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1997
-457
lines changed

invui-kotlin/src/main/kotlin/xyz/xenondevs/invui/PropertyWrappers.kt

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,20 @@ package xyz.xenondevs.invui
33
import xyz.xenondevs.commons.provider.MutableProvider
44
import xyz.xenondevs.commons.provider.Provider
55
import xyz.xenondevs.invui.state.MutableProperty
6-
import xyz.xenondevs.invui.state.Property
76
import java.util.function.Consumer
87

9-
internal class PropertyAdapter<T : Any>(private val provider: Provider<T>) : Property<T> {
10-
11-
override fun get(): T {
12-
return provider.get()
13-
}
14-
15-
override fun <O : Any> observeWeak(owner: O, observer: Consumer<in O>) {
16-
provider.observeWeak(owner, observer::accept)
17-
}
18-
19-
override fun <O : Any> unobserveWeak(owner: O, observer: Consumer<in O>) {
20-
provider.unobserveWeak(owner, observer::accept)
21-
}
22-
23-
override fun unobserveWeak(owner: Any) {
24-
provider.unobserveWeak(owner)
25-
}
26-
27-
}
28-
29-
internal class MutablePropertyAdapter<T : Any>(private val provider: MutableProvider<T>) : MutableProperty<T> {
8+
internal class PropertyAdapter<T>(private val provider: Provider<T>) : MutableProperty<T> {
309

3110
override fun get(): T {
3211
return provider.get()
3312
}
3413

3514
override fun set(value: T) {
36-
provider.set(value)
15+
if (provider is MutableProvider<T>) {
16+
provider.set(value)
17+
} else {
18+
throw UnsupportedOperationException("This property is backed by a non-mutable provider and cannot be written to.")
19+
}
3720
}
3821

3922
override fun <O : Any> observeWeak(owner: O, observer: Consumer<in O>) {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
@file:OptIn(ExperimentalReactiveApi::class)
2+
3+
package xyz.xenondevs.invui.dsl
4+
5+
import org.bukkit.entity.Player
6+
import xyz.xenondevs.invui.ExperimentalReactiveApi
7+
import xyz.xenondevs.invui.gui.Gui
8+
import xyz.xenondevs.invui.window.AnvilWindow
9+
import xyz.xenondevs.invui.window.addRenameHandler
10+
import xyz.xenondevs.invui.window.setResultAlwaysValid
11+
import xyz.xenondevs.invui.window.setTextFieldAlwaysEnabled
12+
13+
@ExperimentalDslApi
14+
fun anvilWindow(viewer: Player, anvilWindow: AnvilWindowDsl.() -> Unit): AnvilWindow =
15+
AnvilWindowDslImpl(viewer).apply(anvilWindow).build()
16+
17+
@ExperimentalDslApi
18+
sealed interface AnvilWindowDsl : SplitWindowDsl {
19+
20+
val upperGui: GuiDslProperty
21+
val text: MutableProviderDslProperty<String>
22+
val textFieldAlwaysEnabled: ProviderDslProperty<Boolean>
23+
val resultAlwaysValid: ProviderDslProperty<Boolean>
24+
25+
}
26+
27+
@ExperimentalDslApi
28+
internal class AnvilWindowDslImpl(
29+
viewer: Player
30+
) : AbstractSplitWindowDsl<AnvilWindow, AnvilWindow.Builder>(viewer), AnvilWindowDsl {
31+
32+
override val upperGui = GuiDslProperty(3, 1)
33+
override val text = MutableProviderDslProperty("")
34+
override val textFieldAlwaysEnabled = ProviderDslProperty(true)
35+
override val resultAlwaysValid = ProviderDslProperty(false)
36+
37+
override fun createBuilder() = AnvilWindow.builder()
38+
39+
override fun applyToBuilder(builder: AnvilWindow.Builder) {
40+
super.applyToBuilder(builder)
41+
builder.apply {
42+
setUpperGui(upperGui.value)
43+
addRenameHandler(text.value)
44+
setTextFieldAlwaysEnabled(textFieldAlwaysEnabled.value)
45+
setResultAlwaysValid(resultAlwaysValid.value)
46+
}
47+
}
48+
49+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
@file:OptIn(ExperimentalReactiveApi::class)
2+
3+
package xyz.xenondevs.invui.dsl
4+
5+
import org.bukkit.entity.Player
6+
import xyz.xenondevs.invui.ExperimentalReactiveApi
7+
import xyz.xenondevs.invui.window.BrewingWindow
8+
import xyz.xenondevs.invui.window.setBrewProgress
9+
import xyz.xenondevs.invui.window.setFuelProgress
10+
11+
@ExperimentalDslApi
12+
fun brewingWindow(viewer: Player, brewingWindow: BrewingWindowDsl.() -> Unit): BrewingWindow =
13+
BrewingWindowDslImpl(viewer).apply(brewingWindow).build()
14+
15+
@ExperimentalDslApi
16+
sealed interface BrewingWindowDsl : SplitWindowDsl {
17+
18+
val inputGui: GuiDslProperty
19+
val fuelGui: GuiDslProperty
20+
val resultGui: GuiDslProperty
21+
val brewProgress: ProviderDslProperty<Double>
22+
val fuelProgress: ProviderDslProperty<Double>
23+
24+
}
25+
26+
@ExperimentalDslApi
27+
internal class BrewingWindowDslImpl(
28+
viewer: Player
29+
) : AbstractSplitWindowDsl<BrewingWindow, BrewingWindow.Builder>(viewer), BrewingWindowDsl {
30+
31+
override val inputGui = GuiDslProperty(1, 1)
32+
override val fuelGui = GuiDslProperty(1, 1)
33+
override val resultGui = GuiDslProperty(3, 1)
34+
override val brewProgress = ProviderDslProperty(0.0)
35+
override val fuelProgress = ProviderDslProperty(0.0)
36+
37+
override fun createBuilder() = BrewingWindow.builder()
38+
39+
override fun applyToBuilder(builder: BrewingWindow.Builder) {
40+
super.applyToBuilder(builder)
41+
builder.apply {
42+
setInputGui(inputGui.value)
43+
setFuelGui(fuelGui.value)
44+
setResultGui(resultGui.value)
45+
setBrewProgress(brewProgress.value)
46+
setFuelProgress(fuelProgress.value)
47+
}
48+
}
49+
50+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
@file:OptIn(ExperimentalReactiveApi::class)
2+
3+
package xyz.xenondevs.invui.dsl
4+
5+
import org.bukkit.entity.Player
6+
import xyz.xenondevs.invui.ExperimentalReactiveApi
7+
import xyz.xenondevs.invui.window.CartographyWindow
8+
import xyz.xenondevs.invui.window.setIcons
9+
import xyz.xenondevs.invui.window.setView
10+
11+
// TODO: map image
12+
13+
@ExperimentalDslApi
14+
fun cartographyWindow(viewer: Player, cartographyWindow: CartographyWindowDsl.() -> Unit): CartographyWindow =
15+
CartographyWindowDslImpl(viewer).apply(cartographyWindow).build()
16+
17+
@ExperimentalDslApi
18+
sealed interface CartographyWindowDsl : SplitWindowDsl {
19+
20+
val inputGui: GuiDslProperty
21+
val resultGui: GuiDslProperty
22+
val icons: ProviderDslProperty<Set<CartographyWindow.MapIcon>>
23+
val view: ProviderDslProperty<CartographyWindow.View>
24+
25+
}
26+
27+
@ExperimentalDslApi
28+
internal class CartographyWindowDslImpl(
29+
viewer: Player
30+
) : AbstractSplitWindowDsl<CartographyWindow, CartographyWindow.Builder>(viewer), CartographyWindowDsl {
31+
32+
override val inputGui = GuiDslProperty(1, 2)
33+
override val resultGui = GuiDslProperty(1, 1)
34+
override val icons = ProviderDslProperty(emptySet<CartographyWindow.MapIcon>())
35+
override val view = ProviderDslProperty(CartographyWindow.View.NORMAL)
36+
37+
override fun createBuilder() = CartographyWindow.builder()
38+
39+
override fun applyToBuilder(builder: CartographyWindow.Builder) {
40+
super.applyToBuilder(builder)
41+
builder.apply {
42+
setInputGui(inputGui.value)
43+
setResultGui(resultGui.value)
44+
setIcons(icons.value)
45+
setView(view.value)
46+
}
47+
}
48+
49+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
@file:OptIn(ExperimentalReactiveApi::class)
2+
3+
package xyz.xenondevs.invui.dsl
4+
5+
import org.bukkit.entity.Player
6+
import xyz.xenondevs.commons.provider.MutableProvider
7+
import xyz.xenondevs.commons.provider.mutableProvider
8+
import xyz.xenondevs.invui.ExperimentalReactiveApi
9+
import xyz.xenondevs.invui.window.CrafterWindow
10+
import xyz.xenondevs.invui.window.setSlots
11+
12+
@ExperimentalDslApi
13+
fun crafterWindow(viewer: Player, crafterWindow: CrafterWindowDsl.() -> Unit): CrafterWindow =
14+
CrafterWindowDslImpl(viewer).apply(crafterWindow).build()
15+
16+
@ExperimentalDslApi
17+
sealed interface CrafterWindowDsl : SplitWindowDsl {
18+
19+
val craftingGui: GuiDslProperty
20+
val resultGui: GuiDslProperty
21+
val slots: MutableProvider2dArrayDslProperty<Boolean>
22+
23+
}
24+
25+
@ExperimentalDslApi
26+
internal class CrafterWindowDslImpl(
27+
viewer: Player
28+
) : AbstractSplitWindowDsl<CrafterWindow, CrafterWindow.Builder>(viewer), CrafterWindowDsl {
29+
30+
override val craftingGui = GuiDslProperty(3, 3)
31+
override val resultGui = GuiDslProperty(1, 1)
32+
override val slots = MutableProvider2dArrayDslProperty(3, 3, false)
33+
34+
override fun createBuilder() = CrafterWindow.builder()
35+
36+
override fun applyToBuilder(builder: CrafterWindow.Builder) {
37+
super.applyToBuilder(builder)
38+
builder.apply {
39+
setCraftingGui(craftingGui.value)
40+
setResultGui(resultGui.value)
41+
setSlots(slots.value)
42+
}
43+
}
44+
45+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
@file:OptIn(ExperimentalReactiveApi::class)
2+
3+
package xyz.xenondevs.invui.dsl
4+
5+
import net.kyori.adventure.key.Key
6+
import org.bukkit.entity.Player
7+
import xyz.xenondevs.invui.ExperimentalReactiveApi
8+
import xyz.xenondevs.invui.window.CraftingWindow
9+
10+
@ExperimentalDslApi
11+
fun craftingWindow(viewer: Player, craftingWindow: CraftingWindowDsl.() -> Unit): CraftingWindow =
12+
CraftingWindowDslImpl(viewer).apply(craftingWindow).build()
13+
14+
@ExperimentalDslApi
15+
sealed interface CraftingWindowDsl : SplitWindowDsl {
16+
17+
val craftingGui: GuiDslProperty
18+
val resultGui: GuiDslProperty
19+
20+
fun onRecipeClick(handler: (recipeKey: Key) -> Unit)
21+
22+
}
23+
24+
@ExperimentalDslApi
25+
internal class CraftingWindowDslImpl(
26+
viewer: Player
27+
) : AbstractSplitWindowDsl<CraftingWindow, CraftingWindow.Builder>(viewer), CraftingWindowDsl {
28+
29+
override val craftingGui = GuiDslProperty(3, 3)
30+
override val resultGui = GuiDslProperty(1, 1)
31+
private val recipeClickHandlers = mutableListOf<(Key) -> Unit>()
32+
33+
override fun onRecipeClick(handler: (Key) -> Unit) {
34+
recipeClickHandlers += handler
35+
}
36+
37+
override fun createBuilder() = CraftingWindow.builder()
38+
39+
override fun applyToBuilder(builder: CraftingWindow.Builder) {
40+
super.applyToBuilder(builder)
41+
builder.apply {
42+
setCraftingGui(craftingGui.value)
43+
setResultGui(resultGui.value)
44+
recipeClickHandlers.forEach { addRecipeClickHandler(it) }
45+
}
46+
}
47+
48+
}

0 commit comments

Comments
 (0)