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.3 skript-reflect: Running Java code

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

  1. TPGamesNL

    Moderator Supporter Addon Developer Dev Programme

    Joined:
    Jan 20, 2018
    Messages:
    1,501
    Likes Received:
    108
    Medals:
    Calling methods
    Syntax:
    Code (Text):
    1. %object%.<method name>(%objects%)
    Example:
    Code (Text):
    1. event-block.breakNaturally()
    2. (last spawned creeper).setPowered(true)
    3. player.giveExpLevels({_levels})
    Methods may be used as effects, expressions, and conditions. If used as a condition, the condition will pass as long as the return value of the method is not false, null, or 0.

    Calling non-public methods
    If the method you're trying to invoke is not public, you must prefix the method name with the declaring class in brackets. Since an object may have a non-public method with the same name in multiple superclasses, you must explicitly specify where to find the method.

    Example:
    Code (Text):
    1. {_arraylist}.[java.util.ArrayList]fastRemove(1)
    2. # or, if you have the declaring class imported:
    3. {_arraylist}.[ArrayList]fastRemove(1)
    Calling overloaded methods
    Generally, skript-reflect can infer the correct overloaded method to call from the arguments passed at runtime. If you need to use a certain implementation of a method, you may append a comma separated list to the end of the method name surrounded in brackets.

    Example:
    Code (Text):
    1. System.out.println[java.lang.Object]({_something})
    2. # or, if you have the parameter classes imported:
    3. System.out.println[Object]({_something})
    4.  
    5. Math.max[int, int](0, {_value})

    Calling fields
    Syntax:
    Code (Text):
    1. %object%.<descriptor>

    Calling non-public fields
    If the field you're trying to use is not public, you must prefix the field name with the declaring class in brackets. Since an object may have a non-public field with the same name in multiple superclasses, you must explicitly specify where to find the field.

    Example:

    Code (Text):
    1. {_hashmap}.[java.util.HashMap]modCount
    2. # or, if you have the declaring class imported:
    3. {_hashmap}.[HashMap]modCount
    Calling constructors
    Syntax:
    Code (Text):
    1. [a] new %javatype%(%objects%)
    Example:
    Code (Text):
    1. new Location(player's world, 0, 0, 0)
     
    #1 TPGamesNL, Jul 20, 2020
    Last edited: Jul 20, 2020

Share This Page

Loading...