1
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:
Carm Jos 2022-09-10 01:04:04 +08:00
parent 21f7742d78
commit 8437c4c904
19 changed files with 147 additions and 212 deletions

View File

@ -17,12 +17,13 @@
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>${project.parent.groupId}</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>easyconfiguration-core</artifactId> <artifactId>easyconfiguration-core</artifactId>
<version>${easyconfiguration.version}</version> <version>3.2.0</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<!--suppress VulnerableLibrariesLocal -->
<dependency> <dependency>
<groupId>net.md-5</groupId> <groupId>net.md-5</groupId>
<artifactId>bungeecord-chat</artifactId> <artifactId>bungeecord-chat</artifactId>

View File

@ -24,7 +24,6 @@ public abstract class ConfigMessageList<M, T extends AbstractText<R>, R> extends
protected final @NotNull Function<String, T> textBuilder; protected final @NotNull Function<String, T> textBuilder;
@SuppressWarnings("NullableProblems")
public ConfigMessageList(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath, public ConfigMessageList(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
@Nullable List<String> headerComments, @Nullable String inlineComments, @Nullable List<String> headerComments, @Nullable String inlineComments,
@NotNull Class<T> textClazz, @NotNull List<T> messages, @NotNull String[] params, @NotNull Class<T> textClazz, @NotNull List<T> messages, @NotNull String[] params,

View File

@ -1,8 +1,10 @@
package cc.carm.lib.mineconfiguration.bukkit.data; package cc.carm.lib.mineconfiguration.bukkit.data;
import cc.carm.lib.configuration.core.source.ConfigurationWrapper; 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 cc.carm.lib.mineconfiguration.bukkit.utils.TextParser;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemFlag;
@ -126,7 +128,11 @@ public class ItemConfig {
return map; 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"); String typeName = section.getString("type");
if (typeName == null) throw new NullPointerException("Item type name is null"); if (typeName == null) throw new NullPointerException("Item type name is null");
@ -156,7 +162,7 @@ public class ItemConfig {
return flags; return flags;
} }
private static Map<Enchantment, Integer> readEnchantments(ConfigurationWrapper section) { private static Map<Enchantment, Integer> readEnchantments(ConfigurationWrapper<?> section) {
Map<Enchantment, Integer> enchantments = new LinkedHashMap<>(); Map<Enchantment, Integer> enchantments = new LinkedHashMap<>();
if (section == null) return enchantments; if (section == null) return enchantments;
section.getKeys(false).forEach(key -> { section.getKeys(false).forEach(key -> {

View File

@ -61,7 +61,7 @@ public class TitleConfig {
return map; 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")); return of(section.getString("line1"), section.getString("line2"));
} }

View File

@ -10,61 +10,66 @@ import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Set; 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) { protected CraftSectionWrapper(ConfigurationSection configuration) {
this.section = section; this.configuration = configuration;
} }
public ConfigurationSection getSourceSection() { public ConfigurationSection getSourceSection() {
return section; return configuration;
}
@Override
public @NotNull ConfigurationSection getSource() {
return this.configuration;
} }
@Override @Override
public @NotNull Set<String> getKeys(boolean deep) { public @NotNull Set<String> getKeys(boolean deep) {
return this.section.getKeys(deep); return this.configuration.getKeys(deep);
} }
@Override @Override
public @NotNull Map<String, Object> getValues(boolean deep) { public @NotNull Map<String, Object> getValues(boolean deep) {
return this.section.getValues(deep); return this.configuration.getValues(deep);
} }
@Override @Override
public void set(@NotNull String path, @Nullable Object value) { public void set(@NotNull String path, @Nullable Object value) {
this.section.set(path, value); this.configuration.set(path, value);
} }
@Override @Override
public boolean contains(@NotNull String path) { public boolean contains(@NotNull String path) {
return this.section.contains(path); return this.configuration.contains(path);
} }
@Override @Override
public @Nullable Object get(@NotNull String path) { public @Nullable Object get(@NotNull String path) {
return this.section.get(path); return this.configuration.get(path);
} }
@Override @Override
public boolean isList(@NotNull String path) { public boolean isList(@NotNull String path) {
return this.section.isList(path); return this.configuration.isList(path);
} }
@Override @Override
public @Nullable List<?> getList(@NotNull String path) { public @Nullable List<?> getList(@NotNull String path) {
return this.section.getList(path); return this.configuration.getList(path);
} }
@Override @Override
public boolean isConfigurationSection(@NotNull String path) { public boolean isConfigurationSection(@NotNull String path) {
return this.section.isConfigurationSection(path); return this.configuration.isConfigurationSection(path);
} }
@Override @Override
public @Nullable ConfigurationWrapper getConfigurationSection(@NotNull String path) { public @Nullable CraftSectionWrapper getConfigurationSection(@NotNull String path) {
return Optional.ofNullable(section.getConfigurationSection(path)) return Optional.ofNullable(configuration.getConfigurationSection(path))
.map(CraftSectionWrapper::of).orElse(null); .map(CraftSectionWrapper::of).orElse(null);
} }

View File

@ -1,13 +1,13 @@
package cc.carm.lib.mineconfiguration.bukkit.value; 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.function.ConfigValueParser;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.source.ConfigurationWrapper; import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
import cc.carm.lib.configuration.core.value.type.ConfiguredSection; 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.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.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -17,6 +17,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@SuppressWarnings("ALL")
public class ConfiguredItem extends ConfiguredSection<ItemConfig> { public class ConfiguredItem extends ConfiguredSection<ItemConfig> {
public static ItemConfigBuilder create() { public static ItemConfigBuilder create() {
@ -33,14 +34,17 @@ public class ConfiguredItem extends ConfiguredSection<ItemConfig> {
protected final @NotNull String[] params; 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 List<String> headerComments, @Nullable String inlineComments,
@Nullable ItemConfig defaultValue, @NotNull String[] params) { @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; this.params = params;
} }
public static ConfigValueParser<ConfigurationWrapper, ItemConfig> getItemParser() { public static ConfigValueParser<ConfigurationWrapper<?>, ItemConfig> getItemParser() {
return (s, d) -> ItemConfig.deserialize(s); return (s, d) -> ItemConfig.deserialize(s);
} }

View File

@ -1,14 +1,14 @@
package cc.carm.lib.mineconfiguration.bukkit.value; 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.function.ConfigValueParser;
import cc.carm.lib.configuration.core.source.ConfigurationProvider; import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.source.ConfigurationWrapper; import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
import cc.carm.lib.configuration.core.value.type.ConfiguredSection; 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.builder.title.TitleConfigBuilder;
import cc.carm.lib.mineconfiguration.bukkit.data.TitleConfig;
import cc.carm.lib.mineconfiguration.bukkit.function.TitleSendConsumer; import cc.carm.lib.mineconfiguration.bukkit.function.TitleSendConsumer;
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; 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); return (s, d) -> TitleConfig.deserialize(s);
} }
} }

View File

@ -16,6 +16,8 @@
<artifactId>mineconfiguration-bukkit</artifactId> <artifactId>mineconfiguration-bukkit</artifactId>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>MineConfiguration-Bukkit</name>
<dependencies> <dependencies>
<dependency> <dependency>

View File

@ -1,10 +1,10 @@
package cc.carm.lib.mineconfiguration.bukkit.source; package cc.carm.lib.mineconfiguration.bukkit.source;
import cc.carm.lib.configuration.core.ConfigInitializer; import cc.carm.lib.configuration.core.ConfigInitializer;
import cc.carm.lib.configuration.core.source.ConfigurationComments;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
@ -12,13 +12,12 @@ import java.io.StringWriter;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.List;
public class BukkitConfigProvider extends CraftConfigProvider { public class BukkitConfigProvider extends CraftConfigProvider {
protected static final char SEPARATOR = '.'; protected static final char SEPARATOR = '.';
protected BukkitYAMLComments bukkitComments = new BukkitYAMLComments(); protected @NotNull BukkitYAMLComments comments = new BukkitYAMLComments();
public BukkitConfigProvider(@NotNull File file) { public BukkitConfigProvider(@NotNull File file) {
super(file); super(file);
@ -39,7 +38,7 @@ public class BukkitConfigProvider extends CraftConfigProvider {
configuration.save(getFile()); configuration.save(getFile());
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
this.bukkitComments.writeComments(configuration, new BufferedWriter(writer)); this.comments.writeComments(configuration, new BufferedWriter(writer));
String value = writer.toString(); // config contents String value = writer.toString(); // config contents
Path toUpdatePath = getFile().toPath(); Path toUpdatePath = getFile().toPath();
@ -49,24 +48,8 @@ public class BukkitConfigProvider extends CraftConfigProvider {
} }
@Override @Override
public void setHeaderComment(@Nullable String path, @Nullable List<String> comments) { public @Nullable ConfigurationComments getComments() {
this.bukkitComments.setHeaderComments(path, comments); 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);
}
} }

View File

@ -1,58 +1,20 @@
package cc.carm.lib.mineconfiguration.bukkit.source; package cc.carm.lib.mineconfiguration.bukkit.source;
import cc.carm.lib.configuration.core.source.ConfigurationComments;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.List;
import java.util.StringJoiner;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.IntStream; import java.util.stream.IntStream;
public class BukkitYAMLComments { public class BukkitYAMLComments extends ConfigurationComments {
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 @Nullable String buildHeaderComments(@Nullable String path, @NotNull String indents) { public @Nullable String buildHeaderComments(@Nullable String path, @NotNull String indents) {
List<String> comments = getHeaderComment(path); List<String> comments = getHeaderComment(path);

View File

@ -22,8 +22,8 @@
<dependency> <dependency>
<groupId>${project.parent.groupId}</groupId> <groupId>${project.parent.groupId}</groupId>
<artifactId>easyconfiguration-core</artifactId> <artifactId>mineconfiguration-common</artifactId>
<version>${easyconfiguration.version}</version> <version>${project.parent.version}</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
@ -43,13 +43,6 @@
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>mineconfiguration-common</artifactId>
<version>${project.parent.version}</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -9,6 +9,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.function.BiFunction; import java.util.function.BiFunction;
@SuppressWarnings("deprecation")
public class BungeeMessageBuilder extends MessageConfigBuilder<CommandSender, MessageText> { public class BungeeMessageBuilder extends MessageConfigBuilder<CommandSender, MessageText> {

View File

@ -1,13 +1,13 @@
package cc.carm.lib.mineconfiguration.bungee.source; package cc.carm.lib.mineconfiguration.bungee.source;
import cc.carm.lib.configuration.core.ConfigInitializer; 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 cc.carm.lib.configuration.core.source.impl.FileConfigProvider;
import net.md_5.bungee.config.Configuration; import net.md_5.bungee.config.Configuration;
import net.md_5.bungee.config.ConfigurationProvider; import net.md_5.bungee.config.ConfigurationProvider;
import net.md_5.bungee.config.YamlConfiguration; import net.md_5.bungee.config.YamlConfiguration;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
@ -16,7 +16,6 @@ import java.io.StringWriter;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.List;
public class BungeeConfigProvider extends FileConfigProvider<BungeeSectionWrapper> { public class BungeeConfigProvider extends FileConfigProvider<BungeeSectionWrapper> {
@ -52,6 +51,11 @@ public class BungeeConfigProvider extends FileConfigProvider<BungeeSectionWrappe
this.configuration = getLoader().load(file); this.configuration = getLoader().load(file);
} }
@Override
public @Nullable ConfigurationComments getComments() {
return this.comments;
}
@Override @Override
public void save() throws Exception { public void save() throws Exception {
getLoader().save(configuration, file); 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 @Override
public @NotNull ConfigInitializer<BungeeConfigProvider> getInitializer() { public @NotNull ConfigInitializer<BungeeConfigProvider> getInitializer() {
return this.initializer; return this.initializer;

View File

@ -11,12 +11,12 @@ import java.util.stream.Collectors;
import static cc.carm.lib.mineconfiguration.bungee.source.BungeeConfigProvider.SEPARATOR; 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) { private BungeeSectionWrapper(@NotNull Configuration section) {
this.section = section; this.configuration = section;
} }
@Contract("!null->!null") @Contract("!null->!null")
@ -37,34 +37,39 @@ public class BungeeSectionWrapper implements ConfigurationWrapper {
return keys; return keys;
} }
@Override
public @NotNull Configuration getSource() {
return this.configuration;
}
@Override @Override
public @NotNull Set<String> getKeys(boolean deep) { public @NotNull Set<String> getKeys(boolean deep) {
if (deep) { if (deep) {
return new LinkedHashSet<>(getAllKeys(section)); return new LinkedHashSet<>(getAllKeys(configuration));
} else { } else {
return new LinkedHashSet<>(section.getKeys()); return new LinkedHashSet<>(configuration.getKeys());
} }
} }
@Override @Override
public @NotNull Map<String, Object> getValues(boolean deep) { public @NotNull Map<String, Object> getValues(boolean deep) {
return getKeys(deep).stream() 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 @Override
public void set(@NotNull String path, @Nullable Object value) { public void set(@NotNull String path, @Nullable Object value) {
this.section.set(path, value); this.configuration.set(path, value);
} }
@Override @Override
public boolean contains(@NotNull String path) { public boolean contains(@NotNull String path) {
return this.section.contains(path); return this.configuration.contains(path);
} }
@Override @Override
public @Nullable Object get(@NotNull String path) { public @Nullable Object get(@NotNull String path) {
return this.section.get(path); return this.configuration.get(path);
} }
@Override @Override
@ -74,7 +79,7 @@ public class BungeeSectionWrapper implements ConfigurationWrapper {
@Override @Override
public @Nullable List<?> getList(@NotNull String path) { public @Nullable List<?> getList(@NotNull String path) {
return this.section.getList(path); return this.configuration.getList(path);
} }
@Override @Override
@ -83,7 +88,7 @@ public class BungeeSectionWrapper implements ConfigurationWrapper {
} }
@Override @Override
public @Nullable ConfigurationWrapper getConfigurationSection(@NotNull String path) { public @Nullable BungeeSectionWrapper getConfigurationSection(@NotNull String path) {
return of(this.section.getSection(path)); return of(this.configuration.getSection(path));
} }
} }

View File

@ -1,60 +1,23 @@
package cc.carm.lib.mineconfiguration.bungee.source; 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.Configuration;
import net.md_5.bungee.config.ConfigurationProvider; import net.md_5.bungee.config.ConfigurationProvider;
import net.md_5.bungee.config.YamlConfiguration; import net.md_5.bungee.config.YamlConfiguration;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.IOException; import java.io.IOException;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.*; import java.util.List;
import java.util.StringJoiner;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import static cc.carm.lib.mineconfiguration.bungee.source.BungeeConfigProvider.SEPARATOR; import static cc.carm.lib.mineconfiguration.bungee.source.BungeeConfigProvider.SEPARATOR;
public class BungeeYAMLComments { public class BungeeYAMLComments extends ConfigurationComments {
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 @Nullable String buildHeaderComments(@Nullable String path, @NotNull String indents) { public @Nullable String buildHeaderComments(@Nullable String path, @NotNull String indents) {
List<String> comments = getHeaderComment(path); List<String> comments = getHeaderComment(path);
@ -144,7 +107,7 @@ public class BungeeYAMLComments {
* @param key * @param key
* @return 该键的缩进文本 * @return 该键的缩进文本
*/ */
protected static String getIndents(String key) { protected static String getIndents(@NotNull String key) {
String[] splitKey = key.split("[" + SEPARATOR + "]"); String[] splitKey = key.split("[" + SEPARATOR + "]");
return IntStream.range(1, splitKey.length).mapToObj(i -> " ").collect(Collectors.joining()); return IntStream.range(1, splitKey.length).mapToObj(i -> " ").collect(Collectors.joining());
} }

View File

@ -17,6 +17,8 @@
<artifactId>mineconfiguration-spigot</artifactId> <artifactId>mineconfiguration-spigot</artifactId>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>MineConfiguration-Spigot</name>
<dependencies> <dependencies>
<dependency> <dependency>

View File

@ -1,15 +1,13 @@
package cc.carm.lib.mineconfiguration.spigot.source; package cc.carm.lib.mineconfiguration.spigot.source;
import cc.carm.lib.configuration.core.ConfigInitializer; import cc.carm.lib.configuration.core.ConfigInitializer;
import cc.carm.lib.configuration.core.source.ConfigurationComments;
import cc.carm.lib.mineconfiguration.bukkit.source.CraftConfigProvider; import cc.carm.lib.mineconfiguration.bukkit.source.CraftConfigProvider;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.io.File; import java.io.File;
import java.util.Collections;
import java.util.List;
public class SpigotConfigProvider extends CraftConfigProvider { public class SpigotConfigProvider extends CraftConfigProvider {
@ -17,38 +15,18 @@ public class SpigotConfigProvider extends CraftConfigProvider {
super(file); super(file);
} }
protected SpigotYAMLComments comments = null;
@Override @Override
public void initializeConfig() { public void initializeConfig() {
this.configuration = YamlConfiguration.loadConfiguration(file); this.configuration = YamlConfiguration.loadConfiguration(file);
this.comments = new SpigotYAMLComments(configuration);
this.initializer = new ConfigInitializer<>(this); this.initializer = new ConfigInitializer<>(this);
} }
@Override @Override
public void setHeaderComment(@Nullable String path, @Nullable List<String> comments) { public @Nullable ConfigurationComments getComments() {
if (path == null) { return this.comments;
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));
}
} }

View File

@ -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));
}
}

View File

@ -9,12 +9,11 @@
<maven.compiler.target>${project.jdk.version}</maven.compiler.target> <maven.compiler.target>${project.jdk.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding> <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<easyconfiguration.version>3.1.0</easyconfiguration.version>
</properties> </properties>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>mineconfiguration-parent</artifactId> <artifactId>mineconfiguration-parent</artifactId>
<version>2.0.2</version> <version>2.1.0</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<modules> <modules>
<module>common</module> <module>common</module>