1. Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the comminuty!

    Now, what are you waiting for? Join the community now!

Dismiss Notice
This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Skript Paginated GUI Example

Discussion in 'Tutorials' started by AsuDev, Aug 22, 2019.

Tags:
  1. AsuDev

    VIP

    Joined:
    Jan 27, 2017
    Messages:
    150
    Likes Received:
    15
    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.

    Code (Skript):
    1.  
    2. # Paginated GUI Example
    3.  
    4. # Made by AsuDev
    5.  
    6. # What I used:
    7. # Skript 2.3.6+
    8.  
    9. # Tested on Paper Spigot 1.13.2
    10.  
    11. # Function that returns true or false based on if the player can open a specific page
    12. function checkForSpecifiedPage(slot: number, guiSize: number, list: objects) :: boolean:
    13.     set {_counter} and {_format} to 0
    14.     loop {_list::*}:
    15.         if {_counter}+1 is between {_slot} and {_slot}+({_guiSize}-1):
    16.             add 1 to {_format}
    17.         add 1 to {_counter}
    18.     if {_format} is 0:
    19.         return false
    20.     else:
    21.         return true
    22.  
    23. # Example Paginated GUI
    24. command /pagtest [<integer=1>]:
    25.     trigger:
    26.  
    27.         # Make sure to block anything below or equal to 0 as a page
    28.         if arg 1 is less than or equal to 0:
    29.             message "&c0 &7is not a valid page."
    30.             stop
    31.  
    32.         # Example size of the inventory / Must be divisible by 9 and up to 54
    33.         # In the example I am using, I cannot use 54 size because I am going to
    34.         # dedicate 1 row for the page switchers, therefore the max size in this
    35.         # example can only be 45
    36.         set {_guiSize} to 9
    37.  
    38.         # List to use as an example for the GUI
    39.         set {_c} to 0
    40.         loop 100 times:
    41.             add 1 of apple named "&cApple %{_c}%" to {_list::*}
    42.             add 1 to {_c}
    43.  
    44.         # Gets the index to start at in the list variable when formatting the GUI
    45.         set {_slot} to ((arg 1 * {_guiSize}) + 1) - {_guiSize}
    46.  
    47.         # Checks if the specified page has anything in it / Gets how many available pages there are
    48.         if checkForSpecifiedPage({_slot}, {_guiSize}, {_list::*}) is false:
    49.             set {_availablePages} to ceil(size of {_list::*}/{_guiSize})
    50.             message "&7Invalid page. &7You have &c%{_availablePages}% &7available pages."
    51.             stop
    52.  
    53.         # Open the Gui to the player / I used +1 because I want a row dedicated for the page turners
    54.         open chest with ({_guiSize}/9) + 1 rows named "&cApples &8(&9Page %arg 1%&8)" to the player
    55.  
    56.         # Example buttons to switch to different pages / The last row in this case is dedicated to these
    57.         set slot {_guiSize}+2 of player's current inventory to 1 of feather named "&c&l<- PREVIOUS PAGE&r"
    58.         set slot {_guiSize}+6 of player's current inventory to 1 of feather named "&a&lNEXT PAGE ->&r"
    59.  
    60.         # Formats the gui
    61.         set {_counter} and {_format} to 0
    62.         loop {_list::*}:
    63.             if {_counter}+1 is between {_slot} and {_slot}+({_guiSize}-1):
    64.                 set slot {_format} of player's current inventory to loop-value
    65.                 add 1 to {_format}
    66.             add 1 to {_counter}
    67.  
    68. # You need some kind of identifier to know what inventory your clicking in.
    69. # In this examples case, I will use the GUI name.
    70. # You also need an identifier to get what page you are currently on. I also used the name for this.
    71. on inventory click:
    72.     name of player's current inventory contains "&cApples &8(&9Page "
    73.     if name of clicked item is "&a&lNEXT PAGE ->&r" or "&c&l<- PREVIOUS PAGE&r":
    74.         cancel event
    75.  
    76.         # Gets the page you are on
    77.         set {_getCurrentPage} to name of player's current inventory
    78.         # Removes all text other than page number from name of the GUI / This is one method
    79.         # of getting the page they are on
    80.         replace all "&cApples &8(&9Page " and "&8)" with "" in {_getCurrentPage}
    81.         set {_getCurrentPage} to {_getCurrentPage} parsed as integer
    82.  
    83.         # Adds 1 or removes 1 based on if your clicking next page or previous page
    84.         if name of clicked item is "&a&lNEXT PAGE ->&r":
    85.             add 1 to {_getCurrentPage}
    86.         else:
    87.             remove 1 from {_getCurrentPage}
    88.  
    89.         # Makes sure the player doesn't open page 0
    90.         if {_getCurrentPage} is 0:
    91.             message "&c0 &7is not a valid page."
    92.             stop
    93.  
    94.         # Open the new page to the player by command
    95.         # You can also create a function if you do not want
    96.         # to use a command for this
    97.         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
     
    #1 AsuDev, Aug 22, 2019
    Last edited: Aug 22, 2019
Loading...