Servizi

Praticamente tutto (eventi, permessi, etc.) è gestito attraverso i servizi. Tutti i servizi sono accessibili attraverso il gestore dei servizi:

import org.spongepowered.api.Sponge;

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

Se hai bisogno di ottenere un oggetto che si riferisca a qualcosa, semplicemente ottienilo dal gestore dei servizi.

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 Ciclo Di Vita Dei Plugin page.

Nota

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

Fornire il tuo servizio

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

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

L’oggetto fornitore deve implementare l’interfaccia o classe service

Progettare l’API in questo modo rende Sponge molto modulare.

Nota

I plugin dovrebbero fornire una opzione per non installare i loro fornitori se il plugin non è dedicato ad una sola funzione.

Example: Providing a simple warp service

Il primo passo è opzionale, ma raccomandato. Specifica i metodi pubblici della classe del tuo servizio in una interfaccia:

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);
}

Ora puoi scrivere la classe che implementa la tua interfaccia:

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.

Questo rende possibile agli altri sviluppatori di scrivere le loro implementazioni del tuo servizio (che implementa l’interfaccia) e rimpiazzare la tua versione.

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

Altri plugins possono ora accedere al tuo servizio tramite il gestore dei servizi:

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

Suggerimento

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