[Solved] "Unable to create injector" regarding EconomyService

I’ve recently started working on a plugin that ties into Pixelmon, and I’ve been getting an error regarding using the @Inject annotation, apparently.

Sponge Build: 1.12.2-2768-7.1.5
Forge Build:
Java Version: Server is hosted on VPS and only accessible through FTP so difficult to check

Forge logs: PiXeloot Crash Report - Pastebin.com

Plugin Source: Since I wrote the code, I’ve posted it on Pastebin

Well, for one

private Optional<EconomyService> serviceOpt = Sponge.getServiceManager().provide(EconomyService.class);
    private EconomyService economy = serviceOpt.get();

These should be set in the init event or something. The service won’t exist at the time that the class loads.

So would I just grab the variable during the GameInitializationEvent?

Actually, they’re typically registered in GameInitializationEvent, not by it. Unless you’re explicitly Order.LATE, you should be checking during GamePostInitializationEvent.

1 Like

Grabbing the Economy service in GameIntializationEvent worked fine for me

It may have worked in the particular economy implementation you tested against, but again, I urge you to use GamePostInitializationEvent instead as that’s when every implementation is done by.

Also, I just read your code. Do not store the service like that because a service provider can change while the server is running. Instead, fetch the service each time, or alternatively keep it in a variable but update the variable on ChangeServiceProviderEvent.