Skip to content

Adding LifeCycle Scripts

Eric Lam edited this page Jun 12, 2022 · 2 revisions

here is an example to add lifecycle scripts

package lifecycles

import com.ericlam.mc.groovier.lifecycle.OnDisable
import com.ericlam.mc.groovier.lifecycle.OnEnable
import com.ericlam.mc.groovier.lifecycle.OnScriptLoad
import com.ericlam.mc.groovier.lifecycle.OnScriptUnload
import groovy.transform.Field
import org.bukkit.plugin.java.JavaPlugin

import javax.inject.Inject


@Field @Inject JavaPlugin plugin

@OnEnable
void printHelloWorld() {
    plugin.logger.info("hello world! this is a groovy script printed when enable")
}

@OnDisable
void printGoodBye(){
    plugin.logger.info("good bye! this is a groovy script printed when disable")
}

@OnScriptLoad
void scriptLoaded(){
    plugin.logger.info("script loaded! printed from groovy script")
    plugin.logger.info("this message is added after server started")
}

@OnScriptUnload
void scriptUnloaded(){
    plugin.logger.info("script unloaded! printed from groovy script")
}

the method parameter declared does not provided a plugin instance to you, you must inject a plugin instance via using @Inject by yourself.

the four annotations represent four lifecycles that you can hook into.