Skip to content

Commit 86550f2

Browse files
committed
Update generics to couple the interface, view and builder
1 parent 2cab50c commit 86550f2

16 files changed

+47
-42
lines changed

examples/src/main/kotlin/com/noxcrew/interfaces/example/CatalogueExampleInterface.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import org.bukkit.Material
1010
public class CatalogueExampleInterface : RegistrableInterface {
1111
override val subcommand: String = "catalogue"
1212

13-
override fun create(): Interface<*> = buildCombinedInterface {
13+
override fun create(): Interface<*, *> = buildCombinedInterface {
1414
rows = 1
1515

1616
withTransform { pane, _ ->

examples/src/main/kotlin/com/noxcrew/interfaces/example/ChangingTitleExampleInterface.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class ChangingTitleExampleInterface : RegistrableInterface {
1313

1414
override val subcommand: String = "changing-title"
1515

16-
override fun create(): Interface<*> = buildCombinedInterface {
16+
override fun create(): Interface<*, *> = buildCombinedInterface {
1717
rows = 1
1818

1919
val numberProperty = interfaceProperty(0)

examples/src/main/kotlin/com/noxcrew/interfaces/example/DelayedRequestExampleInterface.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class DelayedRequestExampleInterface : RegistrableInterface {
2121
override val subcommand: String = "delayed"
2222

2323
@OptIn(DelicateCoroutinesApi::class)
24-
override fun create(): Interface<*> = buildCombinedInterface {
24+
override fun create(): Interface<*, *> = buildCombinedInterface {
2525
initialTitle = text(subcommand)
2626
rows = 2
2727

examples/src/main/kotlin/com/noxcrew/interfaces/example/MovingExampleInterface.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import org.bukkit.Material
1010
public class MovingExampleInterface : RegistrableInterface {
1111
override val subcommand: String = "moving"
1212

13-
override fun create(): Interface<*> = buildCombinedInterface {
13+
override fun create(): Interface<*, *> = buildCombinedInterface {
1414
val countProperty = BoundInteger(4, 1, 7)
1515
var count by countProperty
1616

examples/src/main/kotlin/com/noxcrew/interfaces/example/RegistrableInterface.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ public interface RegistrableInterface {
66

77
public val subcommand: String
88

9-
public fun create(): Interface<*>
9+
public fun create(): Interface<*, *>
1010
}

examples/src/main/kotlin/com/noxcrew/interfaces/example/TabbedExampleInterface.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class TabbedExampleInterface : RegistrableInterface {
2020

2121
override val subcommand: String = "tabbed"
2222

23-
override fun create(): Interface<*> = first
23+
override fun create(): Interface<*, *> = first
2424

2525
private fun CombinedInterfaceBuilder.defaults() {
2626
rows = 6

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

+10-10
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
150150
@EventHandler
151151
public fun onClose(event: InventoryCloseEvent) {
152152
val holder = event.inventory.holder
153-
val view = holder as? AbstractInterfaceView<*, *> ?: return
153+
val view = holder as? AbstractInterfaceView<*, *, *> ?: return
154154
val reason = event.reason
155155

156156
// Saves any persistent items stored in the given inventory before we close it
@@ -233,7 +233,7 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
233233
// Check if the action is prevented if this slot is not freely
234234
// movable
235235
if (!canFreelyMove(view, clickedPoint) &&
236-
event.action in view.backing.properties.preventedInteractions
236+
event.action in view.builder.preventedInteractions
237237
) {
238238
event.isCancelled = true
239239
return
@@ -336,11 +336,11 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
336336
* Converts an inventory holder to an [AbstractInterfaceView] if possible. If the holder is a player
337337
* their currently open player interface is returned.
338338
*/
339-
public fun convertHolderToInterfaceView(holder: InventoryHolder?): AbstractInterfaceView<*, *>? {
339+
public fun convertHolderToInterfaceView(holder: InventoryHolder?): AbstractInterfaceView<*, *, *>? {
340340
if (holder == null) return null
341341

342342
// If it's an abstract view use that one
343-
if (holder is AbstractInterfaceView<*, *>) return holder
343+
if (holder is AbstractInterfaceView<*, *, *>) return holder
344344

345345
// If it's the player's own inventory use the held one
346346
if (holder is HumanEntity) return getOpenInterface(holder.uniqueId)
@@ -350,13 +350,13 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
350350

351351
/** Returns whether [clickedPoint] in [view] can be freely moved. */
352352
private fun canFreelyMove(
353-
view: AbstractInterfaceView<*, *>,
353+
view: AbstractInterfaceView<*, *, *>,
354354
clickedPoint: GridPoint
355-
): Boolean = view.pane.getRaw(clickedPoint) == null && !view.backing.properties.preventClickingEmptySlots
355+
): Boolean = view.pane.getRaw(clickedPoint) == null && !view.builder.preventClickingEmptySlots
356356

357357
/** Handles a [view] being clicked at [clickedPoint] through some [event]. */
358358
private fun handleClick(
359-
view: AbstractInterfaceView<*, *>,
359+
view: AbstractInterfaceView<*, *, *>,
360360
clickedPoint: GridPoint,
361361
click: ClickType,
362362
event: Cancellable,
@@ -367,7 +367,7 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
367367

368368
// Optionally cancel clicking on other slots
369369
if (raw == null) {
370-
if (view.backing.properties.preventClickingEmptySlots) {
370+
if (view.builder.preventClickingEmptySlots) {
371371
event.isCancelled = true
372372
}
373373
return
@@ -384,7 +384,7 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
384384

385385
// Forward this click to all pre-processors
386386
val clickContext = ClickContext(view.player, view, click, slot)
387-
view.backing.properties.clickPreprocessors
387+
view.builder.clickPreprocessors
388388
.forEach { handler -> ClickHandler.process(handler, clickContext) }
389389

390390
// Run the click handler and deal with its result
@@ -502,7 +502,7 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
502502

503503
// Mark the view as properly closed
504504
SCOPE.launch {
505-
(query.view as AbstractInterfaceView<*, *>).markClosed(Reason.PLAYER)
505+
(query.view as AbstractInterfaceView<*, *, *>).markClosed(Reason.PLAYER)
506506
}
507507
}
508508
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import org.bukkit.entity.Player
1010
public class ChestInterface internal constructor(
1111
override val rows: Int,
1212
override val initialTitle: Component?,
13-
override val properties: InterfaceProperties<Pane>
14-
) : Interface<Pane>, TitledInterface {
13+
override val builder: ChestInterfaceBuilder
14+
) : Interface<ChestInterface, Pane>, TitledInterface {
1515

1616
public companion object {
1717
/** The maximum number of rows for a chest GUI. */

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import org.bukkit.entity.Player
1010
public class CombinedInterface internal constructor(
1111
override val rows: Int,
1212
override val initialTitle: Component?,
13-
override val properties: InterfaceProperties<CombinedPane>
14-
) : Interface<CombinedPane>, TitledInterface {
13+
override val builder: CombinedInterfaceBuilder
14+
) : Interface<CombinedInterface, CombinedPane>, TitledInterface {
1515

1616
public companion object {
1717
/** The maximum number of rows for a combined interface. */

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import com.noxcrew.interfaces.view.InterfaceView
66
import org.bukkit.entity.Player
77

88
/** A created interface that can be opened for a player to create a unique view. */
9-
public interface Interface<P : Pane> {
9+
public interface Interface<I : Interface<I, P>, P : Pane> {
1010

1111
/** The amount of rows this interface contains. */
1212
public val rows: Int
1313

14-
/** The properties of this interface. */
15-
public val properties: InterfaceProperties<P>
14+
/** The builder that creates this interface. */
15+
public val builder: InterfaceBuilder<P, I>
1616

1717
/** Returns the total amount of rows. */
1818
public fun totalRows(): Int = rows

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import com.noxcrew.interfaces.transform.Transform
88
import com.noxcrew.interfaces.utilities.IncrementingInteger
99

1010
/** Assists in creating a new interface. */
11-
public abstract class InterfaceBuilder<P : Pane, I : Interface<P>> : InterfaceProperties<P>() {
11+
public abstract class InterfaceBuilder<P : Pane, I : Interface<I, P>> : InterfaceProperties<P>() {
1212

1313
private val transformCounter by IncrementingInteger()
1414
private val _transforms: MutableCollection<AppliedTransform<P>> = mutableListOf()

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import org.bukkit.entity.Player
77

88
/** An interface that uses the entire player inventory. */
99
public class PlayerInterface internal constructor(
10-
override val properties: InterfaceProperties<PlayerPane>
11-
) : Interface<PlayerPane> {
10+
override val builder: PlayerInterfaceBuilder
11+
) : Interface<PlayerInterface, PlayerPane> {
1212

1313
public companion object {
1414
/** The maximum number of rows for a player interface. */

interfaces/src/main/kotlin/com/noxcrew/interfaces/view/AbstractInterfaceView.kt

+17-12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.noxcrew.interfaces.InterfacesListeners
66
import com.noxcrew.interfaces.event.DrawPaneEvent
77
import com.noxcrew.interfaces.grid.GridPoint
88
import com.noxcrew.interfaces.interfaces.Interface
9+
import com.noxcrew.interfaces.interfaces.InterfaceBuilder
910
import com.noxcrew.interfaces.inventory.InterfacesInventory
1011
import com.noxcrew.interfaces.pane.CompletedPane
1112
import com.noxcrew.interfaces.pane.Pane
@@ -34,10 +35,10 @@ import kotlin.time.Duration
3435
import kotlin.time.Duration.Companion.seconds
3536

3637
/** The basis for the implementation of an interface view. */
37-
public abstract class AbstractInterfaceView<I : InterfacesInventory, P : Pane>(
38+
public abstract class AbstractInterfaceView<I : InterfacesInventory, T : Interface<T, P>, P : Pane>(
3839
override val player: Player,
3940
/** The interface backing this view. */
40-
public val backing: Interface<P>,
41+
public val backing: T,
4142
private val parent: InterfaceView?
4243
) : InterfaceView {
4344

@@ -50,7 +51,11 @@ public abstract class AbstractInterfaceView<I : InterfacesInventory, P : Pane>(
5051
private val semaphore = Semaphore(1)
5152
private val queue = AtomicInteger(0)
5253

53-
private val children = WeakHashMap<AbstractInterfaceView<*, *>, Unit>()
54+
private val children = WeakHashMap<AbstractInterfaceView<*, *, *>, Unit>()
55+
56+
/** The builder used by this interface. */
57+
public val builder: InterfaceBuilder<P, T>
58+
get() = backing.builder
5459

5560
/** Added persistent items added when this interface was last closed. */
5661
public val addedItems: MutableMap<GridPoint, ItemStack> = mutableMapOf()
@@ -96,8 +101,8 @@ public abstract class AbstractInterfaceView<I : InterfacesInventory, P : Pane>(
96101
openIfClosed.set(false)
97102

98103
// Run a generic close handler if it's still opened
99-
if (shouldBeOpened.compareAndSet(true, false) && (!changingView || backing.properties.callCloseHandlerOnViewSwitch)) {
100-
backing.properties.closeHandlers[reason]?.invoke(reason, this)
104+
if (shouldBeOpened.compareAndSet(true, false) && (!changingView || builder.callCloseHandlerOnViewSwitch)) {
105+
builder.closeHandlers[reason]?.invoke(reason, this)
101106
}
102107

103108
// Don't close children when changing views!
@@ -118,7 +123,7 @@ public abstract class AbstractInterfaceView<I : InterfacesInventory, P : Pane>(
118123
private fun setup() {
119124
// Determine for each trigger what transforms it updates
120125
val triggers = HashMultimap.create<Trigger, AppliedTransform<P>>()
121-
for (transform in backing.properties.transforms) {
126+
for (transform in builder.transforms) {
122127
for (trigger in transform.triggers) {
123128
triggers.put(trigger, transform)
124129
}
@@ -138,7 +143,7 @@ public abstract class AbstractInterfaceView<I : InterfacesInventory, P : Pane>(
138143
}
139144

140145
override fun redrawComplete() {
141-
applyTransforms(backing.properties.transforms)
146+
applyTransforms(builder.transforms)
142147
}
143148

144149
override suspend fun open() {
@@ -148,7 +153,7 @@ public abstract class AbstractInterfaceView<I : InterfacesInventory, P : Pane>(
148153
shouldBeOpened.set(true)
149154

150155
// Indicate to the parent that this child exists
151-
if (parent is AbstractInterfaceView<*, *>) {
156+
if (parent is AbstractInterfaceView<*, *, *>) {
152157
parent.children[this] = Unit
153158
}
154159

@@ -293,14 +298,14 @@ public abstract class AbstractInterfaceView<I : InterfacesInventory, P : Pane>(
293298
currentInventory.set(
294299
row,
295300
column,
296-
element.itemStack.apply { this?.let { backing.properties.itemPostProcessor?.invoke(it) } }
301+
element.itemStack.apply { this?.let { builder.itemPostProcessor?.invoke(it) } }
297302
)
298303
leftovers -= row to column
299304
madeChanges = true
300305
}
301306

302307
// Apply the overlay of persistent items on top
303-
if (backing.properties.persistAddedItems) {
308+
if (builder.persistAddedItems) {
304309
for ((point, item) in addedItems) {
305310
val row = point.x
306311
val column = point.y
@@ -318,7 +323,7 @@ public abstract class AbstractInterfaceView<I : InterfacesInventory, P : Pane>(
318323
}
319324

320325
// If we inherit existing items we don't clear here!
321-
if (!backing.properties.inheritExistingItems) {
326+
if (!builder.inheritExistingItems) {
322327
// Empty any slots that are not otherwise edited
323328
for ((row, column) in leftovers) {
324329
val isPlayerInventory = currentInventory.isPlayerInventory(row, column)
@@ -335,7 +340,7 @@ public abstract class AbstractInterfaceView<I : InterfacesInventory, P : Pane>(
335340

336341
/** Saves any persistent items based on [inventory]. */
337342
public fun savePersistentItems(inventory: Inventory) {
338-
if (!backing.properties.persistAddedItems) return
343+
if (!builder.persistAddedItems) return
339344

340345
addedItems.clear()
341346
val contents = inventory.contents

interfaces/src/main/kotlin/com/noxcrew/interfaces/view/ChestInterfaceView.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class ChestInterfaceView internal constructor(
1414
player: Player,
1515
backing: ChestInterface,
1616
parent: InterfaceView?
17-
) : AbstractInterfaceView<ChestInterfacesInventory, Pane>(
17+
) : AbstractInterfaceView<ChestInterfacesInventory, ChestInterface, Pane>(
1818
player,
1919
backing,
2020
parent

interfaces/src/main/kotlin/com/noxcrew/interfaces/view/CombinedInterfaceView.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class CombinedInterfaceView internal constructor(
1414
player: Player,
1515
backing: CombinedInterface,
1616
parent: InterfaceView?
17-
) : AbstractInterfaceView<CombinedInterfacesInventory, CombinedPane>(
17+
) : AbstractInterfaceView<CombinedInterfacesInventory, CombinedInterface, CombinedPane>(
1818
player,
1919
backing,
2020
parent

interfaces/src/main/kotlin/com/noxcrew/interfaces/view/PlayerInterfaceView.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import kotlin.time.Duration
1515
public class PlayerInterfaceView internal constructor(
1616
player: Player,
1717
backing: PlayerInterface
18-
) : AbstractInterfaceView<PlayerInterfacesInventory, PlayerPane>(
18+
) : AbstractInterfaceView<PlayerInterfacesInventory, PlayerInterface, PlayerPane>(
1919
player,
2020
backing,
2121
null
@@ -43,7 +43,7 @@ public class PlayerInterfaceView internal constructor(
4343

4444
// Double-check that this inventory is open now!
4545
if (isOpen()) {
46-
if (!backing.properties.inheritExistingItems) {
46+
if (!builder.inheritExistingItems) {
4747
// Clear the player's inventory!
4848
player.inventory.clear()
4949
if (player.openInventory.topInventory.type == InventoryType.CRAFTING ||

0 commit comments

Comments
 (0)