Classe Main de um Plugin

Nota

As instruções da documentação do Sponge pressupõem que tens conhecimento prévio de Java. A Sponge API fornece a base para começares a criar plugins para servidores de Minecraft que tenham o Sponge; no entanto, cabe-te a ti seres criativo e fazeres o teu código trabalhar! Existem vários cursos de Java gratuitos online, caso tenhas pouca experiência com Java.

Starting Your Class

The next step after adding the Sponge API as a dependency is creating a new class. The class can be named however you like, and can be in any package that does not begin with org.spongepowered. By convention, class names should be in title case.

Oracle recommends to use your domain as your package name, if you own a domain. However, in the event that you do not own a domain, a common practice is to use an email address (such as com.gmail.username.project) or an open-source repository (such as io.github.username.project).

After creating your main class, the Plugin annotation must be affixed to it. This annotation allows Sponge to easily find your main plugin class when your plugin is loaded. An example usage is illustrated below.

package io.github.username.project;

import org.spongepowered.api.plugin.Plugin;

@Plugin(id = "exampleplugin", name = "Example Plugin", version = "1.0")
public class ExamplePlugin {

}

Nota

Refer to Plugin Identifiers if you’ve not chosen your plugin ID yet.

Initializing Your Plugin

Plugins are loaded before the game and the world(s). This leaves a specific timeframe when your plugin should begin interacting with the game, such as registering commands or events.

Your plugin can listen for particular events, called state events, to be notified about changes in the state of the game. In the example below, onServerStart() is called when the GameStartedServerEvent occurs; take note of the Listener annotation before the method.

import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.game.state.GameStartedServerEvent;

@Plugin(id = "exampleplugin", name = "Example Plugin", version = "1.0")
public class ExamplePlugin {
    @Listener
    public void onServerStart(GameStartedServerEvent event) {
        // Hey! The server has started!
        // Try instantiating your logger in here.
        // (There's a guide for that)
    }
}

Dica

The Sponge documentation provides a guide with more information on events (see Events). Normally, in addition to prefixing event-handler methods with @Listener, you must also register your object with Sponge’s event bus. However, your main plugin class is registered automatically.

Eventos de estado

It may also be desirable to listen for other state events, particularly the GameStoppingServerEvent. There are two categories of state events:

For information regarding when each state event occurs, see the plugin lifecycle documentation.