Mengkonfigurasi plugin

File konfigurasi mengizinkan plugin untuk menyimpan data, serta mengizinkan administrator server untuk secara mudah mengendalikan bagian sebuah plugin tertentu, jika anda memilih untuk membiarkannya. Sponge menggunakan Konfigurasi untuk mengizinkan anda untuk memanipulasi file konfigurasi secara mudah. Halaman ini akan menjelaskan bagaimana caranya untuk memanfaatkan Konfigurasi dalam urutan agar bisa menggunakan file konfigurasi untuk keuntungan penuh.

Tip

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

Catatan

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 Pengantar ke HTML 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.

Mulai cepat

Membuat sebuah Konfigurasi Plugin Default

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

Memperoleh Konfigurasi Plugin Default anda

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.

Anotasi @DefaultConfig membutuhkan boolean sharedRoot. Jika anda mengatur sharedRoot menjadi true, lalu pengembalian pathname akan berada di dalam direktori konfigurasi yang dibagikan. Dalam kasus tersebut, berkas konfigurasi untuk plugin anda akan menjadi your_plugin_id.conf (dengan "your_plugin_id" digantikan dengan ID tertentu plugin anda).

Tip

Lihat Class Plugin utama untuk informasi pengkonfigurasian ID plugin anda.

Jika anda menyetel sharedRoot ke false, Pengembalian nama jalan akan mengacu pada sebuah file bernama {pluginname}.conf dalam sebuah direktori yang spesifik untuk plugin anda.

Jika anda tidak yakin dari apa yang harus mengatur nilainya dari sharedRoot untuk, pertimbangan hal berikut:

  • Jika anda berencana untuk mendapatkan beberapa file konfigurasi (plugin kompleks) di masa mendatang, tetapkan nilainya ke``false``.

  • Jika anda merencanakan untuk memiliki sebuah file konfigurasi tunggal (plugin kurang-kompleks), mengatur nilainya untuk true.

Anda juga bisa memperoleh sebuah Path contoh petunjuk untuk direktori konfigurasi pengganti dari file khusus. Hanya saja itu disuntik dengan menggunakan ConfigDir anotasi, salah satu dengan sharedRoot atur ke false untuk sebuah plugin direktori tertentu atau ke true untuk mendapatkan direktori konfigurasi bersama.

Catatan

Meskipun itu memungkinkan untuk mendapatkan sebuah File sebagai gantinya dari sebuah Path, Konfigurasi (dan Sponge) menyarankan menggunakan Path.

** Contoh - Field menggunakan ** `` @ DefaultConfig``

import java.nio.file.Path;
import com.google.inject.Inject;
import org.spongepowered.api.config.ConfigDir;
import org.spongepowered.api.config.DefaultConfig;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.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;

Peringatan

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.

Catatan

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.