This commit is contained in:
Kaminy
2024-09-27 13:23:43 +08:00
parent b22fb34efc
commit be58798515
3 changed files with 101 additions and 7 deletions

View File

@@ -1,14 +1,30 @@
package org.verdurae.placeholderplus.API.Abstract;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.bukkit.OfflinePlayer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.verdurae.placeholderplus.PlaceholderPlus;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
public abstract class SubJsPlaceholder extends PlaceholderExpansion {
public abstract @NotNull ScriptEngine getEngine();
@Override
public @Nullable("null") String onRequest(OfflinePlayer player, @NotNull String params) {
Invocable inv = (Invocable) getEngine();
try {
return inv.invokeFunction("onRequest", player, params).toString();
} catch (NoSuchMethodException ignored) {
} catch (ScriptException e) {
throw new RuntimeException(e);
}
return null;
}
@Override
public boolean register() {
if (PlaceholderPlus.jsSupport) {

View File

@@ -1,8 +1,24 @@
package org.verdurae.placeholderplus.API;
import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.verdurae.placeholderplus.API.Abstract.SubJsPlaceholder;
import org.verdurae.placeholderplus.Object.PlayerData;
import org.verdurae.placeholderplus.PlaceholderPlus;
import org.verdurae.placeholderplus.ThisPlaceholder;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Map;
import java.util.Objects;
public class PluginAPI {
public static void saveAllPlayerData() {
@@ -10,9 +26,76 @@ public class PluginAPI {
playerData.save();
}
}
public static void loadOnlinePlayerData() {
for (Player player : Bukkit.getOnlinePlayers()) {
PlayerAPI.getPlayerData(player);
}
}
public static void loadAllHolder() {
PlaceholderPlus.expansions.add(new ThisPlaceholder());
File JsFolder = new File(PlaceholderPlus.instance.getDataFolder(), "Js");
JsFolder.mkdirs();
for (File file : Objects.requireNonNull(JsFolder.listFiles())) {
if (file.getName().endsWith(".js")) {
try {
NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
ScriptEngine engine = factory.getScriptEngine();
Map<String, Object> value = PlaceholderPlus.config.getValues(true);
for (String key : value.keySet()) {
if (key.startsWith("JsImportPacket.")) {
engine.put(key.replace("JsImportPacket.", ""), Class.forName(key));
}
}
InputStreamReader reader = new InputStreamReader(Files.newInputStream(file.toPath()), StandardCharsets.UTF_8);
char[] buffer = new char[1024];
int read;
StringBuilder builder = new StringBuilder();
while ((read = reader.read(buffer, 0, buffer.length)) != -1) {
builder.append(buffer, 0, read);
}
String scriptContent = builder.toString();
engine.eval(scriptContent);
String identifier = (String) engine.get("identifier");
String author = (String) engine.get("author");
String version = (String) engine.get("version");
PlaceholderPlus.expansions.add(new SubJsPlaceholder() {
@Override
public @NotNull String getIdentifier() {
return identifier;
}
@Override
public @NotNull String getAuthor() {
return author;
}
@Override
public @NotNull String getVersion() {
return version;
}
@Override
public @NotNull ScriptEngine getEngine() {
return engine;
}
});
} catch (IOException | ScriptException e) {
System.err.println("Error processing file " + file.getAbsolutePath() + ": " + e.getMessage());
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
}
for (PlaceholderExpansion expansion : PlaceholderPlus.expansions) {
if (expansion.canRegister()) expansion.register();
}
}
public static void unloadAllHolder() {
for (PlaceholderExpansion expansion : PlaceholderPlus.expansions) {
expansion.unregister();
}
}
}

View File

@@ -47,10 +47,7 @@ public final class PlaceholderPlus extends JavaPlugin {
logger.info("开始加载");
dataFolder = new File(getDataFolder(), "PlayerData");
dataFolder.mkdirs();
expansions.add(new ThisPlaceholder());
for (PlaceholderExpansion expansion : expansions) {
if (expansion.canRegister()) expansion.register();
}
PluginAPI.loadAllHolder();
PluginAPI.loadOnlinePlayerData();
getCommand("pp").setExecutor(new PlaceholderPlusCommand());
Bukkit.getScheduler().runTaskAsynchronously(this, () -> {
@@ -73,8 +70,6 @@ public final class PlaceholderPlus extends JavaPlugin {
public void onDisable() {
logger.info("插件卸载");
PluginAPI.saveAllPlayerData();
for (PlaceholderExpansion expansion : expansions) {
expansion.unregister();
}
PluginAPI.unloadAllHolder();
}
}