Useful vanilla Skript functions

  • 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!

EWS

Member
Jan 26, 2017
49
27
18
23
Brazil
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_language.skript:
function roundto(n: number, to: number) :: number:
   return floor(({_n} * 10^{_to}) + 0.5) / 10^{_to}
Usage:
code_language.skript:
roundto(17931.36456, 2)
Result: 17931.36

Format number
Formats a number so it can be easily readable, e.g.:
1000 = 1k
1234567.89 = 1.234M
Code:
code_language.skript:
function format(n: number) :: text:
   set {_data} to "QT,18|Q,15|T,12|B,9|M,6|k,3"
   loop split {_data} at "|":
       set {_s::*} to split loop-value at ","
       {_n} >= 10 ^ {_s::2} parsed as number
       return "%{_n} / 10 ^ {_s::2} parsed as number%%{_s::1}%"
   return "%{_n}%"
Usage:
code_language.skript:
nformat(13618465182.25)
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_language.skript:
function unformat(t: text) :: number:
   set {_v} to ""
   set {_data} to "Q,15|T,12|B,9|M,6|k,3"
   loop split {_data} at "|":
       set {_s::*} to split loop-value at ","
       set {_d::%{_s::1}%} to {_s::2} parsed as number
   loop split {_t} at "":
       if {_d::%loop-value%} is set:
           add {_d::%loop-value%} to {_n}
       else:
           set {_v} to "%{_v}%%loop-value%"
   return ({_v} parsed as number) * 10^{_n}
Usage:
code_language.skript:
unformat("15B")
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_language.skript:
function spaced(n: text) :: text:
   set {_s::*} to split {_n} at "."
   if {_s::*} is not set:
       return a({_n})
   else:
       return "%a({_s::1})%.%{_s::2}%"

function a(b: text) :: text:
   if length of {_b} > 3:
       return "%a(first length of {_b} - 3 characters of {_b})%,%last 3 characters of {_b}%"
   return {_b}
Usage:
code_language.skript:
spaced("12345.67") # must be a text
Result: 12,345.67

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

Block cuboid (no addons & fast)
Gets the block within two locations. In my tests, achieves more than 200k blocks per second.
code_language.skript:
function getblocks(1: location, 2: location) :: blocks:
   set {_x} to 1
   set {_y} to 1
   set {_z} to 1
 
   if x coord of {_1} > x coord of {_2}:
       set {_x} to -1
   if y coord of {_1} > y coord of {_2}:
       set {_y} to -1
   if z coord of {_1} > z coord of {_2}:
       set {_z} to -1

   set {_base1} to {_1}
   loop abs(x coord of {_1} - x coord of {_2}) + 1 times:
       loop abs(z coord of {_1} - z coord of {_2}) + 1 times:
           loop abs(y coord of {_1} - y coord of {_2}) + 1 times:
 
               add 1 to {_current}
               set {_b::%{_current}%} to block at {_1}
             
               add {_y} to y coord of {_1}
         
           add {_z} to z coord of {_1}
           set y coord of {_1} to y coord of {_base1}
 
       add {_x} to x coord of {_1}
       set z coord of {_1} to z coord of {_base1}
    return {_b::*}
Usage:
Code:
getblocks({_pos1}, {_pos2})
Result: will get blocks within {_pos1} and {_pos2} and put them in a list.

Hope you enjoy!​
 
Last edited:
Fixed it, changed > to >=


Thanks :emoji_grinning:
[doublepost=1537985511,1524830992][/doublepost]Um why does this not work:
code_language.skript:
function formatseconds(n: number) :: timespan:
  set {_a} to "%{_n}% seconds" parsed as timespan
  set {_a} to "%{_a}%"
  replace all " seconds" or " second" with "sec" in {_a}
  replace all " minutes" or " minute" with "min" in {_a}
  replace all " hours" or " hour" with "h" in {_a}
  replace all " days" or " day" with "d" in {_a}
  return {_a}

command /water:
  trigger:
    set {_1} to 600
    set {_a} to formatseconds({_1})
    broadcast "the value is: %{_a}%"

The value it says is <none>
Using Skript 1.13 alpha
 
Thanks :emoji_grinning:
[doublepost=1537985511,1524830992][/doublepost]Um why does this not work:
code_language.skript:
function formatseconds(n: number) :: timespan:
  set {_a} to "%{_n}% seconds" parsed as timespan
  set {_a} to "%{_a}%"
  replace all " seconds" or " second" with "sec" in {_a}
  replace all " minutes" or " minute" with "min" in {_a}
  replace all " hours" or " hour" with "h" in {_a}
  replace all " days" or " day" with "d" in {_a}
  return {_a}

command /water:
  trigger:
    set {_1} to 600
    set {_a} to formatseconds({_1})
    broadcast "the value is: %{_a}%"

The value it says is <none>
Using Skript 1.13 alpha
Because the alpha versions of skript are having issues with local variables in functions