Class PluginBase
- All Implemented Interfaces:
CommandExecutor
,Plugin
- Direct Known Subclasses:
InternalPlugin
,PowerNukkitPlugin
A class to be extended by a normal Nukkit plugin.
- Since:
- Nukkit 1.0 | Nukkit API 1.0.0
- Author:
- MagicDroidX(code) @ Nukkit Project, 粉鞋大妈(javadoc) @ Nukkit Project
- See Also:
-
Field Summary
Fields inherited from interface cn.nukkit.plugin.Plugin
EMPTY_ARRAY
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptiongetCommand
(String name) TODO: FINISH JAVADOCfinal File
返回这个Nukkit插件的数据文件夹。
The data folder of this Nukkit plugin.final PluginDescription
返回描述这个Nukkit插件的PluginDescription
对象。
The description this Nukkit plugin as aPluginDescription
object.getFile()
返回这个插件的文件File
对象。对于jar格式的插件,就是jar文件本身。
Returns theFile
object of this plugin itself.final String
返回这个插件完整的名字。
Returns the full name of this plugin.getName()
返回这个插件的名字。
Returns the name of this plugin.getPluginCommand
(String name) getResource
(String filename) 读取这个插件特定的资源文件,并返回为InputStream
对象。
Reads a resource of this plugin, and returns as anInputStream
object.final void
init
(PluginLoader loader, Server server, PluginDescription description, File dataFolder, File file) 初始化这个插件。
Initialize the plugin.final boolean
返回这个Nukkit插件是否已停用。
Whether this Nukkit plugin is disabled.final boolean
返回这个Nukkit插件是否已启用。
Whether this Nukkit plugin is enabled.final boolean
返回这个插件是否已经初始化。
Returns if this plugin is initialized.boolean
onCommand
(CommandSender sender, Command command, String label, String[] args) 在命令执行时会调用的方法。
Called when a command is executed.void
在一个Nukkit插件被停用时调用的方法。
Called when a Nukkit plugin is disabled.void
onEnable()
在一个Nukkit插件被启用时调用的方法。
Called when a Nukkit plugin is enabled.void
onLoad()
在一个Nukkit插件被加载时调用的方法。这个方法会在Plugin.onEnable()
之前调用。
Called when a Nukkit plugin is loaded, beforePlugin.onEnable()
.void
重新读取这个Nukkit插件的默认配置文件。
Reloads the plugin config.void
保存这个Nukkit插件的配置文件。
Saves the plugin config.void
保存这个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) final void
加载这个插件。
Enables this plugin.final void
setEnabled
(boolean value) 加载或卸载这个插件。
Enables or disables this plugin.
-
Constructor Details
-
PluginBase
public PluginBase()
-
-
Method Details
-
onLoad
public void onLoad()Description copied from interface:Plugin
在一个Nukkit插件被加载时调用的方法。这个方法会在Plugin.onEnable()
之前调用。
Called when a Nukkit plugin is loaded, beforePlugin.onEnable()
.应该填写加载插件时需要作出的动作。例如:初始化数组、初始化数据库连接。
Use this to init a Nukkit plugin, such as init arrays or init database connections. -
onEnable
public void onEnable()Description copied from interface:Plugin
在一个Nukkit插件被启用时调用的方法。
Called when a Nukkit plugin is enabled.应该填写插件启用时需要作出的动作。例如:读取配置文件、读取资源、连接数据库。
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. -
onDisable
public void onDisable()Description copied from interface:Plugin
在一个Nukkit插件被停用时调用的方法。
Called when a Nukkit plugin is disabled.应该填写插件停用时需要作出的动作。例如:关闭数据库,断开资源。
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. -
isEnabled
public final boolean isEnabled()Description copied from interface:Plugin
返回这个Nukkit插件是否已启用。
Whether this Nukkit plugin is enabled. -
setEnabled
public final void setEnabled()加载这个插件。
Enables this plugin.如果你需要卸载这个插件,建议使用
setEnabled(boolean)
If you need to disable this plugin, it's recommended to usesetEnabled(boolean)
- Since:
- Nukkit 1.0 | Nukkit API 1.0.0
-
setEnabled
@PowerNukkitDifference(info="Made impossible to disable special the PowerNukkitPlugin", since="1.3.0.0-PN") @PowerNukkitXDifference(info="Made impossible to disable special the PowerNukkitX Internal Plugin", since="1.19.60-r1") public final void setEnabled(boolean value) 加载或卸载这个插件。
Enables or disables this plugin.插件管理器插件常常使用这个方法。
It's normally used by a plugin manager plugin to manage plugins.- Parameters:
value
-true
为加载,false
为卸载。true
for enable,false
for disable.- Since:
- Nukkit 1.0 | Nukkit API 1.0.0
-
isDisabled
public final boolean isDisabled()Description copied from interface:Plugin
返回这个Nukkit插件是否已停用。
Whether this Nukkit plugin is disabled.- Specified by:
isDisabled
in interfacePlugin
- Returns:
- 这个插件是否已经停用。
Whether this plugin is disabled.
-
getDataFolder
Description copied from interface:Plugin
返回这个Nukkit插件的数据文件夹。
The data folder of this Nukkit plugin.一般情况下,数据文件夹名字与插件名字相同,而且都放在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.- Specified by:
getDataFolder
in interfacePlugin
- Returns:
- 这个插件的数据文件夹。
The data folder of this plugin.
-
getDescription
Description copied from interface:Plugin
返回描述这个Nukkit插件的PluginDescription
对象。
The description this Nukkit plugin as aPluginDescription
object.对于jar格式的Nukkit插件,插件的描述在plugin.yml文件内定义。
For jar-packed Nukkit plugins, the description is defined in the 'plugin.yml' file.- Specified by:
getDescription
in interfacePlugin
- Returns:
- 这个插件的描述。
A description of this plugin. - See Also:
-
init
public final void init(PluginLoader loader, Server server, PluginDescription description, File dataFolder, File file) 初始化这个插件。
Initialize the plugin.这个方法会在加载(load)之前被插件加载器调用,初始化关于插件的一些事项,不能被重写。
Called by plugin loader before load, and initialize the plugin. Can't be overridden.- Parameters:
loader
- 加载这个插件的插件加载器的PluginLoader
对象。
The plugin loader ,which loads this plugin, as aPluginLoader
object.server
- 运行这个插件的服务器的Server
对象。
The server running this plugin, as aServer
object.description
- 描述这个插件的PluginDescription
对象。
APluginDescription
object that describes this plugin.dataFolder
- 这个插件的数据的文件夹。
The data folder of this plugin.file
- 这个插件的文件File
对象。对于jar格式的插件,就是jar文件本身。
TheFile
object of this plugin itself. For jar-packed plugins, it is the jar file itself.- Since:
- Nukkit 1.0 | Nukkit API 1.0.0
-
getLogger
Description copied from interface:Plugin
返回这个插件的日志记录器为PluginLogger
对象。
Returns the logger of this plugin as aPluginLogger
object.使用日志记录器,你可以在控制台和日志文件输出信息。
You can use a plugin logger to output messages to the console and log file. -
isInitialized
public final boolean isInitialized()返回这个插件是否已经初始化。
Returns if this plugin is initialized.- Returns:
- 这个插件是否已初始化。
if this plugin is initialized. - Since:
- Nukkit 1.0 | Nukkit API 1.0.0
-
getCommand
TODO: FINISH JAVADOC -
getPluginCommand
@PowerNukkitOnly @Since("1.4.0.0-PN") @Nullable public PluginCommand<?> getPluginCommand(@NotNull String name) -
onCommand
Description copied from interface:CommandExecutor
在命令执行时会调用的方法。
Called when a command is executed.一个命令可以是
/a_LABEL an_arg1 AN_ARG2...
的形式,这时label
变量的值为"a_label"
,args
数组的元素有"an_arg1","AN_ARG2",...
。注意到label
变量会被转化成小写, 而args
数组内字符串元素的大小写不变。
A command can be such a form like/a_LABEL an_arg1 AN_ARG2...
. At this time, the value of variablelabel
is"a_label"
, and the values of elements of arrayargs
are"an_arg1","AN_ARG2",...
. Notice that the value of variablelabel
will be converted to lower case, but the cases of elements of arrayargs
won't change.关于返回值,如果返回
false
,Nukkit会给sender发送这个命令的使用方法等信息,来表示这个命令没有使用成功。 如果你的命令成功的发挥了作用,你应该返回true
来表示这个命令已执行成功。
If this function returnsfalse
, Nukkit will send command usages to command sender, to explain that the command didn't work normally. If your command works properly, atrue
should be returned to explain that the command works.如果你想测试一个命令发送者是否有权限执行这个命令, 可以使用
Command.testPermissionSilent(cn.nukkit.command.CommandSender)
。
If you want to test whether a command sender has the permission to execute a command, you can useCommand.testPermissionSilent(cn.nukkit.command.CommandSender)
.- Specified by:
onCommand
in interfaceCommandExecutor
- Parameters:
sender
- 这个命令的发送者,可以是玩家或控制台等。
The sender of this command, this can be a player or a console.command
- 要被发送的命令。
The command to send.label
- 这个命令的标签。
Label of the command.args
- 这个命令的参数列表。
Arguments of this command.- Returns:
- 这个命令执行是否执行成功。
whether this command is executed successfully.
-
getResource
Description copied from interface:Plugin
读取这个插件特定的资源文件,并返回为InputStream
对象。
Reads a resource of this plugin, and returns as anInputStream
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 useUtils.readFile(java.io.File)
to read from aInputStream
and get whole content as a String. For example:String string = Utils.readFile(this.getResource("string.txt"));
- Specified by:
getResource
in interfacePlugin
- Parameters:
filename
- 要读取的资源文件名字。
The name of the resource file to read.- Returns:
- 读取的资源文件的
InputStream
对象。若错误会返回null
The resource as anInputStream
object, ornull
when an error occurred.
-
saveResource
Description copied from interface:Plugin
保存这个Nukkit插件的资源。
Saves the resource of this plugin.对于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.如果需要替换已存在的资源文件,建议使用
Plugin.saveResource(String, boolean)
If you need to REPLACE an existing resource file, it's recommended to usePlugin.saveResource(String, boolean)
.- Specified by:
saveResource
in interfacePlugin
- Parameters:
filename
- 要保存的资源文件名字。
The name of the resource file to save.- Returns:
- 保存是否成功。
true if the saving action is successful. - See Also:
-
saveResource
Description copied from interface:Plugin
保存或替换这个Nukkit插件的资源。
Saves or replaces the resource of this plugin.对于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.如果需要保存默认的资源文件,建议使用
Plugin.saveResource(String)
If you need to SAVE DEFAULT resource file, it's recommended to usePlugin.saveResource(String)
.- Specified by:
saveResource
in interfacePlugin
- Parameters:
filename
- 要保存的资源文件名字。
The name of the resource file to save.replace
- 是否替换目标文件。
if true, Nukkit will replace the target resource file.- Returns:
- 保存是否成功。
true if the saving action is successful. - See Also:
-
saveResource
- Specified by:
saveResource
in interfacePlugin
-
getConfig
Description copied from interface:Plugin
返回这个Nukkit插件配置文件的Config
对象。
The config file this Nukkit plugin as aConfig
object.一般地,插件的配置保存在数据文件夹下的config.yml文件。
Normally, the plugin config is saved in the 'config.yml' file in its data folder. -
saveConfig
public void saveConfig()Description copied from interface:Plugin
保存这个Nukkit插件的配置文件。
Saves the plugin config.- Specified by:
saveConfig
in interfacePlugin
- See Also:
-
saveDefaultConfig
public void saveDefaultConfig()Description copied from interface:Plugin
保存这个Nukkit插件的默认配置文件。
Saves the DEFAULT plugin config.执行这个函数时,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.- Specified by:
saveDefaultConfig
in interfacePlugin
- See Also:
-
reloadConfig
public void reloadConfig()Description copied from interface:Plugin
重新读取这个Nukkit插件的默认配置文件。
Reloads the plugin config.执行这个函数时,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.- Specified by:
reloadConfig
in interfacePlugin
- See Also:
-
getServer
Description copied from interface:Plugin
-
getName
Description copied from interface:Plugin
返回这个插件的名字。
Returns the name of this plugin.Nukkit会从已经读取的插件描述中获取插件的名字。
Nukkit will read plugin name from plugin description. -
getFullName
返回这个插件完整的名字。
Returns the full name of this plugin.一个插件完整的名字由
名字+" v"+版本号
组成。比如:
A full name of a plugin is composed byname+" v"+version
.for example:HelloWorld v1.0.0
- Returns:
- 这个插件完整的名字。
The full name of this plugin. - Since:
- Nukkit 1.0 | Nukkit API 1.0.0
- See Also:
-
getFile
返回这个插件的文件File
对象。对于jar格式的插件,就是jar文件本身。
Returns theFile
object of this plugin itself. For jar-packed plugins, it is the jar file itself. -
getPluginLoader
Description copied from interface:Plugin
- Specified by:
getPluginLoader
in interfacePlugin
- See Also:
-