Services - noobmobile/GroovyBukkitAPI GitHub Wiki

Creating a service

Just include the suffix Service on your class and @Inject annotation It'll be instantiated (just one time, as it is a Singleton service) and injected everywhere it's called. Not necessary to register it anywhere.

Example Services

@Inject
class ItemService {

	// will be automatically setted to our Main class
	Terminal main

	// unfortunately we need to use this (optional) method if we want something like a constructor.
	// This ocurrs the body of a constructor it's immediately executed when the object is instantiated,
	// so this method it's called after the dependency injection.
	void init(){
		debug "hey i'm a service from $main.name"
	}

	def createACoolItem(){
		new ItemStack(Material.DIAMOND)
	}

	def isACoolItem(item){
		item?.type == Material.DIAMOND
	}

}

If you have multiple services and want a priority between them, just use @Inject(priority = 1)

Using a service

@Inject
class StupidServiceExampleCommand{
	
	Terminal main
	ItemService itemService

	def isthisacoolitem = {Context context -> 
		def player = context.player()
		player.sendMessage(itemService.isACoolItem(player.itemInHand) ? "You're holding a cool item" : "You're not holding a cool item :(")
	}

}