feat: 兼容Sponge部分功能
This commit is contained in:
parent
c7293370ea
commit
e2566c4bf1
@ -29,7 +29,7 @@ public class MiaoScript extends JavaPlugin implements Executor {
|
||||
@SneakyThrows
|
||||
public void onEnable() {
|
||||
new CommandSub("ms", this);
|
||||
engine = new ScriptEngine(getDataFolder().getCanonicalPath(), getClassLoader(), getLogger());
|
||||
engine = new ScriptEngine(getDataFolder().getCanonicalPath(), getLogger());
|
||||
}
|
||||
|
||||
@Cmd
|
||||
|
@ -3,10 +3,10 @@ package pw.yumc.MiaoScript;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.script.ScriptException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.spongepowered.api.Sponge;
|
||||
import org.spongepowered.api.command.CommandResult;
|
||||
import org.spongepowered.api.command.args.GenericArguments;
|
||||
@ -48,6 +48,7 @@ public class MiaoScriptSponge {
|
||||
.permission("MiaoScript.admin")
|
||||
.child(js(), "js")
|
||||
.child(file(), "file")
|
||||
.child(reload(), "reload")
|
||||
.build();
|
||||
}
|
||||
|
||||
@ -81,11 +82,24 @@ public class MiaoScriptSponge {
|
||||
.build();
|
||||
}
|
||||
|
||||
private CommandSpec reload() {
|
||||
return CommandSpec.builder()
|
||||
.description(Text.of("重载 JS 引擎"))
|
||||
.executor((src, args) -> {
|
||||
engine.disableEngine();
|
||||
Sponge.getEventManager().unregisterPluginListeners(this);
|
||||
engine.enableEngine();
|
||||
// src.sendMesssage("§6[§bMiaoScript§6]§r §bMiaoScript §eEngine §a重启完成!");
|
||||
return CommandResult.success();
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
||||
@Listener
|
||||
@SneakyThrows
|
||||
public void onStart(GameStartedServerEvent event) {
|
||||
Sponge.getServer().getConsole();
|
||||
Sponge.getCommandManager().register(this, main(), "ms", "mscript", "MiaoScript");
|
||||
engine = new ScriptEngine(pluginConfigDir.getCanonicalPath(), Thread.currentThread().getContextClassLoader(), logger);
|
||||
engine = new ScriptEngine(pluginConfigDir.getCanonicalPath(), logger);
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,14 @@
|
||||
package pw.yumc.MiaoScript;
|
||||
|
||||
import java.lang.Thread;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
@ -17,13 +22,11 @@ import pw.yumc.YumCore.engine.MiaoScriptEngine;
|
||||
*/
|
||||
public class ScriptEngine {
|
||||
private String root;
|
||||
private ClassLoader classLoader;
|
||||
private Logger logger;
|
||||
private Object logger;
|
||||
private MiaoScriptEngine engine;
|
||||
|
||||
public ScriptEngine(String root, ClassLoader classLoader, Logger logger) {
|
||||
public ScriptEngine(String root, Object logger) {
|
||||
this.root = root;
|
||||
this.classLoader = classLoader;
|
||||
this.logger = logger;
|
||||
enableEngine();
|
||||
}
|
||||
@ -33,10 +36,16 @@ public class ScriptEngine {
|
||||
ScriptEngineManager manager = new ScriptEngineManager(null);
|
||||
this.engine = new MiaoScriptEngine(manager, "nashorn");
|
||||
this.engine.put("base", new Base());
|
||||
this.engine.eval(new InputStreamReader(classLoader.getResourceAsStream("bios.js")));
|
||||
Path bios = Paths.get(root, "bios.js");
|
||||
// 如果存在自定义bios就加载自定义的
|
||||
if (Files.exists(bios)) {
|
||||
this.engine.eval("load('" + bios.toFile().getCanonicalPath() + "')");
|
||||
} else {
|
||||
this.engine.eval(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("bios.js")));
|
||||
}
|
||||
engine.invokeFunction("boot", root, logger);
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.SEVERE, "MiaoScript 启动失败!", e);
|
||||
// logger.log(Level.SEVERE, "MiaoScript 启动失败!", e);
|
||||
}
|
||||
}
|
||||
|
||||
@ -44,7 +53,7 @@ public class ScriptEngine {
|
||||
try {
|
||||
engine.invokeFunction("disable");
|
||||
} catch (ScriptException | NoSuchMethodException e) {
|
||||
logger.log(Level.SEVERE, "MiaoScript 关闭失败!", e);
|
||||
// logger.log(Level.SEVERE, "MiaoScript 关闭失败!", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7,23 +7,28 @@ var disable;
|
||||
*/
|
||||
(function () {
|
||||
boot = function (root, logger) {
|
||||
log = logger;
|
||||
// 开发环境下初始化
|
||||
root = root || "src/main/resources";
|
||||
// 解压文件到根目录
|
||||
release(root, "[core|modules]/.*", true);
|
||||
release(root, "plugins/.*");
|
||||
load(root + '/core/init.js');
|
||||
try {
|
||||
log = logger;
|
||||
// 开发环境下初始化
|
||||
root = root || "src/main/resources";
|
||||
// 解压文件到根目录
|
||||
release(root, "[core|modules]/.*", true);
|
||||
release(root, "plugins/.*");
|
||||
load(root + '/core/init.js');
|
||||
if (__FILE__ !== "<eval>") {
|
||||
logger.info('载入自定义 BIOS 文件 ' + __FILE__);
|
||||
global.debug = true;
|
||||
}
|
||||
init(root);
|
||||
} catch (ex) {
|
||||
if(console.ex){
|
||||
if (console && console.ex) {
|
||||
console.ex(ex);
|
||||
}else{
|
||||
} else {
|
||||
ex.printStackTrace();
|
||||
throw ex;
|
||||
}
|
||||
} finally {
|
||||
disable = disablePlugins
|
||||
disable = disablePlugins;
|
||||
}
|
||||
};
|
||||
|
||||
@ -34,14 +39,12 @@ var disable;
|
||||
|
||||
var classLoader = java.lang.Thread.currentThread().getContextClassLoader();
|
||||
var url = classLoader.getResource("plugin.yml");
|
||||
if (url === null) {
|
||||
return;
|
||||
}
|
||||
if (url === null) { return; }
|
||||
|
||||
var upath = url.getFile().substring(url.getFile().indexOf("/") + 1);
|
||||
var jarPath = java.net.URLDecoder.decode(upath.substring(0, upath.indexOf('!')));
|
||||
if (!Files.exists(Paths.get(jarPath))) {
|
||||
jarPath = "/" + jarPath;
|
||||
}
|
||||
if (!Files.exists(Paths.get(jarPath))) { jarPath = "/" + jarPath; }
|
||||
|
||||
try {
|
||||
var jar = new java.util.jar.JarFile(jarPath);
|
||||
var r = new RegExp(regex);// "[core|modules]/.*"
|
||||
|
@ -5,80 +5,99 @@
|
||||
(function (global) {
|
||||
var Arrays = Java.type('java.util.Arrays');
|
||||
var Level = Java.type('java.util.logging.Level');
|
||||
var String = Java.type('java.lang.String');
|
||||
var Console = function (name) {
|
||||
Object.defineProperty(this, 'name', {
|
||||
get: function () {
|
||||
return this._name;
|
||||
}.bind(this),
|
||||
set: function (name) {
|
||||
this._name = name ? '[' + name + '] ' : '';
|
||||
this.prefix = name ? '§6[§cMS§6][§b' + name + '§6]§r ' : '';
|
||||
}.bind(this)
|
||||
});
|
||||
this.name = name;
|
||||
this.log = function () {
|
||||
log.info(this.name + Array.prototype.join.call(arguments, ' '));
|
||||
};
|
||||
this.info = this.log;
|
||||
this.warn = function () {
|
||||
log.warning(this.name + Array.prototype.join.call(arguments, ' '));
|
||||
};
|
||||
this.error = function () {
|
||||
log.log(Level.SEVERE, this.name + Array.prototype.join.call(arguments, ' '));
|
||||
};
|
||||
switch (DetectServerType) {
|
||||
case ServerType.Bukkit:
|
||||
this.sender = function () {
|
||||
var sender = arguments[0];
|
||||
if (!(sender instanceof org.bukkit.command.CommandSender)) {
|
||||
console.error("第一个参数未实现 org.bukkit.command.CommandSender 无法发送消息!")
|
||||
}
|
||||
var args = Array.prototype.slice.call(arguments, 1);
|
||||
sender.sendMessage(this.prefix + args.join(' '));
|
||||
};
|
||||
this.console = function () {
|
||||
this.sender(MServer.consoleSender, Array.prototype.join.call(arguments, ' '));
|
||||
};
|
||||
break;
|
||||
case ServerType.Sponge:
|
||||
this.sender = function () {
|
||||
var Text = Java.type("org.spongepowered.api.text.Text");
|
||||
var sender = arguments[0];
|
||||
if (!(sender instanceof org.spongepowered.api.command.CommandSource)) {
|
||||
console.error("第一个参数未实现 org.spongepowered.api.command.CommandSource 无法发送消息!")
|
||||
}
|
||||
var args = Array.prototype.slice.call(arguments, 1);
|
||||
sender.sendMessage(Text.of(this.prefix + args.join(' ')));
|
||||
};
|
||||
this.console = function () {
|
||||
this.sender(MServer.server.console, Array.prototype.join.call(arguments, ' '));
|
||||
};
|
||||
break;
|
||||
default:
|
||||
this.sender = function () {
|
||||
throw Error("console.sender 不支持的服务器类型: " + DetectServerType);
|
||||
};
|
||||
this.console = function () {
|
||||
throw Error("console.console 不支持的服务器类型: " + DetectServerType);
|
||||
};
|
||||
}
|
||||
this.debug = this.log;
|
||||
this.ex = function (ex) {
|
||||
this.console('§4' + ex);
|
||||
var track = ex.getStackTrace();
|
||||
if (track.class) {
|
||||
track = Arrays.asList(track)
|
||||
}
|
||||
track.forEach(function (stack) {
|
||||
if (stack.className.startsWith('<')) {
|
||||
this.console(' §e位于 §c%s => §c%s §4行%s'.format(stack.fileName, stack.methodName, stack.lineNumber));
|
||||
} else {
|
||||
this.console(' §e位于 §c%s.%s(§4%s:%s§c)'.format(stack.className, stack.methodName, stack.fileName, stack.lineNumber));
|
||||
var Console = {
|
||||
createNew: function(name) {
|
||||
var console = {};
|
||||
Object.defineProperty(console, 'name', {
|
||||
get: function () {
|
||||
return this._name;
|
||||
}.bind(console),
|
||||
set: function (name) {
|
||||
this._name = name ? '[' + name + '] ' : '';
|
||||
this.prefix = name ? '§6[§cMS§6][§b' + name + '§6]§r ' : '§6[§bMiaoScript§6]§r ';
|
||||
}.bind(console)
|
||||
});
|
||||
console.name = name;
|
||||
console.log = console.info = function () {
|
||||
log.info(this.name + Array.prototype.join.call(arguments, ' '));
|
||||
};
|
||||
console.warn = function () {
|
||||
log.warning(this.name + Array.prototype.join.call(arguments, ' '));
|
||||
};
|
||||
console.error = function () {
|
||||
log.log(Level.SEVERE, this.name + Array.prototype.join.call(arguments, ' '));
|
||||
};
|
||||
console.debug = function () {
|
||||
log.info(this.name + '[DEBUG] ' + Array.prototype.join.call(arguments, ' '));
|
||||
};
|
||||
console.debug = global.debug ? console.debug : global.noop;
|
||||
console.sender = console.info;
|
||||
console.console = console.info;
|
||||
console.ex = function (ex) {
|
||||
this.console('§4' + ex);
|
||||
var track = ex.getStackTrace();
|
||||
if (track.class) {
|
||||
track = Arrays.asList(track)
|
||||
}
|
||||
}.bind(this));
|
||||
track.forEach(function (stack) {
|
||||
if (stack.className.startsWith('<')) {
|
||||
this.console(' §e位于 §c%s => §c%s §4行%s'.format(stack.fileName, stack.methodName, stack.lineNumber));
|
||||
} else {
|
||||
this.console(' §e位于 §c%s.%s(§4%s:%s§c)'.format(stack.className, stack.methodName, stack.fileName, stack.lineNumber));
|
||||
}
|
||||
}.bind(this));
|
||||
}
|
||||
return console;
|
||||
}
|
||||
};
|
||||
global.Console = Console;
|
||||
global.console = new Console();
|
||||
}
|
||||
var BukkitConsole = {
|
||||
createNew: function () {
|
||||
var console = Console.createNew();
|
||||
console.sender = function () {
|
||||
var sender = arguments[0];
|
||||
if (!(sender instanceof org.bukkit.command.CommandSender)) {
|
||||
this.error("第一个参数未实现 org.bukkit.command.CommandSender 无法发送消息!")
|
||||
}
|
||||
var args = Array.prototype.slice.call(arguments, 1);
|
||||
sender.sendMessage(console.prefix + args.join(' '));
|
||||
};
|
||||
console.console = function () {
|
||||
this.sender(MServer.consoleSender, Array.prototype.join.call(arguments, ' '));
|
||||
};
|
||||
return console;
|
||||
}
|
||||
}
|
||||
var SpongeConsole = {
|
||||
createNew: function () {
|
||||
var console = Console.createNew();
|
||||
console.sender = function () {
|
||||
var Text = Java.type("org.spongepowered.api.text.Text");
|
||||
var sender = arguments[0];
|
||||
if (!(sender instanceof org.spongepowered.api.command.CommandSource)) {
|
||||
this.error("第一个参数未实现 org.spongepowered.api.command.CommandSource 无法发送消息!")
|
||||
}
|
||||
var args = Array.prototype.slice.call(arguments, 1);
|
||||
sender.sendMessage(Text.of(console.prefix + args.join(' ')));
|
||||
};
|
||||
console.console = function () {
|
||||
this.sender(MServer.server.console, Array.prototype.join.call(arguments, ' '));
|
||||
};
|
||||
console.warn = function () {
|
||||
log.warn(this.name + Array.prototype.join.call(arguments, ' '));
|
||||
};
|
||||
return console;
|
||||
}
|
||||
}
|
||||
switch (DetectServerType) {
|
||||
case ServerType.Bukkit:
|
||||
global.Console = BukkitConsole;
|
||||
break;
|
||||
case ServerType.Sponge:
|
||||
global.Console = SpongeConsole;
|
||||
break;
|
||||
default:
|
||||
global.Console = Console;
|
||||
break;
|
||||
}
|
||||
global.console = global.Console.createNew();
|
||||
})(global);
|
@ -3,8 +3,8 @@
|
||||
*/
|
||||
/*global base*/
|
||||
var ServerType = {
|
||||
Bukkit: 1,
|
||||
Sponge: 2
|
||||
Bukkit: 'bukkit',
|
||||
Sponge: 'sponge'
|
||||
};
|
||||
var MServer;
|
||||
var DetectServerType = ServerType.Bukkit;
|
||||
|
@ -4,11 +4,12 @@ var global = this;
|
||||
|
||||
// noinspection JSUnusedLocalSymbols
|
||||
function init(root) {
|
||||
log.info('Init MiaoScript System...');
|
||||
global.root = root;
|
||||
loadCore();
|
||||
loadRequire();
|
||||
loadPatch();
|
||||
loadLib4Bukkit();
|
||||
loadServerLib();
|
||||
loadPlugins();
|
||||
}
|
||||
|
||||
@ -16,9 +17,12 @@ function init(root) {
|
||||
* 初始化核心
|
||||
*/
|
||||
function loadCore() {
|
||||
global.noop = function () {};
|
||||
// 加载基础模块
|
||||
load(root + '/core/ext.js');
|
||||
// 探测服务器类型
|
||||
load(root + '/core/detect.js');
|
||||
// 加载Console
|
||||
load(root + '/core/console.js');
|
||||
}
|
||||
|
||||
@ -41,8 +45,7 @@ function loadPatch() {
|
||||
/**
|
||||
* 加载Bukkit的类库
|
||||
*/
|
||||
function loadLib4Bukkit() {
|
||||
require('modules/event');
|
||||
function loadServerLib() {
|
||||
var task = require('modules/task');
|
||||
global.setTimeout = function (func, time, _async) {
|
||||
return _async ? task.laterAsync(func, time) : task.later(func, time);
|
||||
@ -64,11 +67,13 @@ function loadLib4Bukkit() {
|
||||
function loadPlugins() {
|
||||
// 初始化本体插件
|
||||
global.manager = require('modules/plugin');
|
||||
manager.init('plugins');
|
||||
// 只有当在正式环境运行的时候才加载
|
||||
if (manager.$) {
|
||||
if (manager) {
|
||||
manager.init('plugins');
|
||||
// 只有当在正式环境运行的时候才加载
|
||||
manager.load();
|
||||
manager.enable();
|
||||
} else {
|
||||
console.console('§4当前服务器不支持使用MiaoScript插件系统!');
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -20,9 +20,7 @@
|
||||
* @param parent 父目录
|
||||
*/
|
||||
function resolve(name, parent) {
|
||||
if (_canonical(name)) {
|
||||
name = _canonical(name);
|
||||
}
|
||||
name = _canonical(name) || name;
|
||||
// 解析本地目录
|
||||
if (name.startsWith('./') || name.startsWith('../')) {
|
||||
return resolveAsFile(parent, name) || resolveAsDirectory(parent, name) || undefined;
|
||||
|
@ -59,13 +59,13 @@ function register(jsp, cmd) {
|
||||
|
||||
function on(jsp, name, exec) {
|
||||
var c = get(name) || create(jsp, name);
|
||||
console.debug('插件 %s 设置命令 %s(%s) 执行器 ...'.fotmat(jsp.description.name, name, c));
|
||||
console.debug('插件 %s 设置命令 %s(%s) 执行器 ...'.format(jsp.description.name, name, c));
|
||||
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'.fotmat(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, Java.from(args).join(' '), ex));
|
||||
console.ex(ex);
|
||||
}
|
||||
});
|
||||
@ -78,7 +78,7 @@ 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'.fotmat(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, Java.from(args).join(' '), ex));
|
||||
console.ex(ex);
|
||||
}
|
||||
});
|
||||
|
19
src/main/resources/modules/bukkit/console.js
Normal file
19
src/main/resources/modules/bukkit/console.js
Normal file
@ -0,0 +1,19 @@
|
||||
var BukkitConsole = {
|
||||
createNew: function () {
|
||||
var console = Console.createNew();
|
||||
console.sender = function () {
|
||||
var sender = arguments[0];
|
||||
if (!(sender instanceof org.bukkit.command.CommandSender)) {
|
||||
this.error("第一个参数未实现 org.bukkit.command.CommandSender 无法发送消息!")
|
||||
}
|
||||
var args = Array.prototype.slice.call(arguments, 1);
|
||||
sender.sendMessage(console.prefix + args.join(' '));
|
||||
};
|
||||
console.console = function () {
|
||||
this.sender(MServer.consoleSender, Array.prototype.join.call(arguments, ' '));
|
||||
};
|
||||
return console;
|
||||
}
|
||||
}
|
||||
global.Console = BukkitConsole;
|
||||
exports = global.Console;
|
@ -138,7 +138,7 @@ function listen(jsp, event, exec, priority, ignoreCancel) {
|
||||
}
|
||||
listeners.push(off);
|
||||
// noinspection JSUnresolvedVariable
|
||||
console.debug('插件 %s 注册事件 %s 方法 %s'.format(name, eventCls.simpleName, exec.name === '' ? '匿名方法' : exec.name));
|
||||
console.debug('插件 %s 注册事件 %s => %s'.format(name, eventCls.simpleName, exec.name === '' ? '匿名方法' : exec.name));
|
||||
return off;
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
// var zip = require("core/zip");
|
||||
var fs = require('core/fs');
|
||||
var yaml = require('modules/yaml');
|
||||
var event = require('modules/event');
|
||||
var event = require('./event');
|
||||
var bukkit = require('./server');
|
||||
var command = require('./command');
|
||||
var permission = require('./permission');
|
||||
@ -99,7 +99,7 @@ function beforeLoadHook(origin) {
|
||||
// 处理 event 为了不影响 正常逻辑 event 还是手动require吧
|
||||
// result = result + 'var event = {}; module.exports.event = event;';
|
||||
// 注入 console 对象 // 给插件注入单独的 console
|
||||
result = result + 'var console = new Console(); module.exports.console = console;';
|
||||
result = result + 'var console = Console.createNew(); module.exports.console = console;';
|
||||
// 插件注入 self 对象
|
||||
result = result + 'var self = {}; module.exports.self = self;';
|
||||
return result;
|
||||
@ -226,7 +226,6 @@ function checkAndGet(args) {
|
||||
|
||||
var plugins = [];
|
||||
|
||||
exports.$ = undefined;
|
||||
exports.plugins = plugins;
|
||||
exports.init = function (path) {
|
||||
var plugin = bukkit.plugin.self;
|
||||
|
@ -5,10 +5,10 @@
|
||||
*/
|
||||
|
||||
/*global Java, base, module, exports, require, __FILE__*/
|
||||
var Bukkit = Java.type("org.bukkit.Bukkit");
|
||||
var Bukkit = MServer;
|
||||
// noinspection JSUnresolvedVariable
|
||||
var Server = Bukkit.server;
|
||||
var PluginManager = Bukkit.pluginManager;
|
||||
var PluginManager = Server.pluginManager;
|
||||
exports.$ = Bukkit;
|
||||
/**
|
||||
* 获取NMS版本
|
||||
|
@ -1 +1 @@
|
||||
module.exports = require('./bukkit/command');
|
||||
module.exports = require('./mserver').command;
|
@ -1 +1 @@
|
||||
module.exports = require('./bukkit/event');
|
||||
module.exports = require('./mserver').event;
|
8
src/main/resources/modules/mserver.js
Normal file
8
src/main/resources/modules/mserver.js
Normal file
@ -0,0 +1,8 @@
|
||||
module.exports = {
|
||||
command: require('./' + DetectServerType + '/command'),
|
||||
event: require('./' + DetectServerType + '/event'),
|
||||
permission: require('./' + DetectServerType + '/permission'),
|
||||
plugin: require('./' + DetectServerType + '/plugin'),
|
||||
server: require('./' + DetectServerType + '/server'),
|
||||
task: require('./' + DetectServerType + '/task')
|
||||
}
|
@ -1 +1 @@
|
||||
module.exports = require('./bukkit/permission');
|
||||
module.exports = require('./mserver').permission;
|
@ -1 +1 @@
|
||||
module.exports = require('./bukkit/plugin');
|
||||
module.exports = require('./mserver').plugin;
|
@ -1 +1 @@
|
||||
module.exports = require('./bukkit/server');
|
||||
module.exports = require('./mserver').server;
|
93
src/main/resources/modules/sponge/command.js
Normal file
93
src/main/resources/modules/sponge/command.js
Normal file
@ -0,0 +1,93 @@
|
||||
'use strict';
|
||||
/**
|
||||
* Sponge 命令相关类
|
||||
*/
|
||||
|
||||
/*global Java, base, module, exports, require, __FILE__*/
|
||||
var server = require('./server');
|
||||
var plugin = server.plugin.self;
|
||||
|
||||
var CommandManager = server.CommandManager;
|
||||
|
||||
var CommandSpec = Java.type('org.spongepowered.api.command.spec.CommandSpec');
|
||||
var Text = Java.type('org.spongepowered.api.text.Text');
|
||||
|
||||
var ArrayList = Java.type('java.util.ArrayList');
|
||||
var Arrays = Java.type('java.util.Arrays');
|
||||
|
||||
function enable(jsp) {
|
||||
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 = create(jsp, name);
|
||||
if (command.description) newCmd.setDescription(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));
|
||||
}
|
||||
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());
|
||||
}
|
||||
|
||||
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));
|
||||
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.ex(ex);
|
||||
}
|
||||
});
|
||||
}
|
||||
if (exec.tab) {
|
||||
c.setTabCompleter(function (sender, cmd, command, args) {
|
||||
try {
|
||||
var completions = new ArrayList();
|
||||
var token = args[args.length - 1];
|
||||
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.ex(ex);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
exports.enable = enable;
|
||||
|
||||
exports.on = on;
|
||||
exports.off = function () {
|
||||
|
||||
};
|
23
src/main/resources/modules/sponge/console.js
Normal file
23
src/main/resources/modules/sponge/console.js
Normal file
@ -0,0 +1,23 @@
|
||||
var SpongeConsole = {
|
||||
createNew: function () {
|
||||
var console = Console.createNew();
|
||||
console.sender = function () {
|
||||
var Text = Java.type("org.spongepowered.api.text.Text");
|
||||
var sender = arguments[0];
|
||||
if (!(sender instanceof org.spongepowered.api.command.CommandSource)) {
|
||||
this.error("第一个参数未实现 org.spongepowered.api.command.CommandSource 无法发送消息!")
|
||||
}
|
||||
var args = Array.prototype.slice.call(arguments, 1);
|
||||
sender.sendMessage(Text.of(console.prefix + args.join(' ')));
|
||||
};
|
||||
console.console = function () {
|
||||
this.sender(MServer.server.console, Array.prototype.join.call(arguments, ' '));
|
||||
};
|
||||
console.warn = function () {
|
||||
log.warn(this.name + Array.prototype.join.call(arguments, ' '));
|
||||
};
|
||||
return console;
|
||||
}
|
||||
}
|
||||
global.Console = SpongeConsole;
|
||||
exports = global.Console;
|
43
src/main/resources/modules/sponge/server.js
Normal file
43
src/main/resources/modules/sponge/server.js
Normal file
@ -0,0 +1,43 @@
|
||||
'use strict';
|
||||
/**
|
||||
* Sponge基础操作
|
||||
* Created by 蒋天蓓 on 2017/10/27 0009.
|
||||
*/
|
||||
|
||||
/*global Java, base, module, exports, require, __FILE__*/
|
||||
var Sponge = MServer;
|
||||
// noinspection JSUnresolvedVariable
|
||||
var Server = Sponge.server;
|
||||
var PluginManager = Sponge.pluginManager;
|
||||
exports.$ = Sponge;
|
||||
/**
|
||||
* 插件管理
|
||||
* @type {{manager: *, get: exports.plugin.get, load: exports.plugin.load}}
|
||||
*/
|
||||
exports.plugin = {
|
||||
/**
|
||||
* 插件管理工具
|
||||
*/
|
||||
manager: PluginManager,
|
||||
/**
|
||||
* 获得插件实例
|
||||
* @param name 插件名称
|
||||
* @returns {*}
|
||||
*/
|
||||
get: function (name) {
|
||||
return PluginManager.getPlugin(name);
|
||||
},
|
||||
/**
|
||||
* 载入插件 并且返回结果
|
||||
* @param name 插件名称
|
||||
* @returns {*}
|
||||
*/
|
||||
load: function (name) {
|
||||
var plugin = this.get(name);
|
||||
if (ext.notNull(plugin) && !plugin.isEnabled()) {
|
||||
PluginManager.enablePlugin(plugin);
|
||||
}
|
||||
return PluginManager.isPluginEnabled(name);
|
||||
},
|
||||
self: PluginManager.getPlugin('miaoscript').get()
|
||||
};
|
72
src/main/resources/modules/sponge/task.js
Normal file
72
src/main/resources/modules/sponge/task.js
Normal file
@ -0,0 +1,72 @@
|
||||
'use strict';
|
||||
/*global Java, base, module, exports, require, __FILE__*/
|
||||
/**
|
||||
* 任务计划
|
||||
* Created by 蒋天蓓 on 2017/2/9 0009.
|
||||
*/
|
||||
var plugin = require('./server').plugin.self;
|
||||
var Comsumer = Java.type('java.util.function.Consumer');
|
||||
var Task = Java.type("org.spongepowered.api.scheduler.Task");
|
||||
/**
|
||||
* 创建任务对象
|
||||
* @param func 任务
|
||||
*/
|
||||
exports.create = function (func) {
|
||||
return Task.builder().execute(new Comsumer(func));
|
||||
};
|
||||
/**
|
||||
* 运行任务
|
||||
* @param func 任务
|
||||
*/
|
||||
exports.run = function (func) {
|
||||
return exports.create(func).submit(plugin);
|
||||
};
|
||||
/**
|
||||
* 延时运行任务
|
||||
* @param func 任务
|
||||
* @param time 延时时间
|
||||
*/
|
||||
exports.later = function (func, time) {
|
||||
return exports.create(func).delayTicks(time).submit(plugin);
|
||||
};
|
||||
/**
|
||||
* 运行循环任务
|
||||
* @constructor (任务,执行间隔).
|
||||
* @constructor (任务,首次延时,执行间隔)
|
||||
*/
|
||||
exports.timer = function () {
|
||||
switch (arguments.length) {
|
||||
case 2:
|
||||
return exports.create(arguments[0]).intervalTicks(arguments[1]).submit(plugin);
|
||||
case 3:
|
||||
return exports.create(arguments[0]).delayTicks(arguments[1]).intervalTicks(arguments[2]).submit(plugin);
|
||||
}
|
||||
};
|
||||
/**
|
||||
* 运行异步任务
|
||||
* @param func function 任务
|
||||
*/
|
||||
exports.async = function (func) {
|
||||
return exports.create(func).async().submit(plugin);
|
||||
};
|
||||
/**
|
||||
* 延时运行异步任务
|
||||
* @param func 任务
|
||||
* @param time 延时时间
|
||||
*/
|
||||
exports.laterAsync = function (func, time) {
|
||||
return exports.create(func).async().delayTicks(time).submit(plugin);
|
||||
};
|
||||
/**
|
||||
* 运行异步循环任务
|
||||
* @constructor (任务,执行间隔).
|
||||
* @constructor (任务,首次延时,执行间隔)
|
||||
*/
|
||||
exports.timerAsync = function () {
|
||||
switch (arguments.length) {
|
||||
case 2:
|
||||
return exports.create(arguments[0]).async().intervalTicks(arguments[1]).submit(plugin);
|
||||
case 3:
|
||||
return exports.create(arguments[0]).async().delayTicks(arguments[1]).intervalTicks(arguments[2]).submit(plugin);
|
||||
}
|
||||
};
|
@ -1 +1 @@
|
||||
module.exports = require('./bukkit/task');
|
||||
module.exports = require('./mserver').task;
|
Loading…
Reference in New Issue
Block a user