fix: compatible bukkit command

1. fix bukkit command error
2. fix bukkit task error
3. fix bukkit class loader error
4. optimization require
This commit is contained in:
MiaoWoo 2019-02-23 15:51:04 +00:00
parent d67f2843b0
commit 997820d117
7 changed files with 46 additions and 33 deletions

1
.gitignore vendored
View File

@ -38,6 +38,7 @@
# Eclipse # Eclipse
.project .project
.classpath .classpath
.factorypath
.settings .settings
# Visual Studio Code # Visual Studio Code

13
pom.xml
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.3.1</version> <version>1.3.2</version>
<developers> <developers>
<developer> <developer>
<id>502647092</id> <id>502647092</id>
@ -79,7 +79,7 @@
</build> </build>
<ciManagement> <ciManagement>
<system>Jenkins</system> <system>Jenkins</system>
<url>http://ci.yumc.pw/job/${project.artifactId}/</url> <url>https://ci.yumc.pw/job/${project.artifactId}/</url>
</ciManagement> </ciManagement>
<properties> <properties>
<env.GIT_COMMIT>DEV</env.GIT_COMMIT> <env.GIT_COMMIT>DEV</env.GIT_COMMIT>
@ -154,11 +154,12 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.target>1.8</maven.compiler.target>
<repo.url>https://repo.yumc.pw</repo.url>
</properties> </properties>
<repositories> <repositories>
<repository> <repository>
<id>yumc-repo</id> <id>yumc-repo</id>
<url>http://repo.yumc.pw/content/groups/public/</url> <url>${repo.url}/repository/maven-public/</url>
</repository> </repository>
<repository> <repository>
<id>sponge</id> <id>sponge</id>
@ -168,14 +169,14 @@
<pluginRepositories> <pluginRepositories>
<pluginRepository> <pluginRepository>
<id>yumc-repo</id> <id>yumc-repo</id>
<url>http://repo.yumc.pw/content/groups/public/</url> <url>${repo.url}/repository/maven-public/</url>
</pluginRepository> </pluginRepository>
</pluginRepositories> </pluginRepositories>
<distributionManagement> <distributionManagement>
<repository> <repository>
<id>jtb</id> <id>jtb</id>
<name>YUMC</name> <name>YUMC</name>
<url>http://repo.yumc.pw/content/repositories/yumcenter/</url> <url>${repo.url}/repository/yumcenter/</url>
</repository> </repository>
</distributionManagement> </distributionManagement>
<dependencies> <dependencies>
@ -183,7 +184,7 @@
<groupId>pw.yumc</groupId> <groupId>pw.yumc</groupId>
<artifactId>YumCore</artifactId> <artifactId>YumCore</artifactId>
<type>jar</type> <type>jar</type>
<version>[1.8,)</version> <version>1.8.8</version>
<exclusions> <exclusions>
<exclusion> <exclusion>
<groupId>org.bukkit</groupId> <groupId>org.bukkit</groupId>

View File

@ -2,6 +2,7 @@ package pw.yumc.MiaoScript;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.lang.Thread;
import lombok.SneakyThrows; import lombok.SneakyThrows;
/** /**
@ -16,6 +17,7 @@ public class MiaoScript extends JavaPlugin {
@Override @Override
@SneakyThrows @SneakyThrows
public void onEnable() { public void onEnable() {
Thread.currentThread().setContextClassLoader(getClassLoader());
engine = new ScriptEngine(getDataFolder().getCanonicalPath(), getLogger()); engine = new ScriptEngine(getDataFolder().getCanonicalPath(), getLogger());
engine.enableEngine(); engine.enableEngine();
} }

View File

@ -1,10 +1,17 @@
/*global Java, base, module, exports, require*/ /*global Java, base, module, exports, require*/
function CommandHandlerDefault() { function CommandHandlerDefault() {
this.on = function (jsp, name, exec) { this.on = function(jsp, name, exec) {
var cmd = this.create(jsp, { name: name }); var cmd = this.create(jsp, { name: name });
console.debug('插件 %s 设置命令 %s 执行器(%s) ...'.format(jsp.description.name, name, cmd)); if (exec.cmd && typeof exec.cmd === "function") {
if (exec.cmd) { this.onCommand(jsp, cmd, exec.cmd) } console.debug('插件 %s 设置命令 %s 执行器(%s) ...'.format(jsp.description.name, name, cmd));
if (exec.tab) { this.onTabComplete(jsp, cmd, exec.tab) } this.onCommand(jsp, cmd, exec.cmd)
} else {
throw Error("CommandExec Must be a function... Input: " + exec.cmd)
}
if (exec.tab && typeof exec.tab === "function") {
console.debug('插件 %s 设置命令 %s 自动补全(%s) ...'.format(jsp.description.name, name, cmd));
this.onTabComplete(jsp, cmd, exec.tab)
}
} }
} }
var CommandHandler = Object.assign(new CommandHandlerDefault(), requireInternal('command')); var CommandHandler = Object.assign(new CommandHandlerDefault(), requireInternal('command'));

View File

@ -26,7 +26,7 @@
* : MiaoScript 暂不支持多层 modules 加载 暂时不需要(估计以后也不会需要) * : MiaoScript 暂不支持多层 modules 加载 暂时不需要(估计以后也不会需要)
*/ */
/*global Java, base*/ /*global Java, base*/
(function (parent) { (function(parent) {
'use strict'; 'use strict';
var File = Java.type("java.io.File"); var File = Java.type("java.io.File");
var separatorChar = File.separatorChar; var separatorChar = File.separatorChar;
@ -175,15 +175,16 @@
}; };
cacheModules[id] = module; cacheModules[id] = module;
try { try {
if (_canonical(file).endsWith('.js')) { var cfile = _canonical(file);
if (cfile.endsWith('.js')) {
compileJs(module, file, optional); compileJs(module, file, optional);
} } else if (cfile.endsWith('.json')) {
if (_canonical(file).endsWith('.json')) {
compileJson(module, file, optional); compileJson(module, file, optional);
} } else if (cfile.endsWith('.msm')) {
if (_canonical(file).endsWith('.msm')) {
// noinspection ExceptionCaughtLocallyJS // noinspection ExceptionCaughtLocallyJS
throw Error("暂不支持解析 MiaoScript 模块"); throw Error("暂不支持解析 MiaoScript 模块");
} else {
throw Error("未知的文件格式 " + cfile);
} }
} catch (ex) { } catch (ex) {
console.console('§4警告! §c模块§a', name, '§c编译失败! §4ERR:', ex); console.console('§4警告! §c模块§a', name, '§c编译失败! §4ERR:', ex);
@ -242,12 +243,12 @@
function _require(name, path, optional) { function _require(name, path, optional) {
var file = new File(name); var file = new File(name);
file = _isFile(file) ? file : resolve(name, path); file = _isFile(file) ? file : resolve(name, path);
optional = Object.assign({cache: true, warnNotFound: true}, optional); optional = Object.assign({ cache: true, warnNotFound: true }, optional);
if (file === undefined) { if (file === undefined) {
if (optional.warnNotFound) { if (optional.warnNotFound) {
console.console('§c目录§b', path, '§c下模块§a', name, '§c加载失败! §4未找到该模块!'); console.console('§c目录§b', path, '§c下模块§a', name, '§c加载失败! §4未找到该模块!');
} }
return {exports: {}}; return { exports: {} };
} }
// 重定向文件名称和类型 // 重定向文件名称和类型
return getCacheModule(_canonical(file), file.name.split(".")[0], file, optional); return getCacheModule(_canonical(file), file.name.split(".")[0], file, optional);
@ -259,7 +260,7 @@
* @returns {Function} * @returns {Function}
*/ */
function exports(parent) { function exports(parent) {
return function (path, optional) { return function __DynamicRequire__(path, optional) {
return _require(path, parent, optional).exports; return _require(path, parent, optional).exports;
}; };
} }

View File

@ -20,8 +20,9 @@ function enable(jsp) {
// noinspection JSUnfilteredForInLoop // noinspection JSUnfilteredForInLoop
var command = commands[name]; var command = commands[name];
if (typeof command !== 'object') continue; if (typeof command !== 'object') continue;
command.name = name;
// noinspection JSUnfilteredForInLoop // noinspection JSUnfilteredForInLoop
var newCmd = create(jsp, name); var newCmd = create(jsp, command);
if (command.description) newCmd.setDescription(command.description); if (command.description) newCmd.setDescription(command.description);
if (command.usage) newCmd.setUsage(command.usage); if (command.usage) newCmd.setUsage(command.usage);
/** @namespace command.aliases */ /** @namespace command.aliases */
@ -51,7 +52,7 @@ function create(jsp, command) {
var cmd = commandMap.getCommand(command.name) var cmd = commandMap.getCommand(command.name)
if (cmd) { return cmd }; if (cmd) { return cmd };
cmd = ref.on(PluginCommand).create(command.name, plugin).get(); cmd = ref.on(PluginCommand).create(command.name, plugin).get();
register(jsp, cmd); commandMap.register(jsp.description.name, cmd);
return cmd; return cmd;
} }
@ -59,9 +60,9 @@ function onCommand(jsp, c, cmd) {
// 必须指定需要实现的接口类型 否则MOD服会报错 // 必须指定需要实现的接口类型 否则MOD服会报错
// noinspection JSUnusedGlobalSymbols // noinspection JSUnusedGlobalSymbols
c.setExecutor(new org.bukkit.command.CommandExecutor({ c.setExecutor(new org.bukkit.command.CommandExecutor({
onCommand: function (sender, cmd, command, args) { onCommand: function (sender, _, command, args) {
try { try {
return cmd(sender, command, args); return cmd(sender, command, Java.from(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.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); console.ex(ex);
@ -74,10 +75,10 @@ function onTabComplete(jsp, c, tab) {
// 必须指定需要实现的接口类型 否则MOD服会报错 // 必须指定需要实现的接口类型 否则MOD服会报错
// noinspection JSUnusedGlobalSymbols // noinspection JSUnusedGlobalSymbols
c.setTabCompleter(new org.bukkit.command.TabCompleter({ c.setTabCompleter(new org.bukkit.command.TabCompleter({
onTabComplete: function (sender, cmd, command, args) { onTabComplete: function (sender, _, command, args) {
try { try {
var token = args[args.length - 1]; var token = args[args.length - 1];
var complete = tab(sender, command, args) || []; var complete = tab(sender, command, Java.from(args)) || [];
return Arrays.asList(complete.copyPartialMatches(token, [])); return Arrays.asList(complete.copyPartialMatches(token, []));
} 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, Java.from(args).join(' '), ex));

View File

@ -19,7 +19,7 @@ function create(func) {
* @param func 任务 * @param func 任务
*/ */
function run(func) { function run(func) {
return exports.create(func).runTask(plugin); return create(func).runTask(plugin);
}; };
/** /**
* 延时运行任务 * 延时运行任务
@ -27,7 +27,7 @@ function run(func) {
* @param time 延时时间 * @param time 延时时间
*/ */
function later(func, time) { function later(func, time) {
return exports.create(func).runTaskLater(plugin, time); return create(func).runTaskLater(plugin, time);
}; };
/** /**
* 运行循环任务 * 运行循环任务
@ -37,9 +37,9 @@ function later(func, time) {
function timer() { function timer() {
switch (arguments.length) { switch (arguments.length) {
case 2: case 2:
return exports.create(arguments[0]).runTaskTimer(plugin, 0, arguments[1]); return create(arguments[0]).runTaskTimer(plugin, 0, arguments[1]);
case 3: case 3:
return exports.create(arguments[0]).runTaskTimer(plugin, arguments[1], arguments[2]); return create(arguments[0]).runTaskTimer(plugin, arguments[1], arguments[2]);
default: default:
throw TypeError('参数错误 task.timer(func, [delay], interval)'); throw TypeError('参数错误 task.timer(func, [delay], interval)');
} }
@ -49,7 +49,7 @@ function timer() {
* @param func function 任务 * @param func function 任务
*/ */
function _async(func) { function _async(func) {
return exports.create(func).runTaskAsynchronously(plugin); return create(func).runTaskAsynchronously(plugin);
}; };
/** /**
* 延时运行异步任务 * 延时运行异步任务
@ -57,7 +57,7 @@ function _async(func) {
* @param time 延时时间 * @param time 延时时间
*/ */
function laterAsync(func, time) { function laterAsync(func, time) {
return exports.create(func).runTaskLaterAsynchronously(plugin, time); return create(func).runTaskLaterAsynchronously(plugin, time);
}; };
/** /**
* 运行异步循环任务 * 运行异步循环任务
@ -67,9 +67,9 @@ function laterAsync(func, time) {
function timerAsync() { function timerAsync() {
switch (arguments.length) { switch (arguments.length) {
case 2: case 2:
return exports.create(arguments[0]).runTaskTimerAsynchronously(plugin, 0, arguments[1]); return create(arguments[0]).runTaskTimerAsynchronously(plugin, 0, arguments[1]);
case 3: case 3:
return exports.create(arguments[0]).runTaskTimerAsynchronously(plugin, arguments[1], arguments[2]); return create(arguments[0]).runTaskTimerAsynchronously(plugin, arguments[1], arguments[2]);
default: default:
throw TypeError('参数错误 task.timerAsync(func, [delay], interval)'); throw TypeError('参数错误 task.timerAsync(func, [delay], interval)');
} }