mirror of
https://github.com/CarmJos/MineConfiguration.git
synced 2026-06-04 13:55:03 +08:00
feat!(conf): Upgraded to latest EasyConfiguration with optimized.
This commit is contained in:
+5
-12
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>mineconfiguration-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>2.9.3</version>
|
||||
<version>3.0.0</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
@@ -19,7 +19,7 @@
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>MineConfiguration-Bungee</name>
|
||||
<description>轻松(做)配置,适用于BungeeCord的版本,可用JSON与YAML格式。</description>
|
||||
<description>EasyConfiguration for BungeeCord.</description>
|
||||
|
||||
<dependencies>
|
||||
|
||||
@@ -32,11 +32,12 @@
|
||||
|
||||
<dependency>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>yamlcommentwriter</artifactId>
|
||||
<version>${deps.yamlcommentwriter.version}</version>
|
||||
<artifactId>easyconfiguration-yaml</artifactId>
|
||||
<version>${deps.easyconfifuration.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<!--suppress VulnerableLibrariesLocal -->
|
||||
<dependency>
|
||||
<groupId>net.md-5</groupId>
|
||||
<artifactId>bungeecord-api</artifactId>
|
||||
@@ -45,14 +46,6 @@
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>net.md-5</groupId>
|
||||
<artifactId>bungeecord-api</artifactId>
|
||||
<version>1.20-R0.2</version>
|
||||
<type>javadoc</type>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
-31
@@ -1,31 +0,0 @@
|
||||
package cc.carm.lib.mineconfiguration.bungee;
|
||||
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||
import cc.carm.lib.configuration.core.value.ValueManifest;
|
||||
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
|
||||
import cc.carm.lib.mineconfiguration.bungee.builder.BungeeConfigBuilder;
|
||||
import cc.carm.lib.mineconfiguration.bungee.source.BungeeConfigProvider;
|
||||
import cc.carm.lib.mineconfiguration.bungee.source.BungeeSectionWrapper;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public abstract class BungeeConfigValue<T> extends CachedConfigValue<T> {
|
||||
|
||||
public static @NotNull BungeeConfigBuilder builder() {
|
||||
return new BungeeConfigBuilder();
|
||||
}
|
||||
|
||||
public BungeeConfigValue(@NotNull ValueManifest<T> manifest) {
|
||||
super(manifest);
|
||||
}
|
||||
|
||||
public BungeeConfigProvider getBukkitProvider() {
|
||||
ConfigurationProvider<?> provider = getProvider();
|
||||
if (provider instanceof BungeeConfigProvider) return (BungeeConfigProvider) getProvider();
|
||||
else throw new IllegalStateException("Provider is not a SpigotConfigProvider");
|
||||
}
|
||||
|
||||
public BungeeSectionWrapper getBukkitConfig() {
|
||||
return getBukkitProvider().getConfiguration();
|
||||
}
|
||||
|
||||
}
|
||||
+29
-97
@@ -1,120 +1,52 @@
|
||||
package cc.carm.lib.mineconfiguration.bungee;
|
||||
|
||||
import cc.carm.lib.configuration.core.Configuration;
|
||||
import cc.carm.lib.configuration.core.ConfigurationRoot;
|
||||
import cc.carm.lib.mineconfiguration.bungee.source.BungeeConfigProvider;
|
||||
import cc.carm.lib.configuration.Configuration;
|
||||
import cc.carm.lib.configuration.source.ConfigurationHolder;
|
||||
import cc.carm.lib.configuration.source.yaml.YAMLConfigFactory;
|
||||
import cc.carm.lib.configuration.source.yaml.YAMLSource;
|
||||
import cc.carm.lib.mineconfiguration.common.AbstractConfiguration;
|
||||
import net.md_5.bungee.api.plugin.Plugin;
|
||||
import net.md_5.bungee.config.ConfigurationProvider;
|
||||
import net.md_5.bungee.config.JsonConfiguration;
|
||||
import net.md_5.bungee.config.YamlConfiguration;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class MineConfiguration extends AbstractConfiguration<BungeeConfigProvider> {
|
||||
|
||||
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 from(Plugin plugin, String fileName) {
|
||||
return from(plugin, fileName, fileName);
|
||||
}
|
||||
|
||||
public static BungeeConfigProvider from(Plugin plugin, String fileName, String source) {
|
||||
return from(new File(plugin.getDataFolder(), 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 fromYAML(Plugin plugin, String fileName) {
|
||||
return fromYAML(plugin, fileName, fileName);
|
||||
}
|
||||
|
||||
public static BungeeConfigProvider fromYAML(Plugin plugin, String fileName, String source) {
|
||||
return fromYAML(new File(plugin.getDataFolder(), fileName), source);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public static BungeeConfigProvider fromJSON(Plugin plugin, String fileName) {
|
||||
return fromJSON(plugin, fileName, fileName);
|
||||
}
|
||||
|
||||
public static BungeeConfigProvider fromJSON(Plugin plugin, String fileName, String source) {
|
||||
return fromJSON(new File(plugin.getDataFolder(), fileName), source);
|
||||
}
|
||||
|
||||
public class MineConfiguration extends AbstractConfiguration<ConfigurationHolder<YAMLSource>> {
|
||||
|
||||
public MineConfiguration(@NotNull Plugin plugin) {
|
||||
super(from(plugin, "config.yml"), from(plugin, "messages.yml"));
|
||||
this(plugin.getDataFolder());
|
||||
}
|
||||
|
||||
public MineConfiguration(@NotNull Plugin plugin,
|
||||
@NotNull Configuration configRoot,
|
||||
@NotNull Configuration messageRoot) {
|
||||
this(plugin);
|
||||
initializeConfig(configRoot);
|
||||
initializeMessage(messageRoot);
|
||||
this(plugin.getDataFolder(), configRoot, messageRoot);
|
||||
}
|
||||
|
||||
public MineConfiguration(@NotNull Plugin plugin,
|
||||
@NotNull Class<? extends Configuration> configRoot,
|
||||
@NotNull Class<? extends Configuration> messageRoot) {
|
||||
this(plugin);
|
||||
this(plugin.getDataFolder(), configRoot, messageRoot);
|
||||
}
|
||||
|
||||
public MineConfiguration(@NotNull File pluginDataFolder) {
|
||||
super(
|
||||
YAMLConfigFactory.from(pluginDataFolder, "config.yml").resourcePath("config.yml").build(),
|
||||
YAMLConfigFactory.from(pluginDataFolder, "messages.yml").resourcePath("messages.yml").build()
|
||||
);
|
||||
}
|
||||
|
||||
public MineConfiguration(@NotNull File pluginDataFolder,
|
||||
@NotNull Configuration configRoot,
|
||||
@NotNull Configuration messageRoot) {
|
||||
this(pluginDataFolder);
|
||||
initializeConfig(configRoot);
|
||||
initializeMessage(messageRoot);
|
||||
}
|
||||
|
||||
public MineConfiguration(@NotNull File pluginDataFolder,
|
||||
@NotNull Class<? extends Configuration> configRoot,
|
||||
@NotNull Class<? extends Configuration> messageRoot) {
|
||||
this(pluginDataFolder);
|
||||
initializeConfig(configRoot);
|
||||
initializeMessage(messageRoot);
|
||||
}
|
||||
|
||||
-13
@@ -1,13 +0,0 @@
|
||||
package cc.carm.lib.mineconfiguration.bungee.builder;
|
||||
|
||||
import cc.carm.lib.mineconfiguration.bungee.source.BungeeConfigProvider;
|
||||
import cc.carm.lib.configuration.core.builder.AbstractConfigBuilder;
|
||||
|
||||
public abstract class AbstractBungeeBuilder<T, B extends AbstractBungeeBuilder<T, B>>
|
||||
extends AbstractConfigBuilder<T, B, BungeeConfigProvider> {
|
||||
|
||||
public AbstractBungeeBuilder() {
|
||||
super(BungeeConfigProvider.class);
|
||||
}
|
||||
|
||||
}
|
||||
-14
@@ -1,14 +0,0 @@
|
||||
package cc.carm.lib.mineconfiguration.bungee.builder;
|
||||
|
||||
import cc.carm.lib.mineconfiguration.bungee.builder.message.BungeeMessageBuilder;
|
||||
import cc.carm.lib.configuration.core.builder.ConfigBuilder;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class BungeeConfigBuilder extends ConfigBuilder {
|
||||
|
||||
public @NotNull BungeeMessageBuilder createMessage() {
|
||||
return new BungeeMessageBuilder();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
-44
@@ -1,44 +0,0 @@
|
||||
package cc.carm.lib.mineconfiguration.bungee.builder.message;
|
||||
|
||||
import cc.carm.lib.easyplugin.utils.ColorParser;
|
||||
import cc.carm.lib.mineconfiguration.bungee.data.TextConfig;
|
||||
import cc.carm.lib.mineconfiguration.common.builder.message.MessageConfigBuilder;
|
||||
import net.md_5.bungee.api.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public class BungeeMessageBuilder extends MessageConfigBuilder<CommandSender, TextConfig> {
|
||||
|
||||
|
||||
public BungeeMessageBuilder() {
|
||||
super(CommandSender.class, TextConfig.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull <M> BungeeMessageValueBuilder<M> asValue(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
|
||||
return new BungeeMessageValueBuilder<>(parser);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull <M> BungeeMessageListBuilder<M> asList(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
|
||||
return new BungeeMessageListBuilder<>(parser);
|
||||
}
|
||||
|
||||
public @NotNull
|
||||
BungeeMessageValueBuilder<String> asStringValue() {
|
||||
return asValue(defaultParser()).whenSend(CommandSender::sendMessage);
|
||||
}
|
||||
|
||||
public @NotNull
|
||||
BungeeMessageListBuilder<String> asStringList() {
|
||||
return asList(defaultParser()).whenSend((r, m) -> m.forEach(r::sendMessage));
|
||||
}
|
||||
|
||||
protected static @NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable String> defaultParser() {
|
||||
return (receiver, message) -> ColorParser.parse(message);
|
||||
}
|
||||
|
||||
}
|
||||
-36
@@ -1,36 +0,0 @@
|
||||
package cc.carm.lib.mineconfiguration.bungee.builder.message;
|
||||
|
||||
import cc.carm.lib.configuration.core.value.ValueManifest;
|
||||
import cc.carm.lib.mineconfiguration.bungee.data.TextConfig;
|
||||
import cc.carm.lib.mineconfiguration.bungee.value.ConfiguredMessageList;
|
||||
import cc.carm.lib.mineconfiguration.common.builder.message.MessageListBuilder;
|
||||
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
|
||||
import net.md_5.bungee.api.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Optional;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public class BungeeMessageListBuilder<M>
|
||||
extends MessageListBuilder<M, CommandSender, TextConfig, BungeeMessageListBuilder<M>> {
|
||||
|
||||
public BungeeMessageListBuilder(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
|
||||
super(CommandSender.class, TextConfig::of, parser);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NotNull BungeeMessageListBuilder<M> getThis() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ConfiguredMessageList<M> build() {
|
||||
return new ConfiguredMessageList<>(
|
||||
buildManifest(TextConfig.of(new ArrayList<>())),
|
||||
ParamsUtils.formatParams(this.paramFormatter, this.params),
|
||||
this.messageParser, this.sendFunction
|
||||
);
|
||||
}
|
||||
}
|
||||
-35
@@ -1,35 +0,0 @@
|
||||
package cc.carm.lib.mineconfiguration.bungee.builder.message;
|
||||
|
||||
import cc.carm.lib.mineconfiguration.bungee.data.TextConfig;
|
||||
import cc.carm.lib.mineconfiguration.bungee.value.ConfiguredMessage;
|
||||
import cc.carm.lib.mineconfiguration.common.builder.message.MessageValueBuilder;
|
||||
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
|
||||
import net.md_5.bungee.api.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public class BungeeMessageValueBuilder<M>
|
||||
extends MessageValueBuilder<M, CommandSender, TextConfig, BungeeMessageValueBuilder<M>> {
|
||||
|
||||
public BungeeMessageValueBuilder(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
|
||||
super(CommandSender.class, TextConfig::new, parser);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NotNull BungeeMessageValueBuilder<M> getThis() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ConfiguredMessage<M> build() {
|
||||
return new ConfiguredMessage<>(
|
||||
buildManifest(TextConfig.of("")),
|
||||
ParamsUtils.formatParams(this.paramFormatter, this.params),
|
||||
this.messageParser, this.sendHandler
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
-35
@@ -1,35 +0,0 @@
|
||||
package cc.carm.lib.mineconfiguration.bungee.data;
|
||||
|
||||
import cc.carm.lib.mineconfiguration.common.data.AbstractText;
|
||||
import net.md_5.bungee.api.CommandSender;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class TextConfig extends AbstractText<CommandSender> {
|
||||
|
||||
public TextConfig(@NotNull String message) {
|
||||
super(CommandSender.class, message);
|
||||
}
|
||||
|
||||
@Contract("!null,-> !null")
|
||||
public static @Nullable TextConfig of(@Nullable String message) {
|
||||
if (message == null) return null;
|
||||
else return new TextConfig(message);
|
||||
}
|
||||
|
||||
public static @NotNull List<TextConfig> of(@Nullable List<String> messages) {
|
||||
if (messages == null || messages.isEmpty()) return new ArrayList<>();
|
||||
else return messages.stream().map(TextConfig::of).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static @NotNull List<TextConfig> of(@NotNull String... messages) {
|
||||
return of(Arrays.asList(messages));
|
||||
}
|
||||
|
||||
}
|
||||
-107
@@ -1,107 +0,0 @@
|
||||
package cc.carm.lib.mineconfiguration.bungee.source;
|
||||
|
||||
import cc.carm.lib.configuration.core.ConfigInitializer;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationComments;
|
||||
import cc.carm.lib.configuration.core.source.impl.FileConfigProvider;
|
||||
import cc.carm.lib.yamlcommentupdater.CommentedYAML;
|
||||
import cc.carm.lib.yamlcommentupdater.CommentedYAMLWriter;
|
||||
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;
|
||||
import java.io.StringWriter;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class BungeeConfigProvider extends FileConfigProvider<BungeeSectionWrapper> implements CommentedYAML {
|
||||
|
||||
protected static final char SEPARATOR = '.';
|
||||
|
||||
protected ConfigurationProvider loader;
|
||||
protected Configuration configuration;
|
||||
protected ConfigInitializer<BungeeConfigProvider> initializer;
|
||||
|
||||
protected ConfigurationComments comments = new ConfigurationComments();
|
||||
|
||||
public BungeeConfigProvider(@NotNull File file, @NotNull ConfigurationProvider loader) {
|
||||
super(file);
|
||||
this.loader = loader;
|
||||
}
|
||||
|
||||
public BungeeConfigProvider(@NotNull File file, @NotNull Class<? extends ConfigurationProvider> providerClass) {
|
||||
this(file, ConfigurationProvider.getProvider(providerClass));
|
||||
}
|
||||
|
||||
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
|
||||
protected void onReload() throws Exception {
|
||||
this.configuration = getLoader().load(file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ConfigurationComments getComments() {
|
||||
return this.comments;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save() throws Exception {
|
||||
try {
|
||||
CommentedYAMLWriter.writeWithComments(this, this.file);
|
||||
} catch (Exception ex) {
|
||||
getLoader().save(configuration, file);
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ConfigInitializer<BungeeConfigProvider> getInitializer() {
|
||||
return this.initializer;
|
||||
}
|
||||
|
||||
public ConfigurationProvider getLoader() {
|
||||
return loader;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String serializeValue(@NotNull String key, @NotNull Object value) {
|
||||
Configuration tmp = new Configuration();// 该对象用于临时记录配置内容
|
||||
tmp.set(key, value);
|
||||
StringWriter tmpStr = new StringWriter();
|
||||
loader.save(tmp, tmpStr);
|
||||
return tmpStr.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getKeys(@Nullable String sectionKey, boolean deep) {
|
||||
if (sectionKey == null) return BungeeSectionWrapper.getAllKeys(this.configuration);
|
||||
|
||||
Configuration section = configuration.getSection(sectionKey);
|
||||
if (section == null) return null;
|
||||
|
||||
return new HashSet<>(section.getKeys());
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Object getValue(@NotNull String key) {
|
||||
return configuration.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable List<String> getHeaderComments(@Nullable String key) {
|
||||
return comments.getHeaderComment(key);
|
||||
}
|
||||
|
||||
}
|
||||
-94
@@ -1,94 +0,0 @@
|
||||
package cc.carm.lib.mineconfiguration.bungee.source;
|
||||
|
||||
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;
|
||||
|
||||
import static cc.carm.lib.mineconfiguration.bungee.source.BungeeConfigProvider.SEPARATOR;
|
||||
|
||||
public class BungeeSectionWrapper implements ConfigurationWrapper<Configuration> {
|
||||
|
||||
private final Configuration configuration;
|
||||
|
||||
private BungeeSectionWrapper(@NotNull Configuration section) {
|
||||
this.configuration = section;
|
||||
}
|
||||
|
||||
@Contract("!null->!null")
|
||||
public static @Nullable BungeeSectionWrapper of(@Nullable Configuration section) {
|
||||
return section == null ? null : new BungeeSectionWrapper(section);
|
||||
}
|
||||
|
||||
protected static Set<String> getAllKeys(@NotNull Configuration config) {
|
||||
Set<String> keys = new LinkedHashSet<>();
|
||||
for (String key : config.getKeys()) {
|
||||
keys.add(key);
|
||||
Object value = config.get(key);
|
||||
if (value instanceof Configuration) {
|
||||
getAllKeys((Configuration) value).stream()
|
||||
.map(subKey -> key + SEPARATOR + subKey).forEach(keys::add);
|
||||
}
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Configuration getSource() {
|
||||
return this.configuration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Set<String> getKeys(boolean deep) {
|
||||
if (deep) {
|
||||
return new LinkedHashSet<>(getAllKeys(configuration));
|
||||
} else {
|
||||
return new LinkedHashSet<>(configuration.getKeys());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Map<String, Object> getValues(boolean deep) {
|
||||
return getKeys(deep).stream()
|
||||
.collect(Collectors.toMap(key -> key, configuration::get, (a, b) -> b, LinkedHashMap::new));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(@NotNull String path, @Nullable Object value) {
|
||||
this.configuration.set(path, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(@NotNull String path) {
|
||||
return this.configuration.contains(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Object get(@NotNull String path) {
|
||||
return this.configuration.get(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isList(@NotNull String path) {
|
||||
return get(path) instanceof List<?>;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable List<?> getList(@NotNull String path) {
|
||||
return this.configuration.getList(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isConfigurationSection(@NotNull String path) {
|
||||
return get(path) instanceof Configuration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable BungeeSectionWrapper getConfigurationSection(@NotNull String path) {
|
||||
return of(this.configuration.getSection(path));
|
||||
}
|
||||
}
|
||||
+40
-28
@@ -1,52 +1,64 @@
|
||||
package cc.carm.lib.mineconfiguration.bungee.value;
|
||||
|
||||
import cc.carm.lib.configuration.core.value.ValueManifest;
|
||||
import cc.carm.lib.mineconfiguration.bungee.BungeeConfigValue;
|
||||
import cc.carm.lib.mineconfiguration.bungee.builder.message.BungeeMessageValueBuilder;
|
||||
import cc.carm.lib.mineconfiguration.bungee.data.TextConfig;
|
||||
import cc.carm.lib.mineconfiguration.common.value.ConfigMessage;
|
||||
import cc.carm.lib.configuration.value.ValueManifest;
|
||||
import cc.carm.lib.configuration.value.text.ConfiguredText;
|
||||
import cc.carm.lib.configuration.value.text.data.TextContents;
|
||||
import cc.carm.lib.easyplugin.utils.ColorParser;
|
||||
import net.md_5.bungee.api.CommandSender;
|
||||
import net.md_5.bungee.api.ProxyServer;
|
||||
import net.md_5.bungee.api.chat.BaseComponent;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public class ConfiguredMessage<M> extends ConfigMessage<M, TextConfig, CommandSender> {
|
||||
public class ConfiguredMessage extends ConfiguredText<BaseComponent[], CommandSender> {
|
||||
|
||||
@NotNull
|
||||
public static <M> BungeeMessageValueBuilder<@Nullable M> create(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser) {
|
||||
return BungeeConfigValue.builder().createMessage().asValue(messageParser);
|
||||
public static ConfiguredMessage.Builder create() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public static BungeeMessageValueBuilder<String> asString() {
|
||||
return BungeeConfigValue.builder().createMessage().asStringValue();
|
||||
public static ConfiguredMessage ofString(@NotNull String... messages) {
|
||||
return create().defaults(messages).build();
|
||||
}
|
||||
|
||||
public static ConfiguredMessage<String> ofString() {
|
||||
return asString().build();
|
||||
public ConfiguredMessage(@NotNull ValueManifest<TextContents> manifest,
|
||||
@NotNull BiFunction<CommandSender, String, String> parser,
|
||||
@NotNull BiFunction<CommandSender, String, BaseComponent[]> compiler,
|
||||
@NotNull BiConsumer<CommandSender, List<BaseComponent[]>> dispatcher,
|
||||
@NotNull String[] params) {
|
||||
super(manifest, parser, compiler, dispatcher, params);
|
||||
}
|
||||
|
||||
public static ConfiguredMessage<String> ofString(@NotNull String defaultMessage) {
|
||||
return asString().defaults(defaultMessage).build();
|
||||
public void print(Object... values) {
|
||||
prepare(values).to(ProxyServer.getInstance().getConsole());
|
||||
}
|
||||
|
||||
public ConfiguredMessage(@NotNull ValueManifest<TextConfig> manifest, @NotNull String[] params,
|
||||
@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser,
|
||||
@NotNull BiConsumer<@NotNull CommandSender, @NotNull M> sendFunction) {
|
||||
super(manifest, TextConfig.class, params, messageParser, sendFunction, TextConfig::of);
|
||||
}
|
||||
public static class Builder extends ConfiguredText.Builder<BaseComponent[], CommandSender, Builder> {
|
||||
|
||||
@Override
|
||||
public @NotNull Collection<CommandSender> getAllReceivers() {
|
||||
List<CommandSender> senders = new ArrayList<>();
|
||||
senders.add(ProxyServer.getInstance().getConsole());
|
||||
senders.addAll(ProxyServer.getInstance().getPlayers());
|
||||
return senders;
|
||||
public Builder() {
|
||||
super();
|
||||
this.parser = (sender, message) -> ColorParser.parse(message);
|
||||
this.compiler = (sender, message) -> new BaseComponent[]{new TextComponent(message)};
|
||||
this.dispatcher = (sender, message) -> {
|
||||
for (BaseComponent[] component : message) {
|
||||
sender.sendMessage(component);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ConfiguredMessage build() {
|
||||
return new ConfiguredMessage(buildManifest(), this.parser, this.compiler, this.dispatcher, this.params);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ConfiguredMessage.Builder self() {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
-47
@@ -1,47 +0,0 @@
|
||||
package cc.carm.lib.mineconfiguration.bungee.value;
|
||||
|
||||
import cc.carm.lib.configuration.core.value.ValueManifest;
|
||||
import cc.carm.lib.mineconfiguration.bungee.BungeeConfigValue;
|
||||
import cc.carm.lib.mineconfiguration.bungee.builder.message.BungeeMessageListBuilder;
|
||||
import cc.carm.lib.mineconfiguration.bungee.data.TextConfig;
|
||||
import cc.carm.lib.mineconfiguration.common.value.ConfigMessageList;
|
||||
import net.md_5.bungee.api.CommandSender;
|
||||
import net.md_5.bungee.api.ProxyServer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public class ConfiguredMessageList<M> extends ConfigMessageList<M, TextConfig, CommandSender> {
|
||||
|
||||
@NotNull
|
||||
public static <M> BungeeMessageListBuilder<M> create(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser) {
|
||||
return BungeeConfigValue.builder().createMessage().asList(messageParser);
|
||||
}
|
||||
|
||||
public static BungeeMessageListBuilder<String> asStrings() {
|
||||
return BungeeConfigValue.builder().createMessage().asStringList();
|
||||
}
|
||||
|
||||
public static ConfiguredMessageList<String> ofStrings(@NotNull String... defaultMessages) {
|
||||
return asStrings().defaults(defaultMessages).build();
|
||||
}
|
||||
|
||||
public ConfiguredMessageList(@NotNull ValueManifest<List<TextConfig>> manifest, @NotNull String[] params,
|
||||
@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser,
|
||||
@NotNull BiConsumer<@NotNull CommandSender, @NotNull List<M>> sendFunction) {
|
||||
super(manifest, TextConfig.class, params, messageParser, sendFunction, TextConfig::of);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Collection<CommandSender> getAllReceivers() {
|
||||
List<CommandSender> senders = new ArrayList<>();
|
||||
senders.add(ProxyServer.getInstance().getConsole());
|
||||
senders.addAll(ProxyServer.getInstance().getPlayers());
|
||||
return senders;
|
||||
}
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
import cc.carm.lib.configuration.core.ConfigurationRoot;
|
||||
import cc.carm.lib.configuration.core.annotation.HeaderComment;
|
||||
import cc.carm.lib.configuration.core.annotation.InlineComment;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||
import cc.carm.lib.configuration.core.value.ConfigValue;
|
||||
import cc.carm.lib.configuration.core.value.type.ConfiguredList;
|
||||
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
|
||||
import cc.carm.lib.mineconfiguration.bungee.MineConfiguration;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.List;
|
||||
|
||||
public class ConfigTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
File file = new File("target/config.yml");
|
||||
ConfigurationProvider<?> config = MineConfiguration.from(file);
|
||||
config.initialize(Configuration.class);
|
||||
|
||||
try {
|
||||
System.out.println("--------------------------------------------");
|
||||
System.out.println(new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8));
|
||||
System.out.println("--------------------------------------------");
|
||||
} catch (Exception exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@HeaderComment({
|
||||
"MineConfiguration for BungeeCord",
|
||||
"测试实例配置文件", ""
|
||||
})
|
||||
public static class Configuration extends ConfigurationRoot {
|
||||
|
||||
@InlineComment("是否显示DEBUG消息")
|
||||
public static final ConfigValue<Boolean> DEBUG = ConfiguredValue.of(Boolean.class, false);
|
||||
|
||||
@HeaderComment("启动时执行的命令")
|
||||
public static final class START_UP {
|
||||
|
||||
@HeaderComment("延迟执行的时间(单位:秒)")
|
||||
public static final ConfigValue<Integer> DELAY = ConfiguredValue.of(Integer.class, 30);
|
||||
|
||||
@HeaderComment("循环执行的间隔(单位:秒)")
|
||||
public static final ConfigValue<Integer> PERIOD = ConfiguredValue.of(Integer.class, 10);
|
||||
|
||||
@HeaderComment("执行的指令列表")
|
||||
@InlineComment("建议以\"\"包裹")
|
||||
public static final ConfigValue<List<String>> COMMANDS = ConfiguredList.builderOf(String.class)
|
||||
.fromString().defaults("alert Commands here!").build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user