Skip to content

Commit 2cab50c

Browse files
committed
Merge properties into builder
1 parent 6c59fb5 commit 2cab50c

File tree

6 files changed

+90
-115
lines changed

6 files changed

+90
-115
lines changed

interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/AbstractInterfaceBuilder.kt

-91
This file was deleted.

interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/ChestInterfaceBuilder.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import net.kyori.adventure.text.Component
55

66
/** Assists in building a [ChestInterface]. */
77
public class ChestInterfaceBuilder :
8-
AbstractInterfaceBuilder<Pane, ChestInterface>() {
8+
InterfaceBuilder<Pane, ChestInterface>() {
99

1010
/** Sets the amount of rows for this interface to use. */
1111
public var rows: Int = 0
@@ -16,6 +16,6 @@ public class ChestInterfaceBuilder :
1616
override fun build(): ChestInterface = ChestInterface(
1717
rows,
1818
initialTitle,
19-
properties
19+
this
2020
)
2121
}

interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/CombinedInterfaceBuilder.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import net.kyori.adventure.text.Component
55

66
/** Assists in building a [CombinedInterface]. */
77
public class CombinedInterfaceBuilder :
8-
AbstractInterfaceBuilder<CombinedPane, CombinedInterface>() {
8+
InterfaceBuilder<CombinedPane, CombinedInterface>() {
99

1010
/** Sets the amount of rows for this interface to use. */
1111
public var rows: Int = 0
@@ -16,6 +16,6 @@ public class CombinedInterfaceBuilder :
1616
override fun build(): CombinedInterface = CombinedInterface(
1717
rows,
1818
initialTitle,
19-
properties
19+
this
2020
)
2121
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,32 @@
11
package com.noxcrew.interfaces.interfaces
22

33
import com.noxcrew.interfaces.pane.Pane
4+
import com.noxcrew.interfaces.properties.Trigger
5+
import com.noxcrew.interfaces.transform.AppliedTransform
6+
import com.noxcrew.interfaces.transform.ReactiveTransform
7+
import com.noxcrew.interfaces.transform.Transform
8+
import com.noxcrew.interfaces.utilities.IncrementingInteger
49

5-
/** A generic builder for creating an [Interface]. */
6-
public abstract class InterfaceBuilder<P : Pane, T : Interface<P>> {
10+
/** Assists in creating a new interface. */
11+
public abstract class InterfaceBuilder<P : Pane, I : Interface<P>> : InterfaceProperties<P>() {
12+
13+
private val transformCounter by IncrementingInteger()
14+
private val _transforms: MutableCollection<AppliedTransform<P>> = mutableListOf()
15+
16+
/** All transforms in this builder. */
17+
public val transforms: Collection<AppliedTransform<P>>
18+
get() = _transforms
719

820
/** Creates the interface. */
9-
public abstract fun build(): T
21+
public abstract fun build(): I
22+
23+
/** Adds a new transform to the interface that updates whenever [triggers] change. */
24+
public fun withTransform(vararg triggers: Trigger, transform: Transform<P>) {
25+
_transforms += AppliedTransform(transformCounter, triggers.toSet(), transform)
26+
}
27+
28+
/** Adds a new reactive transform to the interface. */
29+
public fun addTransform(reactiveTransform: ReactiveTransform<P>) {
30+
_transforms += AppliedTransform(transformCounter, reactiveTransform.triggers.toSet(), reactiveTransform)
31+
}
1032
}

interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/InterfaceProperties.kt

+59-15
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,73 @@ package com.noxcrew.interfaces.interfaces
22

33
import com.noxcrew.interfaces.click.ClickHandler
44
import com.noxcrew.interfaces.pane.Pane
5-
import com.noxcrew.interfaces.transform.AppliedTransform
65
import org.bukkit.event.block.Action
76
import org.bukkit.event.inventory.InventoryCloseEvent
87
import org.bukkit.inventory.ItemStack
98

109
/** Stores all shared properties of an interface. */
11-
public data class InterfaceProperties<P : Pane>(
10+
public open class InterfaceProperties<P : Pane> {
11+
12+
private companion object {
13+
/** All default reasons used for a new close handler. */
14+
private val DEFAULT_REASONS = InventoryCloseEvent.Reason.entries.minus(InventoryCloseEvent.Reason.PLUGIN)
15+
}
16+
17+
private val _closeHandlers: MutableMap<InventoryCloseEvent.Reason, CloseHandler> = mutableMapOf()
18+
private val _clickPreprocessors: MutableCollection<ClickHandler> = mutableListOf()
19+
private val _preventedInteractions: MutableCollection<Action> = mutableListOf()
20+
1221
/** All close handlers on this interface mapped by closing reason. */
13-
public val closeHandlers: Map<InventoryCloseEvent.Reason, CloseHandler> = emptyMap(),
14-
/** All transforms that make up this interface. */
15-
public val transforms: Collection<AppliedTransform<P>> = emptySet(),
22+
public val closeHandlers: Map<InventoryCloseEvent.Reason, CloseHandler>
23+
get() = _closeHandlers
24+
1625
/** A collection of click handlers that will be run before each click without blocking. */
17-
public val clickPreprocessors: Collection<ClickHandler> = emptySet(),
26+
public val clickPreprocessors: Collection<ClickHandler>
27+
get() = _clickPreprocessors
28+
29+
/** All interactions that will be ignored on this view and cancelled on pane items without calling the handler. */
30+
public val preventedInteractions: Collection<Action>
31+
get() = _preventedInteractions
32+
1833
/** A post-processor applied to all items placed in the inventory. */
19-
public val itemPostProcessor: ((ItemStack) -> Unit)? = {},
34+
public var itemPostProcessor: ((ItemStack) -> Unit)? = {}
35+
2036
/** Whether clicking on empty slots should be cancelled. */
21-
public val preventClickingEmptySlots: Boolean = true,
22-
/** All interactions that will be ignored on this view and cancelled on pane items without calling the handler. */
23-
public val preventedInteractions: Collection<Action> = emptySet(),
24-
/** Persists items added to this pane in a previous instance. */
25-
public val persistAddedItems: Boolean = false,
37+
public var preventClickingEmptySlots: Boolean = true
38+
39+
/**
40+
* Persists items added to this pane in a previous instance.
41+
* Particularly useful for player inventories, this allows the non-interface items
42+
* to function as normal inventory items and be normally added/removed.
43+
*/
44+
public var persistAddedItems: Boolean = false
45+
2646
/** Keeps items that were previously in the inventory before opening this. */
27-
public val inheritExistingItems: Boolean = false,
47+
public var inheritExistingItems: Boolean = false
48+
2849
/** Whether close handlers should be called when switching to a different view. */
29-
public val callCloseHandlerOnViewSwitch: Boolean = true
30-
)
50+
public var callCloseHandlerOnViewSwitch: Boolean = true
51+
52+
/** Whether to place empty air elements in all background slots. */
53+
public var fillMenuWithAir: Boolean = false
54+
55+
/** Adds a new close handler [closeHandler] that triggers whenever the inventory is closed for any of the given [reasons]. */
56+
public fun addCloseHandler(
57+
reasons: Collection<InventoryCloseEvent.Reason> = DEFAULT_REASONS,
58+
closeHandler: CloseHandler
59+
) {
60+
reasons.forEach {
61+
_closeHandlers[it] = closeHandler
62+
}
63+
}
64+
65+
/** Adds a new pre-processor to this menu which will run [handler] before every click without blocking. */
66+
public fun addPreprocessor(handler: ClickHandler) {
67+
_clickPreprocessors += handler
68+
}
69+
70+
/** Adds [action] to be cancelled without triggering any click handlers on valid items in this pane. */
71+
public fun addPreventedAction(action: Action) {
72+
_preventedInteractions += action
73+
}
74+
}

interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/PlayerInterfaceBuilder.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.noxcrew.interfaces.interfaces
33
import com.noxcrew.interfaces.pane.PlayerPane
44

55
/** Assists in building a [PlayerInterface]. */
6-
public class PlayerInterfaceBuilder : AbstractInterfaceBuilder<PlayerPane, PlayerInterface>() {
6+
public class PlayerInterfaceBuilder : InterfaceBuilder<PlayerPane, PlayerInterface>() {
77

8-
override fun build(): PlayerInterface = PlayerInterface(properties)
8+
override fun build(): PlayerInterface = PlayerInterface(this)
99
}

0 commit comments

Comments
 (0)