From 8497c7f7e9fb3d6f69fed054ddcc1a6badcdcf98 Mon Sep 17 00:00:00 2001 From: NyCode Date: Fri, 30 Apr 2021 07:52:24 +0200 Subject: [PATCH] Apply requested changes --- .../kotlin/net/axay/kspigot/gui/GUIBuilder.kt | 44 ++++++++++++++++++- .../gui/elements/GUIButtonPageChange.kt | 8 ++-- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/net/axay/kspigot/gui/GUIBuilder.kt b/src/main/kotlin/net/axay/kspigot/gui/GUIBuilder.kt index 67f85a41..44527871 100644 --- a/src/main/kotlin/net/axay/kspigot/gui/GUIBuilder.kt +++ b/src/main/kotlin/net/axay/kspigot/gui/GUIBuilder.kt @@ -113,15 +113,29 @@ class GUIPageBuilder( * 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, icon: ItemStack, toPage: Int, onChange: ((GUIClickEvent) -> Unit)? = null + ) = pageChanger(slots, icon, toPage, null, onChange) + + /** + * This is a button which loads the specified + * [toPage] if clicked. + */ + fun pageChanger( + slots: InventorySlotCompound, + icon: ItemStack, + toPage: Int, + shouldChange: ((GUIClickEvent) -> Boolean)? = null, + onChange: ((GUIClickEvent) -> Unit)? = null ) = defineSlots( slots, GUIButtonPageChange( icon, GUIPageChangeCalculator.GUIConsistentPageCalculator(toPage), + shouldChange, onChange ) ) @@ -131,14 +145,28 @@ class GUIPageBuilder( * 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, icon: ItemStack, onChange: ((GUIClickEvent) -> 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, + icon: ItemStack, + shouldChange: ((GUIClickEvent) -> Boolean)? = null, + onChange: ((GUIClickEvent) -> Unit)? = null ) = defineSlots( slots, GUIButtonPageChange( icon, GUIPageChangeCalculator.GUIPreviousPageCalculator, + shouldChange, onChange ) ) @@ -148,14 +176,28 @@ class GUIPageBuilder( * 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, icon: ItemStack, onChange: ((GUIClickEvent) -> 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, + icon: ItemStack, + shouldChange: ((GUIClickEvent) -> Boolean)? = null, + onChange: ((GUIClickEvent) -> Unit)? = null ) = defineSlots( slots, GUIButtonPageChange( icon, GUIPageChangeCalculator.GUINextPageCalculator, + shouldChange, onChange ) ) @@ -289,4 +331,4 @@ class GUIPageBuilder( GUISpaceCompoundScrollButton(icon, compound, scrollTimes, reverse) ) -} \ No newline at end of file +} diff --git a/src/main/kotlin/net/axay/kspigot/gui/elements/GUIButtonPageChange.kt b/src/main/kotlin/net/axay/kspigot/gui/elements/GUIButtonPageChange.kt index f7724e9f..2e7d57eb 100644 --- a/src/main/kotlin/net/axay/kspigot/gui/elements/GUIButtonPageChange.kt +++ b/src/main/kotlin/net/axay/kspigot/gui/elements/GUIButtonPageChange.kt @@ -6,8 +6,8 @@ import org.bukkit.inventory.ItemStack class GUIButtonPageChange( icon: ItemStack, calculator: GUIPageChangeCalculator, - beforeChange: ((GUIClickEvent) -> Boolean)?, - afterChange: ((GUIClickEvent) -> Unit)? + shouldChange: ((GUIClickEvent) -> Boolean)?, + onChange: ((GUIClickEvent) -> Unit)? ) : GUIButton(icon, { val currentPage = it.guiInstance.currentPage @@ -19,14 +19,14 @@ class GUIButtonPageChange( ) if (newPage != null) { - val changePage = beforeChange?.invoke(it) ?: true + val changePage = shouldChange?.invoke(it) ?: true if (changePage) { val effect = (newPage.transitionTo ?: currentPage.transitionFrom) ?: PageChangeEffect.INSTANT it.guiInstance.changePage(effect, currentPage, newPage) - afterChange?.invoke(it) + onChange?.invoke(it) } }