Class CommonJSPlugin

java.lang.Object
cn.nukkit.plugin.CommonJSPlugin
All Implemented Interfaces:
CommandExecutor, Listener, Plugin

public class CommonJSPlugin extends Object implements Plugin, Listener
  • Field Details

    • jsPluginIdMap

      public static final it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap<CommonJSPlugin> jsPluginIdMap
    • jsExternalMap

      public static final ConcurrentHashMap<String,JSExternal> jsExternalMap
    • globalMaxId

      public static int globalMaxId
    • pluginName

      protected String pluginName
    • pluginDir

      protected File pluginDir
    • mainJSFile

      protected File mainJSFile
    • server

      protected Server server
    • classLoader

      protected JSClassLoader classLoader
    • fileSystem

      protected ESMFileSystem fileSystem
    • jsContext

      protected org.graalvm.polyglot.Context jsContext
    • jsExports

      protected org.graalvm.polyglot.Value jsExports
    • usedFeatures

      public final LinkedHashMap<String,JSFeature> usedFeatures
    • id

      public final int id
  • Constructor Details

    • CommonJSPlugin

      public CommonJSPlugin()
  • Method Details

    • init

      public final void init(@NotNull JSPluginLoader jsPluginLoader, File pluginDir, PluginDescription pluginDescription)
    • onCommand

      public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
      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 variable label is "a_label", and the values of elements of array args are "an_arg1","AN_ARG2",.... Notice that the value of variable label will be converted to lower case, but the cases of elements of array args won't change.

      关于返回值,如果返回false,Nukkit会给sender发送这个命令的使用方法等信息,来表示这个命令没有使用成功。 如果你的命令成功的发挥了作用,你应该返回true来表示这个命令已执行成功。
      If this function returns false, Nukkit will send command usages to command sender, to explain that the command didn't work normally. If your command works properly, a true 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 use Command.testPermissionSilent(cn.nukkit.command.CommandSender).

      Specified by:
      onCommand in interface CommandExecutor
      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.
    • onLoad

      public void onLoad()
      Description copied from interface: Plugin
      在一个Nukkit插件被加载时调用的方法。这个方法会在Plugin.onEnable()之前调用。
      Called when a Nukkit plugin is loaded, before Plugin.onEnable() .

      应该填写加载插件时需要作出的动作。例如:初始化数组、初始化数据库连接。
      Use this to init a Nukkit plugin, such as init arrays or init database connections.

      Specified by:
      onLoad in interface Plugin
    • 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.

      Specified by:
      onEnable in interface Plugin
    • isEnabled

      public boolean isEnabled()
      Description copied from interface: Plugin
      返回这个Nukkit插件是否已启用。
      Whether this Nukkit plugin is enabled.
      Specified by:
      isEnabled in interface Plugin
      Returns:
      这个插件是否已经启用。
      Whether this plugin is enabled.
    • 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.

      Specified by:
      onDisable in interface Plugin
    • isDisabled

      public boolean isDisabled()
      Description copied from interface: Plugin
      返回这个Nukkit插件是否已停用。
      Whether this Nukkit plugin is disabled.
      Specified by:
      isDisabled in interface Plugin
      Returns:
      这个插件是否已经停用。
      Whether this plugin is disabled.
    • getDataFolder

      public File 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 interface Plugin
      Returns:
      这个插件的数据文件夹。
      The data folder of this plugin.
    • getDescription

      public PluginDescription getDescription()
      Description copied from interface: Plugin
      返回描述这个Nukkit插件的PluginDescription对象。
      The description this Nukkit plugin as a PluginDescription object.

      对于jar格式的Nukkit插件,插件的描述在plugin.yml文件内定义。
      For jar-packed Nukkit plugins, the description is defined in the 'plugin.yml' file.

      Specified by:
      getDescription in interface Plugin
      Returns:
      这个插件的描述。
      A description of this plugin.
      See Also:
    • getResource

      public InputStream getResource(String filename)
      Description copied from interface: Plugin
      读取这个插件特定的资源文件,并返回为InputStream对象。
      Reads a resource of this plugin, and returns as an 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"));

      Specified by:
      getResource in interface Plugin
      Parameters:
      filename - 要读取的资源文件名字。
      The name of the resource file to read.
      Returns:
      读取的资源文件的 InputStream对象。若错误会返回null
      The resource as an InputStream object, or null when an error occurred.
    • saveResource

      public boolean saveResource(String filename)
      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 use Plugin.saveResource(String, boolean).

      Specified by:
      saveResource in interface Plugin
      Parameters:
      filename - 要保存的资源文件名字。
      The name of the resource file to save.
      Returns:
      保存是否成功。
      true if the saving action is successful.
      See Also:
    • saveResource

      public boolean saveResource(String filename, boolean replace)
      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 use Plugin.saveResource(String).

      Specified by:
      saveResource in interface Plugin
      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

      public boolean saveResource(String filename, String outputName, boolean replace)
      Specified by:
      saveResource in interface Plugin
    • getConfig

      public Config getConfig()
      Description copied from interface: Plugin
      返回这个Nukkit插件配置文件的Config对象。
      The config file this Nukkit plugin as a Config object.

      一般地,插件的配置保存在数据文件夹下的config.yml文件。
      Normally, the plugin config is saved in the 'config.yml' file in its data folder.

      Specified by:
      getConfig in interface Plugin
      Returns:
      插件的配置文件。
      The configuration of this plugin.
      See Also:
    • saveConfig

      public void saveConfig()
      Description copied from interface: Plugin
      保存这个Nukkit插件的配置文件。
      Saves the plugin config.
      Specified by:
      saveConfig in interface Plugin
      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 interface Plugin
      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 interface Plugin
      See Also:
    • getServer

      public Server getServer()
      Description copied from interface: Plugin
      返回运行这个插件的服务器的Server对象。
      Gets the server which is running this plugin, and returns as a Server object.
      Specified by:
      getServer in interface Plugin
      See Also:
    • getName

      public String getName()
      Description copied from interface: Plugin
      返回这个插件的名字。
      Returns the name of this plugin.

      Nukkit会从已经读取的插件描述中获取插件的名字。
      Nukkit will read plugin name from plugin description.

      Specified by:
      getName in interface Plugin
      See Also:
    • getLogger

      public PluginLogger getLogger()
      Description copied from interface: Plugin
      返回这个插件的日志记录器为PluginLogger对象。
      Returns the logger of this plugin as a PluginLogger object.

      使用日志记录器,你可以在控制台和日志文件输出信息。
      You can use a plugin logger to output messages to the console and log file.

      Specified by:
      getLogger in interface Plugin
      See Also:
    • getPluginLoader

      public PluginLoader getPluginLoader()
      Description copied from interface: Plugin
      返回这个插件的加载器为PluginLoader对象。
      Returns the loader of this plugin as a PluginLoader object.
      Specified by:
      getPluginLoader in interface Plugin
      See Also:
    • getFile

      @Since("1.19.50-r3") public File getFile()
      Description copied from interface: Plugin
      返回这个插件的文件File对象。
      Returns the File object of this plugin itself.
      Specified by:
      getFile in interface Plugin
      Returns:
      这个插件的文件 File对象。
      The File object of this plugin itself.
    • isInitialized

      public boolean isInitialized()
    • getJsContext

      public org.graalvm.polyglot.Context getJsContext()
    • getJsExports

      public org.graalvm.polyglot.Value getJsExports()
    • getFileSystem

      public ESMFileSystem getFileSystem()
    • getClassLoader

      public JSClassLoader getClassLoader()
    • setClassLoader

      public CommonJSPlugin setClassLoader(JSClassLoader classLoader)