의존성 주입

플러그인에서 Sponge API 인스턴스를 받아오기 위해 의존성 주입을 사용할 수 있습니다. 당신의 플러그인에서 몇 가지 API 타입을 지정하면 플러그인 객체 생성(초기화) 이후 해당 타입의 인스턴스가 주입됩니다.

주입 가능한 타입의 임시 목록

ConfigDir (Path 또는 File 에 붙이는 어노테이션)

플러그인의 설정 폴더 경로를 받아옵니다. ConfigDir#sharedRoot()`에 따라 `./config/`` 또는 ./config/<Plugin#id>/ 값이 반환됨

DefaultConfig (Path, ConfigurationLoader, File에 붙이는 어노테이션)

플러그인의 설정 파일을 받아 옵니다: <Plugin#id>.conf

AssetId (annotation on Asset)

Used to inject a Asset from the asset folder of the plugin

AsynchronousExecutor (annotation on SpongeExecutorService)

Used to inject the plugin’s specific AsynchronousExecutor

SynchronousExecutor (annotation on SpongeExecutorService)

Used to inject the plugin’s specific SynchronousExecutor

ChannelId (annotation on ChannelBinding.IndexedMessageChannel or ChannelBinding.RawDataChannel)

Used to inject a ChannelBinding with the given channel id

Asset

Must be annotated with @AssetId.

SpongeExecutorService

Must be annotated with either @AsynchronousExecutor or @SynchronousExecutor. Depending on the annotation given this will contain a reference to the plugin’s specific Asynchronous or Synchronous Executor.

ConfigurationLoader<CommentedConfigurationNode>

Must be annotated with @DefaultConfig. Used to inject a pre-generated ConfigurationLoader for the File of the same annotation.

EventManager

이벤트 리스너(event handler) 등록과 이벤트 호출을 관리합니다.

File

Must be annotated with either @DefaultConfig or @ConfigDir. Depending on the annotation given this will contain a file reference to the plugins default config file or the directory used for storing configuration files. However, Path (see below) should be preferred.

Game

The Game object is the core accessor of SpongeAPI.

GameRegistry

Provides an easy way to retrieve types from a Game.

GuiceObjectMapperFactory

A tool provided by Configurate to allow easier mapping of objects to configuration nodes. See 객체 직렬화하기 for usage.

Injector

com.google.inject.Injector is available from Guice, it is the injector that was used to inject your plugin’s dependencies. You can use it to create a child injector with your own module in order to inject your own classes with either the Sponge provided dependencies listed on this page, or configure your own classes

Logger

Used to identify the plugin from which logged messages are sent.

Path

Must be annotated with either @DefaultConfig or @ConfigDir. Depending on the annotation given this will contain a path reference to the plugins default config file or the directory used for storing configuration files.

PluginContainer

A Plugin class wrapper, used to retrieve information from the annotation for easier use.

PluginManager

Manages the plugins loaded by the implementation. Can retrieve another plugin’s PluginContainer.

Injection Examples

There are a few references which are difficult to get - or, in some cases, impossible - without injection. While these may not be absolutely vital to every plugin, they’re quite frequently used.

참고

Remember that it’s almost always best practice to inject your objects within the main class, as it’s instantiated with the Guice injector when the plugin is loaded.

Logger

View 로그 및 디버그 for a complete guide, specifically for the Logger.

Game

The Game object is the opening for many of the internal functions of SpongeAPI, from the EventManager to the Server and even the Sync/Async Scheduler.

While it is entirely possible to retrieve the Game object through Sponge.getGame(), it is commonly obtained through an injection.

Example - Field

import com.google.inject.Inject;
import org.spongepowered.api.Game;

@Inject
private Game game;

Example - Method

private Game game;

@Inject
private void setGame(Game game) {
    this.game = game;
}

Example - Constructor

For the purpose of this tutorial, “Apple” is the class name.

private Game game;

@Inject
public Apple(Game game) {
    this.game = game;
}

Config Directory

The recommended way to obtain your config file is through Guice, along with the ConfigDir annotation.

If you set sharedRoot to true, your ConfigDir will be the same directory which - potentially - houses the configuration for other plugins. In most cases where grabbing the ConfigDir is required, this should be false.

Example - Field

import org.spongepowered.api.config.ConfigDir;

import java.nio.file.Path;

@Inject
@ConfigDir(sharedRoot = false)
private Path configDir;

Example - Method

private Path configDir;

@Inject
private void setConfigDir(@ConfigDir(sharedRoot = false) Path configDir) {
    this.configDir = configDir;
}

Example - Constructor

For the purposes of this tutorial, “Orange” is the class name.

private Path configDir;

@Inject
public Orange(@ConfigDir(sharedRoot = false) Path configDir) {
    this.configDir = configDir;
}

DefaultConfig

The way that @DefaultConfig works is very similar to @ConfigDir. The biggest difference is that @DefaultConfig refers to a specific file, whereas @ConfigDir refers to a directory.

View 플러그인 설정 for a complete guide, specifically for @DefaultConfig.