Other Embedding Java code in Skript with skript-mirror

  • 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!

btk5h

Addon Developer
Jan 25, 2017
154
159
0
Pyongyang, North Korea
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_language.skript:
set {_test} to "test".toUpperCase()
set {_test} to "test".[java.lang.String]toUpperCase()

Arguments may be passed within the brackets.

code_language.skript:
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_language.skript:
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_language.skript:
set {_test} to player.health!
set {_test} to player.[org.bukkit.craftbukkit.entity.CraftPlayer]health!

Fields may be set or deleted.

code_language.skript:
set player.health! to 0
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_language.skript:
# set {String} to the java class "java.lang.String"
import "java.lang.String"

# set {MyStringType} to the java class "java.lang.String"
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_language.skript:
{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_language.skript:
{System}.out!.println();

Calling constructors
new %javatype%([%objects%])

code_language.skript:
set {_test} to new {String}("copy of another string")
 
Last edited: