style: 优化代码命名
This commit is contained in:
@@ -19,13 +19,15 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.*;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@SuppressWarnings("CallToPrintStackTrace")
|
||||
public class HamsterScriptPlugin extends JavaPlugin {
|
||||
private static File codeFolder;
|
||||
@Getter
|
||||
private static ScriptEngine engine;
|
||||
private static ScriptEngine scriptEngine;
|
||||
@Getter
|
||||
private static Invocable invocable;
|
||||
|
||||
@@ -42,9 +44,10 @@ public class HamsterScriptPlugin extends JavaPlugin {
|
||||
reloadConfig();
|
||||
FileConfiguration config = getConfig();
|
||||
enableEvalCommand = config.getBoolean("enable-eval-command", false);
|
||||
engine = new ScriptEngineManager(getClassLoader()).getEngineByName("JavaScript");
|
||||
invocable = (Invocable) engine;
|
||||
scriptEngine = new ScriptEngineManager(getClassLoader()).getEngineByName("JavaScript");
|
||||
invocable = (Invocable) scriptEngine;
|
||||
|
||||
Logger logger = getLogger();
|
||||
importClass = new HashMap<>();
|
||||
ConfigurationSection importConfig = config.getConfigurationSection("import");
|
||||
if (importConfig != null) {
|
||||
@@ -53,14 +56,13 @@ public class HamsterScriptPlugin extends JavaPlugin {
|
||||
importClass.put(simpleName, className);
|
||||
try {
|
||||
Class<?> clazz = Class.forName(className);
|
||||
engine.put(simpleName, clazz);
|
||||
engine.eval(String.format("%s = %s.static;", simpleName, simpleName));
|
||||
getLogger().info("已导入 " + className);
|
||||
scriptEngine.put(simpleName, clazz);
|
||||
scriptEngine.eval(String.format("%s = %s.static;", simpleName, simpleName));
|
||||
logger.info("已导入 " + className);
|
||||
} catch (ClassNotFoundException e) {
|
||||
getLogger().warning("导入 " + className + " 失败:未找到这个类");
|
||||
logger.log(Level.WARNING, "导入 " + className + " 失败:未找到这个类", e);
|
||||
} catch (Exception e) {
|
||||
getLogger().warning("导入 " + className + " 失败");
|
||||
e.printStackTrace();
|
||||
logger.log(Level.WARNING, "导入 " + className + " 失败", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -114,14 +116,14 @@ public class HamsterScriptPlugin extends JavaPlugin {
|
||||
}
|
||||
long start = System.currentTimeMillis();
|
||||
try {
|
||||
Bindings bindings = engine.createBindings();
|
||||
Bindings bindings = scriptEngine.createBindings();
|
||||
for (String simpleName : importClass.keySet()) {
|
||||
bindings.put(simpleName, engine.get(simpleName));
|
||||
bindings.put(simpleName, scriptEngine.get(simpleName));
|
||||
}
|
||||
String[] scriptArgs = Arrays.copyOfRange(args, 2, args.length);
|
||||
bindings.put("sender", sender);
|
||||
bindings.put("args", scriptArgs);
|
||||
Object eval = engine.eval(code, bindings);
|
||||
Object eval = scriptEngine.eval(code, bindings);
|
||||
long time = System.currentTimeMillis() - start;
|
||||
sender.sendMessage("§aJavaScript 代码执行完成, 耗时: " + time + " 毫秒");
|
||||
sender.sendMessage("§a返回值: §f" + eval);
|
||||
@@ -149,12 +151,12 @@ public class HamsterScriptPlugin extends JavaPlugin {
|
||||
String code = builder.toString();
|
||||
long start = System.currentTimeMillis();
|
||||
try {
|
||||
Bindings bindings = engine.createBindings();
|
||||
Bindings bindings = scriptEngine.createBindings();
|
||||
for (String simpleName : importClass.keySet()) {
|
||||
bindings.put(simpleName, engine.get(simpleName));
|
||||
bindings.put(simpleName, scriptEngine.get(simpleName));
|
||||
}
|
||||
bindings.put("sender", sender);
|
||||
Object eval = engine.eval(code, bindings);
|
||||
Object eval = scriptEngine.eval(code, bindings);
|
||||
long time = System.currentTimeMillis() - start;
|
||||
sender.sendMessage("§aJavaScript 代码执行完成, 耗时: " + time + " 毫秒");
|
||||
sender.sendMessage("§a返回值: §f" + eval);
|
||||
|
@@ -9,6 +9,7 @@ import:
|
||||
UUID: java.util.UUID
|
||||
Base64: java.util.Base64
|
||||
ArrayList: java.util.ArrayList
|
||||
HashMap: java.util.HashMap
|
||||
|
||||
Bukkit: org.bukkit.Bukkit
|
||||
Material: org.bukkit.Material
|
||||
|
Reference in New Issue
Block a user