プラグインの設定

警告

These docs were written for SpongeAPI 7 and are likely out of date. If you feel like you can help update them, please submit a PR!

Configuration files allow plugins to store data, as well as allow server administrators to easily take control over specific portions of a plugin, if you so choose to let them. Sponge uses Configurate to allow you to easily manipulate configuration files. These pages will explain how to utilize Configurate in order to use configuration files to full advantage.

ちなみに

See the official Configurate wiki to gain more in-depth information about working with its components.

注釈

Sponge makes use of the HOCON configuration format, a superset of JSON, as the default format for saving configuration files. The rest of this guide will assume you are using HOCON as well. See HOCON の入門 more for information regarding the HOCON format. Working with different formats is made relatively similar by the Configurate system, so it should not pose too much of an issue if you use an alternate format instead.

クイックスタート

デフォルトのプラグイン設定の作成

Plugins using SpongeAPI have the option to use one or more configuration files. Configuration files allow plugins to store data, and they allow server administrators to customize plugin options (if applicable).

デフォルトのプラグイン設定の取得

SpongeAPI offers the use of the DefaultConfig annotation on a field or setter method with the type Path to get the default configuration file for your plugin.

If you place the @DefaultConfig annotation on a field with the type ConfigurationLoader<CommentedConfigurationNode> then you can use it to load and save the default config file in the file system. Please keep in mind that the annotated ConfigurationLoader does not use any default config file that you might ship with your jar, unless you explicitly load it.

The @DefaultConfig annotation requires a sharedRoot boolean. If you set sharedRoot to true, then the returned pathname will be in a shared configuration directory. In that case, the configuration file for your plugin will be your_plugin_id.conf (with 「your_plugin_id」 replaced with your plugin’s specified ID).

ちなみに

プラグイン ID の設定については 主要なプラグインのクラス を参照してください。

sharedRootfalse を設定した場合、返されるパス名はそのプラグイン専用のディレクトリ内の {pluginname}.conf というファイルを指します。

sharedRoot の値を何に設定すべきか確信が持てない場合は、次のことを考えてください。

  • 複数の設定ファイル (複雑なプラグイン) を将来使用するつもりであれば、 値を false にしてください。

  • 一つの設定ファイル (比較的簡素なプラグイン) を使用するつもりであれば、 値を true にしてください。

You can also obtain a Path instance pointing to the config directory instead of a particular file. Just have it injected using the ConfigDir annotation, either with sharedRoot set to false for a plugin specific directory or to true to get the shared configuration directory.

注釈

While it may be possible to get a File instead of a Path, Configurate (and Sponge) recommend using Path.

例 - ** ``@DefaultConfig`` **を用いたフィールド

import java.nio.file.Path;
import com.google.inject.Inject;
import org.spongepowered.api.config.ConfigDir;
import org.spongepowered.api.config.DefaultConfig;
import org.spongepowered.configurate.CommentedConfigurationNode;
import org.spongepowered.configurate.loader.ConfigurationLoader;

@Inject
@DefaultConfig(sharedRoot = true)
private Path defaultConfig;

@Inject
@DefaultConfig(sharedRoot = true)
private ConfigurationLoader<CommentedConfigurationNode> configManager;

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

警告

When your plugin is running for the first time, returned pathnames for configuration files and directories may not yet exist. If you delegate all reading / writing of files to Configurate, you do not need to worry about non-existent paths as the library will handle them appropriately.

注釈

The use of YAML format (https://yaml.org/spec/1.1/) and JSON format (https://www.json.org/) is also supported, but the preferred config format for Sponge plugins is HOCON. Conversion from YAML (or JSON) to HOCON can be automated by using a YAMLConfigurationLoader (or GsonConfigurationLoader) to load the old config and then saving it using a HoconConfigurationLoader.