1
mirror of https://github.com/CarmJos/EasyConfiguration.git synced 2026-06-04 18:48:20 +08:00

初始版本完成

This commit is contained in:
2022-04-17 00:32:33 +08:00
commit 3d0927aeeb
62 changed files with 5235 additions and 0 deletions
+47
View File
@@ -0,0 +1,47 @@
<?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.0.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>
</project>
@@ -0,0 +1,34 @@
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.initialize();
} 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);
}
}
@@ -0,0 +1,54 @@
package cc.carm.lib.configuration.bungee;
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
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 {
Configuration configuration;
public BungeeConfigProvider(@NotNull File file) {
super(file);
}
public void initialize() throws IOException {
this.configuration = getLoader().load(file);
}
@Override
public @NotNull ConfigurationWrapper 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;
}
public static ConfigurationProvider getLoader() {
return ConfigurationProvider.getProvider(YamlConfiguration.class);
}
}
@@ -0,0 +1,70 @@
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));
}
}
+39
View File
@@ -0,0 +1,39 @@
<?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.0.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.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,35 @@
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.initialize();
} 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);
}
}
@@ -0,0 +1,73 @@
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;
/**
* An 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);
}
}
}
@@ -0,0 +1,69 @@
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;
}
}
@@ -0,0 +1,7 @@
package cc.carm.lib.configuration.commented;
import java.util.function.Function;
public interface CommentsProvider extends Function<String, String[]> {
}
@@ -0,0 +1,35 @@
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);
}
}
@@ -0,0 +1,49 @@
package cc.carm.lib.configuration.spigot;
import cc.carm.lib.configuration.commented.CommentedYamlConfiguration;
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
import cc.carm.lib.configuration.core.source.impl.FileConfigProvider;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
public class SpigotConfigProvider extends FileConfigProvider {
ConfigComments comments = new ConfigComments();
CommentedYamlConfiguration configuration;
public SpigotConfigProvider(@NotNull File file) {
super(file);
}
public void initialize() {
this.configuration = CommentedYamlConfiguration.loadConfiguration(comments, file);
}
@Override
public @NotNull ConfigurationWrapper 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);
}
}
@@ -0,0 +1,71 @@
package cc.carm.lib.configuration.spigot;
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
import org.bukkit.configuration.ConfigurationSection;
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));
}
}