Hauptklasse des Plugins

Bemerkung

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.

Die Klasse beginnen

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 empfiehlt deine Domain als Package Namen zu verwenden, falls du eine Domain besitzt. Falls du keine hast, wird das Nutzen einer Email Adresse (wie z.B. com.gmail.Benutzername.Projekt) oder eines open-source Repositories (wie z.B. io.github.Benutzername.Projekt) empfohlen, da es die gängige Praxis darstellt.

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 Metadaten.

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 {

}

Bemerkung

Schau dir Plugin Identifiers an, wenn du deine Plugin-ID noch nicht ausgewählt hast.

Initialisiere dein 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.

Dein Plugin kann auf bestimmte Events lauschen, die sogenannten state events. Diese informieren über Änderungen des Spielstatus. Im unten dargestellten Beispiel wird onServerStart() aufgerufen, wenn das GameStartedServerEvent ausgelöst wird. Beachte die Listener Annotation vor der Methode.

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

}

Tipp

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.

Status Events

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

Für weiterführende Informationen, wann welches Event ausgelöst wird, schaue hier nach: Plugin Lebenszyklus.