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.

RandomSK JavaScript guide

Mar 7, 2021
RandomSK JavaScript guide
  • RandomSK javascript guide.

    Warnings/Disclaimer:
    * If you are getting errors you are most likely doing something wrong in Javascript or when you call your javascript in Skript.
    * Only test your code on a test server and confirm there are no errors before using it on a real server.

    This is a really powerful and raw tool.
    Having some knowledge about Javascript and/or Java is great here.
    If you really are clueless about them I suggest learning a bit. Since you will most likely have errors getting thrown and don't understand what too do.



    Javascript features:

    Code (Text):
    1.    Effects:
    2.        evaluate [java]script %string% [[with] arguments %~objects%]
    3.        load javascript from %string%
    4.        (renew|restart) javascript engine
    5.    Expressions:
    6.        [extended] execute [java]script %string% [[with] arguments %~objects%]

    Important info!:
    The "[[with] arguments %~objects%]" is a bit special.
    The "~" in the "%~objects%" forces the objects put there to be expressions or (normal or list)variable.
    Literals such as numbers, booleans or other classinfo that has be parsed to create a object doesn't work to put there.
    Strings work fine there since they don't have to be parsed.
    Example on what you can't do:
    execute javascript "someMathFunction" arguments (10 and 25)
    execute javascript "someBooleanFunction" arguments true
    Example on how you should do it:
    set {_list::*} to (10 and 25)
    execute javascript "someMathFunction" arguments {_list::*}
    set {_bool} to true
    execute javascript "someBooleanFunction" arguments {_bool}
    Reason why I force this is because of a problem with literals and %objects%.
    The literal number '10' becomes unparsed and will error when my code executes.
    I dunno if I have explained it good.
    You might not understand this right now but it's an good idea to put all your arguments in a list and using that list.

    Code (Text):
    1. Evaluate effect:
    2.    Pattern: "evaluate [java]script %string% [[with] arguments %~objects%]"
    3.    Example:
    4.        evaluate javascript "print('Hello')" #Executes the print function. Prints Hello in the console.
    5.        evaluate javascript "function printHello() { print('Hello') }" #Creates and loads the function inside the string and can be used after.
    6.        evaluate javascript "printHello()" #Executes the created function above. Prints Hello in the console.
    7.        evaluate javascript "print" with arguments "Hello"
    8.  
    9. Load Javascript Effect:
    10.    Info: You should store javascripts(*.js files) inside "plugins/RandomSK/javascripts"
    11.    Pattern: "load javascript from %string%"
    12.    Example:
    13.        #Loads the javascript from ScriptTest.js and overwrites the old Functions that were loaded from the same file before.
    14.        on script load:
    15.            load javascript from "ScriptTest.js"
    16.      
    17. Renew Engine Effect:
    18.    Info: This renews the engine and discards all the previous loaded Functions from the above method or created through the "Evaluate" effect or "Execute" expression.
    19.    Pattern: "(renew|restart) javascript engine"
    20.    Example:
    21.        #Renews the engine and loads the ScriptTest.js.
    22.        on script load:
    23.            renew javascript engine
    24.            load javascript from "ScriptTest.js"
    25.    Note: Renewing is not needed if you don't want to. Read the Example comment on "Load Javascript Effect".
    26.  
    27. Execute Expression:
    28.    Pattern:
    29.        [extended] execute [java]script %string% [[with] arguments %~objects%]
    30.    Info:
    31.        The "extended" part is similar to my metadata expression.
    32.        If you expect the executed javascript to return multiple values then use the "extended" prefix.
    33.        Take note this works exactly the same as the Evaluate effect except that this returns values.
    Example:
    Code (Text):
    1.        #I have these two Functions in a .js file loaded previously in the on script load.
    2.    
    3.        function times2(i) {
    4.            return i * 2; #returns single value
    5.        }
    6.    
    7.        function lolxd(s) {
    8.            var lol = 'lol';
    9.            return [lol, s]; #returns multiple values
    10.        }
    11.    
    12.        function stringplus(stringlol, stringxd) {
    13.            return stringlol + stringxd;
    14.        }
    15.  
    16.        #Skript part:
    17.    
    18.        #If you read the "Important info!" a bit above you should understand why I set the 5 to a variable before using it.
    19.        set {_five} to 5
    20.        set {_ten} to execute javascript "times2" with arguments {_five}
    21.        message "%{_ten}%" #Messages 10.
    22.    
    23.        set {_values::*} to extended execute javascript "lolxd" with arguments "xD"
    24.        #In this case you can also do the below since the argument should be a string.
    25.        set {_values::*} to extended execute javascript "lolxd('xD')"
    26.        message "%{_values::*}%" #Messages Lol and xD.
    27.    
    28.        #Take note here that there are multiple arguments in stringplus.
    29.        #Then you have to supply the arguments in the right order.
    30.        #We put "LOL" first because we want it to be "stringlol" in the javascript and "XD" for the "stringxd"
    31.        set {_value} to execute javascript "stringplus" with arguments ("LOL" and "XD")


    About the Javascript engine and the Evaluate and Execute features.
    I basicly think of the engine as a commandline where you can define Functions or Variables.
    And the Evaluate and Execute features are where you write to the commandline.

    Variables Example:
    Code (Text):
    1.    evaluate javascript "var two = 1+1;" #defines the var "two" to be 1+1 which is 2?
    2.    set {_two} to execute javascript "two" #gets the var "two"
    3.    message "%{_two}%"

    Code (Text):
    1. Function Example(Is the same as an another example):
    2.    evaluate javascript "function printHello() { print('Hello') }" #defines function inside the string and can be used anytime after.
    3.    evaluate javascript "printHello()" #Executes the defined function above. Prints Hello in the console.
    You can reuse these Functions or Variables anytime without actually defining them again.
    These Variables/Functions should be kept/stored until you use the "renew" effect or stop the server.



    Now then time to explain the "powerful" part of this.
    You can execute java code. For example the Bukkit API which I will be using in a example.
    You need to have some knowledge about Java/Bukkit API. Understanding how to look through the Bukkit API(https://hub.spigotmc.org/javadocs/spigot/) javadocs can be great here.
    If you are doing something wrong, it will error.
    And I don't think I will answer all your "why does this javascript error/throw warnings" posts in the RandomSK thread because that would usually mean you are not ready for this and are doing something wrong in the javascript or during the execution.


    Basic Bukkit API example:
    Code (Text):
    1. #JavaScript
    2. function createInventory(size, title){
    3.    var Bukkit = Java.type('org.bukkit.Bukkit'); #We can access static methods with this for example.
    4.    var inventory = Bukkit.createInventory(null, size, title); #Accesses the static method Bukkit.createInventory(...)
    5.    return inventory;
    6. }
    7. set {_args::*} to 10 and "Hello"
    8. set {_inventory} to execute javascript "createInventory" with arguments {_args::*}
    9. #Now you have a new inventory with no owner. Works the same/similar to SkQueries BlankInventories.
    10.  
    11. #JavaScript
    12. function creeperSetPowered(creeper, b){
    13.    if(creeper != null && b != null){ #Check to see so the creeper or b is not null.
    14.        if(creeper instanceof (Java.type('org.bukkit.entity.Creeper'))) #Ensure that the creeper object is actually a creeper and not any other entity or something.
    15.            creeper.setPowered(b); #Makes the creeper powered if true and turns off if powered if it's false(if b is not a boolean it will error).
    16.    }
    17. }
    18. set {_args::*} to targeted entity and true
    19. evaluate javascript "creeperSetPowered" with arguments {_args::*}


    Here you are. If you feel ready. Set it to true. Don't write anything below this.
    Enable Javascript: false