mirror of
https://github.com/Verdurae/PlaceholderPlus.git
synced 2026-02-24 14:45:06 +08:00
修改代码结构
添加了api
This commit is contained in:
@@ -3,7 +3,7 @@ plugins {
|
||||
}
|
||||
|
||||
group = 'org.verdurae'
|
||||
version = '1.1-SNAPSHOT'
|
||||
version = '1.1.2-SNAPSHOT'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
@@ -19,7 +19,7 @@ repositories {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compileOnly "org.spigotmc:spigot-api:1.12.2-R0.1-SNAPSHOT"
|
||||
compileOnly "org.spigotmc:spigot-api:1.20.1-R0.1-SNAPSHOT"
|
||||
implementation 'me.clip:placeholderapi:2.+'
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package org.verdurae.placeholderplus.API;
|
||||
|
||||
import org.verdurae.placeholderplus.Util.PlayerAPI;
|
||||
|
||||
/**
|
||||
* @author Kaminy
|
||||
* @date 2025/11/12 22:39
|
||||
* @since 1.0
|
||||
*/
|
||||
public class PlaceholderPlusApi {
|
||||
/**
|
||||
* 获取玩家占位符对应的数据值
|
||||
*
|
||||
* @param player 玩家名
|
||||
* @param placeholder 占位符名称,格式为"xxx_yyy",不需要带%
|
||||
* @return 对应的玩家插件变量值
|
||||
*/
|
||||
public static String getPlaceholder(String player, String placeholder) {
|
||||
String[] perms = placeholder.split("_");
|
||||
return PlayerAPI.getPlayerData(player).getString(perms[0] + "." + perms[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置玩家数据中的占位符
|
||||
*
|
||||
* @param player 玩家名
|
||||
* @param placeholder 占位符名称,格式为"xxx_yyy",不需要带%
|
||||
* @param value 要设置的值
|
||||
*/
|
||||
public static void setPlaceholder(String player, String placeholder, String value) {
|
||||
String[] perms = placeholder.split("_");
|
||||
PlayerAPI.getPlayerData(player).set(perms[0] + "." + perms[1], value);
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,9 @@ package org.verdurae.placeholderplus.Command;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.verdurae.placeholderplus.API.MathAPI;
|
||||
import org.verdurae.placeholderplus.API.PlayerAPI;
|
||||
import org.verdurae.placeholderplus.API.PluginAPI;
|
||||
import org.verdurae.placeholderplus.Util.MathAPI;
|
||||
import org.verdurae.placeholderplus.Util.PlayerAPI;
|
||||
import org.verdurae.placeholderplus.Util.PluginAPI;
|
||||
import org.verdurae.placeholderplus.PlaceholderPlus;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@@ -34,6 +34,13 @@ public class PlaceholderPlusCommand implements CommandExecutor {
|
||||
} else {
|
||||
String holdername = args[2];
|
||||
String[] perms = holdername.split("_");
|
||||
if (perms.length < 1) {
|
||||
sender.sendMessage("请输入正确的变量名");
|
||||
return false;
|
||||
}
|
||||
if (perms.length < 2) {
|
||||
perms = new String[]{"normal", perms[0]};
|
||||
}
|
||||
switch (args[1]) {
|
||||
case "set":
|
||||
PlayerAPI.getPlayerData(args[0]).set(perms[0] + "." + perms[1], args[3]);
|
||||
|
||||
@@ -2,7 +2,7 @@ package org.verdurae.placeholderplus.Object;
|
||||
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.verdurae.placeholderplus.API.PlayerAPI;
|
||||
import org.verdurae.placeholderplus.Util.PlayerAPI;
|
||||
import org.verdurae.placeholderplus.PlaceholderPlus;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,12 +4,15 @@ import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.verdurae.placeholderplus.API.PlayerAPI;
|
||||
import org.verdurae.placeholderplus.Util.PlayerAPI;
|
||||
|
||||
public class ThisPlaceholder extends PlaceholderExpansion {
|
||||
@Override
|
||||
public @Nullable("null") String onRequest(OfflinePlayer player, @NotNull String param) {
|
||||
String[] params = param.split("_");
|
||||
if (params.length < 2) {
|
||||
params = new String[]{"normal", params[0]};
|
||||
}
|
||||
if (params[0].equals("normal")) {
|
||||
String a = PlayerAPI.getPlayerData(player.getName()).getString("normal." + params[1]);
|
||||
return (a == null) ? PlaceholderPlus.config.getString("Placeholders.normal." + params[1]) : a;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.verdurae.placeholderplus.API;
|
||||
package org.verdurae.placeholderplus.Util;
|
||||
|
||||
public class MathAPI {
|
||||
public static <T extends Number> Number calculate(T number, String formula) {
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.verdurae.placeholderplus.API;
|
||||
package org.verdurae.placeholderplus.Util;
|
||||
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.verdurae.placeholderplus.API;
|
||||
package org.verdurae.placeholderplus.Util;
|
||||
|
||||
import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
|
||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||
@@ -1,5 +1,6 @@
|
||||
name: PlaceholderPlus
|
||||
version: '${version}'
|
||||
api-version: 1.13
|
||||
main: org.verdurae.placeholderplus.PlaceholderPlus
|
||||
author: Kaminy
|
||||
depend:
|
||||
|
||||
Reference in New Issue
Block a user