1
mirror of https://github.com/CarmJos/EasyPlugin.git synced 2026-06-05 00:58:17 +08:00

[v1.1.3] 版本更新

- [U] 采用 ConfigUpdater 项目,解决config中注释丢失的问题。
This commit is contained in:
2022-01-06 16:19:15 +08:00
parent 54bf188c5c
commit 024efc7690
17 changed files with 173 additions and 51 deletions
+68 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.1.2</version>
<version>1.1.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@@ -57,6 +57,73 @@
<version>${project.parent.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<!--
ConfigUpdater - to save the comments in default configuration.
-> https://github.com/tchristofferson/Config-Updater
-->
<groupId>com.tchristofferson</groupId>
<artifactId>ConfigUpdater</artifactId>
<version>2.0-SNAPSHOT</version>
<optional>true</optional>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>
</executions>
<configuration>
<relocations>
<relocation>
<pattern>com.tchristofferson.configupdater</pattern>
<shadedPattern>cc.carm.lib.easyplugin.configuration.updater</shadedPattern>
</relocation>
</relocations>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/MANIFEST.MF</exclude>
<exclude>META-INF/*.txt</exclude>
</excludes>
</filter>
</filters>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -49,10 +49,7 @@ public class ConfigStringCast<V> extends FileConfigCachedValue<V> {
}
public void set(@Nullable String value) {
getSourceOptional().ifPresent(source -> {
source.getConfig().set(getSectionName(), value);
source.save();
});
setIfPresent(value, true);
}
}
@@ -1,15 +1,17 @@
package cc.carm.lib.easyplugin.configuration.file;
import com.tchristofferson.configupdater.ConfigUpdater;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.IOException;
import java.io.*;
import java.util.function.Supplier;
@SuppressWarnings("ResultOfMethodCallIgnored")
public class FileConfig {
public static Supplier<FileConfig> pluginConfiguration = null;
@@ -28,34 +30,69 @@ public class FileConfig {
private long updateTime;
private final JavaPlugin plugin;
private final File fileFolder;
private final String fileName;
private final String resourcePath;
private File file;
private FileConfiguration config;
public FileConfig(final JavaPlugin plugin) {
public FileConfig(@NotNull JavaPlugin plugin) throws IOException {
this(plugin, "config.yml");
}
public FileConfig(final JavaPlugin plugin, final String name) {
public FileConfig(@NotNull JavaPlugin plugin,
@NotNull String fileName) throws IOException {
this(plugin, fileName, fileName);
}
public FileConfig(@NotNull JavaPlugin plugin, @NotNull String resourcePath,
@NotNull String fileName) throws IOException {
this(plugin, resourcePath, plugin.getDataFolder(), fileName);
}
public FileConfig(@NotNull JavaPlugin plugin, @NotNull String resourcePath,
@NotNull File fileFolder, @NotNull String fileName) throws IOException {
this.plugin = plugin;
this.fileName = name;
this.resourcePath = resourcePath;
this.fileFolder = fileFolder;
this.fileName = fileName;
initFile();
}
private void initFile() {
this.updateTime = System.currentTimeMillis();
this.file = new File(plugin.getDataFolder(), fileName);
if (!this.file.exists()) {
if (!this.file.getParentFile().exists()) {
boolean success = this.file.getParentFile().mkdirs();
private void initFile() throws IOException {
if (!getFileFolder().exists()) getFileFolder().mkdirs();
this.file = new File(getFileFolder(), fileName);
if (!file.exists()) {
InputStream resourceStream = plugin.getResource(resourcePath);
if (resourceStream == null) {
throw new IOException("The resource " + resourcePath + " cannot find in " + plugin.getName() + " !");
}
plugin.saveResource(fileName, true);
OutputStream out = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int readBytes;
while ((readBytes = resourceStream.read(buffer)) > 0) {
out.write(buffer, 0, readBytes);
}
out.close();
resourceStream.close();
ConfigUpdater.update(plugin, resourcePath, file); // Save comments
}
this.updateTime = System.currentTimeMillis();
this.config = YamlConfiguration.loadConfiguration(this.file);
}
public File getFileFolder() {
return fileFolder;
}
public File getFile() {
return file;
}
@@ -64,15 +101,16 @@ public class FileConfig {
return config;
}
public void save() {
try {
getConfig().save(getFile());
} catch (IOException e) {
e.printStackTrace();
}
public JavaPlugin getPlugin() {
return plugin;
}
public void reload() {
public void save() throws IOException {
getConfig().save(getFile());
ConfigUpdater.update(plugin, resourcePath, file); // Save comments
}
public void reload() throws IOException {
this.updateTime = System.currentTimeMillis();
if (getFile().exists()) {
this.config = YamlConfiguration.loadConfiguration(getFile());
@@ -4,6 +4,7 @@ import org.bukkit.configuration.file.FileConfiguration;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
import java.util.Optional;
public abstract class FileConfigValue {
@@ -25,7 +26,29 @@ public abstract class FileConfigValue {
}
public void save() {
getSourceOptional().ifPresent(FileConfig::save);
getSourceOptional().ifPresent(fileConfig -> {
try {
fileConfig.save();
} catch (Exception ex) {
fileConfig.getPlugin().getLogger().severe("Could not save the " + fileConfig.getFile() + " .");
ex.printStackTrace();
}
});
}
public void setIfPresent(@Nullable Object value, boolean save) {
getConfigOptional().ifPresent(configuration -> configuration.set(getSectionName(), value));
if (save) save();
}
public void createSection(Map<?, ?> values) {
getConfigOptional().ifPresent(configuration -> {
if (values == null) {
configuration.set(getSectionName(), null);
} else {
configuration.createSection(getSectionName(), values);
}
});
}
public @Nullable FileConfig getSource() {
@@ -32,8 +32,14 @@ public class ConfigValue<V> extends FileConfigValue {
public V get() {
return getConfigOptional()
.map(config -> castValue(config.get(getSectionName()), clazz, this.defaultValue))
.orElse(setDefault()); // 如果没有默认值,就把配置写进去,便于配置
.map(config -> {
if (config.contains(getSectionName())) {
return castValue(config.get(getSectionName()), clazz, this.defaultValue);
} else {
return setDefault(); // 如果没有默认值,就把配置写进去,便于配置
}
})
.orElse(defaultValue);
}
public @NotNull Optional<V> getOptional() {
@@ -41,10 +47,7 @@ public class ConfigValue<V> extends FileConfigValue {
}
public void set(@Nullable V value) {
getSourceOptional().ifPresent(source -> {
source.getConfig().set(getSectionName(), value);
source.save();
});
setIfPresent(value, true);
}
public V setDefault() {
@@ -57,10 +57,7 @@ public class ConfigValueList<V> extends FileConfigValue {
}
public void set(@Nullable ArrayList<V> value) {
getSourceOptional().ifPresent(source -> {
source.getConfig().set(getSectionName(), value);
source.save();
});
setIfPresent(value, true);
}
@@ -50,10 +50,7 @@ public class ConfigValueMap<K, V> extends FileConfigCachedValue<Map<K, V>> {
}
public void set(@Nullable Map<K, V> valuesMap) {
getSourceOptional().ifPresent(source -> {
source.getConfig().createSection(getSectionName(), valuesMap);
source.save();
});
createSection(valuesMap);
}
}