diff --git a/src/main/resources/internal/bukkit/command.js b/src/main/resources/internal/bukkit/command.js index 4a8f20f..aaf264b 100644 --- a/src/main/resources/internal/bukkit/command.js +++ b/src/main/resources/internal/bukkit/command.js @@ -39,7 +39,7 @@ function disable(jsp) { var commands = jsp.description.commands; if (commands) { for (var name in commands) { - + //TODO 删除插件命令 } } } diff --git a/src/main/resources/internal/bukkit/event.js b/src/main/resources/internal/bukkit/event.js index 8966d36..b01ab9f 100644 --- a/src/main/resources/internal/bukkit/event.js +++ b/src/main/resources/internal/bukkit/event.js @@ -7,7 +7,7 @@ var Thread = Java.type("java.lang.Thread"); var Bukkit = Java.type("org.bukkit.Bukkit"); var Listener = Java.type("org.bukkit.event.Listener"); 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 EventExecutor = Java.type("org.bukkit.plugin.EventExecutor"); @@ -65,7 +65,7 @@ function mapEventName() { */ function isVaildEvent(clz) { // noinspection JSUnresolvedVariable 继承于 org.bukkit.event.Event - return BukkitEvent.class.isAssignableFrom(clz) && + return Event.class.isAssignableFrom(clz) && // 访问符为Public Modifier.isPublic(clz.getModifiers()) && // 不是抽象类 @@ -87,6 +87,7 @@ function listen(jsp, event, exec, priority, ignoreCancel) { if (!eventCls) { try { eventCls = base.getClass(eventCls); + mapEvent[event] = eventCls; } catch (ex) { console.warn("事件 %s 未找到!".format(event)); return; @@ -142,7 +143,7 @@ function listen(jsp, event, exec, priority, ignoreCancel) { var mapEvent = []; // 映射事件名称 -console.info('Bukkit 事件映射完毕 共计 %s 个事件!'.format(mapEventName().toFixed(0))); +console.info('Sponge 事件映射完毕 共计 %s 个事件!'.format(mapEventName().toFixed(0))); module.exports = { on: listen, diff --git a/src/main/resources/internal/bukkit/player.js b/src/main/resources/internal/bukkit/player.js new file mode 100644 index 0000000..a9262a7 --- /dev/null +++ b/src/main/resources/internal/bukkit/player.js @@ -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; \ No newline at end of file diff --git a/src/main/resources/internal/sponge/command.js b/src/main/resources/internal/sponge/command.js index 5f2a35b..c5566ee 100644 --- a/src/main/resources/internal/sponge/command.js +++ b/src/main/resources/internal/sponge/command.js @@ -4,6 +4,7 @@ */ /*global Java, base, module, exports, require, __FILE__*/ +var Sponge = MServer; var server = require('./server'); var plugin = server.plugin.self; @@ -11,6 +12,7 @@ var CommandManager = server.CommandManager; var CommandSpec = Java.type('org.spongepowered.api.command.spec.CommandSpec'); 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'); @@ -19,75 +21,84 @@ var Optional = Java.type('java.util.Optional'); var ArrayList = Java.type('java.util.ArrayList'); var Arrays = Java.type('java.util.Arrays'); -var SimpleCommandCallable = function () { - this.process = function (source, args) { +var commandMap=[]; +var SimpleCommandCallable = function (name) { + var that = this; + this.name = name; + this.cmd = noop; + 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 getSuggestions(CommandSource source, String arguments, @Nullable Location targetPosition) throws CommandException; + getSuggestions: function (src, args, target) { + return that.tab(src, null, name, args.split(" ")); + }, + //boolean testPermission(CommandSource source); + testPermission: function () { + return true; + }, + //Optional getShortDescription(CommandSource source); + getShortDescription: function () { + return Optional.of(Text.of("")); + }, + //Optional 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.getSuggestions = function (source, args, targetPosition) { - return Arrays.asList(''); - }; - this.testPermission = function (source) { - return true; - }; - this.getShortDescription = function (source) { - return Optional.ofNullable(''); - }; - this.getHelp = function (source) { - + this.setTabCompleter = function (exec) { + that.tab = exec; } -}; +} function enable(jsp) { + var pname = jsp.description.name; var commands = jsp.description.commands; if (commands) { var pluginCmds = []; for (var name in commands) { var command = commands[name]; if (typeof command !== 'object') continue; - var newCmd = CommandSpec.builder(); - 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); + create(jsp, name) console.debug('插件 %s 注册命令 %s ...'.format(jsp.description.name, name)); } - commandMap.registerAll(jsp.description.name, Arrays.asList(pluginCmds)); } } function get(name) { - return commandMap.getCommand(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) { - var c = get(name) || create(jsp, name); - console.debug('插件 %s 设置命令 %s(%s) 执行器 ...'.format(jsp.description.name, name, c)); + var c = create(jsp, name); + console.debug('插件 %s 设置命令 %s 执行器 ...'.format(jsp.description.name, name)); if (exec.cmd) { c.setExecutor(function (sender, cmd, command, args) { try { return exec.cmd(sender, command, args); } 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); } }); @@ -100,16 +111,22 @@ function on(jsp, name, exec) { StringUtil.copyPartialMatches(token, Arrays.asList(exec.tab(sender, command, args)), completions); return completions; } 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); } }); } } -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.off = function () { - -}; \ No newline at end of file +exports = module.exports = { + enable: enable, + on: on, + off: noop +} \ No newline at end of file diff --git a/src/main/resources/internal/sponge/event.js b/src/main/resources/internal/sponge/event.js new file mode 100644 index 0000000..2345fe1 --- /dev/null +++ b/src/main/resources/internal/sponge/event.js @@ -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]; + } + } +}; \ No newline at end of file diff --git a/src/main/resources/internal/sponge/player.js b/src/main/resources/internal/sponge/player.js new file mode 100644 index 0000000..d7e642b --- /dev/null +++ b/src/main/resources/internal/sponge/player.js @@ -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; \ No newline at end of file diff --git a/src/main/resources/internal/sponge/task.js b/src/main/resources/internal/sponge/task.js index da09705..ee17ae2 100644 --- a/src/main/resources/internal/sponge/task.js +++ b/src/main/resources/internal/sponge/task.js @@ -12,7 +12,13 @@ var Task = Java.type("org.spongepowered.api.scheduler.Task"); * @param 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); + } + })); }; /** * 运行任务 diff --git a/src/main/resources/plugins/HelloWorld.js b/src/main/resources/plugins/HelloWorld.js index 0ecc728..b705939 100644 --- a/src/main/resources/plugins/HelloWorld.js +++ b/src/main/resources/plugins/HelloWorld.js @@ -5,10 +5,10 @@ /*global Java, base, module, exports, require*/ var event = require('api/event'); +var wrapper = require('api/wrapper'); var command = require('api/command'); -var server = require('api/server') +var server = require('api/server'); var fs = require('fs'); -var join; var description = { name: 'HelloWorld', @@ -20,37 +20,49 @@ var description = { } }; -// function load() { -// console.log('载入 Hello Wrold 测试插件!'); -// } +function load() { + console.log('载入 Hello Wrold 测试插件!'); +} function enable() { command.on(this, 'hello', { cmd: function (sender, command, args) { - load(fs.file(root, 'test.js')); + global.load(fs.file(root, 'test.js')); return true; } }); - console.log('启用 Hello Wrold 测试插件!'); - join = event.on(this, 'playerloginevent', function join(event) { + console.log('启用 Hello World 测试插件!'); + 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 + console.debug('玩家', player.name, "触发事件", event.class.simpleName); + setTimeout(function () { // noinspection JSUnresolvedVariable - console.debug('玩家', event.player.name, "触发事件", event.name); - setTimeout(function () { - // noinspection JSUnresolvedVariable - event.player.sendMessage("§a欢迎来到 §bMiaoScript §a的世界! 当前在线: " + server.players.length); - }, 10); - }); + player.sendMessage("§a欢迎来到 §bMiaoScript §a的世界! 当前在线: " + server.players.length) + }, 10); } function disable() { - console.log('卸载 Hello Wrold 测试插件!'); - // 可以不用关闭事件 程序将自动处理 - // event.off(join); + console.log('卸载 Hello World 测试插件!'); } module.exports = { description: description, - // load: load, + load: load, enable: enable, disable: disable }; \ No newline at end of file