From e455cbf99633e66de397c3a4a33e480d438a6e73 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Sat, 9 Mar 2019 10:28:14 +0800 Subject: [PATCH] feat: Core Framework Optimization Signed-off-by: MiaoWoo --- src/main/resources/core/console.js | 4 ++-- src/main/resources/core/init.js | 4 ++-- src/main/resources/internal/bukkit/chat.js | 1 - src/main/resources/internal/bukkit/item.js | 21 +++++++++------- src/main/resources/internal/bukkit/server.js | 1 + src/main/resources/internal/bukkit/task.js | 8 ++++++- src/main/resources/internal/internal.d.ts | 1 - src/main/resources/modules/reflect.js | 25 +++++++++++++++----- 8 files changed, 43 insertions(+), 22 deletions(-) diff --git a/src/main/resources/core/console.js b/src/main/resources/core/console.js index 7b4ea1b..ad009be 100644 --- a/src/main/resources/core/console.js +++ b/src/main/resources/core/console.js @@ -48,10 +48,10 @@ message = '' break } - this.console('§4 ' + message + ex) + this.console('§4' + message + ex) this.stack(ex).forEach(function(line) { this.console(line) - }) + }.bind(this)) }; this.stack = function(ex) { var track = ex ? ex.getStackTrace() : new Error().getStackTrace(); diff --git a/src/main/resources/core/init.js b/src/main/resources/core/init.js index 1da09d3..6c39879 100644 --- a/src/main/resources/core/init.js +++ b/src/main/resources/core/init.js @@ -57,11 +57,11 @@ }; // 初始化加载器 global.require = engineLoad(root + '/core/require.js')(root); - global.requireInternal = function requireInternal(name) { + global.requireInternal = function requireInternal(name, ignoreError) { try { return require(root + '/internal/' + DetectServerType + '/' + name + '.js'); } catch (ex) { - if (!arguments[1]) { return {} } + if (ignoreError) { return {} } throw ex; } } diff --git a/src/main/resources/internal/bukkit/chat.js b/src/main/resources/internal/bukkit/chat.js index c5e4295..c96813a 100644 --- a/src/main/resources/internal/bukkit/chat.js +++ b/src/main/resources/internal/bukkit/chat.js @@ -10,7 +10,6 @@ var chatMessageTypes; var String = Java.type('java.lang.String'); function init() { - /** @namespace bukkit.nmsVersion */ nmsChatSerializerClass = bukkit.nmsCls(bukkit.nmsVersion.split("_")[1] > 7 ? "IChatBaseComponent$ChatSerializer" : "ChatSerializer"); packetTypeClass = bukkit.nmsCls("PacketPlayOutChat"); var packetTypeConstructor; diff --git a/src/main/resources/internal/bukkit/item.js b/src/main/resources/internal/bukkit/item.js index 0ef74ab..9d84d03 100644 --- a/src/main/resources/internal/bukkit/item.js +++ b/src/main/resources/internal/bukkit/item.js @@ -15,11 +15,14 @@ var Material = Java.type('org.bukkit.Material'); * @constructor (ID,数量) * @constructor (ID,数量,子ID) */ -item.create = function () { +item.create = function() { var idOrType = arguments[0]; if (isNaN(Number(idOrType))) { idOrType = Material[idOrType]; } + if (!idOrType) { + throw Error('无效的物品ID或枚举!' + arguments[0] + ' => ' + idOrType) + } switch (arguments.length) { case 1: return new ItemStack(idOrType); @@ -33,7 +36,7 @@ item.create = function () { * 创建一个头颅 * @constructor (玩家名称) */ -item.head = function (name) { +item.head = function(name) { var head = item.create(397, 1, 3); var skullMeta = head.getItemMeta(); skullMeta.setOwner(name); @@ -46,10 +49,10 @@ item.head = function (name) { * @param items 物品数组 * @param drop 满背包是否掉落 */ -item.add = function (player, items, drop) { +item.add = function(player, items, drop) { var drops = player.getInventory().addItem(items).values(); if (drops.size() !== 0 && drop) { - drops.forEach(function (itemStack) { + drops.forEach(function(itemStack) { item.drop(player.getLocation(), itemStack); }); } @@ -59,8 +62,8 @@ item.add = function (player, items, drop) { * @param loc 地点 * @param item 物品 */ -item.drop = function (loc, item) { - setTimeout(function () { +item.drop = function(loc, item) { + setTimeout(function() { loc.getWorld().dropItem(loc, item); }, 1); }; @@ -70,7 +73,7 @@ item.drop = function (loc, item) { * @param name * @returns {*} */ -item.setName = function (item, name) { +item.setName = function(item, name) { if (item.getType().name() !== "AIR") { var meta = item.hasItemMeta() ? item.getItemMeta() : Bukkit.getItemFactory().getItemMeta(item.getType()); meta.setDisplayName(name); @@ -84,10 +87,10 @@ item.setName = function (item, name) { * @param lores Lore * @returns {*} 物品 */ -item.setLore = item.setLores = function (item, lores) { +item.setLore = item.setLores = function(item, lores) { if (item.getType().name() !== "AIR") { var meta = item.hasItemMeta() ? item.getItemMeta() : Bukkit.getItemFactory().getItemMeta(item.getType()); - if (typeof(lores) === 'string') { + if (typeof (lores) === 'string') { lores = lores.split("\n") } meta.setLore(lores); diff --git a/src/main/resources/internal/bukkit/server.js b/src/main/resources/internal/bukkit/server.js index 5809a42..35e7f29 100644 --- a/src/main/resources/internal/bukkit/server.js +++ b/src/main/resources/internal/bukkit/server.js @@ -141,6 +141,7 @@ function shutdown () { exports = module.exports = { $: Bukkit, + nmsVersion: nmsVersion, nmsCls: nmsCls, obcCls: obcCls, plugin: plugin, diff --git a/src/main/resources/internal/bukkit/task.js b/src/main/resources/internal/bukkit/task.js index bbe964e..8b0f37a 100644 --- a/src/main/resources/internal/bukkit/task.js +++ b/src/main/resources/internal/bukkit/task.js @@ -12,7 +12,13 @@ var BukkitRunnable = Java.type("org.bukkit.scheduler.BukkitRunnable"); */ function create(func) { if (toString.call(func) !== "[object Function]") { throw TypeError('第一个参数 Task 必须为 function !'); }; - return new BukkitRunnable(func); + return new BukkitRunnable(function() { + try { + func(); + } catch (ex) { + console.ex('§4插件执行任务时发生错误', ex); + } + }); }; /** * 运行任务 diff --git a/src/main/resources/internal/internal.d.ts b/src/main/resources/internal/internal.d.ts index a78b445..46553c3 100644 --- a/src/main/resources/internal/internal.d.ts +++ b/src/main/resources/internal/internal.d.ts @@ -32,7 +32,6 @@ interface Server { pluginManager; serviceManager; servicesManager; - onlinePlayers; } interface Player { diff --git a/src/main/resources/modules/reflect.js b/src/main/resources/modules/reflect.js index 34994ec..3da86b3 100644 --- a/src/main/resources/modules/reflect.js +++ b/src/main/resources/modules/reflect.js @@ -18,7 +18,7 @@ function Reflect(obj) { this.class = obj.class; } - this.field = function (name) { + this.field = function(name) { try { // Try getting a public field var field = this.class.field(name); @@ -33,24 +33,37 @@ function Reflect(obj) { } }; - this.call = function () { + this.fields = function(declared) { + return declared ? this.class.declaredFields : this.class.fields; + } + + this.values = function(declared) { + var cache = {}; + var feds = declared ? this.class.declaredFields : this.class.fields; + Java.from(feds).forEach(function(fed) { + cache[fed.name] = this.field(fed.name).get(); + }.bind(this)) + return cache; + } + + this.call = function() { var name = arguments[0]; var params = Array.prototype.slice.call(arguments, 1); var method = declaredMethod(this.class, name, types(params)); return on(method.invoke(this.get(), params)); }; - this.get = function () { + this.get = function() { return arguments.length === 1 ? this.field(arguments[0]) : this.obj; }; // noinspection JSUnusedGlobalSymbols - this.set = function (name, value) { + this.set = function(name, value) { accessible(declaredField(this.class, name)).set(this.obj, value); return this; }; - this.create = function () { + this.create = function() { var param = Array.prototype.slice.call(arguments); return on(declaredConstructor(this.class, param).newInstance(param)); }; @@ -64,7 +77,7 @@ function types(values, def) { return []; } var result = []; - values.forEach(function (t) { + values.forEach(function(t) { result.push((t || def) ? Object.class : t instanceof Class ? t : t.class) }); return result;