fix: 修复import配置问题
This commit is contained in:
@@ -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