feat: 增强部分API

This commit is contained in:
2024-07-28 12:10:48 +08:00
parent a75853187c
commit 608a6ef289
4 changed files with 119 additions and 32 deletions

View File

@@ -94,7 +94,7 @@ public abstract class ParentCommand extends ChildCommand {
@NotNull @NotNull
public Map<String, String> getCommandHelp(CommandSender sender) { public Map<String, String> getCommandHelp(CommandSender sender) {
HashMap<String, String> map = new HashMap<>(); Map<String, String> map = new LinkedHashMap<>();
for (ChildCommand child : childCommands) { for (ChildCommand child : childCommands) {
if (!child.hasPermission(sender)) { if (!child.hasPermission(sender)) {
continue; continue;
@@ -111,14 +111,12 @@ public abstract class ParentCommand extends ChildCommand {
public void sendHelp(@NotNull CommandSender sender) { public void sendHelp(@NotNull CommandSender sender) {
sender.sendMessage("§e==================== [ " + getName() + " 使用帮助] ===================="); sender.sendMessage("§e==================== [ " + getName() + " 使用帮助] ====================");
Map<String, String> helpMap = getCommandHelp(sender); Map<String, String> map = getCommandHelp(sender);
int maxLength = helpMap.keySet().stream() int maxLength = map.keySet().stream()
.map(String::length) .map(String::length)
.max(Integer::compareTo) .max(Integer::compareTo)
.orElse(-1); .orElse(-1);
ArrayList<Map.Entry<String, String>> list = new ArrayList<>(helpMap.entrySet()); for (Map.Entry<String, String> entry : map.entrySet()) {
list.sort(Map.Entry.comparingByKey());
for (Map.Entry<String, String> entry : list) {
sender.sendMessage(String.format("§a%-" + maxLength + "s - %s", entry.getKey(), entry.getValue())); sender.sendMessage(String.format("§a%-" + maxLength + "s - %s", entry.getKey(), entry.getValue()));
} }
} }

View File

@@ -5,6 +5,8 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.*; import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Function;
@SuppressWarnings({"unused", "unchecked"}) @SuppressWarnings({"unused", "unchecked"})
@Getter @Getter
@@ -71,16 +73,6 @@ public class ConfigSection {
return o.toString(); return o.toString();
} }
public List<String> getStringList(@NotNull String key) {
Object o = map.get(key);
if (o instanceof List) {
return (List<String>) o;
}
ArrayList<String> list = new ArrayList<>();
map.put(key, list);
return list;
}
public boolean getBoolean(@NotNull String key) { public boolean getBoolean(@NotNull String key) {
String string = getString(key); String string = getString(key);
if (string == null) { if (string == null) {
@@ -161,6 +153,45 @@ public class ConfigSection {
return Long.parseLong(string); return Long.parseLong(string);
} }
public <T> List<T> getList(@NotNull String key, Function<Object, T> function) {
ArrayList<T> 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<String> getStringList(@NotNull String key) {
return getList(key, Object::toString);
}
public List<Integer> getIntegerList(@NotNull String key) {
return getList(key, o -> Integer.parseInt(o.toString()));
}
public <T> List<T> toList(BiFunction<ConfigSection, String, T> function) {
ArrayList<T> list = new ArrayList<>();
for (String key : getKeys()) {
T result = function.apply(this, key);
list.add(result);
}
return list;
}
public <T> Map<String, T> toMap(BiFunction<ConfigSection, String, T> function) {
HashMap<String, T> 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) { public void set(@NotNull String key, @Nullable Object value) {
if (value == null) { if (value == null) {
map.remove(key); map.remove(key);

View File

@@ -5,9 +5,12 @@ import org.yaml.snakeyaml.Yaml;
import java.io.*; import java.io.*;
import java.nio.charset.StandardCharsets; 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 class YamlConfig extends ConfigSection {
public static final Yaml YAML_LOADER = new Yaml(); public static final Yaml YAML_LOADER = new Yaml();
@@ -19,24 +22,83 @@ public class YamlConfig extends ConfigSection {
super(map); super(map);
} }
public static List<YamlConfig> loadFolder(@NotNull File file) throws IOException {
if (file.isDirectory()) {
ArrayList<YamlConfig> 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<YamlConfig> loadFolder(@NotNull File file, @NotNull Consumer<Exception> exceptionHandler) {
if (file.isDirectory()) {
ArrayList<YamlConfig> 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 { public static YamlConfig load(@NotNull File file) throws IOException {
try (FileInputStream stream = new FileInputStream(file)) { try (FileInputStream stream = new FileInputStream(file)) {
return load(stream);
}
}
public static YamlConfig load(@NotNull InputStream stream) throws IOException {
try (InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) { try (InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) {
return load(reader); return load(reader);
} }
} }
}
public static YamlConfig load(@NotNull Reader reader) { public static YamlConfig load(@NotNull Reader reader) {
Map<String, Object> load = YAML_LOADER.load(reader); Map<String, Object> load = YAML_LOADER.load(reader);
return new YamlConfig(load); return new YamlConfig(load);
} }
public void save(@NotNull Path path) throws IOException {
try (OutputStream stream = Files.newOutputStream(path)) {
save(stream);
}
}
public void save(@NotNull File file) throws IOException { public void save(@NotNull File file) throws IOException {
try (FileOutputStream stream = new FileOutputStream(file)) { 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)) { try (OutputStreamWriter writer = new OutputStreamWriter(stream, StandardCharsets.UTF_8)) {
save(writer);
}
}
public void save(@NotNull Writer writer) {
YAML_LOADER.dump(map, writer); YAML_LOADER.dump(map, writer);
} }
}
}
} }

View File

@@ -19,9 +19,7 @@ public abstract class SimpleLogger {
try { try {
log(level, String.format(msg, args)); log(level, String.format(msg, args));
} catch (Exception e) { } catch (Exception e) {
log(Level.WARNING, "输出日志 %s 时遇到一个异常", msg); log(Level.WARNING, "输出日志 " + msg + Arrays.toString(args) + " 时遇到一个异常", e);
log(Level.WARNING, "日志参数: %s", Arrays.toString(args));
log(Level.WARNING, e, "异常信息: ");
} }
} }
@@ -29,10 +27,8 @@ public abstract class SimpleLogger {
try { try {
log(level, String.format(msg, args), throwable); log(level, String.format(msg, args), throwable);
} catch (Exception e) { } catch (Exception e) {
log(Level.WARNING, "输出日志 " + msg + " 时遇到一个异常"); log(Level.WARNING, "输出日志 " + msg + Arrays.toString(args) + " 时遇到一个异常", e);
log(Level.WARNING, "日志参数: " + Arrays.toString(args));
log(Level.WARNING, "异常参数: ", throwable); log(Level.WARNING, "异常参数: ", throwable);
log(Level.WARNING, "异常信息: ", e);
} }
} }