diff --git a/pom.xml b/pom.xml index 72c5fd5..95a4cf1 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ cn.lingsmc LingsHTTPUtils - 1.2.4 + 1.2.5 jar LingsHTTPUtils @@ -64,10 +64,6 @@ spigotmc-repo https://hub.spigotmc.org/nexus/content/repositories/snapshots/ - - sonatype - https://oss.sonatype.org/content/groups/public/ - placeholderapi https://repo.extendedclip.com/content/repositories/placeholderapi/ diff --git a/src/main/java/cn/lingsmc/lingshttputils/LingsHttpUtils.java b/src/main/java/cn/lingsmc/lingshttputils/LingsHttpUtils.java index c94f556..62c5246 100644 --- a/src/main/java/cn/lingsmc/lingshttputils/LingsHttpUtils.java +++ b/src/main/java/cn/lingsmc/lingshttputils/LingsHttpUtils.java @@ -39,8 +39,10 @@ // TODO 使间隔适用于intime类 // TODO 支持脚本化操作 // TODO 支持模块分文件 +// TODO 添加多语言支持 public final class LingsHttpUtils extends JavaPlugin { + public static FileConfiguration config; @Getter private static LingsHttpUtils instance; diff --git a/src/main/java/cn/lingsmc/lingshttputils/asyncworkers/AsyncWorkers.java b/src/main/java/cn/lingsmc/lingshttputils/asyncworkers/AsyncWorkers.java index a04302f..e24be6d 100644 --- a/src/main/java/cn/lingsmc/lingshttputils/asyncworkers/AsyncWorkers.java +++ b/src/main/java/cn/lingsmc/lingshttputils/asyncworkers/AsyncWorkers.java @@ -3,9 +3,7 @@ import cn.lingsmc.lingshttputils.LingsHttpUtils; import cn.lingsmc.lingshttputils.utils.HttpUtils; import cn.lingsmc.lingshttputils.utils.JsonUtils; -import org.bukkit.Bukkit; import org.bukkit.plugin.Plugin; -import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scheduler.BukkitRunnable; import java.util.Objects; @@ -19,32 +17,31 @@ */ public class AsyncWorkers { static Plugin plugin = LingsHttpUtils.getInstance(); - Runnable task; - public void asyncworker(String module, int reqTime, String url, String method, int refInterval, String[] keys) { - task = new BukkitRunnable() { + public void runAsyncWorker(String module, int reqTime, String url, String method, int refInterval, String[] keys) { + if (refInterval < 0) { + plugin.getLogger().log(Level.SEVERE, "Worker: {0} 出现错误!模块请求间隔必须为正值!", module); + return; + } + new BukkitRunnable() { @Override public void run() { - String res = HttpUtils.request(url, reqTime, method); - if (res == null) { + // plugin.getLogger().info(String.format("%s进行了一次请求!",module)); + String res = HttpUtils.httpRequest(url, reqTime, method); + if (Objects.isNull(res)) { return; } if ("json".equalsIgnoreCase(LingsHttpUtils.config.getString(String.format("%s.mode", module)))) { res = JsonUtils.getValue(res, keys); if (Objects.isNull(res)) { - plugin.getLogger().log(Level.WARNING, "Worker: {0} 获取Json内容时出现错误! 请检查是否符合格式要求!", module); + plugin.getLogger().log(Level.SEVERE, "Worker: {0} 获取Json内容时出现错误! 请检查是否符合格式要求!", module); } } - LingsHttpUtils.getInstance().getHttpData().put(module, res); try { - Thread.sleep(refInterval); - } catch (InterruptedException e) { - plugin.getLogger().log(Level.WARNING, "Worker: {0} 出现错误!模块请求间隔必须为正值!", module); - e.printStackTrace(); + LingsHttpUtils.getInstance().getHttpData().put(module, res); + } catch (Exception ignored) { } - Bukkit.getScheduler().runTaskAsynchronously(JavaPlugin.getPlugin(LingsHttpUtils.class), task); } - }; - Bukkit.getScheduler().runTaskAsynchronously(JavaPlugin.getPlugin(LingsHttpUtils.class), task); + }.runTaskTimerAsynchronously(plugin, 0, (long) refInterval / 1000 * 20); } } diff --git a/src/main/java/cn/lingsmc/lingshttputils/asyncworkers/WorkerOptions.java b/src/main/java/cn/lingsmc/lingshttputils/asyncworkers/WorkerOptions.java index 450af47..0f45326 100644 --- a/src/main/java/cn/lingsmc/lingshttputils/asyncworkers/WorkerOptions.java +++ b/src/main/java/cn/lingsmc/lingshttputils/asyncworkers/WorkerOptions.java @@ -2,11 +2,11 @@ import cn.lingsmc.lingshttputils.LingsHttpUtils; import lombok.Getter; +import org.bukkit.Bukkit; import org.bukkit.configuration.file.FileConfiguration; import java.util.Objects; import java.util.Set; -import java.util.concurrent.*; import java.util.logging.Level; /** @@ -16,8 +16,7 @@ */ public class WorkerOptions { @Getter - static boolean started = true; - static ExecutorService threadPool; + static boolean started = false; static LingsHttpUtils plugin = LingsHttpUtils.getInstance(); @@ -26,52 +25,45 @@ private WorkerOptions() { } public static void runWorkers() { - started = true; - FileConfiguration config = LingsHttpUtils.config; - Set modules = config.getKeys(false); - modules.remove("version"); - modules.removeIf(module -> !Objects.equals(config.getString(String.format("%s.reqMode", module)), "Cycle")); - modules.removeIf(module -> Objects.equals(config.getBoolean(String.format("%s.enabled", module)), false)); - if (modules.isEmpty()) { - return; - } - - threadPool = new ThreadPoolExecutor(1, modules.size(), - 1L, TimeUnit.SECONDS, - new LinkedBlockingQueue<>(Integer.MAX_VALUE), - Executors.defaultThreadFactory(), - new ThreadPoolExecutor.AbortPolicy()); - - for (String module : modules) { - // 一定要为每个module设置一个worker - AsyncWorkers asyncWorkers = new AsyncWorkers(); - final int reqTime = config.getInt(String.format("%s.reqTime", module)); - final String url = config.getString(String.format("%s.url", module)); - final String method; - final int refInterval = config.getInt(String.format("%s.refInterval", module)); - if (Objects.nonNull(config.getString(String.format("%s.method", module)))) { - method = config.getString(String.format("%s.method", module)); - } else { - method = "GET"; + if (!started) { + started = true; + FileConfiguration config = LingsHttpUtils.config; + Set modules = config.getKeys(false); + modules.remove("version"); + modules.removeIf(module -> !Objects.equals(config.getString(String.format("%s.reqMode", module)), "Cycle")); + modules.removeIf(module -> Objects.equals(config.getBoolean(String.format("%s.enabled", module)), false)); + if (modules.isEmpty()) { + return; } - String[] keys = new String[0]; - if ("json".equalsIgnoreCase(config.getString(String.format("%s.mode", module)))) { - keys = config.getString(String.format("%s.key", module)).split("\\."); + + for (String module : modules) { + // 一定要为每个module设置一个worker + AsyncWorkers asyncWorkers = new AsyncWorkers(); + final int reqTime = config.getInt(String.format("%s.reqTime", module)); + final String url = config.getString(String.format("%s.url", module)); + final String method; + final int refInterval = config.getInt(String.format("%s.refInterval", module)); + if (Objects.nonNull(config.getString(String.format("%s.method", module)))) { + method = config.getString(String.format("%s.method", module)); + } else { + method = "GET"; + } + String[] keys = new String[0]; + if ("json".equalsIgnoreCase(config.getString(String.format("%s.mode", module)))) { + keys = config.getString(String.format("%s.key", module)).split("\\."); + } + String[] finalKeys = keys; + LingsHttpUtils.getInstance().getLogger().log(Level.INFO, "尝试启动{0}...", module); + asyncWorkers.runAsyncWorker(module, reqTime, url, method, refInterval, finalKeys); } - String[] finalKeys = keys; - Runnable runnable = () -> asyncWorkers.asyncworker(module, reqTime, url, method, refInterval, finalKeys); - LingsHttpUtils.getInstance().getLogger().log(Level.INFO, "尝试启动{0}...", module); - threadPool.execute(runnable); } } public static void stopWorkers() { - plugin.getLogger().info("尝试关闭Cycle Workers..."); - try { - threadPool.shutdownNow(); - } catch (Exception ignored) { - plugin.getLogger().info("没有Cycle Workers被关闭,因为没有已启用的Cycle Workers"); + if (started) { + plugin.getLogger().info("尝试关闭Cycle Workers..."); + Bukkit.getScheduler().cancelTasks(plugin); + started = false; } - started = false; } } \ No newline at end of file diff --git a/src/main/java/cn/lingsmc/lingshttputils/commands/Commands.java b/src/main/java/cn/lingsmc/lingshttputils/commands/Commands.java index cb6bdae..b7a29d6 100644 --- a/src/main/java/cn/lingsmc/lingshttputils/commands/Commands.java +++ b/src/main/java/cn/lingsmc/lingshttputils/commands/Commands.java @@ -17,6 +17,9 @@ public class Commands implements CommandExecutor { static String rootMessage1 = String.format("%s此服务器正在运行 %s%s %s%s by %s", ChatColor.DARK_AQUA, ChatColor.AQUA, LingsHttpUtils.getInstance().getName(), LingsHttpUtils.getInstance().getDescription().getVersion(), ChatColor.DARK_AQUA, "§aC§br§cs§du§eh§a2§be§cr§d0"); static String rootMessage2 = String.format("%s命令列表: %s/lhu help", ChatColor.DARK_AQUA, ChatColor.AQUA); static String permission = "lingshttputils.admin"; + static String notEnoughArgs = String.format("%s参数错误! 正确用法:", ChatColor.RED); + static String requestMessage = String.format("%s/lhu request %s请求配置文件中某模块的数据", ChatColor.AQUA, ChatColor.GREEN); + static String workersMessage = String.format("%s/lhu workers %s启动/关闭Cycle Workers", ChatColor.AQUA, ChatColor.GREEN); @Override public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String @NotNull [] args) { @@ -27,13 +30,31 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command } switch (args[0]) { case "reload": - ReloadCommand.reloadCommand(sender); + try { + ReloadCommand.reloadCommand(sender); + } catch (Exception e) { + e.printStackTrace(); + } break; case "help": HelpCommand.helpCommand(sender); break; case "workers": - WorkerOptionCommands.workerOptionCommands(sender, args); + // 检查参数是否充足 + if (args.length < 2) { + sender.sendMessage(notEnoughArgs); + sender.sendMessage(workersMessage); + } else { + WorkerOptionCommands.workerOptionCommands(sender, args); + } + break; + case "request": + if (args.length == 2) { + RequestCommand.request(args[1], sender); + } else { + sender.sendMessage(notEnoughArgs); + sender.sendMessage(requestMessage); + } break; default: sender.sendMessage(helpMessage); diff --git a/src/main/java/cn/lingsmc/lingshttputils/commands/HelpCommand.java b/src/main/java/cn/lingsmc/lingshttputils/commands/HelpCommand.java index eebf45a..fc1da46 100644 --- a/src/main/java/cn/lingsmc/lingshttputils/commands/HelpCommand.java +++ b/src/main/java/cn/lingsmc/lingshttputils/commands/HelpCommand.java @@ -16,6 +16,7 @@ private HelpCommand() { public static void helpCommand(@NotNull CommandSender sender) { sender.sendMessage(String.format("%s%s----- LingsHttpUtils指令 -----", ChatColor.DARK_AQUA, ChatColor.BOLD)); sender.sendMessage(String.format("%s/lhu workers %s启动/关闭Cycle Workers", ChatColor.AQUA, ChatColor.GREEN)); + sender.sendMessage(String.format("%s/lhu request %s请求配置文件中某模块的数据", ChatColor.AQUA, ChatColor.GREEN)); sender.sendMessage(String.format("%s/lhu reload %s重载插件", ChatColor.AQUA, ChatColor.GREEN)); } } diff --git a/src/main/java/cn/lingsmc/lingshttputils/commands/ReloadCommand.java b/src/main/java/cn/lingsmc/lingshttputils/commands/ReloadCommand.java index 61b28a1..4ccd5a5 100644 --- a/src/main/java/cn/lingsmc/lingshttputils/commands/ReloadCommand.java +++ b/src/main/java/cn/lingsmc/lingshttputils/commands/ReloadCommand.java @@ -24,12 +24,14 @@ public static void reloadCommand(@NotNull CommandSender sender) { sender.sendMessage(String.format("%s你没有执行此命令的权限.", ChatColor.RED)); return; } + sender.sendMessage(String.format("%s正在重载中...", ChatColor.AQUA)); WorkerOptions.stopWorkers(); File file = new File(plugin.getDataFolder(), "config.yml"); if (!file.exists()) { plugin.saveDefaultConfig(); } plugin.reloadConfig(); + LingsHttpUtils.config = plugin.getConfig(); WorkerOptions.runWorkers(); } } diff --git a/src/main/java/cn/lingsmc/lingshttputils/commands/RequestCommand.java b/src/main/java/cn/lingsmc/lingshttputils/commands/RequestCommand.java new file mode 100644 index 0000000..1e00cf1 --- /dev/null +++ b/src/main/java/cn/lingsmc/lingshttputils/commands/RequestCommand.java @@ -0,0 +1,72 @@ +package cn.lingsmc.lingshttputils.commands; + +import cn.lingsmc.lingshttputils.LingsHttpUtils; +import cn.lingsmc.lingshttputils.utils.HttpUtils; +import cn.lingsmc.lingshttputils.utils.JsonUtils; +import cn.lingsmc.lingshttputils.utils.MessageUtils; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.scheduler.BukkitRunnable; +import org.jetbrains.annotations.NotNull; + +import java.util.Objects; +import java.util.Set; +import java.util.logging.Level; + +/** + * @author Crsuh2er0 + * @apiNote + * @since 2022/10/20 + */ +public class RequestCommand { + private static final FileConfiguration CONFIG = LingsHttpUtils.getInstance().getConfig(); + static LingsHttpUtils plugin = LingsHttpUtils.getInstance(); + + private RequestCommand() { + } + + public static void request(String module, @NotNull CommandSender sender) { + if (!sender.hasPermission(Commands.permission)) { + sender.sendMessage(String.format("%s你没有执行此命令的权限.", ChatColor.RED)); + return; + } + // 判断是否有此模块 + Set modules = CONFIG.getKeys(false); + if (!modules.contains(module)) { + MessageUtils.sendMessage(sender, String.format("%s错误:未找到此模块. 请检查拼写以及是否已重载配置文件.", ChatColor.RED)); + return; + } + // 不管是循环还是及时都直接请求一次 + new BukkitRunnable() { + @Override + public void run() { + int reqTime = CONFIG.getInt(String.format("%s.reqTime", module)); + String url = CONFIG.getString(String.format("%s.url", module)); + String method; + try { + method = CONFIG.getString(String.format("%s.method", module)); + } catch (Exception ignored) { + method = "GET"; + } + String res = HttpUtils.httpRequest(url, reqTime, method); + if (Objects.isNull(res)) { + return; + } + if ("json".equalsIgnoreCase(LingsHttpUtils.config.getString(String.format("%s.mode", module)))) { + String[] keys = CONFIG.getString(String.format("%s.key", module)).split("\\."); + res = JsonUtils.getValue(res, keys); + if (Objects.isNull(res)) { + plugin.getLogger().log(Level.SEVERE, "Worker: {0} 获取Json内容时出现错误! 请检查是否符合格式要求!", module); + } + } + // 判断结果是否为null或空值 + if (Objects.isNull(res) || res.isEmpty()) { + MessageUtils.sendMessage(sender, String.format("%s%s %s获取到的结果为 %snull", ChatColor.YELLOW, module, ChatColor.GREEN, ChatColor.GOLD)); + } else { + MessageUtils.sendMessage(sender, String.format("%s%s %s获取到的结果为 %s%s", ChatColor.YELLOW, module, ChatColor.GREEN, ChatColor.GOLD, res)); + } + } + }.runTaskAsynchronously(plugin); + } +} diff --git a/src/main/java/cn/lingsmc/lingshttputils/commands/TabComplete.java b/src/main/java/cn/lingsmc/lingshttputils/commands/TabComplete.java index 7d87123..4b5f616 100644 --- a/src/main/java/cn/lingsmc/lingshttputils/commands/TabComplete.java +++ b/src/main/java/cn/lingsmc/lingshttputils/commands/TabComplete.java @@ -1,12 +1,15 @@ package cn.lingsmc.lingshttputils.commands; +import cn.lingsmc.lingshttputils.LingsHttpUtils; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; +import org.bukkit.configuration.file.FileConfiguration; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Set; /** * @author Crsuh2er0 @@ -17,7 +20,7 @@ public class TabComplete implements org.bukkit.command.TabCompleter { @Override public List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String @NotNull [] args) { - final String[] root = new String[]{"workers", "help", "reload"}; + final String[] root = new String[]{"workers", "help", "reload", "request"}; final String[] worker = new String[]{"start", "stop"}; if (args.length == 1) { @@ -30,16 +33,29 @@ public List onTabComplete(@NotNull CommandSender sender, @NotNull Comman return res; } - if (args.length == 2 && root[0].equalsIgnoreCase(args[0])) { - List res = new ArrayList<>(); - for (String val : worker) { - if (val.contains(args[1])) { - res.add(val); + if (args.length == 2) { + if (root[0].equalsIgnoreCase(args[0])) { + List res = new ArrayList<>(); + for (String val : worker) { + if (val.contains(args[1])) { + res.add(val); + } } + return res; + } + if (root[3].equalsIgnoreCase(args[0])) { + List res = new ArrayList<>(); + FileConfiguration config = LingsHttpUtils.config; + Set modules = config.getKeys(false); + modules.remove("version"); + for (String module : modules) { + if (module.contains(args[1])) { + res.add(module); + } + } + return res; } - return res; } - return Collections.emptyList(); } } diff --git a/src/main/java/cn/lingsmc/lingshttputils/commands/WorkerOptionCommands.java b/src/main/java/cn/lingsmc/lingshttputils/commands/WorkerOptionCommands.java index f3f9f41..2ddce66 100644 --- a/src/main/java/cn/lingsmc/lingshttputils/commands/WorkerOptionCommands.java +++ b/src/main/java/cn/lingsmc/lingshttputils/commands/WorkerOptionCommands.java @@ -18,10 +18,7 @@ private WorkerOptionCommands() { } public static void workerOptionCommands(@NotNull CommandSender sender, String @NotNull [] args) { - if (args.length < 2) { - sender.sendMessage(helpMessage); - return; - } + if (!sender.hasPermission(Commands.permission)) { sender.sendMessage(String.format("%s你没有执行此命令的权限.", ChatColor.RED)); return; diff --git a/src/main/java/cn/lingsmc/lingshttputils/placeholderapi/PlaceholderAPI.java b/src/main/java/cn/lingsmc/lingshttputils/placeholderapi/PlaceholderAPI.java index 0d771c9..fb48afe 100644 --- a/src/main/java/cn/lingsmc/lingshttputils/placeholderapi/PlaceholderAPI.java +++ b/src/main/java/cn/lingsmc/lingshttputils/placeholderapi/PlaceholderAPI.java @@ -1,8 +1,6 @@ package cn.lingsmc.lingshttputils.placeholderapi; import cn.lingsmc.lingshttputils.LingsHttpUtils; -import cn.lingsmc.lingshttputils.utils.HttpUtils; -import cn.lingsmc.lingshttputils.utils.JsonUtils; import me.clip.placeholderapi.expansion.PlaceholderExpansion; import org.bukkit.OfflinePlayer; import org.bukkit.configuration.file.FileConfiguration; @@ -10,7 +8,8 @@ import java.util.Objects; import java.util.Set; -import java.util.logging.Level; + +import static cn.lingsmc.lingshttputils.utils.RequestUtils.request; /** * @author Crsuh2er0 @@ -59,26 +58,7 @@ public String onRequest(OfflinePlayer p, @NotNull String params) { if (Objects.equals(params, config.getString(String.format("%s.apiname", module)))) { if ("inTime".equalsIgnoreCase(config.getString(String.format("%s.reqMode", module)))) { // inTime - int reqTime = config.getInt(String.format("%s.reqTime", module)); - String url = config.getString(String.format("%s.url", module)); - String method; - try { - method = config.getString(String.format("%s.method", module)); - } catch (Exception ignored) { - method = "GET"; - } - String res = HttpUtils.request(url, reqTime, method); - if (res == null) { - return ""; - } - if ("json".equalsIgnoreCase(config.getString(String.format("%s.mode", module)))) { - String[] keys = config.getString(String.format("%s.key", module)).split("\\."); - res = JsonUtils.getValue(res, keys); - if (Objects.isNull(res)) { - plugin.getLogger().log(Level.WARNING, "Worker: {0} 获取Json内容时出现错误! 请检查是否符合格式要求!", module); - } - } - return res; + return request(module, config, plugin); } else { // Cycle return this.plugin.getHttpData().get(module); diff --git a/src/main/java/cn/lingsmc/lingshttputils/utils/HttpUtils.java b/src/main/java/cn/lingsmc/lingshttputils/utils/HttpUtils.java index 79c70f5..b767afc 100644 --- a/src/main/java/cn/lingsmc/lingshttputils/utils/HttpUtils.java +++ b/src/main/java/cn/lingsmc/lingshttputils/utils/HttpUtils.java @@ -11,6 +11,7 @@ import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.Objects; +import java.util.logging.Level; /** * @author Crsuh2er0 @@ -18,10 +19,12 @@ * @apiNote */ public class HttpUtils { + static LingsHttpUtils plugin = LingsHttpUtils.getInstance(); + private HttpUtils() { } - public static @Nullable String request(String httpUrl, int reqTime, String method) { + public static @Nullable String httpRequest(String httpUrl, int reqTime, String method) { HttpURLConnection connection = null; InputStream is = null; BufferedReader br = null; @@ -43,7 +46,8 @@ private HttpUtils() { } } } catch (IOException e) { - LingsHttpUtils.getInstance().getLogger().info("请求失败!"); + plugin.getLogger().log(Level.SEVERE, "请求失败!"); + plugin.getLogger().log(Level.SEVERE, e.toString()); return null; } finally { if (br != null) { diff --git a/src/main/java/cn/lingsmc/lingshttputils/utils/JsonUtils.java b/src/main/java/cn/lingsmc/lingshttputils/utils/JsonUtils.java index a37bf29..c5566d0 100644 --- a/src/main/java/cn/lingsmc/lingshttputils/utils/JsonUtils.java +++ b/src/main/java/cn/lingsmc/lingshttputils/utils/JsonUtils.java @@ -21,12 +21,16 @@ public class JsonUtils { private JsonUtils() { } - public static String getValue(String jsonString, String @NotNull [] keys) { - if (plugin.gson) { - return getValueGson(jsonString, keys); - } else { - return getValueJsonSimple(jsonString, keys); + public static @Nullable String getValue(String jsonString, String @NotNull [] keys) { + try { + if (plugin.gson) { + return getValueGson(jsonString, keys); + } else { + return getValueJsonSimple(jsonString, keys); + } + } catch (Exception ignored) { } + return null; } /** diff --git a/src/main/java/cn/lingsmc/lingshttputils/utils/RequestUtils.java b/src/main/java/cn/lingsmc/lingshttputils/utils/RequestUtils.java new file mode 100644 index 0000000..ddf6e41 --- /dev/null +++ b/src/main/java/cn/lingsmc/lingshttputils/utils/RequestUtils.java @@ -0,0 +1,43 @@ +package cn.lingsmc.lingshttputils.utils; + +import cn.lingsmc.lingshttputils.LingsHttpUtils; +import org.bukkit.configuration.file.FileConfiguration; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Objects; +import java.util.logging.Level; + +/** + * @author Crsuh2er0 + * @apiNote + * @since 2022/10/27 + */ +public class RequestUtils { + private RequestUtils() { + } + + public static @Nullable String request(String module, @NotNull FileConfiguration config, @NotNull LingsHttpUtils plugin) { + int reqTime = config.getInt(String.format("%s.reqTime", module)); + String url = config.getString(String.format("%s.url", module)); + String method; + try { + method = config.getString(String.format("%s.method", module)); + } catch (Exception ignored) { + method = "GET"; + } + String res = HttpUtils.httpRequest(url, reqTime, method); + if (Objects.isNull(res)) { + return ""; + } + if ("json".equalsIgnoreCase(config.getString(String.format("%s.mode", module)))) { + String[] keys = config.getString(String.format("%s.key", module)).split("\\."); + res = JsonUtils.getValue(res, keys); + if (Objects.isNull(res)) { + plugin.getLogger().log(Level.WARNING, "Worker: {0} 获取Json内容时出现错误! 请检查是否符合格式要求!", module); + return null; + } + } + return res; + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 7e35496..2de83cb 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -29,7 +29,8 @@ bilibili_followers: # 仅当reqMode: Cycle时可用 reqTime: 10000 # - # refInterval 请求间隔时间 + # refInterval 请求间隔时间(ms) + # 更新: 实际计算时以tick数为准 此处直接填毫秒 refInterval: 30000 # # method 请求方法 不指定则使用GET