public interface Plugin extends CommandExecutor
对于插件作者,我们建议让插件主类继承PluginBase类,而不是实现这个接口。
For plugin developers: it's recommended to use PluginBase for an actual plugin
instead of implement this interface.
PluginBase,
PluginDescription| Modifier and Type | Method and Description |
|---|---|
Config |
getConfig()
|
File |
getDataFolder()
返回这个Nukkit插件的数据文件夹。
The data folder of this Nukkit plugin. |
PluginDescription |
getDescription()
返回描述这个Nukkit插件的
PluginDescription对象。The description this Nukkit plugin as a PluginDescription object. |
org.slf4j.Logger |
getLogger()
|
String |
getName()
返回这个插件的名字。
Returns the name of this plugin. |
PluginLoader |
getPluginLoader()
|
InputStream |
getResource(String filename)
读取这个插件特定的资源文件,并返回为
InputStream对象。Reads a resource of this plugin, and returns as an InputStream object. |
Server |
getServer()
|
boolean |
isDisabled()
返回这个Nukkit插件是否已停用。
Whether this Nukkit plugin is disabled. |
boolean |
isEnabled()
返回这个Nukkit插件是否已启用。
Whether this Nukkit plugin is enabled. |
void |
onDisable()
在一个Nukkit插件被停用时调用的方法。
Called when a Nukkit plugin is disabled. |
void |
onEnable()
在一个Nukkit插件被启用时调用的方法。
Called when a Nukkit plugin is enabled. |
void |
onLoad()
在一个Nukkit插件被加载时调用的方法。这个方法会在
onEnable()之前调用。Called when a Nukkit plugin is loaded, before onEnable() . |
void |
reloadConfig()
重新读取这个Nukkit插件的默认配置文件。
Reloads the plugin config. |
void |
saveConfig()
保存这个Nukkit插件的配置文件。
Saves the plugin config. |
void |
saveDefaultConfig()
保存这个Nukkit插件的默认配置文件。
Saves the DEFAULT plugin config. |
boolean |
saveResource(String filename)
保存这个Nukkit插件的资源。
Saves the resource of this plugin. |
boolean |
saveResource(String filename,
boolean replace)
保存或替换这个Nukkit插件的资源。
Saves or replaces the resource of this plugin. |
boolean |
saveResource(String filename,
String outputName,
boolean replace) |
onCommandvoid onLoad()
onEnable()之前调用。onEnable() .
应该填写加载插件时需要作出的动作。例如:初始化数组、初始化数据库连接。
Use this to init a Nukkit plugin, such as init arrays or init database connections.
void onEnable()
应该填写插件启用时需要作出的动作。例如:读取配置文件、读取资源、连接数据库。
Use this to open config files, open resources, connect databases.
注意到可能存在的插件管理器插件,这个方法在插件多次重启时可能被调用多次。
Notes that there may be plugin manager plugins,
this method can be called many times when a plugin is restarted many times.
boolean isEnabled()
void onDisable()
应该填写插件停用时需要作出的动作。例如:关闭数据库,断开资源。
Use this to free open things and finish actions,
such as disconnecting databases and close resources.
注意到可能存在的插件管理器插件,这个方法在插件多次重启时可能被调用多次。
Notes that there may be plugin manager plugins,
this method can be called many times when a plugin is restarted many times.
boolean isDisabled()
File getDataFolder()
一般情况下,数据文件夹名字与插件名字相同,而且都放在nukkit安装目录下的plugins文件夹里。
Under normal circumstances, the data folder has the same name with the plugin,
and is placed in the 'plugins' folder inside the nukkit installation directory.
PluginDescription getDescription()
PluginDescription对象。PluginDescription object.
对于jar格式的Nukkit插件,插件的描述在plugin.yml文件内定义。
For jar-packed Nukkit plugins, the description is defined in the 'plugin.yml' file.
PluginDescriptionInputStream getResource(String filename)
InputStream对象。InputStream object.
对于jar格式的Nukkit插件,Nukkit会在jar包内的资源文件夹(一般为resources文件夹)寻找资源文件。
For jar-packed Nukkit plugins, Nukkit will look for your resource file in the resources folder,
which is normally named 'resources' and placed in plugin jar file.
当你需要把一个文件的所有内容读取为字符串,可以使用Utils.readFile(java.io.File)函数,
来从InputStream读取所有内容为字符串。例如:
When you need to read the whole file content as a String, you can use Utils.readFile(java.io.File)
to read from a InputStream and get whole content as a String. For example:
String string = Utils.readFile(this.getResource("string.txt"));
filename - 要读取的资源文件名字。InputStream对象。若错误会返回nullInputStream object, or null when an error occurred.boolean saveResource(String filename)
对于jar格式的Nukkit插件,Nukkit会在jar包内的资源文件夹寻找资源文件,然后保存到数据文件夹。
For jar-packed Nukkit plugins, Nukkit will look for your resource file in the resources folder,
which is normally named 'resources' and placed in plugin jar file, and copy it into data folder.
这个函数通常用来在插件被加载(load)时,保存默认的资源文件。这样插件在启用(enable)时不会错误读取空的资源文件,
用户也无需从开发者处手动下载资源文件后再使用插件。
This is usually used to save the default plugin resource when the plugin is LOADED .If this is used,
it won't happen to load an empty resource when plugin is ENABLED, and plugin users are not required to get
default resources from the developer and place it manually.
如果需要替换已存在的资源文件,建议使用saveResource(String, boolean)
If you need to REPLACE an existing resource file, it's recommended
to use saveResource(String, boolean).
filename - 要保存的资源文件名字。saveDefaultConfig(),
saveResource(String, boolean)boolean saveResource(String filename, boolean replace)
对于jar格式的Nukkit插件,Nukkit会在jar包内的资源文件夹寻找资源文件,然后保存到数据文件夹。
For jar-packed Nukkit plugins, Nukkit will look for your resource file in the resources folder,
which is normally named 'resources' and placed in plugin jar file, and copy it into data folder.
如果需要保存默认的资源文件,建议使用saveResource(String)
If you need to SAVE DEFAULT resource file, it's recommended
to use saveResource(String).
filename - 要保存的资源文件名字。replace - 是否替换目标文件。saveResource(String)Config getConfig()
Config对象。Config object.
一般地,插件的配置保存在数据文件夹下的config.yml文件。
Normally, the plugin config is saved in the 'config.yml' file in its data folder.
getDataFolder()void saveConfig()
getDataFolder()void saveDefaultConfig()
执行这个函数时,Nukkit会在资源文件夹内寻找开发者配置好的默认配置文件config.yml,然后保存在数据文件夹。
如果数据文件夹已经有一个config.yml文件,Nukkit不会替换这个文件。
When this is used, Nukkit will look for the default 'config.yml' file which is configured by plugin developer
and save it to the data folder. If a config.yml file exists in the data folder, Nukkit won't replace it.
这个函数通常用来在插件被加载(load)时,保存默认的配置文件。这样插件在启用(enable)时不会错误读取空的配置文件,
用户也无需从开发者处手动下载配置文件保存后再使用插件。
This is usually used to save the default plugin config when the plugin is LOADED .If this is used,
it won't happen to load an empty config when plugin is ENABLED, and plugin users are not required to get
default config from the developer and place it manually.
getDataFolder(),
saveResource(java.lang.String)void reloadConfig()
执行这个函数时,Nukkit会从数据文件夹中的config.yml文件重新加载配置。
这样用户在调整插件配置后,无需重启就可以马上使用新的配置。
By using this, Nukkit will reload the config from 'config.yml' file, then it isn't necessary to restart
for plugin user who changes the config and needs to use new config at once.
getDataFolder()Server getServer()
Server对象。Server object.ServerString getName()
Nukkit会从已经读取的插件描述中获取插件的名字。
Nukkit will read plugin name from plugin description.
getDescription()org.slf4j.Logger getLogger()
PluginLogger对象。PluginLogger object.
使用日志记录器,你可以在控制台和日志文件输出信息。
You can use a plugin logger to output messages to the console and log file.
PluginLoggerPluginLoader getPluginLoader()
PluginLoaderCopyright © 2020. All rights reserved.