mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-04 10:38:19 +08:00
[1.2.0] 版本更新
- `[R]` 独立 bungee 和 bukkit 平台版本到 MineConfiguration 项目。 - `[U]` 重写Initializer逻辑,并支持根据成员变量顺序加载配置文件。
This commit is contained in:
+1
-1
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyconfiguration-parent</artifactId>
|
<artifactId>easyconfiguration-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.1.0</version>
|
<version>1.2.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<properties>
|
<properties>
|
||||||
|
|||||||
+1
-1
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyconfiguration-parent</artifactId>
|
<artifactId>easyconfiguration-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.1.0</version>
|
<version>1.2.0</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|||||||
@@ -1,68 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
||||||
<parent>
|
|
||||||
<artifactId>easyconfiguration-parent</artifactId>
|
|
||||||
<groupId>cc.carm.lib</groupId>
|
|
||||||
<version>1.1.0</version>
|
|
||||||
<relativePath>../../pom.xml</relativePath>
|
|
||||||
</parent>
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
|
||||||
<properties>
|
|
||||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
|
||||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
|
||||||
</properties>
|
|
||||||
|
|
||||||
<artifactId>easyconfiguration-bungee</artifactId>
|
|
||||||
<packaging>jar</packaging>
|
|
||||||
|
|
||||||
<dependencies>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>${project.parent.groupId}</groupId>
|
|
||||||
<artifactId>easyconfiguration-core</artifactId>
|
|
||||||
<version>${project.parent.version}</version>
|
|
||||||
<scope>compile</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>net.md-5</groupId>
|
|
||||||
<artifactId>bungeecord-api</artifactId>
|
|
||||||
<version>1.18-R0.1-SNAPSHOT</version>
|
|
||||||
<type>jar</type>
|
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>net.md-5</groupId>
|
|
||||||
<artifactId>bungeecord-api</artifactId>
|
|
||||||
<version>1.18-R0.1-SNAPSHOT</version>
|
|
||||||
<type>javadoc</type>
|
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
</dependencies>
|
|
||||||
|
|
||||||
<build>
|
|
||||||
<plugins>
|
|
||||||
<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-javadoc-plugin</artifactId>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
</build>
|
|
||||||
|
|
||||||
</project>
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
package cc.carm.lib.configuration;
|
|
||||||
|
|
||||||
import cc.carm.lib.configuration.bungee.BungeeConfigProvider;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public class EasyConfiguration {
|
|
||||||
|
|
||||||
public static BungeeConfigProvider from(File file, String source) {
|
|
||||||
BungeeConfigProvider provider = new BungeeConfigProvider(file);
|
|
||||||
try {
|
|
||||||
provider.initializeFile(source);
|
|
||||||
provider.initializeConfig();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return provider;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static BungeeConfigProvider from(File file) {
|
|
||||||
return from(file, file.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static BungeeConfigProvider from(String fileName) {
|
|
||||||
return from(fileName, fileName);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static BungeeConfigProvider from(String fileName, String source) {
|
|
||||||
return from(new File(fileName), source);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
-61
@@ -1,61 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.bungee;
|
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.ConfigInitializer;
|
|
||||||
import cc.carm.lib.configuration.core.source.impl.FileConfigProvider;
|
|
||||||
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.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public class BungeeConfigProvider extends FileConfigProvider<BungeeSectionWrapper> {
|
|
||||||
|
|
||||||
protected Configuration configuration;
|
|
||||||
protected ConfigInitializer<BungeeConfigProvider> initializer;
|
|
||||||
|
|
||||||
public BungeeConfigProvider(@NotNull File file) {
|
|
||||||
super(file);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void initializeConfig() throws IOException {
|
|
||||||
this.configuration = getLoader().load(file);
|
|
||||||
this.initializer = new ConfigInitializer<>(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull BungeeSectionWrapper getConfiguration() {
|
|
||||||
return BungeeSectionWrapper.of(configuration);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void reload() throws Exception {
|
|
||||||
this.configuration = getLoader().load(file);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void save() throws Exception {
|
|
||||||
getLoader().save(configuration, file);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setComments(@NotNull String path, @NotNull String... comments) {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @Nullable String[] getComments(@NotNull String path) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull ConfigInitializer<BungeeConfigProvider> getInitializer() {
|
|
||||||
return this.initializer;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ConfigurationProvider getLoader() {
|
|
||||||
return ConfigurationProvider.getProvider(YamlConfiguration.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-70
@@ -1,70 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.bungee;
|
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
|
||||||
import net.md_5.bungee.config.Configuration;
|
|
||||||
import org.jetbrains.annotations.Contract;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
public class BungeeSectionWrapper implements ConfigurationWrapper {
|
|
||||||
|
|
||||||
private final Configuration section;
|
|
||||||
|
|
||||||
private BungeeSectionWrapper(Configuration section) {
|
|
||||||
this.section = section;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Contract("!null->!null")
|
|
||||||
public static @Nullable BungeeSectionWrapper of(@Nullable Configuration section) {
|
|
||||||
return section == null ? null : new BungeeSectionWrapper(section);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull Set<String> getKeys(boolean deep) {
|
|
||||||
return new LinkedHashSet<>(section.getKeys());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull Map<String, Object> getValues(boolean deep) {
|
|
||||||
return section.getKeys().stream()
|
|
||||||
.collect(Collectors.toMap(key -> key, section::get, (a, b) -> b, LinkedHashMap::new));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void set(@NotNull String path, @Nullable Object value) {
|
|
||||||
this.section.set(path, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean contains(@NotNull String path) {
|
|
||||||
return this.section.contains(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @Nullable Object get(@NotNull String path) {
|
|
||||||
return this.section.get(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isList(@NotNull String path) {
|
|
||||||
return get(path) instanceof List<?>;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @Nullable List<?> getList(@NotNull String path) {
|
|
||||||
return this.section.getList(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isConfigurationSection(@NotNull String path) {
|
|
||||||
return true; // No provided functions :( SRY
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @Nullable ConfigurationWrapper getConfigurationSection(@NotNull String path) {
|
|
||||||
return of(this.section.getSection(path));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,60 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
||||||
<parent>
|
|
||||||
<artifactId>easyconfiguration-parent</artifactId>
|
|
||||||
<groupId>cc.carm.lib</groupId>
|
|
||||||
<version>1.1.0</version>
|
|
||||||
<relativePath>../../pom.xml</relativePath>
|
|
||||||
</parent>
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
|
||||||
<properties>
|
|
||||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
|
||||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
|
||||||
</properties>
|
|
||||||
|
|
||||||
<artifactId>easyconfiguration-spigot</artifactId>
|
|
||||||
<packaging>jar</packaging>
|
|
||||||
|
|
||||||
<dependencies>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>${project.parent.groupId}</groupId>
|
|
||||||
<artifactId>easyconfiguration-core</artifactId>
|
|
||||||
<version>${project.parent.version}</version>
|
|
||||||
<scope>compile</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.spigotmc</groupId>
|
|
||||||
<artifactId>spigot-api</artifactId>
|
|
||||||
<version>1.18.2-R0.1-SNAPSHOT</version>
|
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
</dependencies>
|
|
||||||
|
|
||||||
<build>
|
|
||||||
<plugins>
|
|
||||||
<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-javadoc-plugin</artifactId>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
</build>
|
|
||||||
|
|
||||||
|
|
||||||
</project>
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
package cc.carm.lib.configuration;
|
|
||||||
|
|
||||||
import cc.carm.lib.configuration.spigot.SpigotConfigProvider;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public class EasyConfiguration {
|
|
||||||
|
|
||||||
|
|
||||||
public static SpigotConfigProvider from(File file, String source) {
|
|
||||||
SpigotConfigProvider provider = new SpigotConfigProvider(file);
|
|
||||||
try {
|
|
||||||
provider.initializeFile(source);
|
|
||||||
provider.initializeConfig();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return provider;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static SpigotConfigProvider from(File file) {
|
|
||||||
return from(file, file.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static SpigotConfigProvider from(String fileName) {
|
|
||||||
return from(fileName, fileName);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static SpigotConfigProvider from(String fileName, String source) {
|
|
||||||
return from(new File(fileName), source);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
-1513
File diff suppressed because it is too large
Load Diff
@@ -1,73 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.commented;
|
|
||||||
|
|
||||||
import org.yaml.snakeyaml.DumperOptions;
|
|
||||||
import org.yaml.snakeyaml.Yaml;
|
|
||||||
import org.yaml.snakeyaml.constructor.BaseConstructor;
|
|
||||||
import org.yaml.snakeyaml.error.YAMLException;
|
|
||||||
import org.yaml.snakeyaml.nodes.Node;
|
|
||||||
import org.yaml.snakeyaml.nodes.Tag;
|
|
||||||
import org.yaml.snakeyaml.representer.Representer;
|
|
||||||
import org.yaml.snakeyaml.serializer.Serializer;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.StringWriter;
|
|
||||||
import java.io.Writer;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A hacky extension of {@link Yaml} that allows to write comments when dumping.
|
|
||||||
*/
|
|
||||||
public class CommentedYaml extends Yaml {
|
|
||||||
|
|
||||||
private final CommentsProvider commentsProvider;
|
|
||||||
|
|
||||||
public CommentedYaml(BaseConstructor constructor, Representer representer, DumperOptions dumperOptions, CommentsProvider commentsProvider) {
|
|
||||||
super(constructor, representer, dumperOptions);
|
|
||||||
this.commentsProvider = commentsProvider;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String dump(Object data) {
|
|
||||||
List<Object> list = new ArrayList<>(1);
|
|
||||||
list.add(data);
|
|
||||||
return this.dumpAll(list.iterator());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void dump(Object data, Writer output) {
|
|
||||||
List<Object> list = new ArrayList<>(1);
|
|
||||||
list.add(data);
|
|
||||||
this.dumpAll(list.iterator(), output, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String dumpAll(Iterator<?> data) {
|
|
||||||
StringWriter buffer = new StringWriter();
|
|
||||||
this.dumpAll(data, buffer, null);
|
|
||||||
return buffer.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void dumpAll(Iterator<?> data, Writer output) {
|
|
||||||
this.dumpAll(data, output, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void dumpAll(Iterator<?> data, Writer output, Tag rootTag) {
|
|
||||||
Serializer serializer = new Serializer(new CommentedEmitter(output, this.dumperOptions, this.commentsProvider), this.resolver, this.dumperOptions, rootTag);
|
|
||||||
|
|
||||||
try {
|
|
||||||
serializer.open();
|
|
||||||
|
|
||||||
while (data.hasNext()) {
|
|
||||||
Node node = this.representer.represent(data.next());
|
|
||||||
serializer.serialize(node);
|
|
||||||
}
|
|
||||||
|
|
||||||
serializer.close();
|
|
||||||
} catch (IOException var6) {
|
|
||||||
throw new YAMLException(var6);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-69
@@ -1,69 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.commented;
|
|
||||||
|
|
||||||
import org.bukkit.configuration.InvalidConfigurationException;
|
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
|
||||||
import org.bukkit.configuration.file.YamlConstructor;
|
|
||||||
import org.bukkit.configuration.file.YamlRepresenter;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.yaml.snakeyaml.DumperOptions;
|
|
||||||
import org.yaml.snakeyaml.representer.Representer;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.Reader;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A yaml file with comments on certain properties, as returned by the given {@link CommentsProvider}.
|
|
||||||
* Unlike {@link YamlConfiguration}, this class does not provide a header support.
|
|
||||||
*/
|
|
||||||
public class CommentedYamlConfiguration extends YamlConfiguration {
|
|
||||||
|
|
||||||
private final DumperOptions yamlOptions = new DumperOptions();
|
|
||||||
private final Representer yamlRepresenter = new YamlRepresenter();
|
|
||||||
private final CommentedYaml yaml;
|
|
||||||
|
|
||||||
public CommentedYamlConfiguration(CommentsProvider commentsProvider) {
|
|
||||||
this.yaml = new CommentedYaml(new YamlConstructor(), this.yamlRepresenter, this.yamlOptions, commentsProvider);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static CommentedYamlConfiguration loadConfiguration(CommentsProvider commentsProvider, File file) {
|
|
||||||
CommentedYamlConfiguration config = new CommentedYamlConfiguration(commentsProvider);
|
|
||||||
|
|
||||||
try {
|
|
||||||
config.load(file);
|
|
||||||
} catch (FileNotFoundException ignored) {
|
|
||||||
} catch (IOException | InvalidConfigurationException var4) {
|
|
||||||
var4.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return config;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static CommentedYamlConfiguration loadConfiguration(CommentsProvider commentsProvider, Reader reader) {
|
|
||||||
CommentedYamlConfiguration config = new CommentedYamlConfiguration(commentsProvider);
|
|
||||||
|
|
||||||
try {
|
|
||||||
config.load(reader);
|
|
||||||
} catch (IOException | InvalidConfigurationException ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return config;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull String saveToString() {
|
|
||||||
this.yamlOptions.setIndent(this.options().indent());
|
|
||||||
this.yamlOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
|
||||||
this.yamlRepresenter.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
|
||||||
String dump = this.yaml.dump(this.getValues(false));
|
|
||||||
if (dump.equals("{}\n")) {
|
|
||||||
dump = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
// No header support.
|
|
||||||
|
|
||||||
return dump;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-7
@@ -1,7 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.commented;
|
|
||||||
|
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
|
|
||||||
public interface CommentsProvider extends Function<String, String[]> {
|
|
||||||
}
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.spigot;
|
|
||||||
|
|
||||||
import cc.carm.lib.configuration.commented.CommentsProvider;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class ConfigComments implements CommentsProvider {
|
|
||||||
|
|
||||||
Map<String, String[]> comments = new HashMap<>();
|
|
||||||
|
|
||||||
protected Map<String, String[]> getComments() {
|
|
||||||
return comments;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void set(@NotNull String path, @NotNull String... comments) {
|
|
||||||
if (comments.length == 0) {
|
|
||||||
getComments().remove(path);
|
|
||||||
} else {
|
|
||||||
getComments().put(path, comments);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public @Nullable String[] get(@NotNull String path) {
|
|
||||||
return getComments().get(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String[] apply(String s) {
|
|
||||||
return get(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-60
@@ -1,60 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.spigot;
|
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.ConfigInitializer;
|
|
||||||
import cc.carm.lib.configuration.core.source.impl.FileConfigProvider;
|
|
||||||
import cc.carm.lib.configuration.commented.CommentedYamlConfiguration;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
|
|
||||||
public class SpigotConfigProvider extends FileConfigProvider<SpigotSectionWrapper> {
|
|
||||||
|
|
||||||
protected final ConfigComments comments = new ConfigComments();
|
|
||||||
protected ConfigInitializer<SpigotConfigProvider> initializer;
|
|
||||||
protected CommentedYamlConfiguration configuration;
|
|
||||||
|
|
||||||
public SpigotConfigProvider(@NotNull File file) {
|
|
||||||
super(file);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void initializeConfig() {
|
|
||||||
this.configuration = CommentedYamlConfiguration.loadConfiguration(comments, file);
|
|
||||||
this.initializer = new ConfigInitializer<>(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void say() {
|
|
||||||
System.out.println("Hello");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull SpigotSectionWrapper getConfiguration() {
|
|
||||||
return SpigotSectionWrapper.of(configuration);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void reload() throws Exception {
|
|
||||||
configuration.load(getFile());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void save() throws Exception {
|
|
||||||
configuration.save(getFile());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setComments(@NotNull String path, @NotNull String... comments) {
|
|
||||||
this.comments.set(path, comments);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @Nullable String[] getComments(@NotNull String path) {
|
|
||||||
return this.comments.get(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull ConfigInitializer<SpigotConfigProvider> getInitializer() {
|
|
||||||
return this.initializer;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-85
@@ -1,85 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.spigot;
|
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
|
||||||
import org.bukkit.configuration.ConfigurationSection;
|
|
||||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
|
||||||
import org.jetbrains.annotations.Contract;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.LinkedHashSet;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
public class SpigotSectionWrapper implements ConfigurationWrapper {
|
|
||||||
|
|
||||||
private final ConfigurationSection section;
|
|
||||||
|
|
||||||
private SpigotSectionWrapper(ConfigurationSection section) {
|
|
||||||
this.section = section;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Contract("!null->!null")
|
|
||||||
public static @Nullable SpigotSectionWrapper of(@Nullable ConfigurationSection section) {
|
|
||||||
return section == null ? null : new SpigotSectionWrapper(section);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull Set<String> getKeys(boolean deep) {
|
|
||||||
return new LinkedHashSet<>(section.getKeys(deep));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull Map<String, Object> getValues(boolean deep) {
|
|
||||||
return section.getValues(deep);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void set(@NotNull String path, @Nullable Object value) {
|
|
||||||
this.section.set(path, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean contains(@NotNull String path) {
|
|
||||||
return this.section.contains(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @Nullable Object get(@NotNull String path) {
|
|
||||||
return this.section.get(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isList(@NotNull String path) {
|
|
||||||
return this.section.isList(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @Nullable List<?> getList(@NotNull String path) {
|
|
||||||
return this.section.getList(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isConfigurationSection(@NotNull String path) {
|
|
||||||
return this.section.isConfigurationSection(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @Nullable ConfigurationWrapper getConfigurationSection(@NotNull String path) {
|
|
||||||
return of(this.section.getConfigurationSection(path));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
public <T extends ConfigurationSerializable> T getSerializable(@NotNull String path, @NotNull Class<T> clazz) {
|
|
||||||
return getSerializable(path, clazz, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
@Contract("_, _, !null -> !null")
|
|
||||||
public <T extends ConfigurationSerializable> T getSerializable(@NotNull String path, @NotNull Class<T> clazz, @Nullable T defaultValue) {
|
|
||||||
return this.section.getSerializable(path, clazz, defaultValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.spigot;
|
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
|
||||||
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
|
|
||||||
import cc.carm.lib.configuration.spigot.builder.SpigotConfigBuilder;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
public abstract class SpigotValue<T> extends CachedConfigValue<T> {
|
|
||||||
|
|
||||||
public static @NotNull SpigotConfigBuilder builder() {
|
|
||||||
return new SpigotConfigBuilder();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public SpigotValue(@Nullable SpigotConfigProvider provider,
|
|
||||||
@Nullable String configPath, @NotNull String[] comments, @Nullable T defaultValue) {
|
|
||||||
super(provider, configPath, comments, defaultValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
public SpigotConfigProvider getSpigotProvider() {
|
|
||||||
ConfigurationProvider<?> provider = getProvider();
|
|
||||||
if (provider instanceof SpigotConfigProvider) return (SpigotConfigProvider) getProvider();
|
|
||||||
else throw new IllegalStateException("Provider is not a SpigotConfigProvider");
|
|
||||||
}
|
|
||||||
|
|
||||||
public SpigotSectionWrapper getSpigotConfig() {
|
|
||||||
return getSpigotProvider().getConfiguration();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
-13
@@ -1,13 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.spigot.builder;
|
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.builder.AbstractConfigBuilder;
|
|
||||||
import cc.carm.lib.configuration.spigot.SpigotConfigProvider;
|
|
||||||
|
|
||||||
public abstract class AbstractSpigotBuilder<T, B extends AbstractSpigotBuilder<T, B>>
|
|
||||||
extends AbstractConfigBuilder<T, B, SpigotConfigProvider> {
|
|
||||||
|
|
||||||
public AbstractSpigotBuilder() {
|
|
||||||
super(SpigotConfigProvider.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-14
@@ -1,14 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.spigot.builder;
|
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.builder.ConfigBuilder;
|
|
||||||
import cc.carm.lib.configuration.spigot.builder.serializable.SerializableBuilder;
|
|
||||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
public class SpigotConfigBuilder extends ConfigBuilder {
|
|
||||||
|
|
||||||
public <V extends ConfigurationSerializable> @NotNull SerializableBuilder<V> ofSerializable(@NotNull Class<V> valueClass) {
|
|
||||||
return new SerializableBuilder<>(valueClass);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-29
@@ -1,29 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.spigot.builder.serializable;
|
|
||||||
|
|
||||||
import cc.carm.lib.configuration.spigot.builder.AbstractSpigotBuilder;
|
|
||||||
import cc.carm.lib.configuration.spigot.value.ConfiguredSerializable;
|
|
||||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
public class SerializableBuilder<T extends ConfigurationSerializable>
|
|
||||||
extends AbstractSpigotBuilder<T, SerializableBuilder<T>> {
|
|
||||||
|
|
||||||
protected final @NotNull Class<T> valueClass;
|
|
||||||
|
|
||||||
public SerializableBuilder(@NotNull Class<T> valueClass) {
|
|
||||||
this.valueClass = valueClass;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected @NotNull SerializableBuilder<T> getThis() {
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull ConfiguredSerializable<T> build() {
|
|
||||||
return new ConfiguredSerializable<>(this.provider, this.path, this.comments, this.valueClass, this.defaultValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
-4
@@ -1,4 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.spigot.item;
|
|
||||||
|
|
||||||
public class ItemConfiguration {
|
|
||||||
}
|
|
||||||
-4
@@ -1,4 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.spigot.value;
|
|
||||||
|
|
||||||
public class ConfiguredItem {
|
|
||||||
}
|
|
||||||
-52
@@ -1,52 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.spigot.value;
|
|
||||||
|
|
||||||
import cc.carm.lib.configuration.spigot.SpigotConfigProvider;
|
|
||||||
import cc.carm.lib.configuration.spigot.SpigotValue;
|
|
||||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
public class ConfiguredSerializable<T extends ConfigurationSerializable> extends SpigotValue<T> {
|
|
||||||
|
|
||||||
public static <V extends ConfigurationSerializable> ConfiguredSerializable<V> of(@NotNull Class<V> valueClass) {
|
|
||||||
return of(valueClass, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <V extends ConfigurationSerializable> ConfiguredSerializable<V> of(@NotNull Class<V> valueClass,
|
|
||||||
@Nullable V defaultValue) {
|
|
||||||
return builder().ofSerializable(valueClass).defaults(defaultValue).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected final @NotNull Class<T> valueClass;
|
|
||||||
|
|
||||||
public ConfiguredSerializable(@Nullable SpigotConfigProvider provider,
|
|
||||||
@Nullable String configPath, @NotNull String[] comments,
|
|
||||||
@NotNull Class<T> valueClass, @Nullable T defaultValue) {
|
|
||||||
super(provider, configPath, comments, defaultValue);
|
|
||||||
this.valueClass = valueClass;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @Nullable T get() {
|
|
||||||
if (isExpired()) { // 已过时的数据,需要重新解析一次。
|
|
||||||
try {
|
|
||||||
// 若未出现错误,则直接更新缓存并返回。
|
|
||||||
return updateCache(getSpigotConfig().getSerializable(getConfigPath(), valueClass, getDefaultValue()));
|
|
||||||
} catch (Exception e) {
|
|
||||||
// 出现了解析错误,提示并返回默认值。
|
|
||||||
e.printStackTrace();
|
|
||||||
return useDefault();
|
|
||||||
}
|
|
||||||
} else return Optional.ofNullable(getCachedValue()).orElse(defaultValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void set(@Nullable T value) {
|
|
||||||
updateCache(value);
|
|
||||||
setValue(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -15,14 +15,10 @@
|
|||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<artifactId>easyconfiguration-parent</artifactId>
|
<artifactId>easyconfiguration-parent</artifactId>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
<version>1.1.0</version>
|
<version>1.2.0</version>
|
||||||
<modules>
|
<modules>
|
||||||
<module>core</module>
|
<module>core</module>
|
||||||
|
|
||||||
<module>impl/yaml</module>
|
<module>impl/yaml</module>
|
||||||
|
|
||||||
<module>platform/spigot</module>
|
|
||||||
<module>platform/bungee</module>
|
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<name>EasyConfiguration</name>
|
<name>EasyConfiguration</name>
|
||||||
@@ -93,7 +89,7 @@
|
|||||||
<downloadUrl>https://github.com/CarmJos/EasyConfiguration/releases</downloadUrl>
|
<downloadUrl>https://github.com/CarmJos/EasyConfiguration/releases</downloadUrl>
|
||||||
<site>
|
<site>
|
||||||
<id>javadoc</id>
|
<id>javadoc</id>
|
||||||
<name>EasySQL JavaDoc (on Github Pages)</name>
|
<name>EasyConfiguration JavaDoc (on Github Pages)</name>
|
||||||
<url>https://CarmJos.github.io/EasyConfiguration</url>
|
<url>https://CarmJos.github.io/EasyConfiguration</url>
|
||||||
</site>
|
</site>
|
||||||
</distributionManagement>
|
</distributionManagement>
|
||||||
|
|||||||
Reference in New Issue
Block a user