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
.project
.classpath
.factorypath
.settings
# Visual Studio Code

13
pom.xml
View File

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

View File

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

View File

@ -1,10 +1,17 @@
/*global Java, base, module, exports, require*/
function CommandHandlerDefault() {
this.on = function (jsp, name, exec) {
this.on = function(jsp, name, exec) {
var cmd = this.create(jsp, { name: name });
console.debug('插件 %s 设置命令 %s 执行器(%s) ...'.format(jsp.description.name, name, cmd));
if (exec.cmd) { this.onCommand(jsp, cmd, exec.cmd) }
if (exec.tab) { this.onTabComplete(jsp, cmd, exec.tab) }
if (exec.cmd && typeof exec.cmd === "function") {
console.debug('插件 %s 设置命令 %s 执行器(%s) ...'.format(jsp.description.name, name, cmd));
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'));

View File

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

View File

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

View File

@ -19,7 +19,7 @@ function create(func) {
* @param func 任务
*/
function run(func) {
return exports.create(func).runTask(plugin);
return create(func).runTask(plugin);
};
/**
* 延时运行任务
@ -27,7 +27,7 @@ function run(func) {
* @param 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() {
switch (arguments.length) {
case 2:
return exports.create(arguments[0]).runTaskTimer(plugin, 0, arguments[1]);
return create(arguments[0]).runTaskTimer(plugin, 0, arguments[1]);
case 3:
return exports.create(arguments[0]).runTaskTimer(plugin, arguments[1], arguments[2]);
return create(arguments[0]).runTaskTimer(plugin, arguments[1], arguments[2]);
default:
throw TypeError('参数错误 task.timer(func, [delay], interval)');
}
@ -49,7 +49,7 @@ function timer() {
* @param func function 任务
*/
function _async(func) {
return exports.create(func).runTaskAsynchronously(plugin);
return create(func).runTaskAsynchronously(plugin);
};
/**
* 延时运行异步任务
@ -57,7 +57,7 @@ function _async(func) {
* @param 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() {
switch (arguments.length) {
case 2:
return exports.create(arguments[0]).runTaskTimerAsynchronously(plugin, 0, arguments[1]);
return create(arguments[0]).runTaskTimerAsynchronously(plugin, 0, arguments[1]);
case 3:
return exports.create(arguments[0]).runTaskTimerAsynchronously(plugin, arguments[1], arguments[2]);
return create(arguments[0]).runTaskTimerAsynchronously(plugin, arguments[1], arguments[2]);
default:
throw TypeError('参数错误 task.timerAsync(func, [delay], interval)');
}