Skip to content
Cupelt edited this page Aug 17, 2023 · 21 revisions

In Trigger Reactor, you can directly access the methods used by plugin developers. This means that there are virtually no limitations in what you can do with your scripts.

Working with methods does require some knowledge of Java however. This article cover the basics that you will need to use them in your scripts.

What are Methods?

What are the methods? A method is the term used by Java to describe a function. Basically, a method is a small bit of code that does some kind of operation.

There are three kinds of methods used in TR:

Common Methods

These methods can be used in any trigger.

  • A list of common methods can be found here: Shared Bukkit Sponge
  • Notice that Bukkit or Sponge specific methods are almost the same.

Internal Variable Methods

These methods are accessed by using a special kind of variable called Internal Variables. Many of these internal variables can only be used on specific triggers.

  • A list of internal variables you can use can be found in the Triggers section of the wiki.

Third Party Plugin Methods

These methods require a third party plugin to be installed.

Using Methods in your Scripts

Methods can be placed in your scripts as is with no special characters. Simply use the syntax exactly as seen in the javadocs.

Lets say that we want to get a random number between 5 to 10. We can use the common method random(int start, int end) and put it in our script like so:

#MESSAGE "Random value is " + random(5, 10)

Or if you were to use local variable's method, it seems like this:

#MESSAGE "Your name is " + player.getName()

Special Data Types

When working with methods, you won't always be working with the usual string or numbers. Sometimes you will be using special data types that have their own unique properties.

For example, the click trigger internal variable item uses the ItemStack data type. This data type includes everything having to do with a stack of items.

It includes:

  • Enchantments
  • Amount of items in stack
  • Durability
  • Item Meta
    • Display Name
    • Lore
  • Type

In order to fully grasp these data types, you will need to understand a few fundamental java concepts.

Reading Java Docs

In order to use methods to their full potential, you need to understand how to read a javadoc. If you open the java doc for the ItemStack datatype here it will list all the available methods you can use in the Method Summary.

In the method summary there are two main things you will want to look at:

  1. Method (Input)
  2. Modifier and Type (Output)

Method (Input)

The Method is what you will need to put into your script.

For example we can use the method item.getAmount() to get the amount of items in the ItemStack.

The brackets are the required input arguments that you must provide for the method to function. If the brackets are empty then you do not need to provide an input. So item.getAmount() will work as is.

For some methods, the brackets are not empty. For example vault.give(Player player, double amount). For each argument it show the required data type, followed by a brief description of what needs to go there.

So, if we put vault.give(player, 200).

  • player is an internal variable for whoever triggered the event, and it has a data type of Player.
  • 200 is an integer or int. Integers are a positive or negative whole number; decimals will cause an error.

This method would give the player who triggered the event 200 in-game currency.

Modifier and Type (Output)

The Modifer and Type is the return data type. This is what what the method will output.

  • Int, short, long, and double are all child classes of Number. You can do any mathematical operations on them, like a number, but keep in mind that you cannot input a double when the method is asking for an int because doubles can be a decimal number while ints cannot.

  • Void means that the method does not return a value. These methods are usually for doing an actual function (eg. giving money to a player) rather than that getting data to work with (eg. getting a players balance).

Dot Operators

Dots are used to access and invoke a classes methods. For item.getAmount(), item would be class you are referencing, and getAmount() is the method you are invoking.

You can invoke multiple methods in a single line by putting in additional dot operators. To get the display name of item, you would do item.getMeta().getDisplayName(). In this case, TR would invoke the getMeta() method first, which will return a type of ItemMeta. If you look inside the ItemMeta java doc, you will see the method getDisplayName(). TR will invoke this method second and return a String to you.

Handling Enum

Sometimes a data type will be an enumeration or enum. An enum is a pre-defined list of values that a method will allow as an input.

For example:

  • The Statistic enum is a list of available stats.
  • The Material enum is a list of available materials.

To put them in a method, simply put in as a string.

player.getStatistic("CRAFT_ITEM", "DIAMOND_SWORD")

Using enum in conditions

Sometimes a method will return an enum and you want to test for it in a condition. The easiest way to do this is to turn the enum into a string. You can do this by using the name() method found in Enum.

IF item.getType().name() == "DIAMOND_SWORD"
    #MESSAGE "This item is a diamond sword."
ENDIF

LAMBDA expression

Starting from 3.3.x, you can now use LAMBDA expression to use the methods that require callbacks. You can provide the LAMBDA expression anywhere in the parameter that is an interface and is FunctionalInterface.

What is FunctionalInterface?

It is the annotation written on any interface to denote that the interface can be used interchangeably with the LAMBDA expression. Such an interface has to have only one method signature. One example is java's Runnable interface. This is also used in the BukkitScheduler.

Though, even I said only one method signature, it is sometimes allowed to have multiple method signatures if there is at least one method signature that doesn't have a default modifier in front of it. That being said, there should be only one method that must be implemented. Another example is the Function, which has three methods with default modifier, yet apply() does not.

Syntax

LAMBDA [0 or more arguments] =>
    [body]
ENDLAMBDA

Example

The most typical use of LAMBDA expression is the Bukkit's BukkitScheduler, which you can submit the task that will run in the server's scheduler.

p = plugin("TriggerReactor")
Bukkit.getScheduler().runTask(p, LAMBDA => 
    #MESSAGE "hey I run in the bukkit scheduler!"
ENDLAMBDA)

Variable Scope

When LAMBDA expression executes, the variables of the caller will be copied to the LAMBDA expression's body. For example:

p = plugin("TriggerReactor")
myMessage = "What's up"
Bukkit.getScheulder().runTask(p, LAMBDA => 
    #MESSAGE "hey " + myMessage
ENDLAMBDA)

This is valid since the variable myMessage is copied to the LAMBDA expression's body.

However, this would not work:

p = plugin("TriggerReactor")
Bukkit.getScheulder().runTask(p, LAMBDA => 
    myMessage = "What's up"
ENDLAMBDA)
#MESSAGE "hey " + myMessage

It will produce "hey null" because the LAMBDA expression's body cannot interact with the caller's variable directly.

FunctionalInterface with return types

Sometimes, FunctionalInterface, such as Function, can have a return type, while the others, such as Runnable, do not.

To return the value, TriggerReactor will use the value produced from the last expression.

For example,

doWithCallback(LAMBDA x => 
    IF x
        true
    ELSE
        false
    ENDIF
ENDLAMBDA)

This will return true if x is true and return `false if x is false.

Or,

doSomethingForTwoNumbers(LAMBDA a, b => 
    a * b
ENDLAMBDA)

This will return the multiple of the variables a and b.

Implementing a 'Function' using LAMBDA (version 3.4.x)

You can also implement a "function" by storing this LAMBDA directly in a variable.

someFunction = LAMBDA =>
    #MESSAGE "This is function!!!"
ENDLAMBDA

someFunction()

Specifying Parameters for LAMBDA Function

Of course, you can also specify parameters for LAMBDA.

someFunction = LAMBDA pl =>
    IF pl IS Player
        #MESSAGE "Player Name is " + pl.getName()
    ELSE
        #MESSAGE "param is not Player"
    ENDIF
ENDLAMBDA

someFunction(player)

Returns a value from the LAMBDA Function.

Like a function where a value goes in and another value comes out, The LAMBDA Function can also specify a return value.

addFunction = LAMBDA a, b =>
    a + b
ENDLAMBDA

#MESSAGE addFunction(10, 15)

Plugin Description / 목차

1. Getting Started () (рус)

S.L. In-game Editor () (рус)

2. Triggers () (рус)

List and usage of Triggers / 트리거 목록과 사용 방법:

  • List of Executors / 실행자(Executor) 목록

4. Placeholders () (рус)

  • Using PlaceholderAPI / PlaceholderAPI 사용법
  • List of Placeholders / 플레이스 홀더(Placeholder) 목록

5. Conditions () (рус)

  • Creating Conditions / 조건식 만들기
    • Boolean Expressions / 부울 (Boolean) 표현 방법
  • Logical Operators / 연산자 사용법
  • IF statement / IF 조건문
  • Null Checking / Null 검사법
  • Switch Case / Switch Case 조건

6. Variables () (рус)

  • Local Variables / 지역 변수
  • Global Variables / 전역 변수

Advanced

Timings () (рус)

7. Methods () (рус)

  • Using Methods / 메소드 사용법
  • Special Data Types / 특수한 데이터 형식
  • Reading Javadocs / Javadoc 읽기
  • Handling Enum / Enum 데이터 처리
  • Lambda Expresion / Lambda(람다) 식 사용법

8. Array () (рус)

  • Creating an empty array / 빈 배열 만들기
  • Storing data into array / 배열에 데이터값 저장하기
  • Read data from array / 배열에서 데이터 읽기(불러오기)

9. Loops () (рус)

  • WHILE loop / WHILE 반복문
  • FOR loop / FOR 반복문
    • Iterating Collection / Collection 형식의 변수 순회법
    • #BREAK executor / #BREAK 실행자
    • #CONTINUE executor / #CONTINUE 실행자

10. Sync Mode () (рус)

  • #CANCELEVENT executor / #CANCELEVENT 실행자
  • Setting Sync/Async Mode / 동기, 비동기 모드 전환
    • Custom Trigger
    • Area Trigger

11. Custom Executors () (рус)

12. Plugin Access () (рус)

  • Check And Use / 플러그인 존재여부 확인
    • Get Third Party Plugin / 제 3자 플러그인 불러오기
    • Check Eligibility / 호환성 확인하기
    • Use the Plugin / 플러그인 사용하기

13. IMPORT Statement () (рус)

  • Creating new instance / 새 인스턴스 생성하기
  • Accessing static method / 종속 메소드 불러오기
  • Accessing static field / 종속 Enum 불러오기

14. IS Statement () (рус)

  • Understanding / 이해하기
    • Understanding Instance / 인스턴스 이해하기
    • Understanding Superclass / 부모클래스 이해하기
    • Understanding Subclass / 자식클래스 이해하기
  • Using IS Statement / IS조건연산자 사용하기

15. TRY-CATCH Statement () (рус)

  • Understanding TRY-CATCH Exception Handling / TRY-CATCH 예외처리 이해하기

Misc

16. Interface Casting () (рус)

module x.x does not "opens x.x" problem

  • List of Custom Events

Examples

Trigger

Trigger Example () (рус)

More Examples: Bukkit, Sponge

Case Specific

Clone this wiki locally