3.2 skript-reflect: Importing classes

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

TPGamesNL

Staff member
Moderator
Supporter
Addon Developer
Dev Programme
Jan 20, 2018
1,501
108
63
21
Importing classes at parse-time (recommended)
In most cases, the exact qualified name of the class you need is known without running the script. If this is the case, you should use skript-mirror's import block.


Code:
import:
    <fully qualified name> [as <alias>]
    # multiple imports may be placed under the import section

Similar to events, import blocks must be placed at the root of your script (no indentation before import). Imports must also be placed before the imported classes are referred to in your code, so we recommend you place your imports as far up in your script as possible.

Once you import a class through an import block, skript-mirror will create an expression allowing you to reference the java class by its simple name.

To avoid conflicts, expressions created by import blocks are only available to the script that imported them. You must import java classes in each script that uses them.



Code:
import:
    java.lang.System
command /example:
    trigger:
        message "%System%" # java.lang.System
        System.out.println("test");

In most cases, expressions created by import blocks will not conflict with each other or with other Skript expressions. In cases where the class's simple name conflicts with another expression (such as with Player and String), you must import the class under an alias.

Code:
import:
    java.lang.String as JavaString
command /example:
    trigger:
        message JavaString.format("Hello %%s", sender)

Aliases must be valid Java identifiers!

Importing NMS classes
Since NMS packages change with each Minecraft version, you should generate the package prefix dynamically. See Computed Options for more details.

Importing classes at runtime
Sometimes, the class reference you need cannot be determined until the script is executed.

From a fully qualified name

Syntax:
Code:
[the] [java] class %text%

Example:
Code:
on script load:
    set {Player} to the class "org.bukkit.entity.Player"
    message "%{Player}%" # org.bukkit.entity.Player

From an object

Syntax:
Code:
[the] [java] class[es] of %objects%
%objects%'[s] [java] class[es]

Example:
Code:
command /example:
    executable by: players
    trigger:
        set {Player} to player's class
        message "%{Player}%" # org.bukkit.entity.Player

Dealing with nested classes
Sometimes, a class may be nested inside another class. When referring to the fully qualified name of the class, the nested class is separated from the surrounding class using a $ rather than a .

For example, org.bukkit.entity.EnderDragon.Phase would become org.bukkit.entity.EnderDragon$Phase

Nested classes usually have more general names than their surrounding classes, so you should import these under an alias.

Code:
import:
  org.bukkit.entity.EnderDragon$Phase as EnderDragonPhase
 
Last edited: