feat: 完善Sponge事件注册 新增player封装类

This commit is contained in:
coding 2017-12-28 08:30:16 +00:00
parent fe615564ce
commit ea5fe60538
8 changed files with 275 additions and 70 deletions

View File

@ -39,7 +39,7 @@ function disable(jsp) {
var commands = jsp.description.commands; var commands = jsp.description.commands;
if (commands) { if (commands) {
for (var name in commands) { for (var name in commands) {
//TODO 删除插件命令
} }
} }
} }

View File

@ -7,7 +7,7 @@ var Thread = Java.type("java.lang.Thread");
var Bukkit = Java.type("org.bukkit.Bukkit"); var Bukkit = Java.type("org.bukkit.Bukkit");
var Listener = Java.type("org.bukkit.event.Listener"); var Listener = Java.type("org.bukkit.event.Listener");
var Modifier = Java.type("java.lang.reflect.Modifier"); var Modifier = Java.type("java.lang.reflect.Modifier");
var BukkitEvent = Java.type("org.bukkit.event.Event"); var Event = Java.type("org.bukkit.event.Event");
var EventPriority = Java.type("org.bukkit.event.EventPriority"); var EventPriority = Java.type("org.bukkit.event.EventPriority");
var EventExecutor = Java.type("org.bukkit.plugin.EventExecutor"); var EventExecutor = Java.type("org.bukkit.plugin.EventExecutor");
@ -65,7 +65,7 @@ function mapEventName() {
*/ */
function isVaildEvent(clz) { function isVaildEvent(clz) {
// noinspection JSUnresolvedVariable 继承于 org.bukkit.event.Event // noinspection JSUnresolvedVariable 继承于 org.bukkit.event.Event
return BukkitEvent.class.isAssignableFrom(clz) && return Event.class.isAssignableFrom(clz) &&
// 访问符为Public // 访问符为Public
Modifier.isPublic(clz.getModifiers()) && Modifier.isPublic(clz.getModifiers()) &&
// 不是抽象类 // 不是抽象类
@ -87,6 +87,7 @@ function listen(jsp, event, exec, priority, ignoreCancel) {
if (!eventCls) { if (!eventCls) {
try { try {
eventCls = base.getClass(eventCls); eventCls = base.getClass(eventCls);
mapEvent[event] = eventCls;
} catch (ex) { } catch (ex) {
console.warn("事件 %s 未找到!".format(event)); console.warn("事件 %s 未找到!".format(event));
return; return;
@ -142,7 +143,7 @@ function listen(jsp, event, exec, priority, ignoreCancel) {
var mapEvent = []; var mapEvent = [];
// 映射事件名称 // 映射事件名称
console.info('Bukkit 事件映射完毕 共计 %s 个事件!'.format(mapEventName().toFixed(0))); console.info('Sponge 事件映射完毕 共计 %s 个事件!'.format(mapEventName().toFixed(0)));
module.exports = { module.exports = {
on: listen, on: listen,

View File

@ -0,0 +1,13 @@
/*global Java, base, module, exports, require*/
var Player = {
createNew: function (inner) {
var player = {};
player.handler = inner;
player.sendMessage = function (msg) {
this.handler.sendMessage(msg);
};
return player;
}
};
exports.$ = Player.createNew;

View File

@ -4,6 +4,7 @@
*/ */
/*global Java, base, module, exports, require, __FILE__*/ /*global Java, base, module, exports, require, __FILE__*/
var Sponge = MServer;
var server = require('./server'); var server = require('./server');
var plugin = server.plugin.self; var plugin = server.plugin.self;
@ -11,6 +12,7 @@ var CommandManager = server.CommandManager;
var CommandSpec = Java.type('org.spongepowered.api.command.spec.CommandSpec'); var CommandSpec = Java.type('org.spongepowered.api.command.spec.CommandSpec');
var CommandCallable = Java.type('org.spongepowered.api.command.CommandCallable'); var CommandCallable = Java.type('org.spongepowered.api.command.CommandCallable');
var CommandResult = Java.type('org.spongepowered.api.command.CommandResult');
var Text = Java.type('org.spongepowered.api.text.Text'); var Text = Java.type('org.spongepowered.api.text.Text');
@ -19,75 +21,84 @@ var Optional = Java.type('java.util.Optional');
var ArrayList = Java.type('java.util.ArrayList'); var ArrayList = Java.type('java.util.ArrayList');
var Arrays = Java.type('java.util.Arrays'); var Arrays = Java.type('java.util.Arrays');
var SimpleCommandCallable = function () { var commandMap=[];
this.process = function (source, args) {
}; var SimpleCommandCallable = function (name) {
this.getSuggestions = function (source, args, targetPosition) { var that = this;
return Arrays.asList(''); this.name = name;
}; this.cmd = noop;
this.testPermission = function (source) { this.tab = function() { return new ArrayList(); };
this.callable = new CommandCallable({
//CommandResult process(CommandSource source, String arguments) throws CommandException;
process: function (src, args) {
return that.cmd(src, null, name, args.split(" ")) ? CommandResult.success() : CommandResult.empty();
},
//List<String> getSuggestions(CommandSource source, String arguments, @Nullable Location<World> targetPosition) throws CommandException;
getSuggestions: function (src, args, target) {
return that.tab(src, null, name, args.split(" "));
},
//boolean testPermission(CommandSource source);
testPermission: function () {
return true; return true;
}; },
this.getShortDescription = function (source) { //Optional<Text> getShortDescription(CommandSource source);
return Optional.ofNullable(''); getShortDescription: function () {
}; return Optional.of(Text.of(""));
this.getHelp = function (source) { },
//Optional<Text> getHelp(CommandSource source);
getHelp: function () {
return Optional.of(Text.of(""));
},
//Text getUsage(CommandSource source);
getUsage: function () {
return Text.of('');
} }
});
this.setExecutor = function (exec) {
that.cmd = exec;
}; };
this.setTabCompleter = function (exec) {
that.tab = exec;
}
}
function enable(jsp) { function enable(jsp) {
var pname = jsp.description.name;
var commands = jsp.description.commands; var commands = jsp.description.commands;
if (commands) { if (commands) {
var pluginCmds = []; var pluginCmds = [];
for (var name in commands) { for (var name in commands) {
var command = commands[name]; var command = commands[name];
if (typeof command !== 'object') continue; if (typeof command !== 'object') continue;
var newCmd = CommandSpec.builder(); create(jsp, name)
if (command.description) newCmd.description(Text.of(command.description));
// if (command.usage) newCmd.setUsage(command.usage);
if (command.aliases) newCmd.setAliases(Arrays.asList(command.aliases));
if (command.permission) newCmd.setPermission(command.permission);
if (command['permission-message']) newCmd.setPermissionMessage(command['permission-message']);
pluginCmds.push(newCmd);
console.debug('插件 %s 注册命令 %s ...'.format(jsp.description.name, name)); console.debug('插件 %s 注册命令 %s ...'.format(jsp.description.name, name));
} }
commandMap.registerAll(jsp.description.name, Arrays.asList(pluginCmds));
} }
} }
function get(name) { function get(name) {
return commandMap.getCommand(name);
} }
function create(jsp, name) { function create(jsp, name) {
return register(jsp, ref.on(PluginCommand).create(name, plugin).get()); var commandKey = jsp.description.name.toLowerCase() + ":" + name;
if(!commandMap[commandKey]){
commandMap[commandKey] = new SimpleCommandCallable();
commandMap[commandKey].name = name;
Sponge.getCommandManager().register(plugin, commandMap[commandKey].callable, name, commandKey);
} }
return commandMap[commandKey];
function register(jsp, cmd) {
commandMap.register(jsp.description.name, cmd);
return cmd;
} }
// var exec = {
// cmd: function (sender, command, args) {
//
// },
// tab: function (sender, command, args) {
//
// }
// };
function on(jsp, name, exec) { function on(jsp, name, exec) {
var c = get(name) || create(jsp, name); var c = create(jsp, name);
console.debug('插件 %s 设置命令 %s(%s) 执行器 ...'.format(jsp.description.name, name, c)); console.debug('插件 %s 设置命令 %s 执行器 ...'.format(jsp.description.name, name));
if (exec.cmd) { if (exec.cmd) {
c.setExecutor(function (sender, cmd, command, args) { c.setExecutor(function (sender, cmd, command, args) {
try { try {
return exec.cmd(sender, command, args); return exec.cmd(sender, command, args);
} catch (ex) { } catch (ex) {
console.console('§6玩家 §a%s §6执行 §b%s §6插件 §d%s %s §6命令时发生异常 §4%s'.format(sender.name, jsp.description.name, command, Java.from(args).join(' '), ex)); console.log(args)
console.console('§6玩家 §a%s §6执行 §b%s §6插件 §d%s %s §6命令时发生异常 §4%s'.format(sender.name, jsp.description.name, command, args, ex));
console.ex(ex); console.ex(ex);
} }
}); });
@ -100,16 +111,22 @@ function on(jsp, name, exec) {
StringUtil.copyPartialMatches(token, Arrays.asList(exec.tab(sender, command, args)), completions); StringUtil.copyPartialMatches(token, Arrays.asList(exec.tab(sender, command, args)), completions);
return completions; return completions;
} catch (ex) { } catch (ex) {
console.console('§6玩家 §a%s §6执行 §b%s §6插件 §d%s %s §6补全时发生异常 §4%s'.format(sender.name, jsp.description.name, command, Java.from(args).join(' '), ex)); console.console('§6玩家 §a%s §6执行 §b%s §6插件 §d%s %s §6补全时发生异常 §4%s'.format(sender.name, jsp.description.name, command, args, ex));
console.ex(ex); console.ex(ex);
} }
}); });
} }
} }
exports.enable = enable; var exist = Sponge.getCommandManager().getOwnedBy(plugin);
exist.forEach(function(commandMapping) {
if (!commandMapping.getAllAliases().contains("ms")) {
Sponge.getCommandManager().removeMapping(commandMapping);
}
});
exports.on = on; exports = module.exports = {
exports.off = function () { enable: enable,
on: on,
}; off: noop
}

View File

@ -0,0 +1,142 @@
'use strict';
/**
* Bukkit 事件相关类
*/
/*global Java, base, module, exports, require, __FILE__*/
var Sponge = MServer;
var Thread = Java.type("java.lang.Thread");
var EventListener = Java.type("org.spongepowered.api.event.EventListener");
var Modifier = Java.type("java.lang.reflect.Modifier");
var Event = Java.type("org.spongepowered.api.event.Event");
var plugin = require('./server').plugin.self;
var ref = require('reflect');
var listenerMap = [];
/**
* 扫描包 org.spongepowered.api.event 下的所有事件
* 映射简写名称 org.spongepowered.api.event.game.state.GameInitializationEvent => gameinitializationevent
*/
function mapEventName() {
var eventPackageDir = "org/spongepowered/api/event";
var count = 0;
var dirs = Thread.currentThread().getContextClassLoader().getResources(eventPackageDir);
while (dirs.hasMoreElements()) {
var url = dirs.nextElement();
var protocol = url.protocol;
if (protocol === "jar") {
// noinspection JSUnresolvedVariable
var jar = url.openConnection().jarFile;
var entries = jar.entries();
while (entries.hasMoreElements()) {
var entry = entries.nextElement();
var name = entry.name;
// 以 org/spongepowered/api/event 开头 并且以 .class 结尾
if (name.startsWith(eventPackageDir) && name.endsWith(".class")) {
var i = name.replaceAll('/', '.');
try {
var clz = base.getClass(i.substring(0, i.length - 6));
// 继承于 org.spongepowered.api.event.Event 访问符为 Public 并且不是抽象类
if (isVaildEvent(clz)) {
// noinspection JSUnresolvedVariable
var clzName = clz.name;
var simpleName = clzName.substring(clzName.lastIndexOf(".") + 1).replace(/\$/g, '.').toLowerCase();
console.debug("Mapping Event [%s] => %s".format(clz.name, simpleName));
mapEvent[simpleName] = clz;
count++;
}
} catch (ex) {
//ignore already loaded class
}
}
}
}
}
return count;
}
/**
* 判断是否为一个有效的事件类
* @param clz
* @returns {*|boolean}
*/
function isVaildEvent(clz) {
// noinspection JSUnresolvedVariable 继承于 org.spongepowered.api.event.Event
return Event.class.isAssignableFrom(clz) &&
// 访问符为Public
Modifier.isPublic(clz.getModifiers()) &&
// Sponge的事件都是接口
Modifier.isAbstract(clz.getModifiers());
}
/**
* 添加事件监听
* @param jsp
* @param event
* @param exec {function}
* @param priority
* @param ignoreCancel
*/
function listen(jsp, event, exec, priority, ignoreCancel) {
var name = jsp.description.name;
if (ext.isNull(name)) throw new TypeError('插件名称为空 请检查传入参数!');
var eventCls = name2Class(event);
if (typeof priority === 'boolean') {
ignoreCancel = priority
}
priority = priority || 'NORMAL';
ignoreCancel = ignoreCancel || false;
var listener = new EventListener({
handle: function handle(event) {
exec(event);
}
});
Sponge.getEventManager().registerListener(plugin, eventCls, listener)
// 添加到缓存 用于关闭插件的时候关闭事件
if (!listenerMap[name]) listenerMap[name] = [];
var listeners = listenerMap[name];
var off = {
event: eventCls,
listener: listener,
off: function () {
Sponge.getEventManager().unregisterListeners(this.listener);
console.debug('插件 %s 注销事件 %s'.format(name, this.event.simpleName));
}
};
listeners.push(off);
// noinspection JSUnresolvedVariable
console.debug('插件 %s 注册事件 %s => %s'.format(name, eventCls.name.substring(eventCls.name.lastIndexOf(".") + 1), exec.name === '' ? '匿名方法' : exec.name));
return off;
}
function name2Class(event) {
var eventCls = mapEvent[event] || mapEvent[event.toLowerCase()] || mapEvent[event + 'Event'] || mapEvent[event.toLowerCase() + 'event'];
if (!eventCls) {
try {
eventCls = base.getClass(eventCls);
mapEvent[event] = eventCls;
} catch (ex) {
console.console("§6插件 §b%s §6注册事件 §c%s §6失败 §4事件未找到!".format(name, event));
console.ex(new Error("插件 %s 注册事件 %s 失败 事件未找到!".format(name, event)))
return;
}
}
return eventCls;
}
var mapEvent = [];
// 映射事件名称
console.info('Sponge 事件映射完毕 共计 %s 个事件!'.format(mapEventName().toFixed(0)));
module.exports = {
on: listen,
disable: function (jsp) {
var jspl = listenerMap[jsp.description.name];
if (jspl) {
jspl.forEach(function (t) t.off());
delete listenerMap[jsp.description.name];
}
}
};

View File

@ -0,0 +1,14 @@
/*global Java, base, module, exports, require*/
var Player = {
createNew: function (inner) {
var player = {};
var Text = Java.type('org.spongepowered.api.text.Text');
player.handler = inner;
player.sendMessage = function (msg) {
this.handler.sendMessage(Text.of(msg));
};
return player;
}
};
exports.$ = Player.createNew;

View File

@ -12,7 +12,13 @@ var Task = Java.type("org.spongepowered.api.scheduler.Task");
* @param func 任务 * @param func 任务
*/ */
exports.create = function (func) { exports.create = function (func) {
return Task.builder().execute(new Comsumer(func)); return Task.builder().execute(new Comsumer(function () {
try {
func();
} catch (ex) {
console.ex('§4插件执行任务时发生错误', ex);
}
}));
}; };
/** /**
* 运行任务 * 运行任务

View File

@ -5,10 +5,10 @@
/*global Java, base, module, exports, require*/ /*global Java, base, module, exports, require*/
var event = require('api/event'); var event = require('api/event');
var wrapper = require('api/wrapper');
var command = require('api/command'); var command = require('api/command');
var server = require('api/server') var server = require('api/server');
var fs = require('fs'); var fs = require('fs');
var join;
var description = { var description = {
name: 'HelloWorld', name: 'HelloWorld',
@ -20,37 +20,49 @@ var description = {
} }
}; };
// function load() { function load() {
// console.log('载入 Hello Wrold 测试插件!'); console.log('载入 Hello Wrold 测试插件!');
// } }
function enable() { function enable() {
command.on(this, 'hello', { command.on(this, 'hello', {
cmd: function (sender, command, args) { cmd: function (sender, command, args) {
load(fs.file(root, 'test.js')); global.load(fs.file(root, 'test.js'));
return true; return true;
} }
}); });
console.log('启用 Hello Wrold 测试插件!'); console.log('启用 Hello World 测试插件!');
join = event.on(this, 'playerloginevent', function join(event) { switch (DetectServerType) {
case ServerType.Bukkit:
event.on(this, 'playerloginevent', function join(event) {
send(event, wrapper.player(event.player));
});
break;
case ServerType.Sponge:
// clientconnectionevent.join
event.on(this, 'clientconnectionevent.join', function join(event) {
send(event, wrapper.player(event.targetEntity));
});
break;
}
}
function send(event, player){
// noinspection JSUnresolvedVariable // noinspection JSUnresolvedVariable
console.debug('玩家', event.player.name, "触发事件", event.name); console.debug('玩家', player.name, "触发事件", event.class.simpleName);
setTimeout(function () { setTimeout(function () {
// noinspection JSUnresolvedVariable // noinspection JSUnresolvedVariable
event.player.sendMessage("§a欢迎来到 §bMiaoScript §a的世界! 当前在线: " + server.players.length); player.sendMessage("§a欢迎来到 §bMiaoScript §a的世界! 当前在线: " + server.players.length)
}, 10); }, 10);
});
} }
function disable() { function disable() {
console.log('卸载 Hello Wrold 测试插件!'); console.log('卸载 Hello World 测试插件!');
// 可以不用关闭事件 程序将自动处理
// event.off(join);
} }
module.exports = { module.exports = {
description: description, description: description,
// load: load, load: load,
enable: enable, enable: enable,
disable: disable disable: disable
}; };