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 a21cf2f0..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,6 +6,7 @@ import org.bukkit.inventory.ItemStack class GUIButtonPageChange( icon: ItemStack, calculator: GUIPageChangeCalculator, + shouldChange: ((GUIClickEvent) -> Boolean)?, onChange: ((GUIClickEvent) -> Unit)? ) : GUIButton(icon, { @@ -18,13 +19,15 @@ class GUIButtonPageChange( ) 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) + } } -}) \ No newline at end of file +})