Injections - noobmobile/GroovyBukkitAPI GitHub Wiki
As said many times, the GroovyBukkitAPI handles automatically to you the injection of some depencies, fields and methods
Every field with the name of main will be setted the instance of our Main class (equivalent to calling Main.getPlugin(Main.class))
Every field with the suffix Service will be instantiated and injected. Every Service it's just instantiated one time and its reference is kept in our Main class.
Commands and listeners are automatically instantiated and registered in Bukkit.
Every FileConfiguration field will create a config (or read if it already exists) with the variable's name
@Inject
class TestCommand {
Terminal main
FileConfiguration i18n // a "i18n.yml" will be created
def test = { Context context ->
}
}These are some methods and fields that are injected in every class annotated with @Inject
-
log()anddebug() -
sync()andasync() -
menu(name, size)creates a custom inventory -
String.translate()andList<String>.translate()- translate & to § usingChatColor.translateAlternateColorCodes() -
String.untranslate()andList<String>.untranslate()- by default, every string that you get of the config will be translated, so if you want it without colors just use this. -
List.random()- random element from a list (why doesn't groovy already has this?) -
ItemStack.edit(). edits item meta e.g.:item.edit({ItemMeta meta -> meta.setDisplayName("test")}) -
ItemStack.editPlaceholders()- edits item's name, lore and skull owner with placeholders e.g.:item.editPlaceholders("{playerName}": player.name) -
String as Location- Convert a String to A Location (duh)
You can change some settings of the @Inject
The order of the instantiation of our services. By default it's 100.
@Inject(priority = 1)
class Service1{
}
@Inject(priority = 2)
class Service2{
}If the annotated class will be instantiated or not. By default it's true. If it's false, the injected fields must be static. Useful for classes with multiple instances.
@Inject(iniatialize = false)
class User{
static transient Terminal main
static transient ItemService itemService
}You can inject your own methods and fields. Just call the method customInject(methodName, method) in your Main class in the preEnable()
@Inject(iniatialize = false)
class Terminal extends AbstractTerminal {
@Override
void preEnable() {
customInject("getPlayer", { String it -> Bukkit.getPlayer(it) })
// now in any class annotated with @Inject we can call getPlayer(name of the player)
}
}