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