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.

AsuDev's Snippets

Discussion in 'Script Tools' started by Revelationage, Aug 22, 2019.

  1. Revelationage

    Revelationage Well-Known Member

    Joined:
    Jan 27, 2017
    Messages:
    128
    Likes Received:
    13
    View attachment 3715
    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 (Skript):
    1.  
    2. # AsuDev's Snippets / Useful and random Skript Mirror and Function things for scripts
    3.  
    4. # Version 1.0 / Made by AsuDev
    5.  
    6. # For full compatibility, you need:
    7. # Skript 2.3.6+
    8. # Skript-Mirror 2.0+
    9. # Sk-NbeeT
    10. # skript-json 1.1.0
    11. # GlowAPI / PacketListenerAPI (For Glow)
    12. # PlaceholderAPI
    13. # FastAsyncWorldEdit
    14.  
    15. # If you are not going to use certain features, you can comment them out or remove them. Also make sure to
    16. # comment out or remove imports that you will not use.
    17.  
    18. # ##############################################################################################################
    19.  
    20. # Skript Mirror Imports
    21.  
    22. import:
    23.  
    24.     # Pretty much needed for anything skript mirror related
    25.     org.bukkit.Bukkit
    26.  
    27.     # If using PlaceholderAPI
    28.     me.clip.placeholderapi.PlaceholderAPI
    29.  
    30.     # If using GlowAPI
    31.     org.inventivetalent.glow.GlowAPI
    32.     org.inventivetalent.glow.GlowAPI$Color
    33.  
    34.     # If using Fawe Schematic pasting
    35.     java.io.File
    36.     com.sk89q.worldedit.bukkit.BukkitWorld
    37.     com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats
    38.     com.sk89q.worldedit.math.BlockVector3
    39.  
    40. # Get the NMS version
    41. option nms:
    42.   get:
    43.     return Bukkit.getServer().getClass().getPackage().getName().split("\.")[3]
    44.  
    45. # NMS Imports
    46.  
    47. import:
    48.     net.minecraft.server.{@nms}.PacketPlayOutEntityDestroy
    49.     net.minecraft.server.{@nms}.PacketPlayOutAnimation
    50.  
    51. # ##############################################################################################################
    52.  
    53. # Functions and Methods in AsuDev's Snippets
    54.  
    55. # ##############################################################################################################
    56.  
    57. # Removes everything in a string and leaves just numbers
    58. # Requires: Skript 2.3.6+
    59. function removeAllButNumbers(s: string) :: number:
    60.     set {_fullString::*} to {_s} split by ""
    61.     loop {_fullString::*}:
    62.         if loop-value is not "1" or "2" or "3" or "4" or "5" or "6" or "7" or "8" or "9" or "0":
    63.             remove loop-value from {_fullString::*}
    64.     set {_reAttached} to ""
    65.     loop {_fullString::*}:
    66.         set {_reAttached} to "%{_reAttached}%%loop-value%"
    67.     set {_result} to {_reAttached} parsed as number
    68.     if {_result} is not set:
    69.         return -1
    70.     if {_result} is less than or equal to 1:
    71.         return -1
    72.     return {_result}
    73.  
    74. #EXAMPLE
    75. #set {_string} to "ABCDE12345"
    76. #set {_number} to removeAllButNumbers({_string})
    77. #broadcast "%{_string}%" # Will return 12345
    78.  
    79. # ##############################################################################################################
    80.  
    81. # Condition for checking if a string is alphanumerical / Feel free to change or add any characters
    82. # Requires: Skript 2.3.6+ / Skript Mirror 2.0+
    83. condition %string% is alphanumeric[al]:
    84.     check:
    85.         set {_stringSeparated::*} to expression-1 split by ""
    86.         set {_size} to size of {_stringSeparated::*}
    87.         delete {_stringSeparated::%{_size}%}
    88.         loop {_stringSeparated::*}:
    89.             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":
    90.                 set {_continue} to false
    91.         if {_continue} is not set:
    92.             continue
    93.  
    94. # EXAMPLE
    95. #set {_string} to "[email protected]*$"
    96. #if {_string} is alphanumeric:
    97. #    message "The string is alphanumerical"
    98. #else:
    99. #    message "The string is not alphanumerical"
    100.  
    101. # ##############################################################################################################
    102.  
    103. # Regex replace in strings
    104. # Requires: Skript 2.3.6+ / Skript-Mirror 2.0+
    105. expression regex replace %string% with %string% in %string%:
    106.     get:
    107.         return expression-3.replaceAll(expr-1 and expr-2)
    108.  
    109. # EXAMPLE BELOW IN NEXT FUNCTION
    110.  
    111. # ##############################################################################################################
    112.  
    113. # This function formats numbers above 999 with commas (REQUIRES EXPRESSION ABOVE)
    114. # Requires: Skript 2.3.6+ / Skript-Mirror 2.0+ / Expression above
    115. function formatNumbers(i: text) :: string:
    116.     return regex replace "(?<=\d)(?=(\d\d\d)+(?!\d))" with "," in {_i}
    117.  
    118. # EXAMPLE
    119. #set {_formatted} to formatNumbers("15000")
    120. #broadcast {_formatted} # {_formatted} will return 15,000
    121.  
    122. # ##############################################################################################################
    123.  
    124. # Made by EWS as a better and more efficient script for a snippet I made awhile back
    125. # This expression returns the amount of an item a player can hold in their inventory
    126. # Requires: Skript 2.3.6+ / Skript-Mirror 2.0+
    127. expression (amount|number) of %itemstack% %inventory% can hold:
    128.     get:
    129.         loop ...expr-2.getStorageContents():
    130.             if loop-value is not set:
    131.                 add max stack of expr-1 to {_a}
    132.             else if loop-value = expr-1:
    133.                 add (expr-1.getMaxStackSize()) - (loop-value.getAmount()) to {_a}
    134.         return {_a} ? 0
    135.  
    136. # EXAMPLE
    137. #command /getstone <player>:
    138. #    trigger:
    139. #        set {_amount} to amount of stone arg 1's inventory can hold
    140.  
    141. # ##############################################################################################################
    142.  
    143. # This expression gets a value from placeholderapi for a specified player
    144. # Requires: Skript 2.3.6+ / Skript-Mirror 2.0+ / PlaceholderAPI
    145.  
    146. expression placeholder %string% from %offline player%:
    147.     get:
    148.         return PlaceholderAPI.setPlaceholders(expression-2, "%%%expression-1%%%")
    149.  
    150. # EXAMPLE
    151. #command /getplaceholder <text> <offline player>:
    152. #    trigger:
    153. #        set {_placeholderValue} to placeholder arg 1 from arg 2
    154. #        message {_placeholderValue}
    155. #
    156. #        set {_placeholderValue2} to placeholder "player_ping" from arg 2
    157. #        message {_placeholderValue2}
    158.  
    159. # ##############################################################################################################
    160.  
    161. # Serializes chest inventories and stores them as a string / Also works for player inventories
    162. # Requires: Skript 2.3.6+ / skript-json 1.1.0
    163. function jsonSerializeInventoryContents(inventory: inventory) :: String:
    164.     set {_size} to {_inventory}'s amount of rows * 9
    165.     set {_c} to 0
    166.     loop {_size} times:
    167.         add slot {_c} of {_inventory} to {_items::*}
    168.         add 1 to {_c}
    169.     if "%{_inventory}%" is not "inventory of <none>":
    170.         loop 5 times:
    171.             set {_i} to slot {_c} of {_inventory}
    172.             if "%{_i}%" is "<none>":
    173.                 add air to {_items::*}
    174.             else:
    175.                 add slot {_c} of {_inventory} to {_items::*}
    176.             add 1 to {_c}
    177.             delete {_i}
    178.     return {_items::*}'s serialized json form
    179.  
    180. # EXAMPLE
    181. #command /saveinventory:
    182. #    trigger:
    183. #        set {Inventory.%uuid of player%} to jsonSerializeInventoryContents(player's inventory)
    184.  
    185. # Deserializes a string and formats a gui with the contents / Also sorks for player inventories and optional arg for setting their saved armor
    186. # Requires: Skript 2.3.6+ / skript-json 1.1.0 (Use function above for serializing)
    187. function jsonSetSerializedInventoryContents(inventory: inventory, json: string, includeArmor: boolean):
    188.     map json {_json} to {_items::*}
    189.     set {_c} to 1
    190.     loop ({_inventory}'s amount of rows * 9) times:
    191.         set slot {_c}-1 of {_inventory} to {_items::%{_c}%}
    192.         add 1 to {_c}
    193.     if "%{_inventory}%" is not "inventory of <none>":
    194.         if {_includeArmor} is true:
    195.             loop 5 times:
    196.                 set slot {_c}-1 of {_inventory} to {_items::%{_c}%}
    197.                 add 1 to {_c}
    198.  
    199. # EXAMPLE
    200. #command /returninventory:
    201. #    trigger:
    202. #        jsonSetSerializedInventoryContents(player's inventory, {Inventory.%uuid of player%})
    203.  
    204. # ##############################################################################################################
    205.  
    206. # Enable or disable an entity having their custom name visible
    207. # Requires: Skript 2.3.6+ / Skript-Mirror 2.0
    208. function entityDisplayNameVisibility(e: entity, b: boolean):
    209.     {_e}.setCustomNameVisible({_b})
    210.  
    211. # Set the custom name of an entity (Display Name)
    212. # Requires: Skript 2.3.6+ / Skript-Mirror 2.0
    213. function setEntityDisplayName(e: entity, s: string):
    214.     {_e}.setCustomName({_s})
    215.  
    216. # EXAMPLE
    217. #on item spawn:
    218. #    wait 1 ticks
    219. #    entityDisplayNameVisibility(event-entity, true)
    220. #    setEntityDisplayName(event-entity, "Test")
    221.  
    222. # ##############################################################################################################
    223.  
    224. # Make an entity glow with a certain color to players
    225. # Requires: Skript 2.3.6+ / Skript-Mirror 2.0 / GlowAPI 1.4.7 / PacketListenerApi
    226. effect make %entity% glow with color %string% for %players%:
    227.     trigger:
    228.         wait 1 ticks
    229.         GlowAPI.setGlowing(expression-1, Color.valueOf(expression-2.toUpperCase()), expression-3)
    230.  
    231. # EXAMPLE
    232. #command /testglow <text>:
    233. #    trigger:
    234. #        make player glow with color arg 1 for all players
    235.  
    236. # LIST OF GLOW COLORS
    237. #black, dark_blue, dark_green, dark_aqua, dark_red, dark_purple, gold, gray, dark_gray, blue, green, aqua, red, purple, yellow, white, none
    238.  
    239. # ##############################################################################################################
    240.  
    241. # Reverses any list variable
    242. # Requires: Skript 2.3.6+
    243. function reverseList(list: objects) :: objects:
    244.     set {_length} to size of {_list::*}
    245.     loop {_length} times:
    246.         set {_index} to {_length} - loop-number - 1
    247.         add {_list::%{_index}%} to {_returnList::*}
    248.     return {_returnList::*}
    249.  
    250. # ##############################################################################################################
    251.  
    252. # Gets the remainder of a number divided by another number / Used for function below
    253. # Requires: Skript 2.3.6
    254. function getNumberRemainder(i: number, amount: number) :: integer:
    255.     set {_remainder} to floor({_i} / {_amount})
    256.     set {_remainder2} to round({_remainder} * {_amount})
    257.     set {_remainder} to round({_i} - {_remainder2})
    258.     return {_remainder}
    259.  
    260. # Converts seconds into days, hours, minutes and seconds and returns it as a easy to split string
    261. # Requires: Skript 2.3.6+
    262. function secondsTimeConverter(i: number) :: text:
    263.     set {_days} to floor({_i} / 86400)
    264.     set {_hours} to floor({_i} / 3600)
    265.     set {_minutes} to floor((getNumberRemainder({_i}, 3600)) / 60)
    266.     set {_seconds} to getNumberRemainder({_i}, 3600)
    267.     set {_seconds} to getNumberRemainder({_seconds}, 60)
    268.     return "%{_days}%,%{_hours}%,%{_minutes}%,%{_seconds}%"
    269.  
    270. # EXAMPLE
    271. #set {_convertedSeconds} to secondsTimeConverter(3721)
    272. #set {_convertedList::*} to {_convertedSeconds} split by ","
    273. #set {_days} to {_convertedList::1} parsed as number
    274. #set {_hours} to {_convertedList::2} parsed as number
    275. #set {_minutes} to {_convertedList::3} parsed as number
    276. #set {_seconds} to {_convertedList::4} parsed as number
    277. #broadcast "&aSeconds Input: 3721"
    278. #broadcast "&bResult: %{_days}% days, %{_hours}% hours, %{_minutes}% minutes and %{_seconds}% seconds"
    279.  
    280. # ##############################################################################################################
    281.  
    282. # Forces a player to swing their mainhand or offhand
    283. # Requires: Skript 2.3.6+ / Skript Mirror 2.0
    284. effect make %player% swing their (1¦mainhand|2¦offhand) seen by %players%:
    285.     trigger:
    286.         if parse mark is 1:
    287.             loop expressions-2:
    288.                 loop-value.getHandle().playerConnection.sendPacket(new PacketPlayOutAnimation(expression-1.getHandle(), 0))
    289.         else:
    290.             loop expressions-2:
    291.                 loop-value.getHandle().playerConnection.sendPacket(new PacketPlayOutAnimation(expression-1.getHandle(), 3))      
    292.  
    293. # EXAMPLE
    294. #make player swing their mainhand seen by all players
    295. #make player swing their offhand seen by player
    296.  
    297. # ##############################################################################################################
    298.  
    299. # Removes a tag from the nbt of an item (EXPERIMENTAL)
    300. # This may not work for all nbt tags.
    301. # Requires: Skript 2.3.6+ / Sk-NBeeT / Skript Mirror 2.0+
    302. expression remove custom nbt tag %strings% from %itemstack%:
    303.     get:
    304.         set {_itemNBT} to item-nbt of expression-2
    305.         loop expressions-1:
    306.             set {_value} to tag "%loop-value%" of item-nbt of expression-2
    307.             replace all "%loop-value%:%{_value}%," with "" in {_itemNBT}
    308.             replace all "%loop-value%:%{_value}%" with "" in {_itemNBT}
    309.             delete {_value}
    310.         set {_item} to expression-2 with nbt "%{_itemNBT}%"
    311.         return {_item}
    312.  
    313. # EXAMPLE
    314. #command /testremovenbt:
    315. #    trigger:
    316. #        set {_i} to 1 of diamond sword with nbt "{HideFlags:63,Unbreakable:1,Test:""RemoveMe""}"
    317. #        set {_i} to remove custom nbt tag "Unbreakable" and "HideFlags" from {_i}
    318. #        set {_i} to remove custom nbt tag "Test" from {_i}
    319. #        give {_i} to player
    320.  
    321. # ##############################################################################################################
    322.  
    323. # Pastes a schematic using FaweAPI (REALLY FAST)
    324. # This works for .schem files (You can try .schematic files but I don't know if it works or not)
    325. # Requires: Skript 2.3.6+ / Skript Mirror 2.0+ / FastAsyncWorldEdit
    326. effect paste schematic %string% at %location% include air %boolean%:
    327.     trigger:
    328.         set {_file} to new File(expression-1)
    329.         set {_pasteLocation} to BlockVector3.at(expression-2.getBlockX(), expression-2.getBlockY(), expression-2.getBlockZ())
    330.         set {_worldToPasteIn} to new BukkitWorld(expression-2.getWorld())
    331.         set {_pasteSchem} to ClipboardFormats.findByFile({_file}).load({_file}).paste({_worldToPasteIn}, {_pasteLocation}, false, expression-3, null)
    332.  
    333. # EXAMPLE
    334. #command /pasteschem <text>:
    335. #    trigger:
    336. #        paste schematic "plugins/FastAsyncWorldEdit/schematics/%arg 1%.schem" at location of player include air false
    337.  
    338. # ##############################################################################################################
    339.  
    340. # Checks to see if a player owns a dropped item (Used in conjunction with the effect below)
    341. # Requires: Skript 2.3.6+ / Skript-Mirror 2.0+
    342. condition %player% (1¦can|2¦cannot) pickup[ item of[ entity] ]%entity%:
    343.     check:
    344.         metadata value "Owner" of expression-2 is set
    345.         set {_owners} to metadata value "Owner" of expression-2
    346.         set {_owners::*} to {_owners} split by ","
    347.         if parse mark is 1:
    348.             loop {_owners::*}:
    349.                 if "%expression-1%" is loop-value:
    350.                     set {_continue} to true
    351.             {_continue} is set
    352.             continue
    353.         else:
    354.             loop {_owners::*}:
    355.                 if "%expression-1%" is loop-value:
    356.                     set {_continue} to false
    357.             {_continue} is not set
    358.             continue
    359.  
    360. # EXAMPLE EVENT
    361. #on pickup:
    362. #    if player can pickup item of event-entity:
    363. #        broadcast "ayy i can pick it up"
    364. #    else:
    365. #        cancel event
    366.  
    367. # Drops an item for a specific player or group of players (Made to work with the condition above)
    368. # Requires: Skript 2.3.6+ / Skript-Mirror 2.0+
    369. effect drop %itemstack% at %location%(1¦ for %players%|2¦):
    370.     trigger:
    371.         set {_entity} to (world of expression-2).dropItem(expression-2, expression-1)
    372.         if parse mark is 1:
    373.             set {_owners} to ""
    374.             set {_players::*} to all players
    375.             loop expressions-3:
    376.                 set {_owners} to "%{_owners}%%loop-value%,"
    377.                 remove loop-value from {_players::*}
    378.             set {_owners} to subtext of {_owners} from 1 to (length of {_owners} - 1)
    379.             set metadata value "Owner" of {_entity} to {_owners}
    380.             loop {_players::*}:
    381.                 loop-value.getHandle().playerConnection.sendPacket(new PacketPlayOutEntityDestroy({_entity}.getEntityId()))
    382.  
    383. # EXAMPLE
    384. #command /dropitem:
    385. #    trigger:
    386.         # Drop an item for one specific player
    387. #        drop 1 of stone at location of player for player
    388. #
    389. #       # Drop an item for all players
    390. #       drop 1 of stone at location of player
    391. #
    392. #       # Drop an item for several but not all players
    393. #       add random player out of all players to {_players::*}
    394. #       add random player out of all players to {_players::*}
    395. #       add random player out of all players to {_players::*}
    396. #       drop 1 of stone at location of player for {_players::*}
    397.  
    398. # ##############################################################################################################
    Feel free to suggest things you would like added and I'll try my best to add them.

    Discord: AsuDev#0714
     
    • Like Like x 1
    • Friendly Friendly x 1
  2. aescraft

    aescraft Well-Known Member

    Joined:
    Mar 1, 2017
    Messages:
    290
    Likes Received:
    13
    Really good, i'll prob use one of those soon!
     
Loading...