Rezz's Snippets - Things you can do in pure Skript 2.2

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

Rezz

Addon Developer
Jan 24, 2017
80
37
18
Snippets:
  1. A replacement for 'contains'
  2. UPPERCASE, lowercase, and Capitalization conversions
  3. Replace only the first or last occurrence of text
  4. Types! Get a variable's type

Snippet #1: A replacement for 'contains'

code_language.skript:
function contains(list: objects, check: object) :: boolean:
    loop {_list::*}:
        if loop-value is {_check}:
            return true
    return false

Usage:

contains(<a list variable>, <absolutely anything>) returns boolean
Example:

code_language.skript:
add "a", "bunch", "of", "list", "items", "to", and "check" to {_list::*}

if contains({_list::*}, "check") is true:
    send "Yep! The list does indeed contain 'check'"
Description:

The 'contains' condition has been known to be broken for quite some time now (particularly when checking a list of text). This functions solves that problem by performing the check manually.

If you end up using this in a script, you may want to change the name to something more unique -- especially if you intend on releasing the script publicly. That way, function name conflicts can be avoided.​

Snippet #2: UPPERCASE, lowercase, and Capitalization conversions

code_language.skript:
on script load:

    set {_letters} to "Aa Bb Cc Dd Ee Ff Gg Hh Ii Jj Kk Ll Mm Nn Oo Pp Qq Rr Ss Tt Uu Vv Ww Xx Yy Zz"
    set {_letter-pairs::*} to {_letters} split at " "
 
    loop {_letter-pairs::*}:
 
        set {_pair::*} to loop-value split at ""
 
        set {_uppercase} to {_pair::1}
        set {_lowercase} to {_pair::2}
 
        set {letters::uppercase::%{_lowercase}%} to {_uppercase}
        set {letters::lowercase::%{_uppercase}%} to {_lowercase}
 
on script unload:

    delete {letters::*}
 
function switchCaseFor(text: text, to-uppercase: boolean) :: text:

    set {_characters::*} to {_text} split at ""
    set {_new} to ""
 
    loop {_characters::*}:
 
        set {_char} to loop-value
 
        if {_to-uppercase} is true:
            set {_char} to {letters::uppercase::%loop-value%}
        else:
            set {_char} to {letters::lowercase::%loop-value%}
 
        set {_new} to "%{_new}%%{_char}%"
 
    return {_new}

function uppercase(text: text) :: text:

    return switchCaseFor({_text}, true)
 
function lowercase(text: text) :: text:

    return switchCaseFor({_text}, false)
 
function capitalize(text: text) :: text:

    set {_char} to the first character of {_text}
    set {_char} to {letters::uppercase::%{_char}%}

    set {_remaining} to ""
    set {_remaining} to the last (length of {_text} - 1) characters of {_text}

    return "%{_char}%%{_remaining}%"

Usage:

uppercase(<text>) returns text

Converts all lowercase characters into their uppercase forms ("a" into "A").
lowercase(<text>) returns text

Converts all uppercase characters into their lowercase forms ("A" into "a").
capitalize(<text>) returns text

Capitalizes the first character of the text ("hello" into "Hello").
Example:

code_language.skript:
command /upper <text>:
    trigger:
        send uppercase(arg-1)
 
command /lower <text>:
    trigger:
        send lowercase(arg-1)
 
command /cap <text>:
    trigger:
        send capitalize(arg-1)
Description:

Several addons offer this functionality, but they're not necessary. Uppercase and lowercase conversions are 100% doable in pure Skript, as I've done here. No addons required. :emoji_smile:

Snippet #3: Replace only the first or last occurrence of text

code_language.skript:
function anchoredStringReplace(string: text, with: text, in: text, first: boolean=true) :: text:

    if {_first}:
        set {_where} to the first index of {_string} in {_in}
    else:
        set {_where} to the last index of {_string} in {_in}
 
    if {_where} is less than or equal to 0:
 
        return {_in}
 
    set {_left-length} to {_where} - 1
    set {_left} to ""
    set {_left} to the first {_left-length} characters of {_in}
 
    set {_right-start} to {_left-length} + length of {_string}
    set {_right-length} to length of {_in} - {_right-start}
    set {_right} to ""
    set {_right} to the last {_right-length} characters of {_in}
 
    return "%{_left}%%{_with}%%{_right}%"

function replaceFirst(string: text, with: text, in: text) :: text:

    return anchoredStringReplace({_string}, {_with}, {_in})
 
function replaceLast(string: text, with: text, in: text) :: text:

    return anchoredStringReplace({_string}, {_with}, {_in}, false)

Usage:

replaceFirst(<specific text>, <replacement text>, <full text>) returns text

Replaces the first occurrence of the specified text within the full text.
replaceLast(<specific text>, <replacement text>, <full text>) returns text

Replaces the last occurrence of the specified text within the full text.
If the specified text doesn't exist in the full text, the original full text is returned without any changes.​
Example:


code_language.skript:
command /replacement <text> <text>:
    trigger:
        set {_demo} to "Not everything in Skript has to be an addon feature. This is pure Skript 2.2!"
 
        set {_first} to replaceFirst(arg-1, arg-2, {_demo})
        send "&6Replacing first:&7 %{_first}%"
 
        set {_last} to replaceLast(arg-1, arg-2, {_demo})
        send "&6Replacing last:&7 %{_last}%"

I97mo5Z.png

Description:

These are simple, convenient functions for replacing the first and last occurrences of text: something that neither Skript's addons nor Skript itself provides out of the box.
Snippet #4: Types! Get a variable's type

code_language.skript:
options:

    DATA: skript-types
 
function declareUsableType(addon: text, user-input: text):

    set {_type} to {_user-input} parsed as type
 
    {_type} is set
 
    add {_type} to {{@DATA}::types::*}
    set {_index} to amount of {{@DATA}::types::*}
 
    set {{@DATA}::type-of::%{_user-input}%} to {_type}
    set {{@DATA}::user-input-of::%{_type}%} to {_user-input}
    set {{@DATA}::index-of::%{_type}%} to {_index}
 
    add {_index} to {{@DATA}::addons::%{_addon}%::types::*}
 
    if {{@DATA}::addons::%{_addon}%} isn't set:
 
        set {{@DATA}::addons::%{_addon}%} to {_addon}
 
function declareUsableTypes(addon: text, types: texts):

    loop {_types::*}:
        declareUsableType({_addon}, loop-value)

on script load:

    delete {{@DATA}::*}
 
    set {{@DATA}::object-type} to "object" parsed as type
 
    # Data Types
    declareUsableTypes("skript", ("type", "boolean", "integer", "number", and "text"))
 
    # Players & Entities
    declareUsableTypes("skript", ("player", "offline player", "command sender", "living entity", "entity", and "projectile"))
 
    # Blocks & Locations
    declareUsableTypes("skript", ("block", "location", "vector", and "direction"))
 
    # Worlds
    declareUsableTypes("skript", ("biome", "chunk", "world", and "weather"))
 
    # Items & Inventories
    declareUsableTypes("skript", ("item", "item type", "potion effect", "inventory", "slot", "inventory action", and "click action"))
 
    # Enchantments
    declareUsableTypes("skript", ("enchantment", and "enchantment type"))
 
    # Time
    declareUsableTypes("skript", ("time", "timespan", "timeperiod", and "date"))
 
    # Misc
    declareUsableTypes("skript", ("gamemode", "damage cause", "color", "tree type", and "particle effect"))

on script unload:

    delete {{@DATA}::*}

function typeof(var: object) :: type:

    loop {{@DATA}::types::*}:
        if {_var} is loop-value:
            return loop-value
    return {{@DATA}::object-type}

Usage:

typeof(<variable>) returns type

Returns the variable's type, or 'object' if its type isn't declared.
declareUsableTypes(<addon name>, <list of types>)

Declare all types listed in their user-input form. This function simply calls declareUsableType() on each list item.
declareUsableType(<addon name>, <type in standard user-input form>)

Declare a usable type. It's important to declare specific types before general types because declaration order is preserved. i.e. A variable of a specific type, like 'player', can also be considered a more general type as well -- 'offline player' in this example. If 'offline player' is declared before 'player', 'player' type variables will always be considered 'offline players'.
Example:

code_language.skript:
command /testtypes:
    trigger:
 
        send "Type of 'true': %typeof(true)%"
     
        send "Type of '1': %typeof(1)%"
     
        send "Type of '3.1': %typeof(3.1)%"
     
        set {_string} to "Hi. I'm a string!"
        send "Type of '%{_string}%': %typeof({_string})%"
     
        send "Type of '%sender%': %typeof(sender)%"
     
        set {_RezzedUp} to "RezzedUp" parsed as offline player
        send "Type of '%{_RezzedUp}%': %typeof({_RezzedUp})%"
     
        send "Type of 'creative': %typeof(creative)%"
     
        send "Type of 'sharpness': %typeof(sharpness)%"
     
        send "Type of 'knockback 2': %typeof(knockback 2)%"
     
        send "Type of '5 minutes': %typeof(5 minutes)%"
     
        send "Type of '%now%': %typeof(now)%"

        send "Type of 'any regular tree': %typeof(any regular tree)%"
     
        send "Type of 'rain': %typeof(rain)%"

Console after executing:

p2TIT0u.png

Description:

I created typeof() because Skript offers no built-in way to get a variable's type. Knowing the types you're dealing with is very important for any programming language, especially dynamically typed languages (like Skript) where variables can be any type.
More coming soon...