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:
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.1.2</version>
|
<version>1.1.3</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.1.2</version>
|
<version>1.1.3</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.1.2</version>
|
<version>1.1.3</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.1.2</version>
|
<version>1.1.3</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
@@ -57,6 +57,73 @@
|
|||||||
<version>${project.parent.version}</version>
|
<version>${project.parent.version}</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</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>
|
</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>
|
</project>
|
||||||
+1
-4
@@ -49,10 +49,7 @@ public class ConfigStringCast<V> extends FileConfigCachedValue<V> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void set(@Nullable String value) {
|
public void set(@Nullable String value) {
|
||||||
getSourceOptional().ifPresent(source -> {
|
setIfPresent(value, true);
|
||||||
source.getConfig().set(getSectionName(), value);
|
|
||||||
source.save();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+59
-21
@@ -1,15 +1,17 @@
|
|||||||
package cc.carm.lib.easyplugin.configuration.file;
|
package cc.carm.lib.easyplugin.configuration.file;
|
||||||
|
|
||||||
|
|
||||||
|
import com.tchristofferson.configupdater.ConfigUpdater;
|
||||||
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.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.*;
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||||
public class FileConfig {
|
public class FileConfig {
|
||||||
|
|
||||||
public static Supplier<FileConfig> pluginConfiguration = null;
|
public static Supplier<FileConfig> pluginConfiguration = null;
|
||||||
@@ -28,34 +30,69 @@ public class FileConfig {
|
|||||||
private long updateTime;
|
private long updateTime;
|
||||||
|
|
||||||
private final JavaPlugin plugin;
|
private final JavaPlugin plugin;
|
||||||
|
private final File fileFolder;
|
||||||
private final String fileName;
|
private final String fileName;
|
||||||
|
private final String resourcePath;
|
||||||
|
|
||||||
private File file;
|
private File file;
|
||||||
private FileConfiguration config;
|
private FileConfiguration config;
|
||||||
|
|
||||||
public FileConfig(final JavaPlugin plugin) {
|
public FileConfig(@NotNull JavaPlugin plugin) throws IOException {
|
||||||
this(plugin, "config.yml");
|
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.plugin = plugin;
|
||||||
this.fileName = name;
|
this.resourcePath = resourcePath;
|
||||||
|
this.fileFolder = fileFolder;
|
||||||
|
this.fileName = fileName;
|
||||||
|
|
||||||
initFile();
|
initFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initFile() {
|
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() + " !");
|
||||||
|
}
|
||||||
|
|
||||||
|
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.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();
|
|
||||||
}
|
|
||||||
plugin.saveResource(fileName, true);
|
|
||||||
}
|
|
||||||
this.config = YamlConfiguration.loadConfiguration(this.file);
|
this.config = YamlConfiguration.loadConfiguration(this.file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public File getFileFolder() {
|
||||||
|
return fileFolder;
|
||||||
|
}
|
||||||
|
|
||||||
public File getFile() {
|
public File getFile() {
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
@@ -64,15 +101,16 @@ public class FileConfig {
|
|||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void save() {
|
public JavaPlugin getPlugin() {
|
||||||
try {
|
return plugin;
|
||||||
getConfig().save(getFile());
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
this.updateTime = System.currentTimeMillis();
|
||||||
if (getFile().exists()) {
|
if (getFile().exists()) {
|
||||||
this.config = YamlConfiguration.loadConfiguration(getFile());
|
this.config = YamlConfiguration.loadConfiguration(getFile());
|
||||||
|
|||||||
+24
-1
@@ -4,6 +4,7 @@ import org.bukkit.configuration.file.FileConfiguration;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
public abstract class FileConfigValue {
|
public abstract class FileConfigValue {
|
||||||
@@ -25,7 +26,29 @@ public abstract class FileConfigValue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void save() {
|
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() {
|
public @Nullable FileConfig getSource() {
|
||||||
|
|||||||
+9
-6
@@ -32,8 +32,14 @@ public class ConfigValue<V> extends FileConfigValue {
|
|||||||
|
|
||||||
public V get() {
|
public V get() {
|
||||||
return getConfigOptional()
|
return getConfigOptional()
|
||||||
.map(config -> castValue(config.get(getSectionName()), clazz, this.defaultValue))
|
.map(config -> {
|
||||||
.orElse(setDefault()); // 如果没有默认值,就把配置写进去,便于配置
|
if (config.contains(getSectionName())) {
|
||||||
|
return castValue(config.get(getSectionName()), clazz, this.defaultValue);
|
||||||
|
} else {
|
||||||
|
return setDefault(); // 如果没有默认值,就把配置写进去,便于配置
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.orElse(defaultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
public @NotNull Optional<V> getOptional() {
|
public @NotNull Optional<V> getOptional() {
|
||||||
@@ -41,10 +47,7 @@ public class ConfigValue<V> extends FileConfigValue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void set(@Nullable V value) {
|
public void set(@Nullable V value) {
|
||||||
getSourceOptional().ifPresent(source -> {
|
setIfPresent(value, true);
|
||||||
source.getConfig().set(getSectionName(), value);
|
|
||||||
source.save();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public V setDefault() {
|
public V setDefault() {
|
||||||
|
|||||||
+1
-4
@@ -57,10 +57,7 @@ public class ConfigValueList<V> extends FileConfigValue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void set(@Nullable ArrayList<V> value) {
|
public void set(@Nullable ArrayList<V> value) {
|
||||||
getSourceOptional().ifPresent(source -> {
|
setIfPresent(value, true);
|
||||||
source.getConfig().set(getSectionName(), value);
|
|
||||||
source.save();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+1
-4
@@ -50,10 +50,7 @@ public class ConfigValueMap<K, V> extends FileConfigCachedValue<Map<K, V>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void set(@Nullable Map<K, V> valuesMap) {
|
public void set(@Nullable Map<K, V> valuesMap) {
|
||||||
getSourceOptional().ifPresent(source -> {
|
createSection(valuesMap);
|
||||||
source.getConfig().createSection(getSectionName(), valuesMap);
|
|
||||||
source.save();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.1.2</version>
|
<version>1.1.3</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
@@ -54,7 +54,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<artifactId>easysql-beecp</artifactId>
|
<artifactId>easysql-beecp</artifactId>
|
||||||
<version>0.2.4</version>
|
<version>0.2.5</version>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.1.2</version>
|
<version>1.1.3</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.1.2</version>
|
<version>1.1.3</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.1.2</version>
|
<version>1.1.3</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.1.2</version>
|
<version>1.1.3</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.1.2</version>
|
<version>1.1.3</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
<version>1.1.2</version>
|
<version>1.1.3</version>
|
||||||
<modules>
|
<modules>
|
||||||
<module>easyplugin-main</module>
|
<module>easyplugin-main</module>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user