feat: Core Framework Optimization

Signed-off-by: MiaoWoo <admin@yumc.pw>
merge/3/MERGE
MiaoWoo 2019-03-09 10:28:14 +08:00
parent 8d40a69ac7
commit e455cbf996
8 changed files with 43 additions and 22 deletions

View File

@ -48,10 +48,10 @@
message = '' message = ''
break break
} }
this.console('§4 ' + message + ex) this.console('§4' + message + ex)
this.stack(ex).forEach(function(line) { this.stack(ex).forEach(function(line) {
this.console(line) this.console(line)
}) }.bind(this))
}; };
this.stack = function(ex) { this.stack = function(ex) {
var track = ex ? ex.getStackTrace() : new Error().getStackTrace(); var track = ex ? ex.getStackTrace() : new Error().getStackTrace();

View File

@ -57,11 +57,11 @@
}; };
// 初始化加载器 // 初始化加载器
global.require = engineLoad(root + '/core/require.js')(root); global.require = engineLoad(root + '/core/require.js')(root);
global.requireInternal = function requireInternal(name) { global.requireInternal = function requireInternal(name, ignoreError) {
try { try {
return require(root + '/internal/' + DetectServerType + '/' + name + '.js'); return require(root + '/internal/' + DetectServerType + '/' + name + '.js');
} catch (ex) { } catch (ex) {
if (!arguments[1]) { return {} } if (ignoreError) { return {} }
throw ex; throw ex;
} }
} }

View File

@ -10,7 +10,6 @@ var chatMessageTypes;
var String = Java.type('java.lang.String'); var String = Java.type('java.lang.String');
function init() { function init() {
/** @namespace bukkit.nmsVersion */
nmsChatSerializerClass = bukkit.nmsCls(bukkit.nmsVersion.split("_")[1] > 7 ? "IChatBaseComponent$ChatSerializer" : "ChatSerializer"); nmsChatSerializerClass = bukkit.nmsCls(bukkit.nmsVersion.split("_")[1] > 7 ? "IChatBaseComponent$ChatSerializer" : "ChatSerializer");
packetTypeClass = bukkit.nmsCls("PacketPlayOutChat"); packetTypeClass = bukkit.nmsCls("PacketPlayOutChat");
var packetTypeConstructor; var packetTypeConstructor;

View File

@ -15,11 +15,14 @@ var Material = Java.type('org.bukkit.Material');
* @constructor (ID,数量) * @constructor (ID,数量)
* @constructor (ID,数量,子ID) * @constructor (ID,数量,子ID)
*/ */
item.create = function () { item.create = function() {
var idOrType = arguments[0]; var idOrType = arguments[0];
if (isNaN(Number(idOrType))) { if (isNaN(Number(idOrType))) {
idOrType = Material[idOrType]; idOrType = Material[idOrType];
} }
if (!idOrType) {
throw Error('无效的物品ID或枚举!' + arguments[0] + ' => ' + idOrType)
}
switch (arguments.length) { switch (arguments.length) {
case 1: case 1:
return new ItemStack(idOrType); return new ItemStack(idOrType);
@ -33,7 +36,7 @@ item.create = function () {
* 创建一个头颅 * 创建一个头颅
* @constructor (玩家名称) * @constructor (玩家名称)
*/ */
item.head = function (name) { item.head = function(name) {
var head = item.create(397, 1, 3); var head = item.create(397, 1, 3);
var skullMeta = head.getItemMeta(); var skullMeta = head.getItemMeta();
skullMeta.setOwner(name); skullMeta.setOwner(name);
@ -46,10 +49,10 @@ item.head = function (name) {
* @param items 物品数组 * @param items 物品数组
* @param drop 满背包是否掉落 * @param drop 满背包是否掉落
*/ */
item.add = function (player, items, drop) { item.add = function(player, items, drop) {
var drops = player.getInventory().addItem(items).values(); var drops = player.getInventory().addItem(items).values();
if (drops.size() !== 0 && drop) { if (drops.size() !== 0 && drop) {
drops.forEach(function (itemStack) { drops.forEach(function(itemStack) {
item.drop(player.getLocation(), itemStack); item.drop(player.getLocation(), itemStack);
}); });
} }
@ -59,8 +62,8 @@ item.add = function (player, items, drop) {
* @param loc 地点 * @param loc 地点
* @param item 物品 * @param item 物品
*/ */
item.drop = function (loc, item) { item.drop = function(loc, item) {
setTimeout(function () { setTimeout(function() {
loc.getWorld().dropItem(loc, item); loc.getWorld().dropItem(loc, item);
}, 1); }, 1);
}; };
@ -70,7 +73,7 @@ item.drop = function (loc, item) {
* @param name * @param name
* @returns {*} * @returns {*}
*/ */
item.setName = function (item, name) { item.setName = function(item, name) {
if (item.getType().name() !== "AIR") { if (item.getType().name() !== "AIR") {
var meta = item.hasItemMeta() ? item.getItemMeta() : Bukkit.getItemFactory().getItemMeta(item.getType()); var meta = item.hasItemMeta() ? item.getItemMeta() : Bukkit.getItemFactory().getItemMeta(item.getType());
meta.setDisplayName(name); meta.setDisplayName(name);
@ -84,10 +87,10 @@ item.setName = function (item, name) {
* @param lores Lore * @param lores Lore
* @returns {*} 物品 * @returns {*} 物品
*/ */
item.setLore = item.setLores = function (item, lores) { item.setLore = item.setLores = function(item, lores) {
if (item.getType().name() !== "AIR") { if (item.getType().name() !== "AIR") {
var meta = item.hasItemMeta() ? item.getItemMeta() : Bukkit.getItemFactory().getItemMeta(item.getType()); var meta = item.hasItemMeta() ? item.getItemMeta() : Bukkit.getItemFactory().getItemMeta(item.getType());
if (typeof(lores) === 'string') { if (typeof (lores) === 'string') {
lores = lores.split("\n") lores = lores.split("\n")
} }
meta.setLore(lores); meta.setLore(lores);

View File

@ -141,6 +141,7 @@ function shutdown () {
exports = module.exports = { exports = module.exports = {
$: Bukkit, $: Bukkit,
nmsVersion: nmsVersion,
nmsCls: nmsCls, nmsCls: nmsCls,
obcCls: obcCls, obcCls: obcCls,
plugin: plugin, plugin: plugin,

View File

@ -12,7 +12,13 @@ var BukkitRunnable = Java.type("org.bukkit.scheduler.BukkitRunnable");
*/ */
function create(func) { function create(func) {
if (toString.call(func) !== "[object Function]") { throw TypeError('第一个参数 Task 必须为 function !'); }; 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);
}
});
}; };
/** /**
* 运行任务 * 运行任务

View File

@ -32,7 +32,6 @@ interface Server {
pluginManager; pluginManager;
serviceManager; serviceManager;
servicesManager; servicesManager;
onlinePlayers;
} }
interface Player { interface Player {

View File

@ -18,7 +18,7 @@ function Reflect(obj) {
this.class = obj.class; this.class = obj.class;
} }
this.field = function (name) { this.field = function(name) {
try { try {
// Try getting a public field // Try getting a public field
var field = this.class.field(name); 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 name = arguments[0];
var params = Array.prototype.slice.call(arguments, 1); var params = Array.prototype.slice.call(arguments, 1);
var method = declaredMethod(this.class, name, types(params)); var method = declaredMethod(this.class, name, types(params));
return on(method.invoke(this.get(), params)); return on(method.invoke(this.get(), params));
}; };
this.get = function () { this.get = function() {
return arguments.length === 1 ? this.field(arguments[0]) : this.obj; return arguments.length === 1 ? this.field(arguments[0]) : this.obj;
}; };
// noinspection JSUnusedGlobalSymbols // noinspection JSUnusedGlobalSymbols
this.set = function (name, value) { this.set = function(name, value) {
accessible(declaredField(this.class, name)).set(this.obj, value); accessible(declaredField(this.class, name)).set(this.obj, value);
return this; return this;
}; };
this.create = function () { this.create = function() {
var param = Array.prototype.slice.call(arguments); var param = Array.prototype.slice.call(arguments);
return on(declaredConstructor(this.class, param).newInstance(param)); return on(declaredConstructor(this.class, param).newInstance(param));
}; };
@ -64,7 +77,7 @@ function types(values, def) {
return []; return [];
} }
var result = []; var result = [];
values.forEach(function (t) { values.forEach(function(t) {
result.push((t || def) ? Object.class : t instanceof Class ? t : t.class) result.push((t || def) ? Object.class : t instanceof Class ? t : t.class)
}); });
return result; return result;