Click callbacks are now typed
This commit is contained in:
@@ -27,14 +27,14 @@ fun HumanEntity.openGUI(gui: InventoryGUI<*>, page: Int? = null): InventoryView?
|
||||
|
||||
class InventoryGUIHolder(kSpigot: KSpigot) : AutoCloseable {
|
||||
|
||||
private val registered = HashSet<InventoryGUI<*>>()
|
||||
private val registered = HashSet<InventoryGUI<ForInventory>>()
|
||||
|
||||
fun register(inventoryGUI: InventoryGUI<*>) {
|
||||
registered += inventoryGUI
|
||||
fun register(inventoryGUI: InventoryGUI<ForInventory>) {
|
||||
registered.add(inventoryGUI)
|
||||
}
|
||||
|
||||
fun unregister(inventoryGUI: InventoryGUI<*>) {
|
||||
registered -= inventoryGUI
|
||||
fun unregister(inventoryGUI: InventoryGUI<ForInventory>) {
|
||||
registered.remove(inventoryGUI)
|
||||
}
|
||||
|
||||
init {
|
||||
@@ -77,7 +77,7 @@ class InventoryGUIData<T : ForInventory>(
|
||||
val plugin: KSpigot,
|
||||
val inventoryType: InventoryType<T>,
|
||||
val title: String?,
|
||||
val pages: Map<Int, InventoryGUIPage>
|
||||
val pages: Map<Int, InventoryGUIPage<T>>
|
||||
)
|
||||
|
||||
abstract class InventoryGUI<T : ForInventory>(
|
||||
@@ -94,8 +94,10 @@ abstract class InventoryGUI<T : ForInventory>(
|
||||
|
||||
abstract operator fun set(slot: InventorySlotCompound<T>, value: ItemStack)
|
||||
|
||||
fun register() = data.plugin.inventoryGUIHolder.register(this)
|
||||
fun unregister() = data.plugin.inventoryGUIHolder.unregister(this)
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun register() = data.plugin.inventoryGUIHolder.register(this as InventoryGUI<ForInventory>)
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun unregister() = data.plugin.inventoryGUIHolder.unregister(this as InventoryGUI<ForInventory>)
|
||||
|
||||
}
|
||||
|
||||
@@ -165,8 +167,8 @@ class InventoryGUIShared<T : ForInventory>(
|
||||
|
||||
}
|
||||
|
||||
class InventoryGUIPage(
|
||||
val slots: Map<Int, InventoryGUISlot>,
|
||||
class InventoryGUIPage<T : ForInventory>(
|
||||
val slots: Map<Int, InventoryGUISlot<T>>,
|
||||
transitionTo: InventoryGUIPageChangeEffect?,
|
||||
transitionFrom: InventoryGUIPageChangeEffect?
|
||||
) {
|
||||
|
@@ -17,7 +17,7 @@ class InventoryGUIBuilder<T : ForInventory>(
|
||||
|
||||
var title: String = ""
|
||||
|
||||
private val guiSlots = HashMap<Int, InventoryGUIPage>()
|
||||
private val guiSlots = HashMap<Int, InventoryGUIPage<T>>()
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -35,7 +35,7 @@ class InventoryGUIPageBuilder<T : ForInventory>(
|
||||
val page: Int
|
||||
) {
|
||||
|
||||
private val guiSlots = HashMap<Int, InventoryGUISlot>()
|
||||
private val guiSlots = HashMap<Int, InventoryGUISlot<T>>()
|
||||
|
||||
var transitionTo: InventoryGUIPageChangeEffect? = null
|
||||
var transitionFrom: InventoryGUIPageChangeEffect? = null
|
||||
@@ -98,7 +98,7 @@ class InventoryGUIPageBuilder<T : ForInventory>(
|
||||
onChange
|
||||
))
|
||||
|
||||
private fun slots(slots: InventorySlotCompound<T>, element: InventoryGUISlot)
|
||||
private fun slots(slots: InventorySlotCompound<T>, element: InventoryGUISlot<T>)
|
||||
= slots.withInvType(type).forEach { curSlot ->
|
||||
curSlot.realSlotIn(type.dimensions)?.let { guiSlots[it] = element }
|
||||
}
|
||||
|
@@ -2,8 +2,8 @@ package net.axay.kspigot.inventory
|
||||
|
||||
import org.bukkit.inventory.ItemStack
|
||||
|
||||
interface InventoryGUISlot {
|
||||
fun onClick(clickEvent: InventoryGUIClickEvent)
|
||||
interface InventoryGUISlot<T : ForInventory> {
|
||||
fun onClick(clickEvent: InventoryGUIClickEvent<T>)
|
||||
}
|
||||
|
||||
// ELEMENT
|
||||
@@ -12,60 +12,60 @@ class InventoryGUIElementData(
|
||||
val itemStack: ItemStack
|
||||
)
|
||||
|
||||
abstract class InventoryGUIElement(
|
||||
abstract class InventoryGUIElement<T : ForInventory>(
|
||||
val inventoryGUIElementData: InventoryGUIElementData
|
||||
) : InventoryGUISlot
|
||||
) : InventoryGUISlot<T>
|
||||
|
||||
// Element implementations
|
||||
|
||||
open class InventoryGUIButton(
|
||||
open class InventoryGUIButton<T : ForInventory>(
|
||||
inventoryGUIElementData: InventoryGUIElementData,
|
||||
val action: (InventoryGUIClickEvent) -> Unit,
|
||||
) : InventoryGUIElement(inventoryGUIElementData) {
|
||||
val action: (InventoryGUIClickEvent<T>) -> Unit,
|
||||
) : InventoryGUIElement<T>(inventoryGUIElementData) {
|
||||
|
||||
override fun onClick(clickEvent: InventoryGUIClickEvent) {
|
||||
override fun onClick(clickEvent: InventoryGUIClickEvent<T>) {
|
||||
clickEvent.bukkitEvent.isCancelled = true
|
||||
action(clickEvent)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class InventoryGUIPlaceholder(
|
||||
class InventoryGUIPlaceholder<T : ForInventory>(
|
||||
inventoryGUIElementData: InventoryGUIElementData
|
||||
) : InventoryGUIElement(inventoryGUIElementData) {
|
||||
) : InventoryGUIElement<T>(inventoryGUIElementData) {
|
||||
|
||||
override fun onClick(clickEvent: InventoryGUIClickEvent) {
|
||||
override fun onClick(clickEvent: InventoryGUIClickEvent<T>) {
|
||||
clickEvent.bukkitEvent.isCancelled = true
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class InventoryGUIButtonPageChange(
|
||||
class InventoryGUIButtonPageChange<T : ForInventory>(
|
||||
inventoryGUIElementData: InventoryGUIElementData,
|
||||
calculator: InventoryGUIPageChangeCalculator,
|
||||
onChange: ((InventoryGUIClickEvent) -> Unit)?
|
||||
onChange: ((InventoryGUIClickEvent<T>) -> Unit)?
|
||||
)
|
||||
: InventoryGUIButton(inventoryGUIElementData, {
|
||||
it.gui.currentPage?.let { currentPageInt ->
|
||||
: InventoryGUIButton<T>(inventoryGUIElementData, {
|
||||
|
||||
val newPageInt = calculator.calculateNewPage(currentPageInt, it.gui.data.pages.keys)
|
||||
if (newPageInt != null) {
|
||||
val currentPage = it.gui.currentPage
|
||||
|
||||
onChange?.invoke(it)
|
||||
val newPageInt = calculator.calculateNewPage(currentPage, it.gui.data.pages.keys)
|
||||
if (newPageInt != null) {
|
||||
|
||||
val pageChanger
|
||||
= (it.gui.data.pages[newPageInt]?.pageChangerTo ?: it.gui.data.pages[currentPageInt]?.pageChangerFrom)
|
||||
?: InventoryGUIPageChanger(InventoryGUIPageChangeEffect.INSTANT)
|
||||
onChange?.invoke(it)
|
||||
|
||||
pageChanger.changePage(it.gui, currentPageInt, newPageInt)
|
||||
val pageChanger
|
||||
= (it.gui.data.pages[newPageInt]?.pageChangerTo ?: it.gui.data.pages[currentPage]?.pageChangerFrom)
|
||||
?: InventoryGUIPageChanger(InventoryGUIPageChangeEffect.INSTANT)
|
||||
|
||||
}
|
||||
pageChanger.changePage(it.gui, currentPage, newPageInt)
|
||||
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
// FREE SLOT
|
||||
|
||||
class InventoryGUIFreeSlot : InventoryGUISlot {
|
||||
override fun onClick(clickEvent: InventoryGUIClickEvent) { /* do nothing */ }
|
||||
class InventoryGUIFreeSlot<T : ForInventory> : InventoryGUISlot<T> {
|
||||
override fun onClick(clickEvent: InventoryGUIClickEvent<T>) { /* do nothing */ }
|
||||
}
|
Reference in New Issue
Block a user