feat: 增强部分API
This commit is contained in:
@@ -94,7 +94,7 @@ public abstract class ParentCommand extends ChildCommand {
|
||||
|
||||
@NotNull
|
||||
public Map<String, String> getCommandHelp(CommandSender sender) {
|
||||
HashMap<String, String> map = new HashMap<>();
|
||||
Map<String, String> 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<String, String> helpMap = getCommandHelp(sender);
|
||||
int maxLength = helpMap.keySet().stream()
|
||||
Map<String, String> map = getCommandHelp(sender);
|
||||
int maxLength = map.keySet().stream()
|
||||
.map(String::length)
|
||||
.max(Integer::compareTo)
|
||||
.orElse(-1);
|
||||
ArrayList<Map.Entry<String, String>> list = new ArrayList<>(helpMap.entrySet());
|
||||
list.sort(Map.Entry.comparingByKey());
|
||||
for (Map.Entry<String, String> entry : list) {
|
||||
for (Map.Entry<String, String> entry : map.entrySet()) {
|
||||
sender.sendMessage(String.format("§a%-" + maxLength + "s - %s", entry.getKey(), entry.getValue()));
|
||||
}
|
||||
}
|
||||
|
@@ -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<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) {
|
||||
String string = getString(key);
|
||||
if (string == null) {
|
||||
@@ -161,6 +153,45 @@ public class ConfigSection {
|
||||
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) {
|
||||
if (value == null) {
|
||||
map.remove(key);
|
||||
|
@@ -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,24 +22,83 @@ public class YamlConfig extends ConfigSection {
|
||||
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 {
|
||||
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)) {
|
||||
return load(reader);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static YamlConfig load(@NotNull Reader reader) {
|
||||
Map<String, Object> load = YAML_LOADER.load(reader);
|
||||
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 {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user