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

feat(comments): 适配上游对配置文件保存方式的更新。

This commit is contained in:
2023-03-20 20:28:40 +08:00
parent 3d1639362b
commit c28ee7d57b
10 changed files with 116 additions and 263 deletions
@@ -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));
}