Solved ServiceManager & ServiceProvider

Discussion in 'Spigot Plugin Development' started by yPedx, Mar 2, 2020.

  1. I remember using Vault as a dependency for an old project of mine, and I've been thinking of this for a good while now. Vault apparently registers some kind of service for permission, chat and economy; but what do they mean?
    Code (Java):
    private boolean setupChat() {
            RegisteredServiceProvider<Chat> rsp = getServer().getServicesManager().getRegistration(Chat.class);
            chat = rsp.getProvider();
            return chat != null;
    What are registered service providers, and what are they used for? I also found the ServiceManager interface, and I'm curious how these things work together. Could someone explain this for me?
  2. SteelPhoenix


    The idea of the services manager is that you can provide / consume a service without knowing who the consumer / provider is.

    In the case of Vault you have some economy plugin, and another plugin that wants to use the economy but it does not know who is providing the economy, instead, they can just grab the registered provider from the services manager.
  3. Ah, that clears up a bit of confusion.

    But.. if you were to use the Economy service, and you don't know which plugin it is that manages the economy, how does one go forth to get a player's balance? Does the *using* plugin have to register the Economy service to use it? If so, why not just access the API for the economy manager instead of doing all this service stuff?
  4. SteelPhoenix


    Vault provides an interface with some methods (e.g. Economg#getBalance(OfflinePlayer)), but does not provide the service itself (vault doesn't save any data etc)

    Because there's dozens of economy plugins. Imagine making something using economy, you'd have to adapt separately to each plugin just so it works on any server setup an end user might have.

    Vault provides an interface, a providing plugin registers an implementation of said interface to the services manager and a consumer can grab that implementation from the services manager without knowing who provided it to begin with.
  5. I made a little flow chart that visualizes how that stuff works. Please correct me if I made any mistakes.

  6. SteelPhoenix



    Also, Vault has built-in support for a few economy plugins and Vault registers the provider in those cases.
    • Informative Informative x 1
  7. Alright, I think I got it now. Thank you both for explaining and even visualizing these confusing service things. Much appreciated!