fix: 修复import配置问题
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
# hamster-script
|
||||
|
||||
为 Minecraft 服务器引入 JavaScript 脚本执行功能
|
||||
|
||||
使用的脚本引擎为 [OpenJDK Nashorn](https://github.com/openjdk/nashorn)
|
||||
为Minecraft服务器导入 [OpenJDK Nashorn](https://github.com/openjdk/nashorn) 引擎来执行 JavaScript 脚本
|
||||
|
||||
# 手动构建
|
||||
|
||||
|
@@ -5,7 +5,8 @@ plugins {
|
||||
}
|
||||
|
||||
group = "cn.hamster3.mc.plugin"
|
||||
version = "1.0.0"
|
||||
version = "1.0.1"
|
||||
description = "为Minecraft服务器导入 OpenJDK Nashorn 引擎来执行 JavaScript 脚本"
|
||||
|
||||
val shade = configurations.create("shade")
|
||||
|
||||
@@ -25,11 +26,10 @@ dependencies {
|
||||
compileOnly("org.spigotmc:spigot-api:1.18.2-R0.1-SNAPSHOT")
|
||||
|
||||
// https://mvnrepository.com/artifact/org.openjdk.nashorn/nashorn-core
|
||||
implementation("org.openjdk.nashorn:nashorn-core:15.4")
|
||||
shade("org.openjdk.nashorn:nashorn-core:15.4")
|
||||
implementation("org.openjdk.nashorn:nashorn-core:15.0")
|
||||
shade("org.openjdk.nashorn:nashorn-core:15.0")
|
||||
}
|
||||
|
||||
|
||||
tasks {
|
||||
withType<JavaCompile>().configureEach {
|
||||
options.encoding = "UTF-8"
|
||||
@@ -47,7 +47,6 @@ tasks {
|
||||
archiveBaseName = "HamsterScript"
|
||||
destinationDirectory = rootProject.layout.buildDirectory
|
||||
|
||||
// from shade
|
||||
from(shade.map { if (it.isDirectory) it else zipTree(it) })
|
||||
}
|
||||
processResources {
|
||||
@@ -56,4 +55,4 @@ tasks {
|
||||
}
|
||||
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -20,12 +20,14 @@ import java.util.stream.Collectors;
|
||||
|
||||
@SuppressWarnings("CallToPrintStackTrace")
|
||||
public class HamsterScriptPlugin extends JavaPlugin {
|
||||
private static File codeFolder;
|
||||
@Getter
|
||||
private static ScriptEngine engine;
|
||||
@Getter
|
||||
private static Invocable invocable;
|
||||
private static File codeFolder;
|
||||
|
||||
private boolean enableEvalCommand;
|
||||
private Map<String, String> importClass;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
@@ -51,19 +53,22 @@ public class HamsterScriptPlugin extends JavaPlugin {
|
||||
enableEvalCommand = config.getBoolean("enable-eval-command", false);
|
||||
engine = new ScriptEngineManager(getClassLoader()).getEngineByName("JavaScript");
|
||||
invocable = (Invocable) engine;
|
||||
|
||||
importClass = new HashMap<>();
|
||||
ConfigurationSection importConfig = config.getConfigurationSection("import");
|
||||
if (importConfig != null) {
|
||||
for (String key : importConfig.getKeys(false)) {
|
||||
String string = importConfig.getString(key);
|
||||
for (String simpleName : importConfig.getKeys(false)) {
|
||||
String className = importConfig.getString(simpleName);
|
||||
importClass.put(simpleName, className);
|
||||
try {
|
||||
Class<?> clazz = Class.forName(string);
|
||||
engine.put(key, clazz);
|
||||
engine.eval(String.format("%s = %s.static;", key, key));
|
||||
getLogger().info("将 " + string + " 导入为 " + key);
|
||||
Class<?> clazz = Class.forName(className);
|
||||
engine.put(simpleName, clazz);
|
||||
engine.eval(String.format("%s = %s.static;", simpleName, simpleName));
|
||||
getLogger().info("将 " + className + " 导入为 " + simpleName);
|
||||
} catch (ClassNotFoundException e) {
|
||||
getLogger().warning("将 " + string + " 导入为 " + key + " 失败:未找到这个类");
|
||||
getLogger().warning("将 " + className + " 导入为 " + simpleName + " 失败:未找到这个类");
|
||||
} catch (ScriptException e) {
|
||||
getLogger().warning("将 " + string + " 导入为 " + key + " 失败");
|
||||
getLogger().warning("将 " + className + " 导入为 " + simpleName + " 失败");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
@@ -99,6 +104,9 @@ public class HamsterScriptPlugin extends JavaPlugin {
|
||||
long start = System.currentTimeMillis();
|
||||
try {
|
||||
Bindings bindings = engine.createBindings();
|
||||
for (String simpleName : importClass.keySet()) {
|
||||
bindings.put(simpleName, engine.get(simpleName));
|
||||
}
|
||||
bindings.put("sender", sender);
|
||||
Object eval = engine.eval(code, bindings);
|
||||
long time = System.currentTimeMillis() - start;
|
||||
@@ -137,6 +145,9 @@ public class HamsterScriptPlugin extends JavaPlugin {
|
||||
long start = System.currentTimeMillis();
|
||||
try {
|
||||
Bindings bindings = engine.createBindings();
|
||||
for (String simpleName : importClass.keySet()) {
|
||||
bindings.put(simpleName, engine.get(simpleName));
|
||||
}
|
||||
bindings.put("sender", sender);
|
||||
Object eval = engine.eval(code, bindings);
|
||||
long time = System.currentTimeMillis() - start;
|
||||
|
@@ -2,6 +2,11 @@ name: HamsterScript
|
||||
main: cn.hamster3.mc.plugin.script.HamsterScriptPlugin
|
||||
version: ${version}
|
||||
api-version: 1.13
|
||||
description: ${description}
|
||||
author: MiniDay
|
||||
|
||||
Plugin:
|
||||
join-classpath: true
|
||||
|
||||
commands:
|
||||
scripts:
|
||||
@@ -9,5 +14,5 @@ commands:
|
||||
|
||||
permissions:
|
||||
hamster.script.admin:
|
||||
description: Admin permission
|
||||
description: 管理员权限
|
||||
default: op
|
||||
|
Reference in New Issue
Block a user