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.

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

Discussion in 'Snippets' started by Rezz, Mar 1, 2017.

  1. Rezz

    Addon Developer

    Joined:
    Jan 24, 2017
    Messages:
    80
    Likes Received:
    37
    Medals:
    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 (Skript):
    1. function contains(list: objects, check: object) :: boolean:
    2.     loop {_list::*}:
    3.         if loop-value is {_check}:
    4.             return true
    5.     return false
    Usage:

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

    Code (Skript):
    1. add "a", "bunch", "of", "list", "items", "to", and "check" to {_list::*}
    2.  
    3. if contains({_list::*}, "check") is true:
    4.     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 (Skript):
    1. on script load:
    2.  
    3.     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"
    4.     set {_letter-pairs::*} to {_letters} split at " "
    5.  
    6.     loop {_letter-pairs::*}:
    7.  
    8.         set {_pair::*} to loop-value split at ""
    9.  
    10.         set {_uppercase} to {_pair::1}
    11.         set {_lowercase} to {_pair::2}
    12.  
    13.         set {letters::uppercase::%{_lowercase}%} to {_uppercase}
    14.         set {letters::lowercase::%{_uppercase}%} to {_lowercase}
    15.  
    16. on script unload:
    17.  
    18.     delete {letters::*}
    19.  
    20. function switchCaseFor(text: text, to-uppercase: boolean) :: text:
    21.  
    22.     set {_characters::*} to {_text} split at ""
    23.     set {_new} to ""
    24.  
    25.     loop {_characters::*}:
    26.  
    27.         set {_char} to loop-value
    28.  
    29.         if {_to-uppercase} is true:
    30.             set {_char} to {letters::uppercase::%loop-value%}
    31.         else:
    32.             set {_char} to {letters::lowercase::%loop-value%}
    33.  
    34.         set {_new} to "%{_new}%%{_char}%"
    35.  
    36.     return {_new}
    37.  
    38. function uppercase(text: text) :: text:
    39.  
    40.     return switchCaseFor({_text}, true)
    41.  
    42. function lowercase(text: text) :: text:
    43.  
    44.     return switchCaseFor({_text}, false)
    45.  
    46. function capitalize(text: text) :: text:
    47.  
    48.     set {_char} to the first character of {_text}
    49.     set {_char} to {letters::uppercase::%{_char}%}
    50.  
    51.     set {_remaining} to ""
    52.     set {_remaining} to the last (length of {_text} - 1) characters of {_text}
    53.  
    54.     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 (Skript):
    1. command /upper <text>:
    2.     trigger:
    3.         send uppercase(arg-1)
    4.  
    5. command /lower <text>:
    6.     trigger:
    7.         send lowercase(arg-1)
    8.  
    9. command /cap <text>:
    10.     trigger:
    11.         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 (Skript):
    1. function anchoredStringReplace(string: text, with: text, in: text, first: boolean=true) :: text:
    2.  
    3.     if {_first}:
    4.         set {_where} to the first index of {_string} in {_in}
    5.     else:
    6.         set {_where} to the last index of {_string} in {_in}
    7.  
    8.     if {_where} is less than or equal to 0:
    9.  
    10.         return {_in}
    11.  
    12.     set {_left-length} to {_where} - 1
    13.     set {_left} to ""
    14.     set {_left} to the first {_left-length} characters of {_in}
    15.  
    16.     set {_right-start} to {_left-length} + length of {_string}
    17.     set {_right-length} to length of {_in} - {_right-start}
    18.     set {_right} to ""
    19.     set {_right} to the last {_right-length} characters of {_in}
    20.  
    21.     return "%{_left}%%{_with}%%{_right}%"
    22.  
    23. function replaceFirst(string: text, with: text, in: text) :: text:
    24.  
    25.     return anchoredStringReplace({_string}, {_with}, {_in})
    26.  
    27. function replaceLast(string: text, with: text, in: text) :: text:
    28.  
    29.     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 (Skript):
    1. command /replacement <text> <text>:
    2.     trigger:
    3.         set {_demo} to "Not everything in Skript has to be an addon feature. This is pure Skript 2.2!"
    4.  
    5.         set {_first} to replaceFirst(arg-1, arg-2, {_demo})
    6.         send "&6Replacing first:&7 %{_first}%"
    7.  
    8.         set {_last} to replaceLast(arg-1, arg-2, {_demo})
    9.         send "&6Replacing last:&7 %{_last}%"
    [​IMG]
    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 (Skript):
    1. options:
    2.  
    3.     DATA: skript-types
    4.  
    5. function declareUsableType(addon: text, user-input: text):
    6.  
    7.     set {_type} to {_user-input} parsed as type
    8.  
    9.     {_type} is set
    10.  
    11.     add {_type} to {{@DATA}::types::*}
    12.     set {_index} to amount of {{@DATA}::types::*}
    13.  
    14.     set {{@DATA}::type-of::%{_user-input}%} to {_type}
    15.     set {{@DATA}::user-input-of::%{_type}%} to {_user-input}
    16.     set {{@DATA}::index-of::%{_type}%} to {_index}
    17.  
    18.     add {_index} to {{@DATA}::addons::%{_addon}%::types::*}
    19.  
    20.     if {{@DATA}::addons::%{_addon}%} isn't set:
    21.  
    22.         set {{@DATA}::addons::%{_addon}%} to {_addon}
    23.  
    24. function declareUsableTypes(addon: text, types: texts):
    25.  
    26.     loop {_types::*}:
    27.         declareUsableType({_addon}, loop-value)
    28.  
    29. on script load:
    30.  
    31.     delete {{@DATA}::*}
    32.  
    33.     set {{@DATA}::object-type} to "object" parsed as type
    34.  
    35.     # Data Types
    36.     declareUsableTypes("skript", ("type", "boolean", "integer", "number", and "text"))
    37.  
    38.     # Players & Entities
    39.     declareUsableTypes("skript", ("player", "offline player", "command sender", "living entity", "entity", and "projectile"))
    40.  
    41.     # Blocks & Locations
    42.     declareUsableTypes("skript", ("block", "location", "vector", and "direction"))
    43.  
    44.     # Worlds
    45.     declareUsableTypes("skript", ("biome", "chunk", "world", and "weather"))
    46.  
    47.     # Items & Inventories
    48.     declareUsableTypes("skript", ("item", "item type", "potion effect", "inventory", "slot", "inventory action", and "click action"))
    49.  
    50.     # Enchantments
    51.     declareUsableTypes("skript", ("enchantment", and "enchantment type"))
    52.  
    53.     # Time
    54.     declareUsableTypes("skript", ("time", "timespan", "timeperiod", and "date"))
    55.  
    56.     # Misc
    57.     declareUsableTypes("skript", ("gamemode", "damage cause", "color", "tree type", and "particle effect"))
    58.  
    59. on script unload:
    60.  
    61.     delete {{@DATA}::*}
    62.  
    63. function typeof(var: object) :: type:
    64.  
    65.     loop {{@DATA}::types::*}:
    66.         if {_var} is loop-value:
    67.             return loop-value
    68.     return {{@DATA}::object-type}
    69.  
    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 (Skript):
    1. command /testtypes:
    2.     trigger:
    3.  
    4.         send "Type of 'true': %typeof(true)%"
    5.      
    6.         send "Type of '1': %typeof(1)%"
    7.      
    8.         send "Type of '3.1': %typeof(3.1)%"
    9.      
    10.         set {_string} to "Hi. I'm a string!"
    11.         send "Type of '%{_string}%': %typeof({_string})%"
    12.      
    13.         send "Type of '%sender%': %typeof(sender)%"
    14.      
    15.         set {_RezzedUp} to "RezzedUp" parsed as offline player
    16.         send "Type of '%{_RezzedUp}%': %typeof({_RezzedUp})%"
    17.      
    18.         send "Type of 'creative': %typeof(creative)%"
    19.      
    20.         send "Type of 'sharpness': %typeof(sharpness)%"
    21.      
    22.         send "Type of 'knockback 2': %typeof(knockback 2)%"
    23.      
    24.         send "Type of '5 minutes': %typeof(5 minutes)%"
    25.      
    26.         send "Type of '%now%': %typeof(now)%"
    27.  
    28.         send "Type of 'any regular tree': %typeof(any regular tree)%"
    29.      
    30.         send "Type of 'rain': %typeof(rain)%"
    Console after executing:

    [​IMG]
    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...
     
    • Useful Useful x 4
    • Like Like x 2
    • Creative Creative x 2
    • Winner Winner x 1

Share This Page

Loading...