Bukkit’s ServicesManager
This is going to be a rather short post, however, I would like to talk about Bukkit’s ServicesManager. Rather than creating one large post going on about features that are underused within the Bukkit or Spigot APIs, I thought I would make a simple post detailing what exactly Bukkit’s ServicesManager is and how you can utilize it.
What is it?
There are two terms you need to know to understand what a service manager is:
Service - An interface specifying a list of methods that a provider must implement
Provider - An implementation of the service
That really is all it is. It is hard to put it any simpler, and it is basically just the definition from the Spigot documentation.
Utilizing it
An important concept in object-orientated programming languages is abstraction, and the ServicesManager allows us to utilize this concept. In its essence, “abstraction is the process of hiding certain details and showing only essential information to the user.” Interfaces (in our case the service) provide abstraction by hiding the implementation details from the user. In other words, it is hiding the details and only providing what it does, not how it does it. The implementation itself will provide how it does it (in our case the provider).
One large project that utilizes the ServicesManager is Vault. This plugin allows server owners to install the vault plugin and then install any economy, chat, or permissions provider (that has its own Vault implementation, whether that be economy, chat, or permissions). Vault can then be used in any plugin (such as a shop plugin or a plugin that fetches permissions) by just utilizing Vault’s API. Instead of the developer having to add support for every economy, chat, or permissions plugin, they can just use Vault. Think of the ServicesManager providing a place for a central API. For example, an economy plugin provides the implementation details for Vault and then a shop plugin can use Vault’s API to utilize the implementation details provided by the economy plugin, but it isn’t set in stone since the server owner can always switch the economy plugin out with another one that provides the implementation for Vault and the shop plugin will work the exact same.
Not only does the ServicesManager provide a central API for some plugins to implement and others to utilize, it allows you to create your own economy provider while also using another economy provider. ServicesManager provide a ServicePriority enum, in which your economy provider can take place of another economy provider if yours has a higher priority. This is incredibly useful in cases where you want to use a vital plugin like EssentialsX but also want to have your own unique economy implementation.
ServicesManager provides a place for a central API as well as allowing you to customize which implementation you want to use without breaking plugins that don’t have any support for your custom implementation. Instead of having a world where every economy plugin has its own API that a shop plugin must use, we have one big central API that eliminates the hassle and provides consistency across every implementation.