mirror of
https://github.com/CarmJos/MineConfiguration.git
synced 2026-06-04 13:55:03 +08:00
完成基础部分
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
<?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>MineConfiguration</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.0.0</version>
|
||||
</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>mineconfiguration-bungee</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>${project.parent.groupId}</groupId>
|
||||
<artifactId>easyconfiguration-core</artifactId>
|
||||
<version>${easyconfiguration.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>
|
||||
@@ -0,0 +1,74 @@
|
||||
package cc.carm.lib.configuration;
|
||||
|
||||
import cc.carm.lib.configuration.bungee.BungeeConfigProvider;
|
||||
import net.md_5.bungee.config.ConfigurationProvider;
|
||||
import net.md_5.bungee.config.JsonConfiguration;
|
||||
import net.md_5.bungee.config.YamlConfiguration;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class MineConfiguration {
|
||||
|
||||
protected static BungeeConfigProvider create(File file, String source, ConfigurationProvider loader) {
|
||||
BungeeConfigProvider provider = new BungeeConfigProvider(file, loader);
|
||||
try {
|
||||
provider.initializeFile(source);
|
||||
provider.initializeConfig();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return provider;
|
||||
}
|
||||
|
||||
public static BungeeConfigProvider from(File file, String source) {
|
||||
return fromYAML(file, source);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public static BungeeConfigProvider fromYAML(File file, String source) {
|
||||
return create(file, source, ConfigurationProvider.getProvider(YamlConfiguration.class));
|
||||
}
|
||||
|
||||
public static BungeeConfigProvider fromYAML(String fileName, String source) {
|
||||
return fromYAML(new File(fileName), source);
|
||||
}
|
||||
|
||||
public static BungeeConfigProvider fromYAML(File file) {
|
||||
return fromYAML(file, file.getName());
|
||||
}
|
||||
|
||||
public static BungeeConfigProvider fromYAML(String fileName) {
|
||||
return fromYAML(fileName, fileName);
|
||||
}
|
||||
|
||||
|
||||
public static BungeeConfigProvider fromJSON(File file, String source) {
|
||||
return create(file, source, ConfigurationProvider.getProvider(JsonConfiguration.class));
|
||||
}
|
||||
|
||||
public static BungeeConfigProvider fromJSON(String fileName, String source) {
|
||||
return fromJSON(new File(fileName), source);
|
||||
}
|
||||
|
||||
public static BungeeConfigProvider fromJSON(File file) {
|
||||
return fromJSON(file, file.getName());
|
||||
}
|
||||
|
||||
public static BungeeConfigProvider fromJSON(String fileName) {
|
||||
return fromJSON(fileName, fileName);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
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 org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class BungeeConfigProvider extends FileConfigProvider<BungeeSectionWrapper> {
|
||||
|
||||
protected ConfigurationProvider loader;
|
||||
protected Configuration configuration;
|
||||
protected ConfigInitializer<BungeeConfigProvider> initializer;
|
||||
|
||||
public BungeeConfigProvider(@NotNull File file, ConfigurationProvider loader) {
|
||||
super(file);
|
||||
this.loader = loader;
|
||||
}
|
||||
|
||||
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) {
|
||||
// BungeeCord version doesn't support comments
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable String[] getComments(@NotNull String path) {
|
||||
// BungeeCord version doesn't support comments
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ConfigInitializer<BungeeConfigProvider> getInitializer() {
|
||||
return this.initializer;
|
||||
}
|
||||
|
||||
public ConfigurationProvider getLoader() {
|
||||
return loader;
|
||||
}
|
||||
}
|
||||
@@ -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(@NotNull 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 get(path) instanceof Configuration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable ConfigurationWrapper getConfigurationSection(@NotNull String path) {
|
||||
return of(this.section.getSection(path));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user