Paginated GUI Example

Difficulty: Basic

This isn't so much as a tutorial but more of an example of a paginated GUI. This is to help you get an idea of how you yourself can create and format one based off of a list variable.


# Paginated GUI Example

# Made by AsuDev

# What I used:
# Skript 2.3.6+

# Tested on Paper Spigot 1.13.2

# Function that returns true or false based on if the player can open a specific page
function checkForSpecifiedPage(slot: number, guiSize: number, list: objects) :: boolean:
set {_counter} and {_format} to 0
loop {_list::*}:
if {_counter}+1 is between {_slot} and {_slot}+({_guiSize}-1):
add 1 to {_format}
add 1 to {_counter}
if {_format} is 0:
return false
else:
return true

# Example Paginated GUI
command /pagtest [<integer=1>]:
trigger:

# Make sure to block anything below or equal to 0 as a page
if arg 1 is less than or equal to 0:
message "&c0 &7is not a valid page."
stop

# Example size of the inventory / Must be divisible by 9 and up to 54
# In the example I am using, I cannot use 54 size because I am going to
# dedicate 1 row for the page switchers, therefore the max size in this
# example can only be 45
set {_guiSize} to 9

# List to use as an example for the GUI
set {_c} to 0
loop 100 times:
add 1 of apple named "&cApple %{_c}%" to {_list::*}
add 1 to {_c}

# Gets the index to start at in the list variable when formatting the GUI
set {_slot} to ((arg 1 * {_guiSize}) + 1) - {_guiSize}

# Checks if the specified page has anything in it / Gets how many available pages there are
if checkForSpecifiedPage({_slot}, {_guiSize}, {_list::*}) is false:
set {_availablePages} to ceil(size of {_list::*}/{_guiSize})
message "&7Invalid page. &7You have &c%{_availablePages}% &7available pages."
stop

# Open the Gui to the player / I used +1 because I want a row dedicated for the page turners
open chest with ({_guiSize}/9) + 1 rows named "&cApples &8(&9Page %arg 1%&8)" to the player

# Example buttons to switch to different pages / The last row in this case is dedicated to these
set slot {_guiSize}+2 of player's current inventory to 1 of feather named "&c&l<- PREVIOUS PAGE&r"
set slot {_guiSize}+6 of player's current inventory to 1 of feather named "&a&lNEXT PAGE ->&r"

# Formats the gui
set {_counter} and {_format} to 0
loop {_list::*}:
if {_counter}+1 is between {_slot} and {_slot}+({_guiSize}-1):
set slot {_format} of player's current inventory to loop-value
add 1 to {_format}
add 1 to {_counter}

# You need some kind of identifier to know what inventory your clicking in.
# In this examples case, I will use the GUI name.
# You also need an identifier to get what page you are currently on. I also used the name for this.
on inventory click:
name of player's current inventory contains "&cApples &8(&9Page "
if name of clicked item is "&a&lNEXT PAGE ->&r" or "&c&l<- PREVIOUS PAGE&r":
cancel event

# Gets the page you are on
set {_getCurrentPage} to name of player's current inventory
# Removes all text other than page number from name of the GUI / This is one method
# of getting the page they are on
replace all "&cApples &8(&9Page " and "&8)" with "" in {_getCurrentPage}
set {_getCurrentPage} to {_getCurrentPage} parsed as integer

# Adds 1 or removes 1 based on if your clicking next page or previous page
if name of clicked item is "&a&lNEXT PAGE ->&r":
add 1 to {_getCurrentPage}
else:
remove 1 from {_getCurrentPage}

# Makes sure the player doesn't open page 0
if {_getCurrentPage} is 0:
message "&c0 &7is not a valid page."
stop

# Open the new page to the player by command
# You can also create a function if you do not want
# to use a command for this
make player execute command "pagtest %{_getCurrentPage}%"

If you want to download this example, here is a link:
https://drive.google.com/open?id=1rPkqACuAB63gdZueESp8X-Ub1uhxLsOE

Also a pastebin link:
https://pastebin.com/6NPPT1Vc