feat: 更新插件基础类库

This commit is contained in:
coding 2017-10-14 07:32:19 +00:00
parent 57554fa644
commit b46da877f6
4 changed files with 63 additions and 29 deletions

View File

@ -4,18 +4,27 @@
/*global base*/ /*global base*/
var log = base.getLog().static; var log = base.getLog().static;
var Level = Java.type('java.util.logging.Level'); var Level = Java.type('java.util.logging.Level');
var console = { var Console = function (name) {
log: function () { this.name = name ? '[' + name + '] ' : '';
log.i(Array.prototype.join.call(arguments, ' ')); 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 () { this.warn = function () {
log.w(Array.prototype.join.call(arguments, ' ')); log.w(this.name + Array.prototype.join.call(arguments, ' '));
}, },
error: function () { this.error = function () {
log.log(Level.SEVERE, Array.prototype.join.call(arguments, ' ')); log.log(Level.SEVERE, this.name + Array.prototype.join.call(arguments, ' '));
}, },
debug: function () { this.debug = function () {
log.d(Array.prototype.join.call(arguments, ' ')); log.d(this.name + Array.prototype.join.call(arguments, ' '));
} }
}; }
global.console = console; global.console = new Console();

View File

@ -9,6 +9,7 @@ var Bukkit = Java.type("org.bukkit.Bukkit");
// noinspection JSUnresolvedVariable // noinspection JSUnresolvedVariable
var PluginManager = Bukkit.pluginManager; var PluginManager = Bukkit.pluginManager;
exports.$ = Bukkit; exports.$ = Bukkit;
exports.nmsVersion = Bukkit.server.class.name.split('.')[3];
/** /**
* 插件管理 * 插件管理
* @type {{manager: *, get: exports.plugin.get, load: exports.plugin.load}} * @type {{manager: *, get: exports.plugin.get, load: exports.plugin.load}}
@ -39,6 +40,9 @@ exports.plugin = {
return PluginManager.isPluginEnabled(name); return PluginManager.isPluginEnabled(name);
} }
}; };
exports.nmsCls = function (name){
return Java.type(['net.minecraft.server', exports.nmsVersion, name].join('.'));
}
/** /**
* 公告 * 公告
* @param message 消息 * @param message 消息

View File

@ -5,8 +5,8 @@
/*global Java, base, module, exports, require, __FILE__*/ /*global Java, base, module, exports, require, __FILE__*/
var plugin = base.plugin; var plugin = base.plugin;
var bukkit = require('bukkit'); var bukkit = require('./bukkit');
var ref = require('kit/reflect'); var ref = require('core/reflect');
var lookupNames = ref.on(bukkit.plugin.manager).get('lookupNames').get(); var lookupNames = ref.on(bukkit.plugin.manager).get('lookupNames').get();
var knownCommands = ref.on(bukkit.plugin.manager).get('commandMap').get('knownCommands').get(); var knownCommands = ref.on(bukkit.plugin.manager).get('commandMap').get('knownCommands').get();
var PluginCommand = Java.type('org.bukkit.command.PluginCommand'); var PluginCommand = Java.type('org.bukkit.command.PluginCommand');

View File

@ -23,8 +23,8 @@ function loadPlugins(path) {
fs.list(path).forEach(function (file) { fs.list(path).forEach(function (file) {
files.push(file.toFile()); files.push(file.toFile());
}); });
loadZipPlugin(files); loadZipPlugins(files);
loadJsPlugin(files); loadJsPlugins(files);
} }
} }
@ -48,7 +48,7 @@ function updatePlugins(path) {
* ZIP类型插件预加载 * ZIP类型插件预加载
* @param files * @param files
*/ */
function loadZipPlugin(files) { function loadZipPlugins(files) {
// // TODO ZIP类型插件加载 // // TODO ZIP类型插件加载
// files.filter(function (file) { // files.filter(function (file) {
// return file.name.endsWith(".zip"); // return file.name.endsWith(".zip");
@ -62,12 +62,23 @@ function loadZipPlugin(files) {
/** /**
* JS类型插件预加载 * JS类型插件预加载
*/ */
function loadJsPlugin(files) { function loadJsPlugins(files) {
files.filter(function (file) { files.filter(function (file) {
return file.name.endsWith(".js") return file.name.endsWith(".js")
}).forEach(function (file) { }).forEach(function (file) {
var p = require(file); loadPlugin(file);
log.d("插件编译结果: %s", JSON.stringify(p)); })
}
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) { if (!p.description || !p.description.name) {
log.w("文件 %s 不存在 description 描述信息 无法加载插件!", file); log.w("文件 %s 不存在 description 描述信息 无法加载插件!", file);
} else { } else {
@ -76,18 +87,23 @@ function loadJsPlugin(files) {
plugins[p.description.name] = p; plugins[p.description.name] = p;
log.i('载入插件 %s 版本 %s By %s', p.description.name, p.description.version || '未知', p.description.author || '未知'); log.i('载入插件 %s 版本 %s By %s', p.description.name, p.description.version || '未知', p.description.author || '未知');
} }
}) return p;
} }
/** /**
* 初始化插件内容(提供config,__DATA__等参数) * 初始化插件内容(提供config,__DATA__等参数)
*/ */
function initPlugin(file, plugin){ function initPlugin(file, plugin){
// 初始化 __FILE__
plugin.__FILE__ = file;
// 初始化 __DATA__ // 初始化 __DATA__
plugin.__DATA__ = fs.file(file.parentFile, plugin.description.name); plugin.__DATA__ = fs.file(file.parentFile, plugin.description.name);
// 初始化 getFile() // 初始化 getFile()
plugin.getFile = function(name) { plugin.getFile = function(name) {
return fs.file(plugin.__DATA__, name); return fs.file(plugin.__DATA__, name);
} }
// 给 console 添加插件名称
plugin.console.name = plugin.description.name;
// 初始化 getConfig() // 初始化 getConfig()
/** /**
* 获取配置文件 * 获取配置文件
@ -115,9 +131,10 @@ function initPlugin(file, plugin){
plugin.saveConfig = function() { plugin.saveConfig = function() {
switch (arguments.length) { switch (arguments.length) {
case 0: case 0:
base.save(plugin.configFile, yaml.safeDump(plugin.config)); plugin.configFile.parentFile.mkdirs()
base.save(plugin.configFile, plugin.config.toYaml());
case 2: case 2:
base.save(arguments[0], yaml.safeDump(arguments[1])); base.save(arguments[0], arguments[1].toYaml());
} }
} }
// 初始化 getDataFolder() // 初始化 getDataFolder()
@ -128,6 +145,9 @@ function initPlugin(file, plugin){
plugin.configFile = plugin.getFile('config.yml'); plugin.configFile = plugin.getFile('config.yml');
if (plugin.configFile.isFile()) { if (plugin.configFile.isFile()) {
plugin.config = plugin.getConfig('config.yml'); 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 () { exports.reload = function () {
checkAndGet(arguments).forEach(function (p) { checkAndGet(arguments).forEach(function (p) {
exports.disable(p); exports.disable(p);
p = loadPlugin(p.__FILE__);
exports.load(p); exports.load(p);
exports.enable(p); exports.enable(p);
}); });