메인 플러그인 클래스

참고

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.

메인 클래스 만들기

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

메인 클래스를 생성했다면, 클래스 선언문 위에 Plugin 어노테이션을 붙여야 합니다. 이 어노테이션은 Sponge가 당신의 플러그인을 불러올 때 메인 클래스를 쉽게 찾아낼 수 있게 합니다. 아래 예제 코드를 확인하세요. 더 자세한 설명은 :doc:`plugin-meta`에 서술되어 있습니다.

package io.github.username.project;

import org.spongepowered.plugin.builtin.jvm.Plugin;

@Plugin("exampleplugin")
public class ExamplePlugin {

}

참고

아직 플러그인 ID를 정하지 않았다면 플러그인 식별자를 참조하세요.

플러그인 초기화 작업

Your plugin can listen for particular events, called lifecycle events, to be notified about changes in the state of the game or be to prompted to peform a specific task, such as registering a command. In the example below, onServerStart(StartedEngineEvent<Server>) is called when the StartedEngineEvent occurs for the Server. Note that the method is annotated with the Listener annotation.

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.event.Listener;
import org.spongepowered.api.event.lifecycle.StartedEngineEvent;
import org.spongepowered.plugin.builtin.jvm.Plugin;

// Imports for logger
import com.google.inject.Inject;
import org.apache.logging.log4j.Logger;

@Plugin("exampleplugin")
public class ExamplePlugin {

    @Inject
    private Logger logger;

    @Listener
    public void onServerStart(final StartedEngineEvent<Server> event) {
        logger.info("Successfully running ExamplePlugin!!!");
    }

}

The Sponge documentation provides a guide with more information on events (see 이벤트). Normally, in addition to prefixing event-handler methods with @Listener, you must also register your object with Sponge’s event bus, which can be done at any time. However, your main plugin class is registered automatically.

Lifecycle Events

It may also be desirable to listen for other lifecycle events in your plugin, such that you can react to re-registration requests or engine/game state changes. See the plugin lifecycle documentation for more information on the lifecycle events available for plugins to listen to.