mirror of
https://github.com/Verdurae/PlaceholderPlus.git
synced 2026-11-25 14:24:32 +08:00
修改代码结构
添加了api
This commit is contained in:
1 parent
11289fa6c6
commit
4d2919cfb3
10 files changed
+101
-15
No files matched your search
@@ -6,12 +6,19 @@ import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
import org.verdurae.placeholderplus.API.PlayerAPI;
|
||||
import org.verdurae.placeholderplus.API.PluginAPI;
|
||||
import org.verdurae.placeholderplus.Util.PlayerAPI;
|
||||
import org.verdurae.placeholderplus.Util.PluginAPI;
|
||||
import org.verdurae.placeholderplus.Command.PlaceholderPlusCommand;
|
||||
import org.verdurae.placeholderplus.Object.PlayerData;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.util.ArrayList;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@@ -25,6 +32,9 @@ public final class PlaceholderPlus extends JavaPlugin {
|
||||
public static boolean autosave;
|
||||
public static BukkitTask timer;
|
||||
public static int timeCount = 0;
|
||||
private static final String DEPENDENCY_URL = "https://repo1.maven.org/maven2/org/openjdk/nashorn/nashorn-core/15.2/nashorn-core-15.2.jar";
|
||||
private static final File LOCAL_JAR = new File("plugins/PlaceholderPlus/lib/nashorn-core.jar");
|
||||
|
||||
|
||||
@Override
|
||||
public void onLoad() {
|
||||
@@ -40,6 +50,8 @@ public final class PlaceholderPlus extends JavaPlugin {
|
||||
logger.warning("你的运行Java中没有JS引擎,无法使用JS变量功能噢");
|
||||
logger.warning("解决办法:安装一个带有nashornJs引擎的插件或模组/使用Java15-的版本/催更我内置一个JS引擎");
|
||||
logger.warning("状态决定类:jdk.nashorn.api.scripting.NashornScriptEngine");
|
||||
logger.warning("准备开始自动下载nashorn....");
|
||||
loadDependencyIfMissing();
|
||||
}
|
||||
saveDefaultConfig();
|
||||
config = getConfig();
|
||||
@@ -67,7 +79,6 @@ public final class PlaceholderPlus extends JavaPlugin {
|
||||
public static void newTimer() {
|
||||
timer = Bukkit.getScheduler().runTaskTimerAsynchronously(PlaceholderPlus.instance, () -> {
|
||||
timeCount++;
|
||||
System.out.println(timeCount);
|
||||
if (PlaceholderPlus.config.getBoolean("Task.SaveTask.enable") && timeCount % PlaceholderPlus.config.getInt("Task.SaveTask.period") == 0) {
|
||||
logger.info("正在自动保存数据");
|
||||
for (PlayerData playerData : PlayerAPI.playerData.values()) {
|
||||
@@ -76,8 +87,6 @@ public final class PlaceholderPlus extends JavaPlugin {
|
||||
}
|
||||
ConfigurationSection updates = PlaceholderPlus.config.getConfigurationSection("Placeholders.update");
|
||||
for (String update : updates.getKeys(false)) {
|
||||
System.out.println(update);
|
||||
System.out.println(PlaceholderPlus.config.getInt("Placeholders.update." + update + ".period"));
|
||||
if (timeCount % PlaceholderPlus.config.getInt("Placeholders.update." + update + ".period") == 0) {
|
||||
for (PlayerData playerData : PlayerAPI.playerData.values()) {
|
||||
if (playerData.data.getInt("update." + update) < PlaceholderPlus.config.getInt("Placeholders.update." + update + ".max")) {
|
||||
@@ -89,4 +98,36 @@ public final class PlaceholderPlus extends JavaPlugin {
|
||||
}
|
||||
}, 1, 20);
|
||||
}
|
||||
|
||||
public static void loadDependencyIfMissing() {
|
||||
if (!LOCAL_JAR.exists()) {
|
||||
LOCAL_JAR.getParentFile().mkdirs();
|
||||
downloadFile(DEPENDENCY_URL, LOCAL_JAR);
|
||||
}
|
||||
addJarToClasspath(LOCAL_JAR.getAbsolutePath());
|
||||
}
|
||||
|
||||
private static void downloadFile(String urlStr, File destination) {
|
||||
try (InputStream in = new URL(urlStr).openStream()) {
|
||||
ReadableByteChannel rbc = Channels.newChannel(in);
|
||||
try (FileOutputStream fos = new FileOutputStream(destination)) {
|
||||
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static void addJarToClasspath(String jarPath) {
|
||||
try {
|
||||
URL url = new File(jarPath).toURI().toURL();
|
||||
URLClassLoader loader = (URLClassLoader) ClassLoader.getSystemClassLoader();
|
||||
Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
|
||||
method.setAccessible(true);
|
||||
method.invoke(loader, url);
|
||||
System.out.println("成功将依赖添加到类路径: " + jarPath);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user