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.

Other Embedding Java code in Skript with skript-mirror

Discussion in 'Tutorials' started by btk5h, Jun 23, 2017.

  1. btk5h

    Addon Developer

    Joined:
    Jan 25, 2017
    Messages:
    154
    Likes Received:
    158
    Although Skript and its addons wrap many of the essential utilities from Java and Bukkit, some projects have special requirements that utilize features not yet been implemented by addons. One of the features of skript-mirror is the ability to call arbitrary Java methods and fields, effectively allowing scripters to embed Java code into their scripts and access newer or more obscure parts of Java and Bukkit.

    This is not a Java tutorial! In order for this tutorial to make sense, you should understand the basics of methods and fields.

    Calling instance methods
    %object%.<method descriptor>([%objects%])

    A <method descriptor> is the name of the method. The descriptor may also be prefixed with the name of the class in brackets. A class prefix is recommended (and, in certain cases, is required) if the method is private. If you include a class prefix, Skript will error at parse-time if the method does not exist.

    Code (Skript):
    1. set {_test} to "test".toUpperCase()
    2. set {_test} to "test".[java.lang.String]toUpperCase()
    Arguments may be passed within the brackets.

    Code (Skript):
    1. set {_test} to "test".charAt(1)
    If you want to execute a method without using as an expression (you're calling the method as an effect), you must end the line with a semicolon.

    Code (Skript):
    1. player.setDisplayName("Notch");
    Calling instance fields
    %object%.<field descriptor>!

    A <field descriptor> is the name of the field. The descriptor may also be prefixed with the name of the class in brackets. A class prefix is recommended (and, in certain cases, is required) if the field is private. If you include a class prefix, Skript will error at parse-time if the field does not exist.

    Code (Skript):
    1. set {_test} to player.health!
    2. set {_test} to player.[org.bukkit.craftbukkit.entity.CraftPlayer]health!
    Fields may be set or deleted.

    Code (Skript):
    1. set player.health! to 0
    2. delete player.conversationTracker!
    Import syntax
    import %string% [as %variable%]

    The below samples require a reference to a Java type, which can be obtained using the Java type expression [the] ([java] class|java[ ]type) %string%. For convenience, skript-mirror provides import syntax to save a Java type to a variable.

    Code (Skript):
    1. # set {String} to the java class "java.lang.String"
    2. import "java.lang.String"
    3.  
    4. # set {MyStringType} to the java class "java.lang.String"
    5. import "java.lang.String" as {MyStringType}
    Calling static methods
    Calling static methods is the same as calling instance methods, but using a Java type as the target object.

    Code (Skript):
    1. {System}.exit(0);
    Calling static fields
    Calling static fields is the same as calling instance fields, but using a Java type as the target object.

    Code (Skript):
    1. {System}.out!.println();
    Calling constructors
    new %javatype%([%objects%])

    Code (Skript):
    1. set {_test} to new {String}("copy of another string")
     
    #1 btk5h, Jun 23, 2017
    Last edited: Jun 24, 2017
    • Like Like x 3
    • Winner Winner x 1
  2. ShaneBee

    Moderator Resource Staff Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,181
    Likes Received:
    220
    Ohh, I see a quite use of this! thanks for creating this addon!
     

Share This Page

Loading...