mirror of
https://github.com/CarmJos/MineConfiguration.git
synced 2024-09-19 20:05:49 +00:00
feat(comments): 适配上游对配置文件保存方式的更新。
This commit is contained in:
parent
3d1639362b
commit
c28ee7d57b
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>mineconfiguration-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>2.5.0</version>
|
||||
<version>2.5.1</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<properties>
|
||||
|
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>mineconfiguration-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>2.5.0</version>
|
||||
<version>2.5.1</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
@ -30,6 +30,13 @@
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>yamlcommentwriter</artifactId>
|
||||
<version>${deps.yamlcommentwriter.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<!--suppress VulnerableLibrariesLocal -->
|
||||
<dependency>
|
||||
<groupId>org.bukkit</groupId>
|
||||
|
@ -1,6 +1,7 @@
|
||||
package cc.carm.lib.mineconfiguration.bukkit.data;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
@ -40,6 +41,12 @@ public class SoundConfig {
|
||||
player.playSound(player.getLocation(), type, volume, pitch);
|
||||
}
|
||||
|
||||
public void playAt(Location location) {
|
||||
if (type == null) return;
|
||||
if (location.getWorld() == null) return;
|
||||
location.getWorld().playSound(location, type, volume, pitch);
|
||||
}
|
||||
|
||||
public void playToAll() {
|
||||
Bukkit.getOnlinePlayers().forEach(this::playTo);
|
||||
}
|
||||
|
@ -2,22 +2,23 @@ package cc.carm.lib.mineconfiguration.bukkit.source;
|
||||
|
||||
import cc.carm.lib.configuration.core.ConfigInitializer;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationComments;
|
||||
import cc.carm.lib.yamlcommentupdater.CommentedYAML;
|
||||
import cc.carm.lib.yamlcommentupdater.CommentedYAMLWriter;
|
||||
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 java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.StringWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class BukkitConfigProvider extends CraftConfigProvider {
|
||||
public class BukkitConfigProvider extends CraftConfigProvider implements CommentedYAML {
|
||||
|
||||
protected static final char SEPARATOR = '.';
|
||||
|
||||
protected @NotNull BukkitYAMLComments comments = new BukkitYAMLComments();
|
||||
protected @NotNull ConfigurationComments comments = new ConfigurationComments();
|
||||
|
||||
public BukkitConfigProvider(@NotNull File file) {
|
||||
super(file);
|
||||
@ -35,21 +36,44 @@ public class BukkitConfigProvider extends CraftConfigProvider {
|
||||
|
||||
@Override
|
||||
public void save() throws Exception {
|
||||
configuration.save(getFile());
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
this.comments.writeComments(configuration, new BufferedWriter(writer));
|
||||
String value = writer.toString(); // config contents
|
||||
|
||||
Path toUpdatePath = getFile().toPath();
|
||||
if (!value.equals(new String(Files.readAllBytes(toUpdatePath), StandardCharsets.UTF_8))) {
|
||||
Files.write(toUpdatePath, value.getBytes(StandardCharsets.UTF_8));
|
||||
try {
|
||||
CommentedYAMLWriter.writeWithComments(this, this.file);
|
||||
} catch (Exception ex) {
|
||||
configuration.save(file);
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable ConfigurationComments getComments() {
|
||||
public @NotNull ConfigurationComments getComments() {
|
||||
return this.comments;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String serializeValue(@NotNull String key, @NotNull Object value) {
|
||||
FileConfiguration temp = new YamlConfiguration();
|
||||
temp.set(key, value);
|
||||
return temp.saveToString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getKeys(@Nullable String sectionKey, boolean deep) {
|
||||
if (sectionKey == null) return configuration.getKeys(deep);
|
||||
|
||||
ConfigurationSection section = configuration.getConfigurationSection(sectionKey);
|
||||
if (section == null) return null;
|
||||
|
||||
return section.getKeys(deep);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Object getValue(@NotNull String key) {
|
||||
return configuration.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable List<String> getHeaderComments(@Nullable String key) {
|
||||
return comments.getHeaderComment(key);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,108 +0,0 @@
|
||||
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 java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.StringJoiner;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class BukkitYAMLComments extends ConfigurationComments {
|
||||
|
||||
public @Nullable String buildHeaderComments(@Nullable String path, @NotNull String indents) {
|
||||
List<String> comments = getHeaderComment(path);
|
||||
if (comments == null || comments.size() == 0) return null;
|
||||
|
||||
StringJoiner joiner = new StringJoiner("\n");
|
||||
for (String comment : comments) {
|
||||
if (comment.length() == 0) joiner.add(" ");
|
||||
else joiner.add(indents + "# " + comment);
|
||||
}
|
||||
return joiner + "\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* 从一个文件读取配置并写入注释到某个写入器中。
|
||||
* 该方法的部分源代码借鉴自 tchristofferson/ConfigUpdater 项目。
|
||||
*
|
||||
* @param source 源配置文件
|
||||
* @param writer 配置写入器
|
||||
* @throws IOException 当写入发生错误时抛出
|
||||
*/
|
||||
public void writeComments(@NotNull YamlConfiguration source, @NotNull BufferedWriter writer) throws IOException {
|
||||
FileConfiguration temp = new YamlConfiguration(); // 该对象用于临时记录配置内容
|
||||
|
||||
String configHeader = buildHeaderComments(null, "");
|
||||
if (configHeader != null) writer.write(configHeader);
|
||||
|
||||
for (String fullKey : source.getKeys(true)) {
|
||||
Object currentValue = source.get(fullKey);
|
||||
|
||||
String indents = getIndents(fullKey);
|
||||
String headerComments = buildHeaderComments(fullKey, indents);
|
||||
String inlineComment = getInlineComment(fullKey);
|
||||
|
||||
if (headerComments != null) writer.write(headerComments);
|
||||
|
||||
String[] splitFullKey = fullKey.split("[" + CraftConfigProvider.SEPARATOR + "]");
|
||||
String trailingKey = splitFullKey[splitFullKey.length - 1];
|
||||
|
||||
if (currentValue instanceof ConfigurationSection) {
|
||||
ConfigurationSection section = (ConfigurationSection) currentValue;
|
||||
writer.write(indents + trailingKey + ":");
|
||||
if (inlineComment != null && inlineComment.length() > 0) {
|
||||
writer.write(" # " + inlineComment);
|
||||
}
|
||||
if (!section.getKeys(false).isEmpty()) {
|
||||
writer.write("\n");
|
||||
} else {
|
||||
writer.write(" {}\n");
|
||||
if (indents.length() == 0) writer.write("\n");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
temp.set(trailingKey, currentValue);
|
||||
String yaml = temp.saveToString();
|
||||
temp.set(trailingKey, null);
|
||||
|
||||
yaml = yaml.substring(0, yaml.length() - 1);
|
||||
|
||||
if (inlineComment != null && inlineComment.length() > 0) {
|
||||
if (yaml.contains("\n")) {
|
||||
// section为多行内容,需要 InlineComment 加在首行末尾
|
||||
String[] splitLine = yaml.split("\n", 2);
|
||||
yaml = splitLine[0] + " # " + inlineComment + "\n" + splitLine[1];
|
||||
} else {
|
||||
// 其他情况下就直接加载后面就好。
|
||||
yaml += " # " + inlineComment;
|
||||
}
|
||||
}
|
||||
|
||||
writer.write(indents + yaml.replace("\n", "\n" + indents) + "\n");
|
||||
if (indents.length() == 0) writer.write("\n");
|
||||
}
|
||||
|
||||
writer.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到一个键的缩进。
|
||||
* 该方法的源代码来自 tchristofferson/ConfigUpdater 项目。
|
||||
*
|
||||
* @param key 键
|
||||
* @return 该键的缩进文本
|
||||
*/
|
||||
protected static String getIndents(String key) {
|
||||
String[] splitKey = key.split("[" + BukkitConfigProvider.SEPARATOR + "]");
|
||||
return IntStream.range(1, splitKey.length).mapToObj(i -> " ").collect(Collectors.joining());
|
||||
}
|
||||
|
||||
}
|
@ -6,6 +6,7 @@ import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
|
||||
import cc.carm.lib.mineconfiguration.bukkit.CraftConfigValue;
|
||||
import cc.carm.lib.mineconfiguration.bukkit.builder.sound.SoundConfigBuilder;
|
||||
import cc.carm.lib.mineconfiguration.bukkit.data.SoundConfig;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@ -66,6 +67,10 @@ public class ConfiguredSound extends ConfiguredValue<SoundConfig> {
|
||||
Optional.ofNullable(get()).ifPresent(SoundConfig::playToAll);
|
||||
}
|
||||
|
||||
public void playAt(Location location) {
|
||||
Optional.ofNullable(get()).ifPresent(s -> s.playAt(location));
|
||||
}
|
||||
|
||||
public static ConfigValueParser<Object, SoundConfig> getSoundParser() {
|
||||
return ConfigValueParser.castToString().andThen((s, d) -> SoundConfig.deserialize(s));
|
||||
}
|
||||
|
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>mineconfiguration-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>2.5.0</version>
|
||||
<version>2.5.1</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
@ -30,6 +30,13 @@
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>yamlcommentwriter</artifactId>
|
||||
<version>${deps.yamlcommentwriter.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>net.md-5</groupId>
|
||||
<artifactId>bungeecord-api</artifactId>
|
||||
|
@ -3,21 +3,21 @@ 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 cc.carm.lib.yamlcommentupdater.CommentedYAML;
|
||||
import cc.carm.lib.yamlcommentupdater.CommentedYAMLWriter;
|
||||
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 java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class BungeeConfigProvider extends FileConfigProvider<BungeeSectionWrapper> {
|
||||
public class BungeeConfigProvider extends FileConfigProvider<BungeeSectionWrapper> implements CommentedYAML {
|
||||
|
||||
protected static final char SEPARATOR = '.';
|
||||
|
||||
@ -25,7 +25,7 @@ public class BungeeConfigProvider extends FileConfigProvider<BungeeSectionWrappe
|
||||
protected Configuration configuration;
|
||||
protected ConfigInitializer<BungeeConfigProvider> initializer;
|
||||
|
||||
protected BungeeYAMLComments comments = new BungeeYAMLComments();
|
||||
protected ConfigurationComments comments = new ConfigurationComments();
|
||||
|
||||
public BungeeConfigProvider(@NotNull File file, @NotNull ConfigurationProvider loader) {
|
||||
super(file);
|
||||
@ -52,22 +52,17 @@ public class BungeeConfigProvider extends FileConfigProvider<BungeeSectionWrappe
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable ConfigurationComments getComments() {
|
||||
public @NotNull ConfigurationComments getComments() {
|
||||
return this.comments;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save() throws Exception {
|
||||
getLoader().save(configuration, file);
|
||||
if (getLoader() instanceof YamlConfiguration) {
|
||||
StringWriter writer = new StringWriter();
|
||||
this.comments.writeComments(configuration, new BufferedWriter(writer));
|
||||
String value = writer.toString(); // config contents
|
||||
|
||||
Path toUpdatePath = getFile().toPath();
|
||||
if (!value.equals(new String(Files.readAllBytes(toUpdatePath), StandardCharsets.UTF_8))) {
|
||||
Files.write(toUpdatePath, value.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
try {
|
||||
CommentedYAMLWriter.writeWithComments(this, this.file);
|
||||
} catch (Exception ex) {
|
||||
getLoader().save(configuration, file);
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
@ -79,4 +74,34 @@ public class BungeeConfigProvider extends FileConfigProvider<BungeeSectionWrappe
|
||||
public ConfigurationProvider getLoader() {
|
||||
return loader;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String serializeValue(@NotNull String key, @NotNull Object value) {
|
||||
Configuration tmp = new Configuration();// 该对象用于临时记录配置内容
|
||||
tmp.set(key, value);
|
||||
StringWriter tmpStr = new StringWriter();
|
||||
loader.save(tmp, tmpStr);
|
||||
return tmpStr.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getKeys(@Nullable String sectionKey, boolean deep) {
|
||||
if (sectionKey == null) return BungeeSectionWrapper.getAllKeys(this.configuration);
|
||||
|
||||
Configuration section = configuration.getSection(sectionKey);
|
||||
if (section == null) return null;
|
||||
|
||||
return new HashSet<>(section.getKeys());
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Object getValue(@NotNull String key) {
|
||||
return configuration.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable List<String> getHeaderComments(@Nullable String key) {
|
||||
return comments.getHeaderComment(key);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,115 +0,0 @@
|
||||
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 java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
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 extends ConfigurationComments {
|
||||
|
||||
public @Nullable String buildHeaderComments(@Nullable String path, @NotNull String indents) {
|
||||
List<String> comments = getHeaderComment(path);
|
||||
if (comments == null || comments.size() == 0) return null;
|
||||
|
||||
StringJoiner joiner = new StringJoiner("\n");
|
||||
for (String comment : comments) {
|
||||
if (comment.length() == 0) joiner.add(" ");
|
||||
else joiner.add(indents + "# " + comment);
|
||||
}
|
||||
return joiner + "\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* 从一个文件读取配置并写入注释到某个写入器中。
|
||||
* 该方法的部分源代码借鉴自 tchristofferson/ConfigUpdater 项目。
|
||||
*
|
||||
* @param source 源配置文件
|
||||
* @param writer 配置写入器
|
||||
* @throws IOException 当写入发生错误时抛出
|
||||
*/
|
||||
public void writeComments(@NotNull Configuration source, @NotNull BufferedWriter writer) throws IOException {
|
||||
ConfigurationProvider provider = ConfigurationProvider.getProvider(YamlConfiguration.class);
|
||||
Configuration tmp = new Configuration();// 该对象用于临时记录配置内容
|
||||
|
||||
String configHeader = buildHeaderComments(null, "");
|
||||
if (configHeader != null) writer.write(configHeader);
|
||||
|
||||
for (String fullKey : BungeeSectionWrapper.getAllKeys(source)) {
|
||||
Object currentValue = source.get(fullKey);
|
||||
|
||||
String indents = getIndents(fullKey);
|
||||
String headerComments = buildHeaderComments(fullKey, indents);
|
||||
String inlineComment = getInlineComment(fullKey);
|
||||
|
||||
if (headerComments != null) writer.write(headerComments);
|
||||
|
||||
String[] splitFullKey = fullKey.split("[" + SEPARATOR + "]");
|
||||
String trailingKey = splitFullKey[splitFullKey.length - 1];
|
||||
|
||||
if (currentValue instanceof Configuration) {
|
||||
Configuration section = (Configuration) currentValue;
|
||||
writer.write(indents + trailingKey + ":");
|
||||
if (inlineComment != null && inlineComment.length() > 0) {
|
||||
writer.write(" # " + inlineComment);
|
||||
}
|
||||
if (!section.getKeys().isEmpty()) {
|
||||
writer.write("\n");
|
||||
} else {
|
||||
writer.write(" {}\n");
|
||||
if (indents.length() == 0) writer.write("\n");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
tmp.set(trailingKey, currentValue);
|
||||
StringWriter tmpStr = new StringWriter();
|
||||
provider.save(tmp, tmpStr);
|
||||
String yaml = tmpStr.toString();
|
||||
tmpStr.close();
|
||||
tmp.set(trailingKey, null);
|
||||
|
||||
yaml = yaml.substring(0, yaml.length() - 1);
|
||||
|
||||
if (inlineComment != null && inlineComment.length() > 0) {
|
||||
if (yaml.contains("\n")) {
|
||||
// section为多行内容,需要 InlineComment 加在首行末尾
|
||||
String[] splitLine = yaml.split("\n", 2);
|
||||
yaml = splitLine[0] + " # " + inlineComment + "\n" + splitLine[1];
|
||||
} else {
|
||||
// 其他情况下就直接加载后面就好。
|
||||
yaml += " # " + inlineComment;
|
||||
}
|
||||
}
|
||||
|
||||
writer.write(indents + yaml.replace("\n", "\n" + indents) + "\n");
|
||||
if (indents.length() == 0) writer.write("\n");
|
||||
}
|
||||
|
||||
writer.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到一个键的缩进。
|
||||
* 该方法的源代码来自 tchristofferson/ConfigUpdater 项目。
|
||||
*
|
||||
* @param key 键
|
||||
* @return 该键的缩进文本
|
||||
*/
|
||||
protected static String getIndents(@NotNull String key) {
|
||||
String[] splitKey = key.split("[" + SEPARATOR + "]");
|
||||
return IntStream.range(1, splitKey.length).mapToObj(i -> " ").collect(Collectors.joining());
|
||||
}
|
||||
|
||||
}
|
5
pom.xml
5
pom.xml
@ -10,13 +10,14 @@
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||
|
||||
<deps.easyconfifuration.version>3.5.0</deps.easyconfifuration.version>
|
||||
<deps.easyconfifuration.version>3.5.1</deps.easyconfifuration.version>
|
||||
<deps.easyplugin.version>1.5.4</deps.easyplugin.version>
|
||||
<deps.yamlcommentwriter.version>1.0.1</deps.yamlcommentwriter.version>
|
||||
</properties>
|
||||
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>mineconfiguration-parent</artifactId>
|
||||
<version>2.5.0</version>
|
||||
<version>2.5.1</version>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>common</module>
|
||||
|
Loading…
Reference in New Issue
Block a user