@@ -2,29 +2,73 @@ package com.noxcrew.interfaces.interfaces
2
2
3
3
import com.noxcrew.interfaces.click.ClickHandler
4
4
import com.noxcrew.interfaces.pane.Pane
5
- import com.noxcrew.interfaces.transform.AppliedTransform
6
5
import org.bukkit.event.block.Action
7
6
import org.bukkit.event.inventory.InventoryCloseEvent
8
7
import org.bukkit.inventory.ItemStack
9
8
10
9
/* * 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
+
12
21
/* * 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
+
16
25
/* * 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
+
18
33
/* * A post-processor applied to all items placed in the inventory. */
19
- public val itemPostProcessor : ((ItemStack ) -> Unit )? = {},
34
+ public var itemPostProcessor: ((ItemStack ) -> Unit )? = {}
35
+
20
36
/* * 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
+
26
46
/* * Keeps items that were previously in the inventory before opening this. */
27
- public val inheritExistingItems : Boolean = false ,
47
+ public var inheritExistingItems: Boolean = false
48
+
28
49
/* * 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
+ }
0 commit comments