Skip to content

External Usage

alexstaeding edited this page Mar 7, 2020 · 1 revision

If you wish to use Anvil's API without defining your own Anvil-style plugin, that is possible too. Anvil provides static methods that have direct access to the injector of any Anvil plugin.

For example, to use the TextService in Sponge code, all you have to do is:

import org.anvilpowered.anvil.api.Anvil;
import org.anvilpowered.anvil.api.util.TextService;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.text.Text;

public class Foo {

    TextService<Text, CommandSource> textService;

    public Foo() {
        textService = Anvil.getEnvironmentManager()
            .getCoreEnvironment().getInjector()
            .getInstance(Key.get(new TypeLiteral<TextService<Text, CommandSource>>() {}));
    }

    void bar() {
        textService.builder()
            ...

    }
}

For other platforms, it looks very similar. The only things you need to change are the type parameters for TextService. For example, in a Spigot plugin, Foo would look like this:

import net.md_5.bungee.api.chat.TextComponent;
import org.anvilpowered.anvil.api.Anvil;
import org.anvilpowered.anvil.api.util.TextService;
import org.bukkit.command.CommandSender;

public class Foo {

    TextService<TextComponent, CommandSender> textService;

    public Foo() {
        textService = Anvil.getEnvironmentManager()
            .getCoreEnvironment().getInjector()
            .getInstance(Key.get(new TypeLiteral<TextService<TextComponent, CommandSender>>() {}));
    }

    void bar() {
        textService.builder()
            ...

    }
}