feat: 降低调试等级 优化事件监听

This commit is contained in:
coding 2017-10-23 07:18:59 +00:00
parent 1058677c5f
commit 86d3e74d47
7 changed files with 22 additions and 21 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>pw.yumc</groupId> <groupId>pw.yumc</groupId>
<artifactId>MiaoScript</artifactId> <artifactId>MiaoScript</artifactId>
<version>1.1</version> <version>1.1.1</version>
<developers> <developers>
<developer> <developer>
<id>502647092</id> <id>502647092</id>

View File

@ -36,12 +36,12 @@ public class Base {
} }
public String read(String path) throws IOException { public String read(String path) throws IOException {
Log.d("读取文件 %s ...", path); Log.fd("读取文件 %s ...", path);
return new String(Files.readAllBytes(new File(path).toPath()), "UTF-8"); return new String(Files.readAllBytes(new File(path).toPath()), "UTF-8");
} }
public void save(String path, String content) throws IOException { public void save(String path, String content) throws IOException {
Log.d("保存文件 %s ...", path); Log.fd("保存文件 %s ...", path);
File file = new File(path); File file = new File(path);
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
Files.write(file.toPath(), content.getBytes("UTF-8")); Files.write(file.toPath(), content.getBytes("UTF-8"));
@ -54,7 +54,7 @@ public class Base {
public void delete(Path path) throws IOException { public void delete(Path path) throws IOException {
val file = path.toFile(); val file = path.toFile();
if (!file.exists()) { return; } if (!file.exists()) { return; }
Log.d("删除文件 %s ...", path); Log.fd("删除文件 %s ...", path);
if (file.isDirectory()) { if (file.isDirectory()) {
for (Path f : Files.list(file.toPath()).collect(Collectors.toList())) { for (Path f : Files.list(file.toPath()).collect(Collectors.toList())) {
delete(f); delete(f);

View File

@ -79,6 +79,7 @@ public class MiaoScript extends JavaPlugin implements Executor {
private void saveScript() { private void saveScript() {
P.saveFile(true, "core", "modules"); P.saveFile(true, "core", "modules");
P.saveFile('plugins');
} }
private void enableEngine() { private void enableEngine() {

View File

@ -42,7 +42,7 @@ function Reflect(obj) {
}; };
this.cacheMethod = function (name, clazzs) { this.cacheMethod = function (name, clazzs) {
var mkey = this.class.name + '.' + name; var mkey = this.class.name + '.' + name + ':' + clazzs.join(':');
if (!methodCache[mkey]) { if (!methodCache[mkey]) {
methodCache[mkey] = this.method(name, clazzs); methodCache[mkey] = this.method(name, clazzs);
} }
@ -53,7 +53,7 @@ function Reflect(obj) {
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 = this.cacheMethod(name, types(params)); var method = this.cacheMethod(name, types(params));
return exports.on(method.invoke(this.get(), params)); return on(method.invoke(this.get(), params));
}; };
this.get = function () { this.get = function () {
@ -84,7 +84,7 @@ function types(values, def) {
} }
var result = []; var result = [];
values.forEach(function (t) { values.forEach(function (t) {
result.push((t === null || 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;
} }

View File

@ -103,7 +103,7 @@
* @returns {Object} * @returns {Object}
*/ */
function compileModule(id, name, file, optional) { function compileModule(id, name, file, optional) {
log.d('加载模块 %s 位于 %s Optional %s', name, id, optional.toJson()); log.fd('加载模块 %s 位于 %s Optional %s', name, id, optional.toJson());
// noinspection JSUnresolvedVariable // noinspection JSUnresolvedVariable
var module = { var module = {
id: id, id: id,
@ -116,7 +116,7 @@
compiledWrapper.apply(module.exports, [ compiledWrapper.apply(module.exports, [
module, module.exports, module.require, file.parentFile, file module, module.exports, module.require, file.parentFile, file
]); ]);
log.d('模块 %s 编译成功!', name); log.fd('模块 %s 编译成功!', name);
module.loaded = true; module.loaded = true;
} catch (ex) { } catch (ex) {
log.console("§4警告! §b模块 §a%s §4编译失败! ERR: %s", name, ex); log.console("§4警告! §b模块 §a%s §4编译失败! ERR: %s", name, ex);
@ -150,7 +150,7 @@
function _require(name, path, optional) { function _require(name, path, optional) {
var file = resolve(name, path); var file = resolve(name, path);
if (file === undefined) { if (file === undefined) {
log.w("模块 %s 加载失败! 未找到该模块!", name); log.console("§c模块 §a%s §c加载失败! §4未找到该模块!", name);
return {exports:{}}; return {exports:{}};
} }
if (!optional) optional = { cache: true } if (!optional) optional = { cache: true }

View File

@ -25,7 +25,7 @@ var listenerMap = [];
*/ */
function mapEventName() { function mapEventName() {
var eventPackageDir = "org/bukkit/event"; var eventPackageDir = "org/bukkit/event";
var count = 0;
var dirs = Thread.currentThread().getContextClassLoader().getResources(eventPackageDir); var dirs = Thread.currentThread().getContextClassLoader().getResources(eventPackageDir);
while (dirs.hasMoreElements()) { while (dirs.hasMoreElements()) {
var url = dirs.nextElement(); var url = dirs.nextElement();
@ -48,6 +48,7 @@ function mapEventName() {
var simpleName = clz.simpleName.toLowerCase(); var simpleName = clz.simpleName.toLowerCase();
log.fd("Mapping Event [%s] => %s", clz.name, simpleName); log.fd("Mapping Event [%s] => %s", clz.name, simpleName);
mapEvent[simpleName] = clz; mapEvent[simpleName] = clz;
count++;
} }
} catch (ex) { } catch (ex) {
//ignore already loaded class //ignore already loaded class
@ -56,6 +57,7 @@ function mapEventName() {
} }
} }
} }
return count;
} }
/** /**
@ -83,7 +85,7 @@ function isVaildEvent(clz) {
function listen(jsp, event, exec, priority, ignoreCancel) { function listen(jsp, event, exec, priority, ignoreCancel) {
var name = jsp.description.name; var name = jsp.description.name;
if (ext.isNull(name)) throw new TypeError('插件名称为空 请检查传入参数!'); if (ext.isNull(name)) throw new TypeError('插件名称为空 请检查传入参数!');
var eventCls = mapEvent[event]; var eventCls = mapEvent[event] || mapEvent[event.toLowerCase()] || mapEvent[event +'Event'] || mapEvent[event.toLowerCase() + 'event'];
if (!eventCls) { if (!eventCls) {
try { try {
eventCls = base.getClass(eventCls); eventCls = base.getClass(eventCls);
@ -92,12 +94,11 @@ function listen(jsp, event, exec, priority, ignoreCancel) {
return; return;
} }
} }
if (priority === undefined) { if (typeof priority === 'boolean') {
priority = 'NORMAL' ignoreCancel = priority
}
if (ignoreCancel === undefined) {
ignoreCancel = false;
} }
priority = priority || 'NORMAL';
ignoreCancel = ignoreCancel || false;
var listener = new Listener({}); var listener = new Listener({});
// noinspection JSUnusedGlobalSymbols // noinspection JSUnusedGlobalSymbols
/** /**
@ -143,8 +144,7 @@ function listen(jsp, event, exec, priority, ignoreCancel) {
var mapEvent = []; var mapEvent = [];
// 映射事件名称 // 映射事件名称
mapEventName(); log.i('Bukkit 事件映射完毕 共计 %s 个事件!', mapEventName().toFixed(0));
// log.i('Bukkit 事件映射完毕 共计 %s 个事件!', mapEvent.length);
module.exports = { module.exports = {
on: listen, on: listen,

View File

@ -197,7 +197,7 @@ function runAndCatch(jsp, exec, ext) {
exec.bind(jsp)(); exec.bind(jsp)();
if (ext) { ext(); } if (ext) { ext(); }
} catch (ex) { } catch (ex) {
log.w('§6插件 §b%s §6执行 §d%s §6方法时发生错误 §4%s', jsp.description.name, exec.name, ex.message); log.console('§6插件 §b%s §6执行 §d%s §6方法时发生错误 §4%s', jsp.description.name, exec.name, ex.message);
console.ex(ex); console.ex(ex);
} }
} }
@ -208,7 +208,7 @@ function checkAndGet(args) {
var name = args[0]; var name = args[0];
// 如果是插件 则直接返回 // 如果是插件 则直接返回
if (name.description) { return [name]; } if (name.description) { return [name]; }
if (!exports.plugins[name]) { throw new Error("插件 " + name + "不存在!"); } if (!exports.plugins[name]) { throw new Error("插件 " + name + " 不存在!"); }
return [exports.plugins[name]]; return [exports.plugins[name]];
} }