메인 플러그인 클래스

참고

The instructions within the Sponge Documentation assume that you have prior knowledge of Java. The Sponge API 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.

메인 클래스 만들기

Sponge API를 종속물(dependency)로 추가하고 난 다음 단계는 새로운 클래스를 만드는 것입니다. 클래스 이름은 어떤 것이든 상관 없으며, org.spongepowered제외한 어느 패키지에 속해 있어도 괜찮습니다. 관습적으로, 클래스 이름은 표제 형식을 따릅니다 (각 단어의 앞 글자를 대문자로 쓰고 띄어쓰기를 포함하지 않는다).

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가 당신의 플러그인을 불러올 때 메인 클래스를 쉽게 찾아낼 수 있게 합니다. 아래 예제 코드를 확인하세요.

package io.github.username.project;

import org.spongepowered.api.plugin.Plugin;

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

}

참고

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

플러그인 초기화 작업

플러그인들은 게임과 세계가 준비되기 전에 로드됩니다. 이는 커맨드나 이벤트 등록과 같이 당신의 플러그인이 게임과 상호작용을 시작해야 할 시간을 남겨줍니다.

플러그인은 State Events라고 불리는 특정한 이벤트에 리스너를 등록하여 게임의 상태가 변화할 때마다 이벤트를 호출받을 수 있습니다. 아래 예제 코드에서, GameStartedServerEvent 이벤트가 발생하면 onServerStart()가 호출됩니다; 메소드 선언문 위에 붙은 Listener 어노테이션을 주목해 주세요.

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

Sponge 문서는 이벤트에 관해 더 많은 정보를 제공합니다 (이벤트 참조). 원래는 이벤트 관리(event-handler) 메소드 위에 @Listener를 붙인 다음, Sponge 이벤트 버스에 당신의 객체를 등록시켜야 합니다. 하지만, 플러그인의 메인 클래스에서 이벤트 객체는 자동으로 등록되어 있습니다.

상태 이벤트

플러그인에서 GameStoppingServerEvent 등의 State Event의 이벤트 호출을 받는 것이 바람직할 수 있습니다. State Event는 두 가지 범주로 볼 수 있습니다:

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