Services

Quasi alles (events, permissies, enz…) wordt behandeld door services. Alle services worden aangesproken via de servicebeheerder.

import org.spongepowered.api.Sponge;

Sponge.getServiceManager().provide(EventManager.class);

Indien je een referentie naar een object nodig hebt, neem het dan van de servicebeheerder.

Service Guidelines

  • Services should be registered during the POST_INITIALIZATION game state at the latest.

  • Services should be fully operational by the SERVER_ABOUT_TO_START game state.

You can read more about game states on the De levensloop van een plugin page.

Notitie

It is a good practice to register services as soon as possible so that other plugins can note that the service will be provided.

Je eigen service voorzien

Your plugin can provide the implementation for a core interface like PermissionService, or for a custom interface that is not part of SpongeAPI (e.g. economy, web server):

Sponge.getServiceManager().setProvider(Object plugin, Class<T> service, T provider);

Het provider object moet de service interface implementeren of klasse overerven.

De API op deze manier ontwerpen maakt Sponge enorm modulair.

Notitie

Plugins zouden opties moeten voorzien om hun providers niet te installeren indien de plugin niet toegewijd is tot een enkele functie.

Example: Providing a simple warp service

De eerste stap is optioneel maar aangeraden. Je kan de publieke methodes van je service klasse specificeren in een interface.

import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;
import java.util.Optional;

public interface WarpService {
    void setWarp(String name, Location<World> location);
    Optional<Location<World>> getWarp(String name);
}

Nu kan je de klasse schrijven die jouw interface implementeert.

import java.util.HashMap;

public class SimpleWarpService implements WarpService {
    HashMap<String, Location<World>> warpMap = new HashMap<String, Location<World>>();

    @Override
    public Optional<Location<World>> getWarp(String name) {
        if(!warpMap.containsKey(name)) {
            return Optional.empty();
        } else {
            return Optional.of(warpMap.get(name));
        }
    }

    @Override
    public void setWarp(String name, Location<World> location) {
        warpMap.put(name, location);
    }
}

Now we can register a new instance of the class in the service manager. We are using the interface WarpService.class as the service key.

Dit maakt het mogelijk voor andere plugin ontwikkelaars om hun eigen implementatie van jouw service (die de interface implementeert) te schrijven en jouw versie te vervangen.

PluginContainer plugin = ...;

Sponge.getServiceManager().setProvider(plugin, WarpService.class, new SimpleWarpService());

Andere plugins kunnen nu jouw service aanspreken via de service beheerder.

Sponge.getServiceManager().provide(WarpService.class);

Tip

If you don’t want to use interfaces, just replace the service key with your class (SimpleWarpService.class in the example).