AsuDev's Snippets

API AsuDev's Snippets 1.0.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!

Contributors
AsuDev
Supported Minecraft Versions
  1. 1.13
  2. 1.14
pic1.png

This small script API is just a collection of random but useful things I have used in projects and I wanted to share them.

Note: I have not tried this on versions lower than 1.13.2 so I do not know if it will work or not. I do not plan to test on lower versions and I will not reply to those who are on lower versions.

Here is the whole API along with explanations and usage of everything in it. This also includes the needed addons and such for everything.
code_language.skript:
# AsuDev's Snippets / Useful and random Skript Mirror and Function things for scripts

# Version 1.0.1 / Made by AsuDev

# For full compatibility, you need:
# Skript 2.3.6+
# Skript-Mirror 2.0+
# Sk-NbeeT
# skript-json 1.1.0
# GlowAPI / PacketListenerAPI (For Glow)
# PlaceholderAPI
# FastAsyncWorldEdit

# If you are not going to use certain features, you can comment them out or remove them. Also make sure to
# comment out or remove imports that you will not use.

# ##############################################################################################################

# Skript Mirror Imports

import:

    # Pretty much needed for anything skript mirror related
    org.bukkit.Bukkit

    # If using PlaceholderAPI
    me.clip.placeholderapi.PlaceholderAPI

    # If using GlowAPI
    org.inventivetalent.glow.GlowAPI
    org.inventivetalent.glow.GlowAPI$Color

    # If using Fawe Schematic pasting
    java.io.File
    com.sk89q.worldedit.bukkit.BukkitWorld
    com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats
    com.sk89q.worldedit.math.BlockVector3

# Get the NMS version
option nms:
  get:
    return Bukkit.getServer().getClass().getPackage().getName().split("\.")[3]

# NMS Imports

import:
    net.minecraft.server.{@nms}.PacketPlayOutEntityDestroy
    net.minecraft.server.{@nms}.PacketPlayOutAnimation

# ##############################################################################################################

# Functions and Methods in AsuDev's Snippets

# ##############################################################################################################

# Removes everything in a string and leaves just numbers
# Requires: Skript 2.3.6+
function removeAllButNumbers(s: string) :: number:
    set {_fullString::*} to {_s} split by ""
    loop {_fullString::*}:
        if loop-value is not "1" or "2" or "3" or "4" or "5" or "6" or "7" or "8" or "9" or "0":
            remove loop-value from {_fullString::*}
    set {_reAttached} to ""
    loop {_fullString::*}:
        set {_reAttached} to "%{_reAttached}%%loop-value%"
    set {_result} to {_reAttached} parsed as number
    if {_result} is not set:
        return -1
    if {_result} is less than or equal to 1:
        return -1
    return {_result}

#EXAMPLE
#set {_string} to "ABCDE12345"
#set {_number} to removeAllButNumbers({_string})
#broadcast "%{_string}%" # Will return 12345

# ##############################################################################################################

# Condition for checking if a string is alphanumerical / Feel free to change or add any characters
# Requires: Skript 2.3.6+ / Skript Mirror 2.0+
condition %string% is alphanumeric[al]:
    check:
        set {_stringSeparated::*} to expression-1 split by ""
        set {_size} to size of {_stringSeparated::*}
        delete {_stringSeparated::%{_size}%}
        loop {_stringSeparated::*}:
            if loop-value is not "a" or "b" or "c" or "d" or "e" or "f" or "g" or "h" or "i" or "j" or "k" or "l" or "m" or "n" or "o" or "p" or "q" or "r" or "s" or "t" or "u" or "v" or "w" or "x" or "y" or "z" or "1" or "2" or "3" or "4" or "5" or "6" or "7" or "8" or "9" or "0":
                set {_continue} to false
        if {_continue} is not set:
            continue

# EXAMPLE
#set {_string} to "ABCDEFG!@*$"
#if {_string} is alphanumeric:
#    message "The string is alphanumerical"
#else:
#    message "The string is not alphanumerical"

# ##############################################################################################################

# Regex replace in strings
# Requires: Skript 2.3.6+ / Skript-Mirror 2.0+
expression regex replace %string% with %string% in %string%:
    get:
        return expression-3.replaceAll(expr-1 and expr-2)

# EXAMPLE BELOW IN NEXT FUNCTION

# ##############################################################################################################

# This function formats numbers above 999 with commas (REQUIRES EXPRESSION ABOVE)
# Requires: Skript 2.3.6+ / Skript-Mirror 2.0+ / Expression above
function formatNumbers(i: text) :: string:
    return regex replace "(?<=\d)(?=(\d\d\d)+(?!\d))" with "," in {_i}

# EXAMPLE
#set {_formatted} to formatNumbers("15000")
#broadcast {_formatted} # {_formatted} will return 15,000

# ##############################################################################################################

# Made by EWS as a better and more efficient script for a snippet I made awhile back
# This expression returns the amount of an item a player can hold in their inventory
# Requires: Skript 2.3.6+ / Skript-Mirror 2.0+
expression (amount|number) of %itemstack% %inventory% can hold:
    get:
        loop ...expr-2.getStorageContents():
            if loop-value is not set:
                add max stack of expr-1 to {_a}
            else if loop-value = expr-1:
                add (expr-1.getMaxStackSize()) - (loop-value.getAmount()) to {_a}
        return {_a} ? 0

# EXAMPLE
#command /getstone <player>:
#    trigger:
#        set {_amount} to amount of stone arg 1's inventory can hold

# ##############################################################################################################

# This expression gets a value from placeholderapi for a specified player
# Requires: Skript 2.3.6+ / Skript-Mirror 2.0+ / PlaceholderAPI

expression placeholder %string% from %offline player%:
    get:
        return PlaceholderAPI.setPlaceholders(expression-2, "%%%expression-1%%%")

# EXAMPLE
#command /getplaceholder <text> <offline player>:
#    trigger:
#        set {_placeholderValue} to placeholder arg 1 from arg 2
#        message {_placeholderValue}
#
#        set {_placeholderValue2} to placeholder "player_ping" from arg 2
#        message {_placeholderValue2}

# ##############################################################################################################

# Serializes chest inventories and stores them as a string / Also works for player inventories
# Requires: Skript 2.3.6+ / skript-json 1.1.0
function jsonSerializeInventoryContents(inventory: inventory) :: String:
    set {_size} to {_inventory}'s amount of rows * 9
    set {_c} to 0
    loop {_size} times:
        add slot {_c} of {_inventory} to {_items::*}
        add 1 to {_c}
    if "%{_inventory}%" is not "inventory of <none>":
        loop 5 times:
            set {_i} to slot {_c} of {_inventory}
            if "%{_i}%" is "<none>":
                add air to {_items::*}
            else:
                add slot {_c} of {_inventory} to {_items::*}
            add 1 to {_c}
            delete {_i}
    return {_items::*}'s serialized json form

# EXAMPLE
#command /saveinventory:
#    trigger:
#        set {Inventory.%uuid of player%} to jsonSerializeInventoryContents(player's inventory)

# Deserializes a string and formats a gui with the contents / Also sorks for player inventories and optional arg for setting their saved armor
# Requires: Skript 2.3.6+ / skript-json 1.1.0 (Use function above for serializing)
function jsonSetSerializedInventoryContents(inventory: inventory, json: string, includeArmor: boolean):
    map json {_json} to {_items::*}
    set {_c} to 1
    loop ({_inventory}'s amount of rows * 9) times:
        set slot {_c}-1 of {_inventory} to {_items::%{_c}%}
        add 1 to {_c}
    if "%{_inventory}%" is not "inventory of <none>":
        if {_includeArmor} is true:
            loop 5 times:
                set slot {_c}-1 of {_inventory} to {_items::%{_c}%}
                add 1 to {_c}

# EXAMPLE
#command /returninventory:
#    trigger:
#        jsonSetSerializedInventoryContents(player's inventory, {Inventory.%uuid of player%})

# ##############################################################################################################

# Enable or disable an entity having their custom name visible
# Requires: Skript 2.3.6+ / Skript-Mirror 2.0
function entityDisplayNameVisibility(e: entity, b: boolean):
    {_e}.setCustomNameVisible({_b})

# Set the custom name of an entity (Display Name)
# Requires: Skript 2.3.6+ / Skript-Mirror 2.0
function setEntityDisplayName(e: entity, s: string):
    {_e}.setCustomName({_s})

# EXAMPLE
#on item spawn:
#    wait 1 ticks
#    entityDisplayNameVisibility(event-entity, true)
#    setEntityDisplayName(event-entity, "Test")

# ##############################################################################################################

# Make an entity glow with a certain color to players
# Requires: Skript 2.3.6+ / Skript-Mirror 2.0 / GlowAPI 1.4.7 / PacketListenerApi
effect make %entity% glow with color %string% for %players%:
    trigger:
        wait 1 ticks
        GlowAPI.setGlowing(expression-1, Color.valueOf(expression-2.toUpperCase()), expression-3)

# EXAMPLE
#command /testglow <text>:
#    trigger:
#        make player glow with color arg 1 for all players

# LIST OF GLOW COLORS
#black, dark_blue, dark_green, dark_aqua, dark_red, dark_purple, gold, gray, dark_gray, blue, green, aqua, red, purple, yellow, white, none

# ##############################################################################################################

# Reverses any list variable
# Requires: Skript 2.3.6+
function reverseList(list: objects) :: objects:
    set {_length} to size of {_list::*}
    loop {_length} times:
        set {_index} to {_length} - loop-number - 1
        add {_list::%{_index}%} to {_returnList::*}
    return {_returnList::*}

# ##############################################################################################################

# Gets the remainder of a number divided by another number / Used for function below
# Requires: Skript 2.3.6
function getNumberRemainder(i: number, amount: number) :: integer:
    set {_remainder} to floor({_i} / {_amount})
    set {_remainder2} to round({_remainder} * {_amount})
    set {_remainder} to round({_i} - {_remainder2})
    return {_remainder}

# Converts seconds into days, hours, minutes and seconds and returns it as a easy to split string
# Requires: Skript 2.3.6+
function secondsTimeConverter(i: number) :: text:
    set {_days} to floor({_i} / 86400)
    set {_hours} to floor(({_i} / 3600) - {_days}*24)
    set {_minutes} to floor((getNumberRemainder({_i}, 3600)) / 60)
    set {_seconds} to getNumberRemainder({_i}, 3600)
    set {_seconds} to getNumberRemainder({_seconds}, 60)
    return "%{_days}%,%{_hours}%,%{_minutes}%,%{_seconds}%"

# EXAMPLE
#set {_convertedSeconds} to secondsTimeConverter(3721)
#set {_convertedList::*} to {_convertedSeconds} split by ","
#set {_days} to {_convertedList::1} parsed as number
#set {_hours} to {_convertedList::2} parsed as number
#set {_minutes} to {_convertedList::3} parsed as number
#set {_seconds} to {_convertedList::4} parsed as number
#broadcast "&aSeconds Input: 3721"
#broadcast "&bResult: %{_days}% days, %{_hours}% hours, %{_minutes}% minutes and %{_seconds}% seconds"

# ##############################################################################################################

# Forces a player to swing their mainhand or offhand
# Requires: Skript 2.3.6+ / Skript Mirror 2.0
effect make %player% swing their (1¦mainhand|2¦offhand) seen by %players%:
    trigger:
        if parse mark is 1:
            loop expressions-2:
                loop-value.getHandle().playerConnection.sendPacket(new PacketPlayOutAnimation(expression-1.getHandle(), 0))
        else:
            loop expressions-2:
                loop-value.getHandle().playerConnection.sendPacket(new PacketPlayOutAnimation(expression-1.getHandle(), 3))        

# EXAMPLE
#make player swing their mainhand seen by all players
#make player swing their offhand seen by player

# ##############################################################################################################

# Removes a tag from the nbt of an item (EXPERIMENTAL)
# This may not work for all nbt tags.
# Requires: Skript 2.3.6+ / Sk-NBeeT / Skript Mirror 2.0+
expression remove custom nbt tag %strings% from %itemstack%:
    get:
        set {_itemNBT} to item-nbt of expression-2
        loop expressions-1:
            set {_value} to tag "%loop-value%" of item-nbt of expression-2
            replace all "%loop-value%:%{_value}%," with "" in {_itemNBT}
            replace all "%loop-value%:%{_value}%" with "" in {_itemNBT}
            delete {_value}
        set {_item} to expression-2 with nbt "%{_itemNBT}%"
        return {_item}

# EXAMPLE
#command /testremovenbt:
#    trigger:
#        set {_i} to 1 of diamond sword with nbt "{HideFlags:63,Unbreakable:1,Test:""RemoveMe""}"
#        set {_i} to remove custom nbt tag "Unbreakable" and "HideFlags" from {_i}
#        set {_i} to remove custom nbt tag "Test" from {_i}
#        give {_i} to player

# ##############################################################################################################

# Pastes a schematic using FaweAPI (REALLY FAST)
# This works for .schem files (You can try .schematic files but I don't know if it works or not)
# Requires: Skript 2.3.6+ / Skript Mirror 2.0+ / FastAsyncWorldEdit
effect paste schematic %string% at %location% include air %boolean%:
    trigger:
        set {_file} to new File(expression-1)
        set {_pasteLocation} to BlockVector3.at(expression-2.getBlockX(), expression-2.getBlockY(), expression-2.getBlockZ())
        set {_worldToPasteIn} to new BukkitWorld(expression-2.getWorld())
        set {_pasteSchem} to ClipboardFormats.findByFile({_file}).load({_file}).paste({_worldToPasteIn}, {_pasteLocation}, false, expression-3, null)

# EXAMPLE
#command /pasteschem <text>:
#    trigger:
#        paste schematic "plugins/FastAsyncWorldEdit/schematics/%arg 1%.schem" at location of player include air false

# ##############################################################################################################

# Checks to see if a player owns a dropped item (Used in conjunction with the effect below)
# Requires: Skript 2.3.6+ / Skript-Mirror 2.0+
condition %player% (1¦can|2¦cannot) pickup[ item of[ entity] ]%entity%:
    check:
        metadata value "Owner" of expression-2 is set
        set {_owners} to metadata value "Owner" of expression-2
        set {_owners::*} to {_owners} split by ","
        if parse mark is 1:
            loop {_owners::*}:
                if "%expression-1%" is loop-value:
                    set {_continue} to true
            {_continue} is set
            continue
        else:
            loop {_owners::*}:
                if "%expression-1%" is loop-value:
                    set {_continue} to false
            {_continue} is not set
            continue

# EXAMPLE EVENT
#on pickup:
#    if player can pickup item of event-entity:
#        broadcast "ayy i can pick it up"
#    else:
#        cancel event

# Drops an item for a specific player or group of players (Made to work with the condition above)
# Requires: Skript 2.3.6+ / Skript-Mirror 2.0+
effect drop %itemstack% at %location%(1¦ for %players%|2¦):
    trigger:
        set {_entity} to (world of expression-2).dropItem(expression-2, expression-1)
        if parse mark is 1:
            set {_owners} to ""
            set {_players::*} to all players
            loop expressions-3:
                set {_owners} to "%{_owners}%%loop-value%,"
                remove loop-value from {_players::*}
            set {_owners} to subtext of {_owners} from 1 to (length of {_owners} - 1)
            set metadata value "Owner" of {_entity} to {_owners}
            loop {_players::*}:
                loop-value.getHandle().playerConnection.sendPacket(new PacketPlayOutEntityDestroy({_entity}.getEntityId()))

# EXAMPLE
#command /dropitem:
#    trigger:
        # Drop an item for one specific player
#        drop 1 of stone at location of player for player
#
#       # Drop an item for all players
#       drop 1 of stone at location of player
#
#       # Drop an item for several but not all players
#       add random player out of all players to {_players::*}
#       add random player out of all players to {_players::*}
#       add random player out of all players to {_players::*}
#       drop 1 of stone at location of player for {_players::*}

# ##############################################################################################################
Feel free to suggest things you would like added and I'll try my best to add them.

Discord: AsuDev#0714
Author
AsuDev
Downloads
782
Views
782
First release
Last update
Rating
4.00 star(s) 1 ratings

More resources from AsuDev

Latest updates

  1. Fixed one small bug

    The function secondsTimeConverter was returning the wrong hours when over 24.

Latest reviews

One of the best APIs! But unfortunately I get a few errors.

[16:26:13] [Server thread/WARN]: [skript-reflect] Loaded class org.inventivetalent.glow.GlowAPI from GlowAPI v1.4.13-SNAPSHOT which is not a depend, softdepend or loadbefore of this plugin.
[16:26:13] [Server thread/WARN]: [Skript] me.clip.placeholderapi.PlaceholderAPI refers to a non-existent class. (AAA_Tools.sk, line 28: me.clip.placeholderapi.PlaceholderAPI')
[16:26:13] [Server thread/WARN]: [Skript] com.sk89q.worldedit.bukkit.BukkitWorld refers to a non-existent class. (AAA_Tools.sk, line 36: com.sk89q.worldedit.bukkit.BukkitWorld')
[16:26:13] [Server thread/WARN]: [Skript] com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats refers to a non-existent class. (AAA_Tools.sk, line 37: com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats')
[16:26:13] [Server thread/WARN]: [Skript] com.sk89q.worldedit.math.BlockVector3 refers to a non-existent class. (AAA_Tools.sk, line 38: com.sk89q.worldedit.math.BlockVector3')
[16:26:14] [Server thread/ERROR]: [Skript] Return may not be used if the code before it contains any delays. (AAA_Tools.sk, line 148: return PlaceholderAPI.setPlaceholders(expression-2, "%%%expression-1%%%")')
[16:26:14] [Server thread/ERROR]: [Skript] Return may only be used in custom expression getters. (AAA_Tools.sk, line 178: return {_items::*}'s serialized json form')
[16:26:14] [Server thread/ERROR]: [Skript] Can't understand this condition/effect: map json {_json} to {_items::*} (AAA_Tools.sk, line 188: map json {_json} to {_items::*}')
[16:26:21] [Server thread/ERROR]: [Skript] Can't understand this expression: 'BlockVector3.at(expression-2.getBlockX(), expression-2.getBlockY(), expression-2.getBlockZ())' (AAA_Tools.sk, line 329: set {_pasteLocation} to BlockVector3.at(expression-2.getBlockX(), expression-2.getBlockY(), expression-2.getBlockZ())')
[16:26:21] [Server thread/ERROR]: [Skript] The function 'BukkitWorld' does not exist. (AAA_Tools.sk, line 330: set {_worldToPasteIn} to new BukkitWorld(expression-2.getWorld())')
[16:26:21] [Server thread/ERROR]: [Skript] Can't understand this expression: 'ClipboardFormats.findByFile({_file}).load({_file}).paste({_worldToPasteIn}, {_pasteLocation}, false, expression-3, null)' (AAA_Tools.sk, line 331: set {_pasteSchem} to ClipboardFormats.findByFile({_file}).load({_file}).paste({_worldToPasteIn}, {_pasteLocation}, false, expression-3, null)')