Skip to content

Hook into Tresor

Max Lee edited this page Jul 4, 2023 · 3 revisions

How to hook into Tresor

Tresor uses the ServiceManager which is built into Bukkit/Spigot/Paper.

With the ServiceHook utility

E.g. if you want to hook into the ModernEconomy service you can use the ServiceHook utility like this in your onEnable:

public class MyPlugin extends JavaPlugin implements Listener {
    private ServiceHook<ModernEconomy> economyHook;

    public void onEnable() {
        // create the hook
        this.economyHook = new ServiceHook<>(this, ModernEconomy.class);
    }

    public ModernEconomy getEconomy() {
        // get the provider from the hook, might return null if none exists!
        return this.economyHook.getProvider();
    }
}

This utility can be used for all services, not just Tresor ones!

Manually

If you want to do the hooking manually then make sure to get the provider in your onEnable and then listen for the ServiceRegisterEvent and ServiceUnregisterEvent! (This can be used for all services!)

public class MyPlugin extends JavaPlugin implements Listener {
    private TresorAPI tresorApi = null;
    private ModernEconomy economy = null;

    private void onEnable() {
        this.updateEconomyProvider();
        // get the TresorAPI to make sure we use our extended services manager
        this.tresorApi = (TresorAPI) getServer().getPlugin("Tresor");
        // register the service events listener
        this.getServer().getPluginManager().registerEvents(this, this);
    }

    private void updateEconomyProvider() {
        // get the service provider registration for this plugin
        this.economy = this.tresorApi.getServicesManager().load(this, ModernEconomy.class);
    }

    @EventHandler
    public void onServiceRegister(ServiceRegisterEvent event) {
        if (event.getProvider().getProvider() instanceof ModernEconomy) {
            this.updateEconomyProvider();
        }
    }

    @EventHandler
    public void onServiceUnregister(ServiceUnregisterEvent event) {
        if (event.getProvider().getProvider() instanceof ModernEconomy) {
            this.updateEconomyProvider();
        }
    }
}

Legacy (Vault backwards-compatibility)

This lets you manually hook into services even if the user only has Vault installed!

It basically works the same but using the Bukkit API ServiceManager and not Tresor's extended one by getting the provider in your onEnable and then listening for the ServiceRegisterEvent and ServiceUnregisterEvent! (This again should be used for all services, not just Vault/Tresor ones!)

public class MyPlugin extends JavaPlugin implements Listener {
    private Economy economy = null;

    private void onEnable() {
        this.updateEconomyProvider();
        // register the service events listener
        this.getServer().getPluginManager().registerEvents(this, this);
    }

    private void updateEconomyProvider() {
        // get the service provider registration for this plugin
        this.economy = this.getServer().getServicesManager().load(Economy.class);
    }

    @EventHandler
    public void onServiceRegister(ServiceRegisterEvent event) {
        if (event.getProvider().getProvider() instanceof Economy) {
            this.updateEconomyProvider();
        }
    }

    @EventHandler
    public void onServiceUnregister(ServiceUnregisterEvent event) {
        if (event.getProvider().getProvider() instanceof Economy) {
            this.updateEconomyProvider();
        }
    }
}