mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-04 18:48:20 +08:00
[1.1.0] 实现 ConfigurationSerializable 相关数据的加载、获取与写入。
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyconfiguration-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.0.6</version>
|
||||
<version>1.1.0</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
+11
-4
@@ -1,6 +1,6 @@
|
||||
package cc.carm.lib.configuration.bungee;
|
||||
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||
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;
|
||||
@@ -11,9 +11,10 @@ import org.jetbrains.annotations.Nullable;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class BungeeConfigProvider extends FileConfigProvider {
|
||||
public class BungeeConfigProvider extends FileConfigProvider<BungeeSectionWrapper> {
|
||||
|
||||
Configuration configuration;
|
||||
protected Configuration configuration;
|
||||
protected ConfigInitializer<BungeeConfigProvider> initializer;
|
||||
|
||||
public BungeeConfigProvider(@NotNull File file) {
|
||||
super(file);
|
||||
@@ -21,10 +22,11 @@ public class BungeeConfigProvider extends FileConfigProvider {
|
||||
|
||||
public void initializeConfig() throws IOException {
|
||||
this.configuration = getLoader().load(file);
|
||||
this.initializer = new ConfigInitializer<>(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ConfigurationWrapper getConfiguration() {
|
||||
public @NotNull BungeeSectionWrapper getConfiguration() {
|
||||
return BungeeSectionWrapper.of(configuration);
|
||||
}
|
||||
|
||||
@@ -47,6 +49,11 @@ public class BungeeConfigProvider extends FileConfigProvider {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ConfigInitializer<BungeeConfigProvider> getInitializer() {
|
||||
return this.initializer;
|
||||
}
|
||||
|
||||
public static ConfigurationProvider getLoader() {
|
||||
return ConfigurationProvider.getProvider(YamlConfiguration.class);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyconfiguration-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.0.6</version>
|
||||
<version>1.1.0</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
+1
-1
@@ -17,7 +17,7 @@ import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* An hacky extension of {@link Yaml} that allows to write comments when dumping.
|
||||
* A hacky extension of {@link Yaml} that allows to write comments when dumping.
|
||||
*/
|
||||
public class CommentedYaml extends Yaml {
|
||||
|
||||
|
||||
+17
-6
@@ -1,17 +1,18 @@
|
||||
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.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 {
|
||||
public class SpigotConfigProvider extends FileConfigProvider<SpigotSectionWrapper> {
|
||||
|
||||
ConfigComments comments = new ConfigComments();
|
||||
CommentedYamlConfiguration configuration;
|
||||
protected final ConfigComments comments = new ConfigComments();
|
||||
protected ConfigInitializer<SpigotConfigProvider> initializer;
|
||||
protected CommentedYamlConfiguration configuration;
|
||||
|
||||
public SpigotConfigProvider(@NotNull File file) {
|
||||
super(file);
|
||||
@@ -19,10 +20,15 @@ public class SpigotConfigProvider extends FileConfigProvider {
|
||||
|
||||
public void initializeConfig() {
|
||||
this.configuration = CommentedYamlConfiguration.loadConfiguration(comments, file);
|
||||
this.initializer = new ConfigInitializer<>(this);
|
||||
}
|
||||
|
||||
public void say() {
|
||||
System.out.println("Hello");
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ConfigurationWrapper getConfiguration() {
|
||||
public @NotNull SpigotSectionWrapper getConfiguration() {
|
||||
return SpigotSectionWrapper.of(configuration);
|
||||
}
|
||||
|
||||
@@ -46,4 +52,9 @@ public class SpigotConfigProvider extends FileConfigProvider {
|
||||
return this.comments.get(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ConfigInitializer<SpigotConfigProvider> getInitializer() {
|
||||
return this.initializer;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+14
@@ -2,6 +2,7 @@ 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;
|
||||
@@ -68,4 +69,17 @@ public class SpigotSectionWrapper implements ConfigurationWrapper {
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
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
@@ -0,0 +1,13 @@
|
||||
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
@@ -0,0 +1,14 @@
|
||||
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
@@ -0,0 +1,29 @@
|
||||
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
@@ -0,0 +1,4 @@
|
||||
package cc.carm.lib.configuration.spigot.item;
|
||||
|
||||
public class ItemConfiguration {
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
package cc.carm.lib.configuration.spigot.value;
|
||||
|
||||
public class ConfiguredItem {
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user