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 Sorting Method / Toplists

Discussion in 'Tutorials' started by AsuDev, Sep 24, 2019.

  1. AsuDev

    VIP

    Joined:
    Jan 27, 2017
    Messages:
    150
    Likes Received:
    15
    Requirements: Skript and Skript-Mirror 2.0
    Tested on: 1.8.8, 1.13.2, 1.14.4
    Difficulty: Semi-Basic/Complex

    This is a small tutorial on how to create a toplist with Skript based on a couple expressions made in skript-mirror.

    The skript-mirror expressions / sorting algorithm:
    Code (Skript):
    1. import:
    2.     java.util.ArrayList
    3.     java.util.Collections
    4.     java.util.Map$Entry
    5.     ch.njol.skript.variables.Variables
    6.  
    7. expression replace values %strings% with %strings% in %string%:
    8.     get:
    9.         set {_string} to expression-3
    10.         loop expressions-1:
    11.             add 1 to {_index}
    12.             replace all "%loop-value%" with "%{_index}th element of expressions-2%" in {_string}
    13.         return {_string}
    14.  
    15. plural expression [the] (1¦(highest|top)|2¦(lowest|last)) %integer% values of %objects% [formatted] as %string%:
    16.     get:
    17.         set {_list} to new ArrayList(Variables.getVariable(raw expressions-2.getName().toString(event), event, raw expressions-2.isLocal()).entrySet())
    18.         {_list}.sort(Entry.comparingByValue())
    19.         if parse mark = 1:
    20.             Collections.reverse({_list})
    21.         loop ...{_list}:
    22.             "%loop-value.getValue()%" is not "0"
    23.             add 1 to {_index}
    24.             set {_sorted::%{_index}%} to replace values "@place", "@index" and "@value" with "%{_index}%", "%loop-value.getKey()%" and "%loop-value.getValue()%" in expression-3
    25.             {_index} = expression-1
    26.             stop loop
    27.         return {_sorted::*}
    These expressions will allow us to make toplists. The syntax is not that hard to use and it uses a very efficient sorting algorithm. It is much faster than anything you can really make with vanilla Skript. I did some testing and this algorithm can sort up to 50,000 values in one second!

    You may be thinking, what are "@place" and "@index" and "@value" in this list. @place represents what place the value is in, for example, first place or second place. @index represents the key. For example, in a toplist, the key would be the player name or UUID. @value represents the value. For example, if a toplist was based on kills, it would be the amount of kills the player has.

    Example toplist #1 ( Top Kills based on {Kills::*} ):
    Code (Skript):
    1. command /topkillers:
    2.     trigger:
    3.         set {_topKillers::*} to the highest (size of {Kills::*}) values of {Kills::*} as "&e@place. &b@index &7- &6@value"
    4.         message "&eTop Killers"
    5.         loop {_topkillers::*}:
    6.             message loop-value
    7.             #if loop-index is "10":
    8.             #    stop loop
    This toplist example will sort everything in {Kills::*} from highest to lowest.

    Example toplist #2 ( Top Kills based on {Kills::*}, 10 displayed per page )
    Code (Skript):
    1. command /topkillers [<integer=1>]:
    2.     trigger:
    3.  
    4.         # Example amount to show per page. In this example, its 10
    5.         set {_showPerPage} to 10
    6.         set {_toSortTo} to arg 1 * {_showPerPage}
    7.  
    8.         set {_topKillers::*} to the highest {_toSortTo} values of {Kills::*} as "&e@place. &b@index &7- Kills: &6@value"
    9.         message "&eTop Killers Page %arg 1%"
    10.         loop {_topkillers::*}:
    11.             if ("%loop-index%" parsed as integer) is between {_toSortTo} - ({_showPerPage} - 1) and {_toSortTo}:
    12.                 message loop-value
    This toplist example will set values until a certain point (more efficient than sorting all unless you need to). This toplist uses a page system.

    Picture Example of toplist #2: Capture.PNG

    I could add a few more examples but I should have given you enough info to get started on your own. Good luck with your toplists!

    Credits: EWS (I didn't originally make this method of sorting. I edited the original version from Mirrorutils and also showed how to use it.)
     
    #1 AsuDev, Sep 24, 2019
    Last edited: Oct 3, 2019
  2. Runakai1

    Runakai1 Well-Known Member

    Joined:
    Apr 27, 2018
    Messages:
    394
    Likes Received:
    20
    Also add that this is from Mirrorutils
     
    • Agree Agree x 1
  3. AsuDev

    VIP

    Joined:
    Jan 27, 2017
    Messages:
    150
    Likes Received:
    15
    While it is originally from Mirrorutils, I will simply state that it was originally from Mirrorutils but I am not saying that you need it as a requirement. This is a standalone piece of code that was edited and credit was given.
     
  4. BaeFell

    BaeFell I'm Poppy
    Admin

    Joined:
    Nov 27, 2016
    Messages:
    479
    Likes Received:
    214
    This is exactly what I needed! You've just saved me a lot of time reworking my old toplist code lol. Thanks @AsuDev! :emoji_heart:
     
    • Friendly Friendly x 1
  5. P

    PuchiGFX Member

    Joined:
    Jan 26, 2017
    Messages:
    205
    Likes Received:
    4
    upload_2019-10-20_0-52-15.png
    I get this error :-:
     
  6. AsuDev

    VIP

    Joined:
    Jan 27, 2017
    Messages:
    150
    Likes Received:
    15
  7. P

    PuchiGFX Member

    Joined:
    Jan 26, 2017
    Messages:
    205
    Likes Received:
    4
    Using spigot 1.8.8 and skript dev2.25
     
  8. AsuDev

    VIP

    Joined:
    Jan 27, 2017
    Messages:
    150
    Likes Received:
    15
  9. P

    PuchiGFX Member

    Joined:
    Jan 26, 2017
    Messages:
    205
    Likes Received:
    4
Loading...