mirror of
https://github.com/CarmJos/MineConfiguration.git
synced 2024-09-19 20:05:49 +00:00
feat(source): 为 ConfigurationWrapper 添加 getSource() 方法以获取源实现内容。
This commit is contained in:
parent
21f7742d78
commit
8437c4c904
@ -17,12 +17,13 @@
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>${project.parent.groupId}</groupId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>easyconfiguration-core</artifactId>
|
||||
<version>${easyconfiguration.version}</version>
|
||||
<version>3.2.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<!--suppress VulnerableLibrariesLocal -->
|
||||
<dependency>
|
||||
<groupId>net.md-5</groupId>
|
||||
<artifactId>bungeecord-chat</artifactId>
|
||||
|
@ -24,7 +24,6 @@ public abstract class ConfigMessageList<M, T extends AbstractText<R>, R> extends
|
||||
|
||||
protected final @NotNull Function<String, T> textBuilder;
|
||||
|
||||
@SuppressWarnings("NullableProblems")
|
||||
public ConfigMessageList(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
|
||||
@Nullable List<String> headerComments, @Nullable String inlineComments,
|
||||
@NotNull Class<T> textClazz, @NotNull List<T> messages, @NotNull String[] params,
|
||||
|
@ -1,8 +1,10 @@
|
||||
package cc.carm.lib.mineconfiguration.bukkit.data;
|
||||
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||
import cc.carm.lib.mineconfiguration.bukkit.source.CraftSectionWrapper;
|
||||
import cc.carm.lib.mineconfiguration.bukkit.utils.TextParser;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemFlag;
|
||||
@ -126,7 +128,11 @@ public class ItemConfig {
|
||||
return map;
|
||||
}
|
||||
|
||||
public static @NotNull ItemConfig deserialize(@NotNull ConfigurationWrapper section) throws Exception {
|
||||
public static @NotNull ItemConfig deserialize(@NotNull ConfigurationSection section) throws Exception {
|
||||
return deserialize(CraftSectionWrapper.of(section));
|
||||
}
|
||||
|
||||
public static @NotNull ItemConfig deserialize(@NotNull ConfigurationWrapper<?> section) throws Exception {
|
||||
String typeName = section.getString("type");
|
||||
if (typeName == null) throw new NullPointerException("Item type name is null");
|
||||
|
||||
@ -156,7 +162,7 @@ public class ItemConfig {
|
||||
return flags;
|
||||
}
|
||||
|
||||
private static Map<Enchantment, Integer> readEnchantments(ConfigurationWrapper section) {
|
||||
private static Map<Enchantment, Integer> readEnchantments(ConfigurationWrapper<?> section) {
|
||||
Map<Enchantment, Integer> enchantments = new LinkedHashMap<>();
|
||||
if (section == null) return enchantments;
|
||||
section.getKeys(false).forEach(key -> {
|
||||
|
@ -61,7 +61,7 @@ public class TitleConfig {
|
||||
return map;
|
||||
}
|
||||
|
||||
public static @NotNull TitleConfig deserialize(@NotNull ConfigurationWrapper section) {
|
||||
public static @NotNull TitleConfig deserialize(@NotNull ConfigurationWrapper<?> section) {
|
||||
return of(section.getString("line1"), section.getString("line2"));
|
||||
}
|
||||
|
||||
|
@ -10,61 +10,66 @@ import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
public class CraftSectionWrapper implements ConfigurationWrapper {
|
||||
public class CraftSectionWrapper implements ConfigurationWrapper<ConfigurationSection> {
|
||||
|
||||
protected final ConfigurationSection section;
|
||||
protected final ConfigurationSection configuration;
|
||||
|
||||
protected CraftSectionWrapper(ConfigurationSection section) {
|
||||
this.section = section;
|
||||
protected CraftSectionWrapper(ConfigurationSection configuration) {
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
public ConfigurationSection getSourceSection() {
|
||||
return section;
|
||||
return configuration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ConfigurationSection getSource() {
|
||||
return this.configuration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Set<String> getKeys(boolean deep) {
|
||||
return this.section.getKeys(deep);
|
||||
return this.configuration.getKeys(deep);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Map<String, Object> getValues(boolean deep) {
|
||||
return this.section.getValues(deep);
|
||||
return this.configuration.getValues(deep);
|
||||
}
|
||||
|
||||
@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
|
||||
public boolean isList(@NotNull String path) {
|
||||
return this.section.isList(path);
|
||||
return this.configuration.isList(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable List<?> getList(@NotNull String path) {
|
||||
return this.section.getList(path);
|
||||
return this.configuration.getList(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isConfigurationSection(@NotNull String path) {
|
||||
return this.section.isConfigurationSection(path);
|
||||
return this.configuration.isConfigurationSection(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable ConfigurationWrapper getConfigurationSection(@NotNull String path) {
|
||||
return Optional.ofNullable(section.getConfigurationSection(path))
|
||||
public @Nullable CraftSectionWrapper getConfigurationSection(@NotNull String path) {
|
||||
return Optional.ofNullable(configuration.getConfigurationSection(path))
|
||||
.map(CraftSectionWrapper::of).orElse(null);
|
||||
}
|
||||
|
||||
|
@ -1,13 +1,13 @@
|
||||
package cc.carm.lib.mineconfiguration.bukkit.value;
|
||||
|
||||
import cc.carm.lib.mineconfiguration.bukkit.CraftConfigValue;
|
||||
import cc.carm.lib.mineconfiguration.bukkit.data.ItemConfig;
|
||||
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
|
||||
import cc.carm.lib.configuration.core.function.ConfigValueParser;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||
import cc.carm.lib.configuration.core.value.type.ConfiguredSection;
|
||||
import cc.carm.lib.mineconfiguration.bukkit.CraftConfigValue;
|
||||
import cc.carm.lib.mineconfiguration.bukkit.builder.item.ItemConfigBuilder;
|
||||
import cc.carm.lib.mineconfiguration.bukkit.data.ItemConfig;
|
||||
import cc.carm.lib.mineconfiguration.bukkit.source.CraftConfigProvider;
|
||||
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@ -17,6 +17,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@SuppressWarnings("ALL")
|
||||
public class ConfiguredItem extends ConfiguredSection<ItemConfig> {
|
||||
|
||||
public static ItemConfigBuilder create() {
|
||||
@ -33,14 +34,17 @@ public class ConfiguredItem extends ConfiguredSection<ItemConfig> {
|
||||
|
||||
protected final @NotNull String[] params;
|
||||
|
||||
public ConfiguredItem(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
|
||||
public ConfiguredItem(@Nullable CraftConfigProvider provider, @Nullable String sectionPath,
|
||||
@Nullable List<String> headerComments, @Nullable String inlineComments,
|
||||
@Nullable ItemConfig defaultValue, @NotNull String[] params) {
|
||||
super(provider, sectionPath, headerComments, inlineComments, ItemConfig.class, defaultValue, getItemParser(), ItemConfig::serialize);
|
||||
super(
|
||||
provider, sectionPath, headerComments, inlineComments, ItemConfig.class, defaultValue,
|
||||
getItemParser(), ItemConfig::serialize
|
||||
);
|
||||
this.params = params;
|
||||
}
|
||||
|
||||
public static ConfigValueParser<ConfigurationWrapper, ItemConfig> getItemParser() {
|
||||
public static ConfigValueParser<ConfigurationWrapper<?>, ItemConfig> getItemParser() {
|
||||
return (s, d) -> ItemConfig.deserialize(s);
|
||||
}
|
||||
|
||||
|
@ -1,14 +1,14 @@
|
||||
package cc.carm.lib.mineconfiguration.bukkit.value;
|
||||
|
||||
import cc.carm.lib.mineconfiguration.bukkit.CraftConfigValue;
|
||||
import cc.carm.lib.mineconfiguration.bukkit.data.TitleConfig;
|
||||
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
|
||||
import cc.carm.lib.configuration.core.function.ConfigValueParser;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||
import cc.carm.lib.configuration.core.value.type.ConfiguredSection;
|
||||
import cc.carm.lib.mineconfiguration.bukkit.CraftConfigValue;
|
||||
import cc.carm.lib.mineconfiguration.bukkit.builder.title.TitleConfigBuilder;
|
||||
import cc.carm.lib.mineconfiguration.bukkit.data.TitleConfig;
|
||||
import cc.carm.lib.mineconfiguration.bukkit.function.TitleSendConsumer;
|
||||
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@ -103,7 +103,7 @@ public class ConfiguredTitle extends ConfiguredSection<TitleConfig> {
|
||||
}
|
||||
|
||||
|
||||
public static ConfigValueParser<ConfigurationWrapper, TitleConfig> getTitleParser() {
|
||||
public static ConfigValueParser<ConfigurationWrapper<?>, TitleConfig> getTitleParser() {
|
||||
return (s, d) -> TitleConfig.deserialize(s);
|
||||
}
|
||||
}
|
||||
|
@ -16,6 +16,8 @@
|
||||
<artifactId>mineconfiguration-bukkit</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>MineConfiguration-Bukkit</name>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
|
@ -1,10 +1,10 @@
|
||||
package cc.carm.lib.mineconfiguration.bukkit.source;
|
||||
|
||||
import cc.carm.lib.configuration.core.ConfigInitializer;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationComments;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.Unmodifiable;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
@ -12,13 +12,12 @@ 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 BukkitConfigProvider extends CraftConfigProvider {
|
||||
|
||||
protected static final char SEPARATOR = '.';
|
||||
|
||||
protected BukkitYAMLComments bukkitComments = new BukkitYAMLComments();
|
||||
protected @NotNull BukkitYAMLComments comments = new BukkitYAMLComments();
|
||||
|
||||
public BukkitConfigProvider(@NotNull File file) {
|
||||
super(file);
|
||||
@ -39,7 +38,7 @@ public class BukkitConfigProvider extends CraftConfigProvider {
|
||||
configuration.save(getFile());
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
this.bukkitComments.writeComments(configuration, new BufferedWriter(writer));
|
||||
this.comments.writeComments(configuration, new BufferedWriter(writer));
|
||||
String value = writer.toString(); // config contents
|
||||
|
||||
Path toUpdatePath = getFile().toPath();
|
||||
@ -49,24 +48,8 @@ public class BukkitConfigProvider extends CraftConfigProvider {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHeaderComment(@Nullable String path, @Nullable List<String> comments) {
|
||||
this.bukkitComments.setHeaderComments(path, comments);
|
||||
public @Nullable ConfigurationComments getComments() {
|
||||
return this.comments;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setInlineComment(@NotNull String path, @Nullable String comment) {
|
||||
this.bukkitComments.setInlineComment(path, comment);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
@Unmodifiable
|
||||
public List<String> getHeaderComment(@Nullable String path) {
|
||||
return this.bukkitComments.getHeaderComment(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable String getInlineComment(@NotNull String path) {
|
||||
return this.bukkitComments.getInlineComment(path);
|
||||
}
|
||||
}
|
||||
|
@ -1,58 +1,20 @@
|
||||
package cc.carm.lib.mineconfiguration.bukkit.source;
|
||||
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationComments;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.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.util.*;
|
||||
import java.util.List;
|
||||
import java.util.StringJoiner;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class BukkitYAMLComments {
|
||||
|
||||
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 BukkitYAMLComments extends ConfigurationComments {
|
||||
|
||||
public @Nullable String buildHeaderComments(@Nullable String path, @NotNull String indents) {
|
||||
List<String> comments = getHeaderComment(path);
|
||||
|
@ -22,8 +22,8 @@
|
||||
|
||||
<dependency>
|
||||
<groupId>${project.parent.groupId}</groupId>
|
||||
<artifactId>easyconfiguration-core</artifactId>
|
||||
<version>${easyconfiguration.version}</version>
|
||||
<artifactId>mineconfiguration-common</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
@ -43,13 +43,6 @@
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>${project.parent.groupId}</groupId>
|
||||
<artifactId>mineconfiguration-common</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -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());
|
||||
}
|
||||
|
@ -17,6 +17,8 @@
|
||||
<artifactId>mineconfiguration-spigot</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>MineConfiguration-Spigot</name>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
|
@ -1,15 +1,13 @@
|
||||
package cc.carm.lib.mineconfiguration.spigot.source;
|
||||
|
||||
import cc.carm.lib.configuration.core.ConfigInitializer;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationComments;
|
||||
import cc.carm.lib.mineconfiguration.bukkit.source.CraftConfigProvider;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.Unmodifiable;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class SpigotConfigProvider extends CraftConfigProvider {
|
||||
|
||||
@ -17,38 +15,18 @@ public class SpigotConfigProvider extends CraftConfigProvider {
|
||||
super(file);
|
||||
}
|
||||
|
||||
protected SpigotYAMLComments comments = null;
|
||||
|
||||
@Override
|
||||
public void initializeConfig() {
|
||||
this.configuration = YamlConfiguration.loadConfiguration(file);
|
||||
this.comments = new SpigotYAMLComments(configuration);
|
||||
this.initializer = new ConfigInitializer<>(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHeaderComment(@Nullable String path, @Nullable List<String> comments) {
|
||||
if (path == null) {
|
||||
this.configuration.options().setHeader(comments);
|
||||
} else {
|
||||
this.configuration.setComments(path, comments);
|
||||
}
|
||||
public @Nullable ConfigurationComments getComments() {
|
||||
return this.comments;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setInlineComment(@NotNull String path, @Nullable String comment) {
|
||||
if (comment == null) {
|
||||
this.configuration.setInlineComments(path, null);
|
||||
} else {
|
||||
this.configuration.setComments(path, Collections.singletonList(comment));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable @Unmodifiable List<String> getHeaderComment(@Nullable String path) {
|
||||
if (path == null) return Collections.unmodifiableList(this.configuration.options().getHeader());
|
||||
else return this.configuration.getComments(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable String getInlineComment(@NotNull String path) {
|
||||
return String.join(" ", this.configuration.getInlineComments(path));
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,48 @@
|
||||
package cc.carm.lib.mineconfiguration.spigot.source;
|
||||
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationComments;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.Unmodifiable;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class SpigotYAMLComments extends ConfigurationComments {
|
||||
|
||||
protected final YamlConfiguration configuration;
|
||||
|
||||
public SpigotYAMLComments(YamlConfiguration configuration) {
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHeaderComments(@Nullable String path, @Nullable List<String> comments) {
|
||||
if (path == null) {
|
||||
this.configuration.options().setHeader(comments);
|
||||
} else {
|
||||
this.configuration.setComments(path, comments);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setInlineComment(@NotNull String path, @Nullable String comment) {
|
||||
if (comment == null) {
|
||||
this.configuration.setInlineComments(path, null);
|
||||
} else {
|
||||
this.configuration.setComments(path, Collections.singletonList(comment));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable @Unmodifiable List<String> getHeaderComment(@Nullable String path) {
|
||||
if (path == null) return Collections.unmodifiableList(this.configuration.options().getHeader());
|
||||
else return this.configuration.getComments(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable String getInlineComment(@NotNull String path) {
|
||||
return String.join(" ", this.configuration.getInlineComments(path));
|
||||
}
|
||||
}
|
5
pom.xml
5
pom.xml
@ -9,12 +9,11 @@
|
||||
<maven.compiler.target>${project.jdk.version}</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||
|
||||
<easyconfiguration.version>3.1.0</easyconfiguration.version>
|
||||
</properties>
|
||||
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>mineconfiguration-parent</artifactId>
|
||||
<version>2.0.2</version>
|
||||
<version>2.1.0</version>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>common</module>
|
||||
|
Loading…
Reference in New Issue
Block a user