1
mirror of https://github.com/CarmJos/MineConfiguration.git synced 2026-06-04 21:58:16 +08:00

feat(source): 为 ConfigurationWrapper 添加 getSource() 方法以获取源实现内容。

This commit is contained in:
2022-09-10 01:04:04 +08:00
parent 21f7742d78
commit 8437c4c904
19 changed files with 147 additions and 212 deletions
@@ -9,6 +9,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.function.BiFunction;
@SuppressWarnings("deprecation")
public class BungeeMessageBuilder extends MessageConfigBuilder<CommandSender, MessageText> {
@@ -1,13 +1,13 @@
package cc.carm.lib.mineconfiguration.bungee.source;
import cc.carm.lib.configuration.core.ConfigInitializer;
import cc.carm.lib.configuration.core.source.ConfigurationComments;
import cc.carm.lib.configuration.core.source.impl.FileConfigProvider;
import net.md_5.bungee.config.Configuration;
import net.md_5.bungee.config.ConfigurationProvider;
import net.md_5.bungee.config.YamlConfiguration;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.io.BufferedWriter;
import java.io.File;
@@ -16,7 +16,6 @@ import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
public class BungeeConfigProvider extends FileConfigProvider<BungeeSectionWrapper> {
@@ -52,6 +51,11 @@ public class BungeeConfigProvider extends FileConfigProvider<BungeeSectionWrappe
this.configuration = getLoader().load(file);
}
@Override
public @Nullable ConfigurationComments getComments() {
return this.comments;
}
@Override
public void save() throws Exception {
getLoader().save(configuration, file);
@@ -67,26 +71,6 @@ public class BungeeConfigProvider extends FileConfigProvider<BungeeSectionWrappe
}
}
@Override
public void setHeaderComment(@Nullable String path, @Nullable List<String> comments) {
this.comments.setHeaderComments(path, comments);
}
@Override
public void setInlineComment(@NotNull String path, @Nullable String comment) {
this.comments.setInlineComment(path, comment);
}
@Override
public @Nullable @Unmodifiable List<String> getHeaderComment(@Nullable String path) {
return this.comments.getHeaderComment(path);
}
@Override
public @Nullable String getInlineComment(@NotNull String path) {
return this.comments.getInlineComment(path);
}
@Override
public @NotNull ConfigInitializer<BungeeConfigProvider> getInitializer() {
return this.initializer;
@@ -11,12 +11,12 @@ import java.util.stream.Collectors;
import static cc.carm.lib.mineconfiguration.bungee.source.BungeeConfigProvider.SEPARATOR;
public class BungeeSectionWrapper implements ConfigurationWrapper {
public class BungeeSectionWrapper implements ConfigurationWrapper<Configuration> {
private final Configuration section;
private final Configuration configuration;
private BungeeSectionWrapper(@NotNull Configuration section) {
this.section = section;
this.configuration = section;
}
@Contract("!null->!null")
@@ -37,34 +37,39 @@ public class BungeeSectionWrapper implements ConfigurationWrapper {
return keys;
}
@Override
public @NotNull Configuration getSource() {
return this.configuration;
}
@Override
public @NotNull Set<String> getKeys(boolean deep) {
if (deep) {
return new LinkedHashSet<>(getAllKeys(section));
return new LinkedHashSet<>(getAllKeys(configuration));
} else {
return new LinkedHashSet<>(section.getKeys());
return new LinkedHashSet<>(configuration.getKeys());
}
}
@Override
public @NotNull Map<String, Object> getValues(boolean deep) {
return getKeys(deep).stream()
.collect(Collectors.toMap(key -> key, section::get, (a, b) -> b, LinkedHashMap::new));
.collect(Collectors.toMap(key -> key, configuration::get, (a, b) -> b, LinkedHashMap::new));
}
@Override
public void set(@NotNull String path, @Nullable Object value) {
this.section.set(path, value);
this.configuration.set(path, value);
}
@Override
public boolean contains(@NotNull String path) {
return this.section.contains(path);
return this.configuration.contains(path);
}
@Override
public @Nullable Object get(@NotNull String path) {
return this.section.get(path);
return this.configuration.get(path);
}
@Override
@@ -74,7 +79,7 @@ public class BungeeSectionWrapper implements ConfigurationWrapper {
@Override
public @Nullable List<?> getList(@NotNull String path) {
return this.section.getList(path);
return this.configuration.getList(path);
}
@Override
@@ -83,7 +88,7 @@ public class BungeeSectionWrapper implements ConfigurationWrapper {
}
@Override
public @Nullable ConfigurationWrapper getConfigurationSection(@NotNull String path) {
return of(this.section.getSection(path));
public @Nullable BungeeSectionWrapper getConfigurationSection(@NotNull String path) {
return of(this.configuration.getSection(path));
}
}
@@ -1,60 +1,23 @@
package cc.carm.lib.mineconfiguration.bungee.source;
import cc.carm.lib.configuration.core.source.ConfigurationComments;
import net.md_5.bungee.config.Configuration;
import net.md_5.bungee.config.ConfigurationProvider;
import net.md_5.bungee.config.YamlConfiguration;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.*;
import java.util.List;
import java.util.StringJoiner;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static cc.carm.lib.mineconfiguration.bungee.source.BungeeConfigProvider.SEPARATOR;
public class BungeeYAMLComments {
protected final @NotNull Map<String, List<String>> headerComments = new HashMap<>();
protected final @NotNull Map<String, String> inlineComments = new HashMap<>();
protected @NotNull Map<String, List<String>> getHeaderComments() {
return headerComments;
}
protected @NotNull Map<String, String> getInlineComments() {
return inlineComments;
}
public void setHeaderComments(@Nullable String path, @Nullable List<String> comments) {
if (comments == null) {
getHeaderComments().remove(path);
} else {
getHeaderComments().put(path, comments);
}
}
public void setInlineComment(@NotNull String path, @Nullable String comment) {
if (comment == null) {
getInlineComments().remove(path);
} else {
getInlineComments().put(path, comment);
}
}
@Nullable
@Unmodifiable
public List<String> getHeaderComment(@Nullable String path) {
return Optional.ofNullable(getHeaderComments().get(path)).map(Collections::unmodifiableList).orElse(null);
}
public @Nullable String getInlineComment(@NotNull String path) {
return getInlineComments().get(path);
}
public class BungeeYAMLComments extends ConfigurationComments {
public @Nullable String buildHeaderComments(@Nullable String path, @NotNull String indents) {
List<String> comments = getHeaderComment(path);
@@ -144,7 +107,7 @@ public class BungeeYAMLComments {
* @param key 键
* @return 该键的缩进文本
*/
protected static String getIndents(String key) {
protected static String getIndents(@NotNull String key) {
String[] splitKey = key.split("[" + SEPARATOR + "]");
return IntStream.range(1, splitKey.length).mapToObj(i -> " ").collect(Collectors.joining());
}