Classe Main de um Plugin

Nota

The instructions within the Sponge Documentation assume that you have prior knowledge of Java. SpongeAPI provides the foundation for you to begin creating plugins for Minecraft servers powered by Sponge; however, it is up to you to be creative and make your code work! There are several free Java courses online if you have had little experience with Java.

Starting Your Class

The next step after adding SpongeAPI 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, more detailed usage is explained on Plugin Metadata.

package io.github.username.project;

import org.spongepowered.api.plugin.Plugin;

@Plugin(id = "exampleplugin", name = "Example Plugin", version = "1.0", description = "Example")
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.

The example below will log a message upon starting the server. If your plugin is correctly loaded, you should see this message as part of the server’s initialization output.

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

// Imports for logger
import com.google.inject.Inject;
import org.slf4j.Logger;

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

    @Inject
    private Logger logger;

    @Listener
    public void onServerStart(GameStartedServerEvent event) {
        logger.info("Successfully running ExamplePlugin!!!");
    }

}

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.

State Events

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.