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.

Useful vanilla Skript functions

Discussion in 'Snippets' started by EWS, Apr 18, 2018.

  1. EWS

    EWS Active Member

    Joined:
    Jan 26, 2017
    Messages:
    51
    Likes Received:
    26
    Medals:
    Here is a list of some functions which do plenty of useful stuff (mostly with numbers). Most of them are made by me.

    Round to x decimal cases
    Does what Skript's built-in function should do, rounds to the amount of decimal places you want.
    Code:
    Code (Skript):
    1.  
    2. function roundto(n: number, to: number) :: number:
    3.    return floor(({_n} * 10^{_to}) + 0.5) / 10^{_to}
    4.  
    Usage:
    Code (Skript):
    1.  
    2. roundto(17931.36456, 2)
    3.  
    Result: 17931.36

    Format number
    Formats a number so it can be easily readable, e.g.:
    1000 = 1k
    1234567.89 = 1.234M
    Code:
    Code (Skript):
    1.  
    2. function format(n: number) :: text:
    3.    set {_data} to "QT,18|Q,15|T,12|B,9|M,6|k,3"
    4.    loop split {_data} at "|":
    5.        set {_s::*} to split loop-value at ","
    6.        {_n} >= 10 ^ {_s::2} parsed as number
    7.        return "%{_n} / 10 ^ {_s::2} parsed as number%%{_s::1}%"
    8.    return "%{_n}%"
    9.  
    Usage:
    Code (Skript):
    1.  
    2. nformat(13618465182.25)
    3.  
    Result: 13.618B

    Unformat number
    Does the opposite of format number, you get "10k", and it returns "10000".
    * Only supports one digit characters.
    Code:
    Code (Skript):
    1.  
    2. function unformat(t: text) :: number:
    3.    set {_v} to ""
    4.    set {_data} to "Q,15|T,12|B,9|M,6|k,3"
    5.    loop split {_data} at "|":
    6.        set {_s::*} to split loop-value at ","
    7.        set {_d::%{_s::1}%} to {_s::2} parsed as number
    8.    loop split {_t} at "":
    9.        if {_d::%loop-value%} is set:
    10.            add {_d::%loop-value%} to {_n}
    11.        else:
    12.            set {_v} to "%{_v}%%loop-value%"
    13.    return ({_v} parsed as number) * 10^{_n}
    14.  
    Usage:
    Code (Skript):
    1.  
    2. unformat("15B")
    3.  
    Result: 15000000000

    Spaced number
    Author: @Tlatoani

    Returns a spaced number, so you can read it better, e.g.:
    1234 = 1,234
    3475613312312 = 3,475,613,312,312
    Code (Skript):
    1.  
    2. function spaced(n: text) :: text:
    3.    set {_s::*} to split {_n} at "."
    4.    if {_s::*} is not set:
    5.        return a({_n})
    6.    else:
    7.        return "%a({_s::1})%.%{_s::2}%"
    8.  
    9. function a(b: text) :: text:
    10.    if length of {_b} > 3:
    11.        return "%a(first length of {_b} - 3 characters of {_b})%,%last 3 characters of {_b}%"
    12.    return {_b}
    13.  
    Usage:
    Code (Skript):
    1.  
    2. spaced("12345.67") # must be a text
    3.  
    Result: 12,345.67

    Format seconds
    Shorter formatted seconds:
    60 seconds = 1min
    100 seconds = 1min 40s
    86401 = 1h 0min 1s
    Code (Skript):
    1.  
    2. function formatseconds(n: number) :: timespan:
    3.     set {_a} to "%{_n}% seconds" parsed as timespan
    4.     set {_a} to "%{_a}%"
    5.     replace all " seconds" or " second" with "sec" in {_a}
    6.     replace all " minutes" or " minute" with "min" in {_a}
    7.     replace all " hours" or " hour" with "h" in {_a}
    8.     replace all " days" or " day" with "d" in {_a}
    9.     return {_a}
    10.  
    Usage:
    Code (Skript):
    1.  
    2. formatseconds(1200)
    3.  
    Result: 20min

    Block cuboid (no addons & fast)
    Gets the block within two locations. In my tests, achieves more than 200k blocks per second.
    Code (Skript):
    1.  
    2. function getblocks(1: location, 2: location) :: blocks:
    3.    set {_x} to 1
    4.    set {_y} to 1
    5.    set {_z} to 1
    6.  
    7.    if x coord of {_1} > x coord of {_2}:
    8.        set {_x} to -1
    9.    if y coord of {_1} > y coord of {_2}:
    10.        set {_y} to -1
    11.    if z coord of {_1} > z coord of {_2}:
    12.        set {_z} to -1
    13.  
    14.    set {_base1} to {_1}
    15.    loop abs(x coord of {_1} - x coord of {_2}) + 1 times:
    16.        loop abs(z coord of {_1} - z coord of {_2}) + 1 times:
    17.            loop abs(y coord of {_1} - y coord of {_2}) + 1 times:
    18.  
    19.                add 1 to {_current}
    20.                set {_b::%{_current}%} to block at {_1}
    21.              
    22.                add {_y} to y coord of {_1}
    23.          
    24.            add {_z} to z coord of {_1}
    25.            set y coord of {_1} to y coord of {_base1}
    26.  
    27.        add {_x} to x coord of {_1}
    28.        set z coord of {_1} to z coord of {_base1}
    29.     return {_b::*}
    30.  
    Usage:
    Code (Text):
    1.  
    2. getblocks({_pos1}, {_pos2})
    3.  
    Result: will get blocks within {_pos1} and {_pos2} and put them in a list.

    Hope you enjoy!​
     
    #1 EWS, Apr 18, 2018
    Last edited: Apr 27, 2018
    • Like Like x 2
    • Winner Winner x 2
    • Informative Informative x 1
  2. JustADev

    JustADev Well-Known Member

    Joined:
    Apr 8, 2017
    Messages:
    380
    Likes Received:
    9
    @EWS Your Format Number Snippit is incorrect sometimes,
    It shows 1000k instead of 1M
     
  3. EWS

    EWS Active Member

    Joined:
    Jan 26, 2017
    Messages:
    51
    Likes Received:
    26
    Medals:
    Fixed it, changed > to >=
     
    • Winner Winner x 2
  4. JustADev

    JustADev Well-Known Member

    Joined:
    Apr 8, 2017
    Messages:
    380
    Likes Received:
    9

    Thanks :emoji_grinning:
    --- Double Post Merged, Sep 26, 2018, Original Post Date: Apr 27, 2018 ---
    Um why does this not work:
    Code (Skript):
    1. function formatseconds(n: number) :: timespan:
    2.   set {_a} to "%{_n}% seconds" parsed as timespan
    3.   set {_a} to "%{_a}%"
    4.   replace all " seconds" or " second" with "sec" in {_a}
    5.   replace all " minutes" or " minute" with "min" in {_a}
    6.   replace all " hours" or " hour" with "h" in {_a}
    7.   replace all " days" or " day" with "d" in {_a}
    8.   return {_a}
    9.  
    10. command /water:
    11.   trigger:
    12.     set {_1} to 600
    13.     set {_a} to formatseconds({_1})
    14.     broadcast "the value is: %{_a}%"
    The value it says is <none>
    Using Skript 1.13 alpha
     
  5. ShaneBee

    Moderator Resource Staff Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,184
    Likes Received:
    225
    Because the alpha versions of skript are having issues with local variables in functions
     

Share This Page

Loading...