From b46da877f67be8bdb607887135fc89c45d7306b9 Mon Sep 17 00:00:00 2001 From: coding Date: Sat, 14 Oct 2017 07:32:19 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E5=9F=BA=E7=A1=80=E7=B1=BB=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/core/console.js | 31 ++++++++++------ src/main/resources/modules/bukkit.js | 4 ++ src/main/resources/modules/command.js | 4 +- src/main/resources/modules/plugin.js | 53 +++++++++++++++++++-------- 4 files changed, 63 insertions(+), 29 deletions(-) diff --git a/src/main/resources/core/console.js b/src/main/resources/core/console.js index 84280e0..2bb360c 100644 --- a/src/main/resources/core/console.js +++ b/src/main/resources/core/console.js @@ -4,18 +4,27 @@ /*global base*/ var log = base.getLog().static; var Level = Java.type('java.util.logging.Level'); -var console = { - log: function () { - log.i(Array.prototype.join.call(arguments, ' ')); +var Console = function (name) { + this.name = name ? '[' + name + '] ' : ''; + Object.defineProperty(this, 'name', { + get: function () { + return this._name; + }.bind(this), + set: function (name) { + this._name = name ? '[' + name + '] ' : ''; + }.bind(this) + }); + this.log = function () { + log.i(this.name + Array.prototype.join.call(arguments, ' ')); }, - warn: function () { - log.w(Array.prototype.join.call(arguments, ' ')); + this.warn = function () { + log.w(this.name + Array.prototype.join.call(arguments, ' ')); }, - error: function () { - log.log(Level.SEVERE, Array.prototype.join.call(arguments, ' ')); + this.error = function () { + log.log(Level.SEVERE, this.name + Array.prototype.join.call(arguments, ' ')); }, - debug: function () { - log.d(Array.prototype.join.call(arguments, ' ')); + this.debug = function () { + log.d(this.name + Array.prototype.join.call(arguments, ' ')); } -}; -global.console = console; \ No newline at end of file +} +global.console = new Console(); \ No newline at end of file diff --git a/src/main/resources/modules/bukkit.js b/src/main/resources/modules/bukkit.js index 16e06c7..216ec6e 100644 --- a/src/main/resources/modules/bukkit.js +++ b/src/main/resources/modules/bukkit.js @@ -9,6 +9,7 @@ var Bukkit = Java.type("org.bukkit.Bukkit"); // noinspection JSUnresolvedVariable var PluginManager = Bukkit.pluginManager; exports.$ = Bukkit; +exports.nmsVersion = Bukkit.server.class.name.split('.')[3]; /** * 插件管理 * @type {{manager: *, get: exports.plugin.get, load: exports.plugin.load}} @@ -39,6 +40,9 @@ exports.plugin = { return PluginManager.isPluginEnabled(name); } }; +exports.nmsCls = function (name){ + return Java.type(['net.minecraft.server', exports.nmsVersion, name].join('.')); +} /** * 公告 * @param message 消息 diff --git a/src/main/resources/modules/command.js b/src/main/resources/modules/command.js index 435ed6a..8078864 100644 --- a/src/main/resources/modules/command.js +++ b/src/main/resources/modules/command.js @@ -5,8 +5,8 @@ /*global Java, base, module, exports, require, __FILE__*/ var plugin = base.plugin; -var bukkit = require('bukkit'); -var ref = require('kit/reflect'); +var bukkit = require('./bukkit'); +var ref = require('core/reflect'); var lookupNames = ref.on(bukkit.plugin.manager).get('lookupNames').get(); var knownCommands = ref.on(bukkit.plugin.manager).get('commandMap').get('knownCommands').get(); var PluginCommand = Java.type('org.bukkit.command.PluginCommand'); diff --git a/src/main/resources/modules/plugin.js b/src/main/resources/modules/plugin.js index cfa7bab..9bf8d69 100644 --- a/src/main/resources/modules/plugin.js +++ b/src/main/resources/modules/plugin.js @@ -23,8 +23,8 @@ function loadPlugins(path) { fs.list(path).forEach(function (file) { files.push(file.toFile()); }); - loadZipPlugin(files); - loadJsPlugin(files); + loadZipPlugins(files); + loadJsPlugins(files); } } @@ -48,7 +48,7 @@ function updatePlugins(path) { * ZIP类型插件预加载 * @param files */ -function loadZipPlugin(files) { +function loadZipPlugins(files) { // // TODO ZIP类型插件加载 // files.filter(function (file) { // return file.name.endsWith(".zip"); @@ -62,32 +62,48 @@ function loadZipPlugin(files) { /** * JS类型插件预加载 */ -function loadJsPlugin(files) { +function loadJsPlugins(files) { files.filter(function (file) { return file.name.endsWith(".js") }).forEach(function (file) { - var p = require(file); - log.d("插件编译结果: %s", JSON.stringify(p)); - if (!p.description || !p.description.name) { - log.w("文件 %s 不存在 description 描述信息 无法加载插件!", file); - } else { - initPlugin(file, p); - plugins.push(p); - plugins[p.description.name] = p; - log.i('载入插件 %s 版本 %s By %s', p.description.name, p.description.version || '未知', p.description.author || '未知'); - } + loadPlugin(file); }) } + +function loadPlugin(file) { + var p = require(file, { + cache: false, + // 给插件注入单独的 console + hook: function (origin) { + return 'var console = new Console();' + origin + 'module.exports.console = console;' + } + }); + log.d("插件编译结果: %s", p.toJson()); + if (!p.description || !p.description.name) { + log.w("文件 %s 不存在 description 描述信息 无法加载插件!", file); + } else { + initPlugin(file, p); + plugins.push(p); + plugins[p.description.name] = p; + log.i('载入插件 %s 版本 %s By %s', p.description.name, p.description.version || '未知', p.description.author || '未知'); + } + return p; +} + /** * 初始化插件内容(提供config,__DATA__等参数) */ function initPlugin(file, plugin){ + // 初始化 __FILE__ + plugin.__FILE__ = file; // 初始化 __DATA__ plugin.__DATA__ = fs.file(file.parentFile, plugin.description.name); // 初始化 getFile() plugin.getFile = function(name) { return fs.file(plugin.__DATA__, name); } + // 给 console 添加插件名称 + plugin.console.name = plugin.description.name; // 初始化 getConfig() /** * 获取配置文件 @@ -115,9 +131,10 @@ function initPlugin(file, plugin){ plugin.saveConfig = function() { switch (arguments.length) { case 0: - base.save(plugin.configFile, yaml.safeDump(plugin.config)); + plugin.configFile.parentFile.mkdirs() + base.save(plugin.configFile, plugin.config.toYaml()); case 2: - base.save(arguments[0], yaml.safeDump(arguments[1])); + base.save(arguments[0], arguments[1].toYaml()); } } // 初始化 getDataFolder() @@ -128,6 +145,9 @@ function initPlugin(file, plugin){ plugin.configFile = plugin.getFile('config.yml'); if (plugin.configFile.isFile()) { plugin.config = plugin.getConfig('config.yml'); + } else if ( plugin.description.config ){ + plugin.config = plugin.description.config; + plugin.saveConfig(); } } @@ -185,6 +205,7 @@ exports.disable = function () { exports.reload = function () { checkAndGet(arguments).forEach(function (p) { exports.disable(p); + p = loadPlugin(p.__FILE__); exports.load(p); exports.enable(p); });