From 608a6ef289a42f72e0a451e101ad2a2aabebfacb Mon Sep 17 00:00:00 2001 From: MiniDay <372403923@qq.com> Date: Sun, 28 Jul 2024 12:10:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=BC=BA=E9=83=A8=E5=88=86API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/bukkit/command/ParentCommand.java | 10 +-- .../core/common/config/ConfigSection.java | 51 +++++++++--- .../plugin/core/common/config/YamlConfig.java | 82 ++++++++++++++++--- .../plugin/core/common/util/SimpleLogger.java | 8 +- 4 files changed, 119 insertions(+), 32 deletions(-) diff --git a/core-bukkit/src/main/java/cn/hamster3/mc/plugin/core/bukkit/command/ParentCommand.java b/core-bukkit/src/main/java/cn/hamster3/mc/plugin/core/bukkit/command/ParentCommand.java index 5764792..833b575 100644 --- a/core-bukkit/src/main/java/cn/hamster3/mc/plugin/core/bukkit/command/ParentCommand.java +++ b/core-bukkit/src/main/java/cn/hamster3/mc/plugin/core/bukkit/command/ParentCommand.java @@ -94,7 +94,7 @@ public abstract class ParentCommand extends ChildCommand { @NotNull public Map getCommandHelp(CommandSender sender) { - HashMap map = new HashMap<>(); + Map map = new LinkedHashMap<>(); for (ChildCommand child : childCommands) { if (!child.hasPermission(sender)) { continue; @@ -111,14 +111,12 @@ public abstract class ParentCommand extends ChildCommand { public void sendHelp(@NotNull CommandSender sender) { sender.sendMessage("§e==================== [ " + getName() + " 使用帮助] ===================="); - Map helpMap = getCommandHelp(sender); - int maxLength = helpMap.keySet().stream() + Map map = getCommandHelp(sender); + int maxLength = map.keySet().stream() .map(String::length) .max(Integer::compareTo) .orElse(-1); - ArrayList> list = new ArrayList<>(helpMap.entrySet()); - list.sort(Map.Entry.comparingByKey()); - for (Map.Entry entry : list) { + for (Map.Entry entry : map.entrySet()) { sender.sendMessage(String.format("§a%-" + maxLength + "s - %s", entry.getKey(), entry.getValue())); } } diff --git a/core-common/src/main/java/cn/hamster3/mc/plugin/core/common/config/ConfigSection.java b/core-common/src/main/java/cn/hamster3/mc/plugin/core/common/config/ConfigSection.java index c4a4ea1..5c94ac8 100644 --- a/core-common/src/main/java/cn/hamster3/mc/plugin/core/common/config/ConfigSection.java +++ b/core-common/src/main/java/cn/hamster3/mc/plugin/core/common/config/ConfigSection.java @@ -5,6 +5,8 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.*; +import java.util.function.BiFunction; +import java.util.function.Function; @SuppressWarnings({"unused", "unchecked"}) @Getter @@ -71,16 +73,6 @@ public class ConfigSection { return o.toString(); } - public List getStringList(@NotNull String key) { - Object o = map.get(key); - if (o instanceof List) { - return (List) o; - } - ArrayList list = new ArrayList<>(); - map.put(key, list); - return list; - } - public boolean getBoolean(@NotNull String key) { String string = getString(key); if (string == null) { @@ -161,6 +153,45 @@ public class ConfigSection { return Long.parseLong(string); } + public List getList(@NotNull String key, Function function) { + ArrayList list = new ArrayList<>(); + Object object = map.get(key); + if (object instanceof List) { + List mapList = (List) object; + for (Object o : mapList) { + T result = function.apply(o); + list.add(result); + } + } + return list; + } + + public List getStringList(@NotNull String key) { + return getList(key, Object::toString); + } + + public List getIntegerList(@NotNull String key) { + return getList(key, o -> Integer.parseInt(o.toString())); + } + + public List toList(BiFunction function) { + ArrayList list = new ArrayList<>(); + for (String key : getKeys()) { + T result = function.apply(this, key); + list.add(result); + } + return list; + } + + public Map toMap(BiFunction function) { + HashMap map = new HashMap<>(); + for (String key : getKeys()) { + T result = function.apply(this, key); + map.put(key, result); + } + return map; + } + public void set(@NotNull String key, @Nullable Object value) { if (value == null) { map.remove(key); diff --git a/core-common/src/main/java/cn/hamster3/mc/plugin/core/common/config/YamlConfig.java b/core-common/src/main/java/cn/hamster3/mc/plugin/core/common/config/YamlConfig.java index a8724ab..c658ac8 100644 --- a/core-common/src/main/java/cn/hamster3/mc/plugin/core/common/config/YamlConfig.java +++ b/core-common/src/main/java/cn/hamster3/mc/plugin/core/common/config/YamlConfig.java @@ -5,9 +5,12 @@ import org.yaml.snakeyaml.Yaml; import java.io.*; import java.nio.charset.StandardCharsets; -import java.util.Map; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.*; +import java.util.function.Consumer; -@SuppressWarnings({"unused", "VulnerableCodeUsages"}) +@SuppressWarnings("unused") public class YamlConfig extends ConfigSection { public static final Yaml YAML_LOADER = new Yaml(); @@ -19,11 +22,56 @@ public class YamlConfig extends ConfigSection { super(map); } + public static List loadFolder(@NotNull File file) throws IOException { + if (file.isDirectory()) { + ArrayList list = new ArrayList<>(); + for (File subFile : Objects.requireNonNull(file.listFiles())) { + list.addAll(loadFolder(subFile)); + } + return list; + } + if (file.isFile() && file.getName().endsWith(".yml")) { + return Collections.singletonList(load(file)); + } + return Collections.emptyList(); + } + + public static List loadFolder(@NotNull File file, @NotNull Consumer exceptionHandler) { + if (file.isDirectory()) { + ArrayList list = new ArrayList<>(); + for (File subFile : Objects.requireNonNull(file.listFiles())) { + list.addAll(loadFolder(subFile, exceptionHandler)); + } + return list; + } + if (file.isFile() && file.getName().endsWith(".yml")) { + try { + YamlConfig load = load(file); + return Collections.singletonList(load); + } catch (IOException e) { + exceptionHandler.accept(e); + } + } + return Collections.emptyList(); + } + + public static YamlConfig load(@NotNull Path path) { + try { + return load(Files.newInputStream(path)); + } catch (IOException e) { + return new YamlConfig(); + } + } + public static YamlConfig load(@NotNull File file) throws IOException { try (FileInputStream stream = new FileInputStream(file)) { - try (InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) { - return load(reader); - } + return load(stream); + } + } + + public static YamlConfig load(@NotNull InputStream stream) throws IOException { + try (InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) { + return load(reader); } } @@ -32,11 +80,25 @@ public class YamlConfig extends ConfigSection { return new YamlConfig(load); } - public void save(@NotNull File file) throws IOException { - try (FileOutputStream stream = new FileOutputStream(file)) { - try (OutputStreamWriter writer = new OutputStreamWriter(stream, StandardCharsets.UTF_8)) { - YAML_LOADER.dump(map, writer); - } + public void save(@NotNull Path path) throws IOException { + try (OutputStream stream = Files.newOutputStream(path)) { + save(stream); } } + + public void save(@NotNull File file) throws IOException { + try (FileOutputStream stream = new FileOutputStream(file)) { + save(stream); + } + } + + public void save(@NotNull OutputStream stream) throws IOException { + try (OutputStreamWriter writer = new OutputStreamWriter(stream, StandardCharsets.UTF_8)) { + save(writer); + } + } + + public void save(@NotNull Writer writer) { + YAML_LOADER.dump(map, writer); + } } diff --git a/core-common/src/main/java/cn/hamster3/mc/plugin/core/common/util/SimpleLogger.java b/core-common/src/main/java/cn/hamster3/mc/plugin/core/common/util/SimpleLogger.java index ebf844a..ea743c5 100644 --- a/core-common/src/main/java/cn/hamster3/mc/plugin/core/common/util/SimpleLogger.java +++ b/core-common/src/main/java/cn/hamster3/mc/plugin/core/common/util/SimpleLogger.java @@ -19,9 +19,7 @@ public abstract class SimpleLogger { try { log(level, String.format(msg, args)); } catch (Exception e) { - log(Level.WARNING, "输出日志 %s 时遇到一个异常", msg); - log(Level.WARNING, "日志参数: %s", Arrays.toString(args)); - log(Level.WARNING, e, "异常信息: "); + log(Level.WARNING, "输出日志 " + msg + Arrays.toString(args) + " 时遇到一个异常", e); } } @@ -29,10 +27,8 @@ public abstract class SimpleLogger { try { log(level, String.format(msg, args), throwable); } catch (Exception e) { - log(Level.WARNING, "输出日志 " + msg + " 时遇到一个异常"); - log(Level.WARNING, "日志参数: " + Arrays.toString(args)); + log(Level.WARNING, "输出日志 " + msg + Arrays.toString(args) + " 时遇到一个异常", e); log(Level.WARNING, "异常参数: ", throwable); - log(Level.WARNING, "异常信息: ", e); } }