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 = ''
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();

View File

@ -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;
}
}

View File

@ -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;

View File

@ -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);

View File

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

View File

@ -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);
}
});
};
/**
* 运行任务

View File

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

View File

@ -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;