fix: 修复import配置问题

This commit is contained in:
2024-03-06 10:54:57 +08:00
parent 6f3ae704f9
commit 907fea3d40
4 changed files with 32 additions and 19 deletions

View File

@@ -1,8 +1,6 @@
# hamster-script # hamster-script
Minecraft 服务器入 JavaScript 脚本执行功能 为Minecraft服务器 [OpenJDK Nashorn](https://github.com/openjdk/nashorn) 引擎来执行 JavaScript 脚本
使用的脚本引擎为 [OpenJDK Nashorn](https://github.com/openjdk/nashorn)
# 手动构建 # 手动构建

View File

@@ -5,7 +5,8 @@ plugins {
} }
group = "cn.hamster3.mc.plugin" group = "cn.hamster3.mc.plugin"
version = "1.0.0" version = "1.0.1"
description = "为Minecraft服务器导入 OpenJDK Nashorn 引擎来执行 JavaScript 脚本"
val shade = configurations.create("shade") val shade = configurations.create("shade")
@@ -25,11 +26,10 @@ dependencies {
compileOnly("org.spigotmc:spigot-api:1.18.2-R0.1-SNAPSHOT") compileOnly("org.spigotmc:spigot-api:1.18.2-R0.1-SNAPSHOT")
// https://mvnrepository.com/artifact/org.openjdk.nashorn/nashorn-core // https://mvnrepository.com/artifact/org.openjdk.nashorn/nashorn-core
implementation("org.openjdk.nashorn:nashorn-core:15.4") implementation("org.openjdk.nashorn:nashorn-core:15.0")
shade("org.openjdk.nashorn:nashorn-core:15.4") shade("org.openjdk.nashorn:nashorn-core:15.0")
} }
tasks { tasks {
withType<JavaCompile>().configureEach { withType<JavaCompile>().configureEach {
options.encoding = "UTF-8" options.encoding = "UTF-8"
@@ -47,7 +47,6 @@ tasks {
archiveBaseName = "HamsterScript" archiveBaseName = "HamsterScript"
destinationDirectory = rootProject.layout.buildDirectory destinationDirectory = rootProject.layout.buildDirectory
// from shade
from(shade.map { if (it.isDirectory) it else zipTree(it) }) from(shade.map { if (it.isDirectory) it else zipTree(it) })
} }
processResources { processResources {
@@ -56,4 +55,4 @@ tasks {
} }
duplicatesStrategy = DuplicatesStrategy.EXCLUDE duplicatesStrategy = DuplicatesStrategy.EXCLUDE
} }
} }

View File

@@ -20,12 +20,14 @@ import java.util.stream.Collectors;
@SuppressWarnings("CallToPrintStackTrace") @SuppressWarnings("CallToPrintStackTrace")
public class HamsterScriptPlugin extends JavaPlugin { public class HamsterScriptPlugin extends JavaPlugin {
private static File codeFolder;
@Getter @Getter
private static ScriptEngine engine; private static ScriptEngine engine;
@Getter @Getter
private static Invocable invocable; private static Invocable invocable;
private static File codeFolder;
private boolean enableEvalCommand; private boolean enableEvalCommand;
private Map<String, String> importClass;
@Override @Override
public void onEnable() { public void onEnable() {
@@ -51,19 +53,22 @@ public class HamsterScriptPlugin extends JavaPlugin {
enableEvalCommand = config.getBoolean("enable-eval-command", false); enableEvalCommand = config.getBoolean("enable-eval-command", false);
engine = new ScriptEngineManager(getClassLoader()).getEngineByName("JavaScript"); engine = new ScriptEngineManager(getClassLoader()).getEngineByName("JavaScript");
invocable = (Invocable) engine; invocable = (Invocable) engine;
importClass = new HashMap<>();
ConfigurationSection importConfig = config.getConfigurationSection("import"); ConfigurationSection importConfig = config.getConfigurationSection("import");
if (importConfig != null) { if (importConfig != null) {
for (String key : importConfig.getKeys(false)) { for (String simpleName : importConfig.getKeys(false)) {
String string = importConfig.getString(key); String className = importConfig.getString(simpleName);
importClass.put(simpleName, className);
try { try {
Class<?> clazz = Class.forName(string); Class<?> clazz = Class.forName(className);
engine.put(key, clazz); engine.put(simpleName, clazz);
engine.eval(String.format("%s = %s.static;", key, key)); engine.eval(String.format("%s = %s.static;", simpleName, simpleName));
getLogger().info("" + string + " 导入为 " + key); getLogger().info("" + className + " 导入为 " + simpleName);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
getLogger().warning("" + string + " 导入为 " + key + " 失败:未找到这个类"); getLogger().warning("" + className + " 导入为 " + simpleName + " 失败:未找到这个类");
} catch (ScriptException e) { } catch (ScriptException e) {
getLogger().warning("" + string + " 导入为 " + key + " 失败"); getLogger().warning("" + className + " 导入为 " + simpleName + " 失败");
e.printStackTrace(); e.printStackTrace();
} }
} }
@@ -99,6 +104,9 @@ public class HamsterScriptPlugin extends JavaPlugin {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
try { try {
Bindings bindings = engine.createBindings(); Bindings bindings = engine.createBindings();
for (String simpleName : importClass.keySet()) {
bindings.put(simpleName, engine.get(simpleName));
}
bindings.put("sender", sender); bindings.put("sender", sender);
Object eval = engine.eval(code, bindings); Object eval = engine.eval(code, bindings);
long time = System.currentTimeMillis() - start; long time = System.currentTimeMillis() - start;
@@ -137,6 +145,9 @@ public class HamsterScriptPlugin extends JavaPlugin {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
try { try {
Bindings bindings = engine.createBindings(); Bindings bindings = engine.createBindings();
for (String simpleName : importClass.keySet()) {
bindings.put(simpleName, engine.get(simpleName));
}
bindings.put("sender", sender); bindings.put("sender", sender);
Object eval = engine.eval(code, bindings); Object eval = engine.eval(code, bindings);
long time = System.currentTimeMillis() - start; long time = System.currentTimeMillis() - start;

View File

@@ -2,6 +2,11 @@ name: HamsterScript
main: cn.hamster3.mc.plugin.script.HamsterScriptPlugin main: cn.hamster3.mc.plugin.script.HamsterScriptPlugin
version: ${version} version: ${version}
api-version: 1.13 api-version: 1.13
description: ${description}
author: MiniDay
Plugin:
join-classpath: true
commands: commands:
scripts: scripts:
@@ -9,5 +14,5 @@ commands:
permissions: permissions:
hamster.script.admin: hamster.script.admin:
description: Admin permission description: 管理员权限
default: op default: op