Help with EconomyAPI

I have the economyService and all, the code has no errors but when I run the command on the server, it outputs an error [19:23:43] [Server thread/ERROR] [Sponge/]: Error occurred while executing comma - Line 39 is the line with Optional<UniqueAccount>... so on.

	public void onChangeServiceProvider(ChangeServiceProviderEvent event) {
    	if (event.getService().equals(EconomyService.class)) {
           	economyService = (EconomyService) event.getNewProviderRegistration().getProvider();

    public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
    	if (src instanceof Player) {
    		Player player = (Player) src;
    		if (src.hasPermission("")) {
    			Optional<UniqueAccount> uOpt = economyService.getOrCreateAccount(player.getUniqueId());

Am I missing something?

You need to have an economy plugin installed for it to work. Sponge doesn’t have their own implementation for the EconomyService

Ah yes, I was testing with TotalEconomy. Sorry forgot to mention that.

That seems like it should work, only reason it wouldn’t is if the economy service hasn’t registered yet, or it wasn’t able to be registered. You should make absolute sure the economy service is actually properly registering, something I like to do with my plugins that use economy is this:

    public void onStarted(GameStartedServerEvent event) {
        if (economyService == null) {
            logger.warn("Economy plugin not present, this plugin will not function properly!");

If a plugin absolutely relies on economy, you could also disable your listeners and commands.
Back to why the service isn’t registering, I’d make sure there’s no errors in console by your economy plugin.

1 Like

Thanks. I added a warn logger like you suggested but the message did not seem to appear anywhere in the logs so I assume there is an economyService. It still gives me the same error when I run the command. I believe I’m missing something fundamental here, like a stupid mistake.

Would you mind inspecting my code in private message? It’s only a small plugin.

Edit: Fixed. (Thanks Rysing!) The issue was that I had the listener in the command.