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.

3.2 skript-reflect: Importing classes

Discussion in 'skUnity Pages' started by TPGamesNL, Jul 14, 2020.

  1. TPGamesNL

    Supporter Addon Developer Dev Programme

    Joined:
    Jan 20, 2018
    Messages:
    1,500
    Likes Received:
    102
    Medals:
    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 (Text):
    1. import:
    2.     <fully qualified name> [as <alias>]
    3.     # 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 (Text):
    1. import:
    2.     java.lang.System
    3. command /example:
    4.     trigger:
    5.         message "%System%" # java.lang.System
    6.         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 (Text):
    1. import:
    2.     java.lang.String as JavaString
    3. command /example:
    4.     trigger:
    5.         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 (Text):
    1. [the] [java] class %text%
    Example:
    Code (Text):
    1. on script load:
    2.     set {Player} to the class "org.bukkit.entity.Player"
    3.     message "%{Player}%" # org.bukkit.entity.Player
    From an object

    Syntax:
    Code (Text):
    1. [the] [java] class[es] of %objects%
    2. %objects%'[s] [java] class[es]
    Example:
    Code (Text):
    1. command /example:
    2.     executable by: players
    3.     trigger:
    4.         set {Player} to player's class
    5.         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 (Text):
    1. import:
    2.   org.bukkit.entity.EnderDragon$Phase as EnderDragonPhase
     
    #1 TPGamesNL, Jul 14, 2020
    Last edited: Jul 20, 2020

Share This Page

Loading...