Merge pull request #16 from NyCodeGHG/gui-page-change

Extend gui page change button functionality
This commit is contained in:
Jakob K
2021-04-30 14:34:02 +02:00
committed by GitHub
2 changed files with 52 additions and 7 deletions

View File

@@ -113,15 +113,29 @@ class GUIPageBuilder<T : ForInventory>(
* This is a button which loads the specified
* [toPage] if clicked.
*/
@Deprecated("Use the new function instead.", ReplaceWith("pageChanger(slots, icon, toPage, null, onChange)"))
fun pageChanger(
slots: InventorySlotCompound<T>,
icon: ItemStack,
toPage: Int,
onChange: ((GUIClickEvent<T>) -> Unit)? = null
) = pageChanger(slots, icon, toPage, null, onChange)
/**
* This is a button which loads the specified
* [toPage] if clicked.
*/
fun pageChanger(
slots: InventorySlotCompound<T>,
icon: ItemStack,
toPage: Int,
shouldChange: ((GUIClickEvent<T>) -> Boolean)? = null,
onChange: ((GUIClickEvent<T>) -> Unit)? = null
) = defineSlots(
slots, GUIButtonPageChange(
icon,
GUIPageChangeCalculator.GUIConsistentPageCalculator(toPage),
shouldChange,
onChange
)
)
@@ -131,14 +145,28 @@ class GUIPageBuilder<T : ForInventory>(
* page if clicked, and if a previous page
* exists it is loaded.
*/
@Deprecated("Use the new function instead.", ReplaceWith("previousPage(slots, icon, null, onChange)"))
fun previousPage(
slots: InventorySlotCompound<T>,
icon: ItemStack,
onChange: ((GUIClickEvent<T>) -> Unit)? = null
) = previousPage(slots, icon, null, onChange)
/**
* This button always tries to find the previous
* page if clicked, and if a previous page
* exists it is loaded.
*/
fun previousPage(
slots: InventorySlotCompound<T>,
icon: ItemStack,
shouldChange: ((GUIClickEvent<T>) -> Boolean)? = null,
onChange: ((GUIClickEvent<T>) -> Unit)? = null
) = defineSlots(
slots, GUIButtonPageChange(
icon,
GUIPageChangeCalculator.GUIPreviousPageCalculator,
shouldChange,
onChange
)
)
@@ -148,14 +176,28 @@ class GUIPageBuilder<T : ForInventory>(
* page if clicked, and if a next page
* exists it is loaded.
*/
@Deprecated("Use the new function instead.", ReplaceWith("nextPage(slots, icon, null, onChange)"))
fun nextPage(
slots: InventorySlotCompound<T>,
icon: ItemStack,
onChange: ((GUIClickEvent<T>) -> Unit)? = null
) = nextPage(slots, icon, null, onChange)
/**
* This button always tries to find the next
* page if clicked, and if a next page
* exists it is loaded.
*/
fun nextPage(
slots: InventorySlotCompound<T>,
icon: ItemStack,
shouldChange: ((GUIClickEvent<T>) -> Boolean)? = null,
onChange: ((GUIClickEvent<T>) -> Unit)? = null
) = defineSlots(
slots, GUIButtonPageChange(
icon,
GUIPageChangeCalculator.GUINextPageCalculator,
shouldChange,
onChange
)
)
@@ -289,4 +331,4 @@ class GUIPageBuilder<T : ForInventory>(
GUISpaceCompoundScrollButton(icon, compound, scrollTimes, reverse)
)
}
}

View File

@@ -6,6 +6,7 @@ import org.bukkit.inventory.ItemStack
class GUIButtonPageChange<T : ForInventory>(
icon: ItemStack,
calculator: GUIPageChangeCalculator,
shouldChange: ((GUIClickEvent<T>) -> Boolean)?,
onChange: ((GUIClickEvent<T>) -> Unit)?
) : GUIButton<T>(icon, {
@@ -18,13 +19,15 @@ class GUIButtonPageChange<T : ForInventory>(
)
if (newPage != null) {
val effect = (newPage.transitionTo ?: currentPage.transitionFrom)
?: PageChangeEffect.INSTANT
val changePage = shouldChange?.invoke(it) ?: true
it.guiInstance.changePage(effect, currentPage, newPage)
onChange?.invoke(it)
if (changePage) {
val effect = (newPage.transitionTo ?: currentPage.transitionFrom)
?: PageChangeEffect.INSTANT
it.guiInstance.changePage(effect, currentPage, newPage)
onChange?.invoke(it)
}
}
})
})