船新两个 Sendable Json 和 ActionBar
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
package me.skymc.taboolib.javashell;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.HashMap;
|
||||
|
||||
import com.ilummc.tlib.dependency.TDependencyLoader;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
import org.apache.commons.lang.ArrayUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.HandlerList;
|
||||
@@ -13,195 +12,190 @@ import org.bukkit.event.Listener;
|
||||
import org.bukkit.plugin.RegisteredListener;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.javashell.utils.JarUtils;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class JavaShell {
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private static String paths = "";
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private static File javaShellFolder;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private static File scriptFolder;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private static File cacheFolder;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private static File libFolder;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private static HashMap<String, Class<?>> shells = new HashMap<>();
|
||||
|
||||
public static void javaShellSetup() {
|
||||
File dataFolder = Main.getInst().getDataFolder();
|
||||
File pluginsFolder = dataFolder.getParentFile();
|
||||
File serverRoot = Bukkit.getWorldContainer();
|
||||
|
||||
File[] rootJars = serverRoot.listFiles((dir, name) -> name.toLowerCase().endsWith("jar"));
|
||||
@Getter
|
||||
@Setter
|
||||
private static String paths = "";
|
||||
|
||||
File[] pluginJars = pluginsFolder.listFiles((dir, name) -> name.toLowerCase().endsWith("jar"));
|
||||
@Getter
|
||||
@Setter
|
||||
private static File javaShellFolder;
|
||||
|
||||
for (File file : (File[]) ArrayUtils.addAll(rootJars, pluginJars)) {
|
||||
String path = file.getAbsolutePath();
|
||||
paths += File.pathSeparator + path;
|
||||
}
|
||||
|
||||
javaShellFolder = new File(Main.getInst().getDataFolder(), "JavaShells");
|
||||
if (!javaShellFolder.exists()) {
|
||||
Main.getInst().saveResource("JavaShells/scripts/-testshell.java", true);
|
||||
}
|
||||
|
||||
scriptFolder = new File(javaShellFolder, "scripts");
|
||||
if (!scriptFolder.exists()) {
|
||||
scriptFolder.mkdir();
|
||||
}
|
||||
|
||||
cacheFolder = new File(javaShellFolder, "cache");
|
||||
if (!cacheFolder.exists()) {
|
||||
cacheFolder.mkdir();
|
||||
}
|
||||
|
||||
libFolder = new File(javaShellFolder, "lib");
|
||||
if (!libFolder.exists()) {
|
||||
libFolder.mkdir();
|
||||
}
|
||||
|
||||
File tools = new File(Main.getInst().getDataFolder(), "JavaShells/lib/com.sun.tools.jar");
|
||||
if (!tools.exists()) {
|
||||
MsgUtils.warn("&4JavaShell &c工具的必要依赖 &4com.sun.tools.jar &c不存在, 功能关闭!");
|
||||
return;
|
||||
}
|
||||
|
||||
loadLibrary();
|
||||
new BukkitRunnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
long time = System.currentTimeMillis();
|
||||
for (File file : scriptFolder.listFiles()) {
|
||||
if (!file.getName().startsWith("-")) {
|
||||
reloadShell(file.getName());
|
||||
}
|
||||
}
|
||||
MsgUtils.send("载入 " + shells.size() + " 个脚本, 耗时 &f" + (System.currentTimeMillis() - time) + "ms");
|
||||
}
|
||||
}.runTask(Main.getInst());
|
||||
}
|
||||
|
||||
public static void javaShellCancel() {
|
||||
for (File cacheFile : cacheFolder.listFiles()) {
|
||||
cacheFile.delete();
|
||||
}
|
||||
for (String name : shells.keySet()) {
|
||||
invokeMethod(name, "onDisable");
|
||||
}
|
||||
}
|
||||
|
||||
public static void invokeMethod(String name, String method) {
|
||||
if (shells.containsKey(name)) {
|
||||
Class<?> clazz = shells.get(name);
|
||||
try {
|
||||
Method disableMethod = clazz.getMethod(method);
|
||||
if (disableMethod != null) {
|
||||
disableMethod.invoke(clazz.newInstance());
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
//
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void unloadShell(String shell) {
|
||||
invokeMethod(shell, "onDisable");
|
||||
Class<?> clazz = shells.remove(shell);
|
||||
try {
|
||||
if (clazz.newInstance() instanceof Listener) {
|
||||
for (RegisteredListener listener : HandlerList.getRegisteredListeners(Main.getInst())) {
|
||||
if (listener.getListener().getClass().getName().equals(clazz.getName())) {
|
||||
HandlerList.unregisterAll(listener.getListener());
|
||||
}
|
||||
}
|
||||
MsgUtils.send("已为脚本 &f" + shell + " &7注销监听器");
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
//
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean reloadShell(String shell) {
|
||||
unloadShell(shell = shell.replace(".java", ""));
|
||||
|
||||
try {
|
||||
Class.forName("com.sun.tools.javac.main.Main");
|
||||
}
|
||||
catch (Exception e) {
|
||||
MsgUtils.warn("&4JavaShell &c工具的必要依赖 &4com.sun.tools.jar &c丢失, 无法载入!");
|
||||
return false;
|
||||
}
|
||||
|
||||
File javaFile = new File(scriptFolder, shell + ".java");
|
||||
if (!javaFile.exists()) {
|
||||
MsgUtils.send("&c脚本 &4" + shell + "&c 不存在");
|
||||
return false;
|
||||
}
|
||||
|
||||
String[] args = {
|
||||
"-nowarn",
|
||||
"-classpath", "." + File.pathSeparator + JavaShell.getPaths(),
|
||||
"-d", cacheFolder.getAbsolutePath() + File.separator,
|
||||
javaFile.getAbsolutePath()
|
||||
};
|
||||
|
||||
int code = new com.sun.tools.javac.main.Main("javac").compile(args).exitCode;
|
||||
if (code == 0) {
|
||||
MsgUtils.send("&f" + shell + "&7 载入成功");
|
||||
try {
|
||||
URL[] urls = { cacheFolder.toURI().toURL() };
|
||||
URLClassLoader sysloader = new URLClassLoader(urls, Main.class.getClassLoader());
|
||||
Class<?> clazz = sysloader.loadClass(shell);
|
||||
shells.put(shell, clazz);
|
||||
sysloader.close();
|
||||
|
||||
invokeMethod(shell, "onEnable");
|
||||
if (clazz.newInstance() instanceof Listener) {
|
||||
Bukkit.getPluginManager().registerEvents((Listener) clazz.newInstance(), Main.getInst());
|
||||
MsgUtils.send("已为脚本 &f" + shell + " &7注册监听器");
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
//
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
MsgUtils.send("&4" + shell + "&c 载入失败");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static void loadLibrary() {
|
||||
for (File jar : libFolder.listFiles()) {
|
||||
try {
|
||||
JarUtils.addClassPath(JarUtils.getJarUrl(jar));
|
||||
MsgUtils.send("成功载入 &f" + jar.getName() + " &7到运行库");
|
||||
} catch (Exception e) {
|
||||
//
|
||||
}
|
||||
}
|
||||
}
|
||||
@Getter
|
||||
@Setter
|
||||
private static File scriptFolder;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private static File cacheFolder;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private static File libFolder;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private static HashMap<String, Class<?>> shells = new HashMap<>();
|
||||
|
||||
public static void javaShellSetup() {
|
||||
File dataFolder = Main.getInst().getDataFolder();
|
||||
File pluginsFolder = dataFolder.getParentFile();
|
||||
File serverRoot = Bukkit.getWorldContainer();
|
||||
|
||||
File[] rootJars = serverRoot.listFiles((dir, name) -> name.toLowerCase().endsWith("jar"));
|
||||
|
||||
File[] pluginJars = pluginsFolder.listFiles((dir, name) -> name.toLowerCase().endsWith("jar"));
|
||||
|
||||
for (File file : (File[]) ArrayUtils.addAll(rootJars, pluginJars)) {
|
||||
String path = file.getAbsolutePath();
|
||||
paths += File.pathSeparator + path;
|
||||
}
|
||||
|
||||
javaShellFolder = new File(Main.getInst().getDataFolder(), "JavaShells");
|
||||
if (!javaShellFolder.exists()) {
|
||||
Main.getInst().saveResource("JavaShells/scripts/-testshell.java", true);
|
||||
}
|
||||
|
||||
scriptFolder = new File(javaShellFolder, "scripts");
|
||||
if (!scriptFolder.exists()) {
|
||||
scriptFolder.mkdir();
|
||||
}
|
||||
|
||||
cacheFolder = new File(javaShellFolder, "cache");
|
||||
if (!cacheFolder.exists()) {
|
||||
cacheFolder.mkdir();
|
||||
}
|
||||
|
||||
libFolder = new File(javaShellFolder, "lib");
|
||||
if (!libFolder.exists()) {
|
||||
libFolder.mkdir();
|
||||
}
|
||||
|
||||
File tools = new File(Main.getInst().getDataFolder(), "JavaShells/lib/com.sun.tools.jar");
|
||||
if (!tools.exists()) {
|
||||
MsgUtils.warn("&4JavaShell &c工具的必要依赖 &4com.sun.tools.jar &c不存在, 功能关闭!");
|
||||
return;
|
||||
}
|
||||
|
||||
loadLibrary();
|
||||
new BukkitRunnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
long time = System.currentTimeMillis();
|
||||
for (File file : scriptFolder.listFiles()) {
|
||||
if (!file.getName().startsWith("-")) {
|
||||
reloadShell(file.getName());
|
||||
}
|
||||
}
|
||||
MsgUtils.send("载入 " + shells.size() + " 个脚本, 耗时 &f" + (System.currentTimeMillis() - time) + "ms");
|
||||
}
|
||||
}.runTask(Main.getInst());
|
||||
}
|
||||
|
||||
public static void javaShellCancel() {
|
||||
for (File cacheFile : cacheFolder.listFiles()) {
|
||||
cacheFile.delete();
|
||||
}
|
||||
for (String name : shells.keySet()) {
|
||||
invokeMethod(name, "onDisable");
|
||||
}
|
||||
}
|
||||
|
||||
public static void invokeMethod(String name, String method) {
|
||||
if (shells.containsKey(name)) {
|
||||
Class<?> clazz = shells.get(name);
|
||||
try {
|
||||
Method disableMethod = clazz.getMethod(method);
|
||||
if (disableMethod != null) {
|
||||
disableMethod.invoke(clazz.newInstance());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
//
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void unloadShell(String shell) {
|
||||
invokeMethod(shell, "onDisable");
|
||||
Class<?> clazz = shells.remove(shell);
|
||||
try {
|
||||
if (clazz.newInstance() instanceof Listener) {
|
||||
for (RegisteredListener listener : HandlerList.getRegisteredListeners(Main.getInst())) {
|
||||
if (listener.getListener().getClass().getName().equals(clazz.getName())) {
|
||||
HandlerList.unregisterAll(listener.getListener());
|
||||
}
|
||||
}
|
||||
MsgUtils.send("已为脚本 &f" + shell + " &7注销监听器");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
//
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean reloadShell(String shell) {
|
||||
unloadShell(shell = shell.replace(".java", ""));
|
||||
|
||||
try {
|
||||
Class.forName("com.sun.tools.javac.main.Main");
|
||||
} catch (Exception e) {
|
||||
MsgUtils.warn("&4JavaShell &c工具的必要依赖 &4com.sun.tools.jar &c丢失, 无法载入!");
|
||||
return false;
|
||||
}
|
||||
|
||||
File javaFile = new File(scriptFolder, shell + ".java");
|
||||
if (!javaFile.exists()) {
|
||||
MsgUtils.send("&c脚本 &4" + shell + "&c 不存在");
|
||||
return false;
|
||||
}
|
||||
|
||||
String[] args = {
|
||||
"-nowarn",
|
||||
"-classpath", "." + File.pathSeparator + JavaShell.getPaths(),
|
||||
"-d", cacheFolder.getAbsolutePath() + File.separator,
|
||||
javaFile.getAbsolutePath()
|
||||
};
|
||||
|
||||
int code = new com.sun.tools.javac.main.Main("javac").compile(args).exitCode;
|
||||
if (code == 0) {
|
||||
MsgUtils.send("&f" + shell + "&7 载入成功");
|
||||
try {
|
||||
URL[] urls = {cacheFolder.toURI().toURL()};
|
||||
URLClassLoader sysloader = new URLClassLoader(urls, Main.class.getClassLoader());
|
||||
Class<?> clazz = sysloader.loadClass(shell);
|
||||
shells.put(shell, clazz);
|
||||
sysloader.close();
|
||||
|
||||
invokeMethod(shell, "onEnable");
|
||||
if (clazz.newInstance() instanceof Listener) {
|
||||
Bukkit.getPluginManager().registerEvents((Listener) clazz.newInstance(), Main.getInst());
|
||||
MsgUtils.send("已为脚本 &f" + shell + " &7注册监听器");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
//
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
MsgUtils.send("&4" + shell + "&c 载入失败");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static void loadLibrary() {
|
||||
for (File jar : libFolder.listFiles()) {
|
||||
try {
|
||||
TDependencyLoader.addToPath(Main.getInst(), jar);
|
||||
MsgUtils.send("成功载入 &f" + jar.getName() + " &7到运行库");
|
||||
} catch (Exception e) {
|
||||
//
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
package me.skymc.taboolib.javashell.utils;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
@@ -15,10 +12,6 @@ import java.util.Enumeration;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarFile;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
|
||||
public class JarUtils {
|
||||
|
||||
public static boolean extractFromJar(final String fileName, final String dest) throws IOException {
|
||||
@@ -65,6 +58,7 @@ public class JarUtils {
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static URL getJarUrl(final File file) throws IOException {
|
||||
return new URL("jar:" + file.toURI().toURL().toExternalForm() + "!/");
|
||||
}
|
||||
@@ -88,13 +82,14 @@ public class JarUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static void addClassPath(final URL url) {
|
||||
@Deprecated
|
||||
public static void addClassPath(final URL url) {
|
||||
final URLClassLoader sysloader = (URLClassLoader) Bukkit.class.getClassLoader();
|
||||
final Class<URLClassLoader> sysclass = URLClassLoader.class;
|
||||
try {
|
||||
final Method method = sysclass.getDeclaredMethod("addURL", URL.class);
|
||||
final Method method = sysclass.getDeclaredMethod("addURL", URL.class);
|
||||
method.setAccessible(true);
|
||||
method.invoke(sysloader, url);
|
||||
method.invoke(sysloader, url);
|
||||
} catch (Throwable t) {
|
||||
MsgUtils.warn("无法添加添加 &4" + url + "&c 到运行库");
|
||||
MsgUtils.warn(t.getMessage());
|
||||
|
||||
Reference in New Issue
Block a user