1
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:
2025-02-17 18:45:42 +08:00
parent 87799d2c70
commit bba135e911
80 changed files with 1185 additions and 3465 deletions
+22 -1
View File
@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>mineconfiguration-parent</artifactId> <artifactId>mineconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>2.9.3</version> <version>3.0.0</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<properties> <properties>
@@ -28,6 +28,27 @@
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>cc.carm.lib</groupId>
<artifactId>easyconfiguration-feature-commentable</artifactId>
<version>${deps.easyconfifuration.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cc.carm.lib</groupId>
<artifactId>easyconfiguration-feature-file</artifactId>
<version>${deps.easyconfifuration.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cc.carm.lib</groupId>
<artifactId>easyconfiguration-feature-text</artifactId>
<version>${deps.easyconfifuration.version}</version>
<scope>compile</scope>
</dependency>
<dependency> <dependency>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-color</artifactId> <artifactId>easyplugin-color</artifactId>
@@ -1,51 +1,51 @@
package cc.carm.lib.mineconfiguration.common; package cc.carm.lib.mineconfiguration.common;
import cc.carm.lib.configuration.core.Configuration; import cc.carm.lib.configuration.Configuration;
import cc.carm.lib.configuration.core.source.ConfigurationProvider; import cc.carm.lib.configuration.source.ConfigurationHolder;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
public abstract class AbstractConfiguration<P extends ConfigurationProvider<?>> { public abstract class AbstractConfiguration<HOLDER extends ConfigurationHolder<?>> {
private final P configProvider; private final HOLDER config;
private final P messageProvider; private final HOLDER message;
protected AbstractConfiguration(@NotNull P configProvider, @NotNull P messageProvider) { protected AbstractConfiguration(@NotNull HOLDER config, @NotNull HOLDER message) {
this.configProvider = configProvider; this.config = config;
this.messageProvider = messageProvider; this.message = message;
} }
public void initializeConfig(@NotNull Configuration configRoot) { public void initializeConfig(@NotNull Configuration configRoot) {
this.configProvider.initialize(configRoot); this.config.initialize(configRoot);
} }
public void initializeMessage(@NotNull Configuration messageRoot) { public void initializeMessage(@NotNull Configuration messageRoot) {
this.messageProvider.initialize(messageRoot); this.message.initialize(messageRoot);
} }
public void initializeConfig(@NotNull Class<? extends Configuration> configRoot) { public void initializeConfig(@NotNull Class<? extends Configuration> configRoot) {
this.configProvider.initialize(configRoot); this.config.initialize(configRoot);
} }
public void initializeMessage(@NotNull Class<? extends Configuration> messageRoot) { public void initializeMessage(@NotNull Class<? extends Configuration> messageRoot) {
this.messageProvider.initialize(messageRoot); this.message.initialize(messageRoot);
} }
public P getConfigProvider() { public HOLDER getConfig() {
return configProvider; return config;
} }
public P getMessageProvider() { public HOLDER getMessage() {
return messageProvider; return message;
} }
public void reload() throws Exception { public void reload() throws Exception {
getConfigProvider().reload(); getConfig().reload();
getMessageProvider().reload(); getMessage().reload();
} }
public void save() throws Exception { public void save() throws Exception {
getConfigProvider().save(); getConfig().save();
getMessageProvider().save(); getMessage().save();
} }
} }
@@ -1,39 +0,0 @@
package cc.carm.lib.mineconfiguration.common.builder.message;
import cc.carm.lib.mineconfiguration.common.data.AbstractText;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.function.BiFunction;
public abstract class MessageConfigBuilder<R, T extends AbstractText<R>> {
protected final @NotNull Class<R> receiverClazz;
protected final @NotNull Class<T> textClazz;
public MessageConfigBuilder(@NotNull Class<R> receiverClazz,
@NotNull Class<T> textClazz) {
this.receiverClazz = receiverClazz;
this.textClazz = textClazz;
}
/**
* 以单条消息为目标,构建一个消息配置。
*
* @param parser 消息解析器,负责将String转换为目标消息类型。
* @param <M> 消息类型
* @return 单条消息构建器
*/
public abstract <M> @NotNull MessageValueBuilder<M, R, T, ?> asValue(@NotNull BiFunction<@Nullable R, @NotNull String, @Nullable M> parser);
/**
* 以多行消息为目标,构建一个消息配置。
*
* @param parser 消息解析器
* @param <M> 消息类型
* @return 多行消息构建器
*/
public abstract <M> @NotNull MessageListBuilder<M, R, T, ?> asList(@NotNull BiFunction<@Nullable R, @NotNull String, @Nullable M> parser);
}
@@ -1,79 +0,0 @@
package cc.carm.lib.mineconfiguration.common.builder.message;
import cc.carm.lib.configuration.core.builder.CommonConfigBuilder;
import cc.carm.lib.configuration.core.value.ValueManifest;
import cc.carm.lib.mineconfiguration.common.data.AbstractText;
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
import cc.carm.lib.mineconfiguration.common.value.ConfigMessageList;
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.Optional;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
public abstract class MessageListBuilder<M, R, T extends AbstractText<R>, B extends MessageListBuilder<M, R, T, B>>
extends CommonConfigBuilder<List<T>, B> {
protected final @NotNull Class<R> receiverClazz;
protected @NotNull String[] params;
protected @NotNull BiFunction<@Nullable R, @NotNull String, @Nullable M> messageParser;
protected @NotNull BiConsumer<@NotNull R, @NotNull List<M>> sendFunction;
protected @NotNull Function<@NotNull String, @NotNull String> paramFormatter;
protected final @NotNull Function<String, T> textBuilder;
public MessageListBuilder(@NotNull Class<R> receiverClazz,
@NotNull Function<String, T> textBuilder,
@NotNull BiFunction<@Nullable R, @NotNull String, @Nullable M> parser) {
this.receiverClazz = receiverClazz;
this.textBuilder = textBuilder;
this.params = new String[0];
this.messageParser = parser;
this.paramFormatter = ParamsUtils.DEFAULT_PARAM_FORMATTER;
this.sendFunction = (sender, M) -> {
};
}
public B defaults(@NotNull String... messages) {
return defaults(new ArrayList<>(Arrays.stream(messages).map(textBuilder).collect(Collectors.toList())));
}
public B params(@NotNull String... params) {
this.params = params;
return getThis();
}
public B params(@NotNull List<String> params) {
this.params = params.toArray(new String[0]);
return getThis();
}
public B formatParam(@NotNull Function<@NotNull String, @NotNull String> paramFormatter) {
this.paramFormatter = paramFormatter;
return getThis();
}
public B whenSend(@NotNull BiConsumer<@NotNull R, @NotNull List<M>> sendFunction) {
this.sendFunction = sendFunction;
return getThis();
}
@Override
public abstract @NotNull ConfigMessageList<M, T, R> build();
protected @NotNull ValueManifest<List<T>> buildManifest(@NotNull List<T> emptyValue) {
return new ValueManifest<>(
this.provider, this.path, this.headerComments, this.inlineComment,
Optional.ofNullable(this.defaultValue).orElse(emptyValue)
);
}
}
@@ -1,75 +0,0 @@
package cc.carm.lib.mineconfiguration.common.builder.message;
import cc.carm.lib.configuration.core.builder.CommonConfigBuilder;
import cc.carm.lib.configuration.core.value.ValueManifest;
import cc.carm.lib.mineconfiguration.common.data.AbstractText;
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
import cc.carm.lib.mineconfiguration.common.value.ConfigMessage;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
public abstract class MessageValueBuilder<M, R, T extends AbstractText<R>, B extends MessageValueBuilder<M, R, T, B>>
extends CommonConfigBuilder<T, B> {
protected final @NotNull Class<R> receiverClazz;
protected @NotNull String[] params;
protected @NotNull BiFunction<@Nullable R, @NotNull String, @Nullable M> messageParser;
protected @NotNull BiConsumer<@NotNull R, @NotNull M> sendHandler;
protected @NotNull Function<@NotNull String, @NotNull String> paramFormatter;
protected final @NotNull Function<String, T> textBuilder;
public MessageValueBuilder(@NotNull Class<R> receiverClazz,
@NotNull Function<String, T> textBuilder,
@NotNull BiFunction<@Nullable R, @NotNull String, @Nullable M> parser) {
this.receiverClazz = receiverClazz;
this.params = new String[0];
this.paramFormatter = ParamsUtils.DEFAULT_PARAM_FORMATTER;
this.textBuilder = textBuilder;
this.messageParser = parser;
this.sendHandler = (receiver, M) -> {
};
}
public B defaults(@NotNull String message) {
return defaults(this.textBuilder.apply(message));
}
public B params(@NotNull String... params) {
this.params = params;
return getThis();
}
public B params(@NotNull List<String> params) {
this.params = params.toArray(new String[0]);
return getThis();
}
public B formatParam(@NotNull Function<@NotNull String, @NotNull String> paramFormatter) {
this.paramFormatter = paramFormatter;
return getThis();
}
public B whenSend(@NotNull BiConsumer<@NotNull R, @NotNull M> sendFunction) {
this.sendHandler = sendFunction;
return getThis();
}
@Override
public abstract @NotNull ConfigMessage<M, T, R> build();
protected @NotNull ValueManifest<T> buildManifest(@NotNull T emptyValue) {
return new ValueManifest<>(
this.provider, this.path, this.headerComments, this.inlineComment,
Optional.ofNullable(this.defaultValue).orElse(emptyValue)
);
}
}
@@ -1,42 +0,0 @@
package cc.carm.lib.mineconfiguration.common.data;
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
import java.util.function.BiFunction;
/**
* @param <R> Receiver type
*/
public abstract class AbstractText<R> {
private final @NotNull Class<R> receiverClazz;
protected @NotNull String message;
public AbstractText(@NotNull Class<R> receiverClazz, @NotNull String message) {
this.receiverClazz = receiverClazz;
this.message = message;
}
public @NotNull Class<R> getReceiverClazz() {
return receiverClazz;
}
public @NotNull String getMessage() {
return this.message;
}
public <M> @NotNull M parse(@NotNull BiFunction<@Nullable R, @NotNull String, @NotNull M> parser,
@Nullable R receiver, @Nullable String[] params, @Nullable Object[] values) {
return parse(parser, receiver, ParamsUtils.buildParams(params, values));
}
public <M> @NotNull M parse(@NotNull BiFunction<@Nullable R, @NotNull String, @NotNull M> parser,
@Nullable R receiver, @NotNull Map<String, Object> placeholders) {
return parser.apply(receiver, ParamsUtils.setPlaceholders(message, placeholders));
}
}
@@ -1,43 +0,0 @@
package cc.carm.lib.mineconfiguration.common.utils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
public class ParamsUtils {
/**
* 默认的变量格式为 {@code %(变量名)}。
*/
public static Function<@NotNull String, @NotNull String> DEFAULT_PARAM_FORMATTER = (s) -> "%(" + s + ")";
public static String[] formatParams(@NotNull Function<String, String> formatter, @NotNull String[] params) {
return Arrays.stream(params).map(formatter).toArray(String[]::new);
}
public static Map<String, Object> buildParams(@Nullable String[] params, @Nullable Object[] values) {
Map<String, Object> map = new HashMap<>();
if (params == null || params.length == 0) return map;
for (int i = 0; i < params.length; i++) {
map.put(params[i], values.length > i ? values[i] : "?");
}
return map;
}
public static String setPlaceholders(@NotNull String messages, @NotNull Map<String, Object> placeholders) {
if (messages.isEmpty()) return messages;
String parsed = messages;
for (Map.Entry<String, Object> entry : placeholders.entrySet()) {
Object value = entry.getValue();
parsed = parsed.replace(entry.getKey(), value == null ? "" : value.toString());
}
return parsed;
}
}
@@ -1,157 +0,0 @@
package cc.carm.lib.mineconfiguration.common.value;
import cc.carm.lib.mineconfiguration.common.builder.message.MessageValueBuilder;
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
public interface BaseMessage<R, M> {
/**
* 得到所有的接收者
*
* @return 全部可能的接收者
*/
@Unmodifiable
@NotNull Iterable<R> getAllReceivers();
/**
* 得到消息中的通过 {@link MessageValueBuilder#params(String...)}已定变量名(按定义顺序)
*
* @return 已定变量
*/
@NotNull String[] getParams();
/**
* 向接收者发送消息的根方法。
*
* @param receiver 接收者
* @param message 消息内容
*/
@ApiStatus.OverrideOnly
void apply(@NotNull R receiver, @NotNull M message);
/**
* 填入变量值,返回一个准备好待发送的消息。
*
* @param values 变量值
* @return 准备好待发送的消息
*/
default @NotNull PreparedMessage<R, M> prepare(@NotNull Object... values) {
return new PreparedMessage<>(this, values);
}
/**
* 为某位接收者解析此消息。
*
* @param receiver 接收者
* @param placeholders 消息中的变量与对应参数
* @return 解析变量后的消息内容
*/
@Nullable M parse(@Nullable R receiver, @NotNull Map<String, Object> placeholders);
/**
* 为某位接收者解析此消息。
*
* @param receiver 接收者
* @param values 已定变量的对应参数
* @return 解析变量后的消息内容
*/
default @Nullable M parse(@Nullable R receiver, @Nullable Object... values) {
return parse(receiver, ParamsUtils.buildParams(getParams(), values));
}
/**
* 向某位接收者发送消息
*
* @param receiver 消息的接收者
* @param values 已定变量的对应参数
*/
default void send(@Nullable R receiver, @Nullable Object... values) {
send(receiver, ParamsUtils.buildParams(getParams(), values));
}
/**
* 向某位接收者发送消息
*
* @param receiver 消息的接收者
* @param placeholders 消息中的变量与对应参数
*/
default void send(@Nullable R receiver, @NotNull Map<String, Object> placeholders) {
if (receiver == null) return;
M parsed = parse(receiver, placeholders);
if (parsed == null) return;
apply(receiver, parsed);
}
/**
* 向全部接收者(包括后台)发送不同参数的消息
*
* @param eachValues 每位接收者将收到已定变量的对应参数(按定义顺序)
*/
default void sendToEach(@NotNull Function<@NotNull R, Object[]> eachValues) {
sendToEach(null, eachValues);
}
/**
* 向特定接收者发送不同参数的消息
*
* @param limiter 接收者限定器,为空则不限定接收者。
* @param eachValues 每位接收者将收到已定变量的对应参数(按定义顺序)
*/
default void sendToEach(@Nullable Predicate<R> limiter,
@NotNull Function<@NotNull R, Object[]> eachValues) {
Predicate<R> predicate = Optional.ofNullable(limiter).orElse(r -> true);
for (R r : getAllReceivers()) {
if (predicate.test(r)) {
send(r, ParamsUtils.buildParams(getParams(), eachValues.apply(r)));
}
}
}
/**
* 广播此消息(包括后台)
*
* @param values 已定变量的对应参数(按定义顺序)
*/
default void sendToAll(@Nullable Object... values) {
broadcast(values);
}
/**
* 广播此消息(包括后台)
*
* @param placeholders 消息中的变量与对应参数
*/
default void sendToAll(@NotNull Map<String, Object> placeholders) {
broadcast(placeholders);
}
/**
* 广播此消息(包括后台)
*
* @param values 已定变量的对应参数(按定义顺序)
*/
default void broadcast(@Nullable Object... values) {
broadcast(ParamsUtils.buildParams(getParams(), values));
}
/**
* 广播此消息(包括后台)
*
* @param placeholders 消息中的变量与对应参数
*/
default void broadcast(@NotNull Map<String, Object> placeholders) {
getAllReceivers().forEach(r -> send(r, placeholders));
}
}
@@ -1,99 +0,0 @@
package cc.carm.lib.mineconfiguration.common.value;
import cc.carm.lib.configuration.core.function.ConfigValueParser;
import cc.carm.lib.configuration.core.value.ValueManifest;
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
import cc.carm.lib.mineconfiguration.common.data.AbstractText;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
public abstract class ConfigMessage<M, T extends AbstractText<R>, R>
extends ConfiguredValue<T> implements BaseMessage<R, M> {
protected final @NotNull String[] params;
protected final @NotNull BiFunction<@Nullable R, @NotNull String, @Nullable M> messageParser;
protected final @NotNull BiConsumer<@NotNull R, @NotNull M> sendFunction;
protected final @NotNull Function<String, T> textBuilder;
public ConfigMessage(@NotNull ValueManifest<T> manifest,
@NotNull Class<T> textClazz, @NotNull String[] params,
@NotNull BiFunction<@Nullable R, @NotNull String, @Nullable M> messageParser,
@NotNull BiConsumer<@NotNull R, @NotNull M> sendFunction,
@NotNull Function<String, T> textBuilder) {
super(
manifest, textClazz,
ConfigValueParser.castToString().andThen((s, d) -> textBuilder.apply(s)),
AbstractText::getMessage
);
this.params = params;
this.messageParser = messageParser;
this.sendFunction = sendFunction;
this.textBuilder = textBuilder;
}
@Override
public String[] getParams() {
return params;
}
@Override
public void apply(@NotNull R receiver, @NotNull M message) {
sendFunction.accept(receiver, message);
}
protected <N> @Nullable N parseTo(@Nullable R sender, @NotNull Map<String, Object> placeholders,
@NotNull BiFunction<@Nullable R, @NotNull String, @Nullable N> parser) {
T value = get();
if (value == null || value.getMessage().isEmpty()) return null;
else return value.parse(parser, sender, placeholders);
}
public @Nullable String parseString(@Nullable R sender, @NotNull Map<String, Object> placeholders) {
return parseTo(sender, placeholders, (r, s) -> s);
}
@Override
public @Nullable M parse(@Nullable R sender, @NotNull Map<String, Object> placeholders) {
return parseTo(sender, placeholders, this.messageParser);
}
public void set(@Nullable String value) {
this.set(value == null ? null : buildText(value));
}
protected T buildText(String value) {
return textBuilder.apply(value);
}
public abstract class PreparedMessage<P, N> {
protected final @NotNull Object[] values;
protected PreparedMessage(@NotNull Object[] values) {
this.values = values;
}
public Object[] getValues() {
return values;
}
public abstract void to(P receiver);
public void to(Collection<P> receivers) {
receivers.forEach(this::to);
}
public N get(P receiver) {
return null;
}
}
}
@@ -1,109 +0,0 @@
package cc.carm.lib.mineconfiguration.common.value;
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
import cc.carm.lib.configuration.core.value.ValueManifest;
import cc.carm.lib.configuration.core.value.type.ConfiguredList;
import cc.carm.lib.mineconfiguration.common.data.AbstractText;
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
public abstract class ConfigMessageList<M, T extends AbstractText<R>, R>
extends ConfiguredList<T> implements BaseMessage<R, List<M>> {
protected final @NotNull String[] params;
protected final @NotNull BiFunction<@Nullable R, @NotNull String, @Nullable M> messageParser;
protected final @NotNull BiConsumer<@NotNull R, @NotNull List<M>> sendFunction;
protected final @NotNull Function<String, T> textBuilder;
public ConfigMessageList(@NotNull ValueManifest<List<T>> manifest,
@NotNull Class<T> textClazz, @NotNull String[] params,
@NotNull BiFunction<@Nullable R, @NotNull String, @Nullable M> messageParser,
@NotNull BiConsumer<@NotNull R, @NotNull List<M>> sendFunction,
@NotNull Function<String, @NotNull T> textBuilder) {
super(
manifest, textClazz,
ConfigDataFunction.castToString().andThen(textBuilder::apply),
AbstractText::getMessage
);
this.params = params;
this.messageParser = messageParser;
this.sendFunction = sendFunction;
this.textBuilder = textBuilder;
}
@Override
public String[] getParams() {
return params;
}
@Override
public void apply(@NotNull R receiver, @NotNull List<M> message) {
sendFunction.accept(receiver, message);
}
/**
* 为某位接收者解析消息
*
* @param receiver 消息的接收者
* @param placeholders 消息中的变量与对应参数
*/
@Override
public @Nullable List<M> parse(@Nullable R receiver, @NotNull Map<String, Object> placeholders) {
List<T> list = get();
if (list.isEmpty()) return null;
List<String> messages = list.stream().map(T::getMessage).collect(Collectors.toList());
if (String.join("", messages).isEmpty()) return null;
return list.stream().map(value -> value.parse(this.messageParser, receiver, placeholders))
.collect(Collectors.toList());
}
public @Nullable M parseToLine(@Nullable R receiver, @NotNull Object... values) {
return parseToLine(receiver, "\n", ParamsUtils.buildParams(this.params, values));
}
public @Nullable M parseToLine(@Nullable R receiver, @NotNull Map<String, Object> placeholders) {
return parseToLine(receiver, "\n", placeholders);
}
public @Nullable M parseToLine(@Nullable R receiver, @NotNull String delimiter, @NotNull Map<String, Object> placeholders) {
List<T> list = get();
if (list.isEmpty()) return null;
List<String> messages = list.stream().map(T::getMessage).collect(Collectors.toList());
if (String.join("", messages).isEmpty()) return null;
String combined = String.join(delimiter, messages);
T text = textBuilder.apply(combined);
return text.parse(this.messageParser, receiver, placeholders);
}
public void setMessages(@NotNull String... values) {
setMessages(values.length == 0 ? null : Arrays.asList(values));
}
public void setMessages(@Nullable List<String> values) {
if (values == null) {
set(null);
} else {
set(buildText(values));
}
}
protected List<T> buildText(List<String> values) {
return values.stream().map(textBuilder).collect(Collectors.toList());
}
}
@@ -1,48 +0,0 @@
package cc.carm.lib.mineconfiguration.common.value;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class PreparedMessage<R, M> {
protected final @NotNull BaseMessage<R, M> message;
protected final @NotNull Object[] values;
protected PreparedMessage(@NotNull BaseMessage<R, M> message, @NotNull Object[] values) {
this.message = message;
this.values = values;
}
/**
* 为某位接收者解析此消息。
*
* @param receiver 接收者
* @return 解析变量后的消息内容
*/
public @Nullable M parse(@Nullable R receiver) {
return message.parse(receiver, values);
}
/**
* 向某位接收者发送消息
*
* @param receiver 消息的接收者
*/
public void to(@Nullable R receiver) {
message.send(receiver, values);
}
/**
* 向某位接收者发送消息
*
* @param receivers 消息的接收者们
*/
public void to(@NotNull Iterable<? extends R> receivers) {
receivers.forEach(this::to);
}
public void toAll() {
to(message.getAllReceivers());
}
}
+2 -2
View File
@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>mineconfiguration-parent</artifactId> <artifactId>mineconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>2.9.3</version> <version>3.0.0</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@@ -19,7 +19,7 @@
<packaging>jar</packaging> <packaging>jar</packaging>
<name>MineConfiguration-Bukkit</name> <name>MineConfiguration-Bukkit</name>
<description>轻松(做)配置,适用于Bukkit系服务端的版本。</description> <description>EasyConfiguration for Bukkit.</description>
<dependencies> <dependencies>
@@ -1,26 +0,0 @@
package cc.carm.lib.mineconfiguration.bukkit;
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.bukkit.source.CraftConfigProvider;
import cc.carm.lib.mineconfiguration.bukkit.source.CraftSectionWrapper;
import org.jetbrains.annotations.NotNull;
public abstract class CraftConfigValue<T> extends CachedConfigValue<T> {
protected CraftConfigValue(@NotNull ValueManifest<T> manifest) {
super(manifest);
}
public CraftConfigProvider getBukkitProvider() {
ConfigurationProvider<?> provider = getProvider();
if (provider instanceof CraftConfigProvider) return (CraftConfigProvider) getProvider();
else throw new IllegalStateException("Provider is not a CraftConfigProvider");
}
public CraftSectionWrapper getBukkitConfig() {
return getBukkitProvider().getConfiguration();
}
}
@@ -1,47 +1,24 @@
package cc.carm.lib.mineconfiguration.bukkit; package cc.carm.lib.mineconfiguration.bukkit;
import cc.carm.lib.configuration.core.Configuration; import cc.carm.lib.configuration.Configuration;
import cc.carm.lib.configuration.core.ConfigurationRoot; import cc.carm.lib.configuration.source.ConfigurationHolder;
import cc.carm.lib.mineconfiguration.bukkit.source.BukkitConfigProvider; import cc.carm.lib.mineconfiguration.bukkit.source.BukkitConfigFactory;
import cc.carm.lib.mineconfiguration.bukkit.source.BukkitSource;
import cc.carm.lib.mineconfiguration.common.AbstractConfiguration; import cc.carm.lib.mineconfiguration.common.AbstractConfiguration;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.io.File; import java.io.File;
import java.io.IOException;
public class MineConfiguration extends AbstractConfiguration<BukkitConfigProvider> { public class MineConfiguration extends AbstractConfiguration<ConfigurationHolder<BukkitSource>> {
public static BukkitConfigProvider from(File file, String source) { public static ConfigurationHolder<BukkitSource> from(File file, String source) {
BukkitConfigProvider provider = new BukkitConfigProvider(file); return BukkitConfigFactory.from(file).resourcePath(source).build();
try {
provider.initializeFile(source);
provider.initializeConfig();
} catch (IOException e) {
e.printStackTrace();
}
return provider;
} }
public static BukkitConfigProvider from(File file) { public static ConfigurationHolder<BukkitSource> from(Plugin plugin, String fileName) {
return from(file, file.getName()); return from(new File(plugin.getDataFolder(), fileName), fileName);
}
public static BukkitConfigProvider from(String fileName) {
return from(fileName, fileName);
}
public static BukkitConfigProvider from(String fileName, String source) {
return from(new File(fileName), source);
}
public static BukkitConfigProvider from(Plugin plugin, String fileName) {
return from(plugin, fileName, fileName);
}
public static BukkitConfigProvider from(Plugin plugin, String fileName, String source) {
return from(new File(plugin.getDataFolder(), fileName), source);
} }
public MineConfiguration(@NotNull JavaPlugin plugin) { public MineConfiguration(@NotNull JavaPlugin plugin) {
@@ -1,13 +0,0 @@
package cc.carm.lib.mineconfiguration.bukkit.builder;
import cc.carm.lib.mineconfiguration.bukkit.source.CraftConfigProvider;
import cc.carm.lib.configuration.core.builder.AbstractConfigBuilder;
public abstract class AbstractCraftBuilder<T, B extends AbstractCraftBuilder<T, B>>
extends AbstractConfigBuilder<T, B, CraftConfigProvider> {
public AbstractCraftBuilder() {
super(CraftConfigProvider.class);
}
}
@@ -1,139 +0,0 @@
package cc.carm.lib.mineconfiguration.bukkit.builder.item;
import cc.carm.lib.configuration.core.value.ValueManifest;
import cc.carm.lib.mineconfiguration.bukkit.builder.AbstractCraftBuilder;
import cc.carm.lib.mineconfiguration.bukkit.value.item.ConfiguredItem;
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
import java.util.function.Function;
import java.util.function.UnaryOperator;
public class ItemConfigBuilder extends AbstractCraftBuilder<ItemStack, ItemConfigBuilder> {
protected Material type;
protected short data = 0;
protected String name = null;
protected List<String> lore = new ArrayList<>();
protected Map<Enchantment, Integer> enchants = new LinkedHashMap<>();
protected Set<ItemFlag> flags = new LinkedHashSet<>();
protected @NotNull String[] params = new String[0];
protected @NotNull Function<@NotNull String, @NotNull String> paramFormatter = ParamsUtils.DEFAULT_PARAM_FORMATTER;
public ItemConfigBuilder defaults(@NotNull Material type,
@Nullable String name, @NotNull String... lore) {
return defaults(type, (short) 0, name, Arrays.asList(lore));
}
public ItemConfigBuilder defaults(@NotNull Material type, short data,
@Nullable String name, @NotNull String... lore) {
return defaults(type, data, name, Arrays.asList(lore));
}
public ItemConfigBuilder defaults(@NotNull Material type, short data,
@Nullable String name, @NotNull List<String> lore) {
return defaultType(type).defaultDataID(data).defaultName(name).defaultLore(lore);
}
public ItemConfigBuilder defaultType(@NotNull Material type) {
this.type = type;
return this;
}
public ItemConfigBuilder defaultName(@Nullable String name) {
this.name = name;
return this;
}
public ItemConfigBuilder defaultDataID(short dataID) {
this.data = dataID;
return this;
}
public ItemConfigBuilder defaultLore(@NotNull String... lore) {
return defaultLore(Arrays.asList(lore));
}
public ItemConfigBuilder defaultLore(@NotNull List<String> lore) {
this.lore = new ArrayList<>(lore);
return this;
}
public ItemConfigBuilder defaultEnchants(@NotNull Map<Enchantment, Integer> enchants) {
this.enchants = new LinkedHashMap<>(enchants);
return this;
}
public ItemConfigBuilder defaultEnchant(@NotNull Enchantment enchant, int level) {
return defaultEnchants(Collections.singletonMap(enchant, level));
}
public ItemConfigBuilder defaultFlags(@NotNull Set<ItemFlag> flags) {
this.flags = new LinkedHashSet<>(flags);
return this;
}
public ItemConfigBuilder defaultFlags(@NotNull ItemFlag... flags) {
return defaultFlags(new LinkedHashSet<>(Arrays.asList(flags)));
}
public ItemConfigBuilder formatParam(@NotNull UnaryOperator<String> paramFormatter) {
this.paramFormatter = paramFormatter;
return getThis();
}
public ItemConfigBuilder params(@NotNull String... params) {
this.params = params;
return getThis();
}
public ItemConfigBuilder params(@NotNull List<String> params) {
this.params = params.toArray(new String[0]);
return getThis();
}
@Override
protected @NotNull ItemConfigBuilder getThis() {
return this;
}
protected @Nullable ItemStack buildDefault() {
if (this.type == null) return null;
ItemStack item = new ItemStack(type, 1, data);
ItemMeta meta = item.getItemMeta();
if (meta == null) return item;
Optional.ofNullable(this.name).ifPresent(meta::setDisplayName);
Optional.ofNullable(this.lore).ifPresent(meta::setLore);
enchants.forEach((enchant, level) -> meta.addEnchant(enchant, level, true));
flags.forEach(meta::addItemFlags);
item.setItemMeta(meta);
return item;
}
@Override
public @NotNull ConfiguredItem build() {
return new ConfiguredItem(
new ValueManifest<>(
this.provider, this.path, this.headerComments, this.inlineComment,
Optional.ofNullable(this.defaultValue).orElse(buildDefault())
), buildParams()
);
}
protected final String[] buildParams() {
return Arrays.stream(params).map(param -> paramFormatter.apply(param)).toArray(String[]::new);
}
}
@@ -1,44 +0,0 @@
package cc.carm.lib.mineconfiguration.bukkit.builder.message;
import cc.carm.lib.mineconfiguration.bukkit.data.TextConfig;
import cc.carm.lib.mineconfiguration.bukkit.utils.TextParser;
import cc.carm.lib.mineconfiguration.common.builder.message.MessageConfigBuilder;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.function.BiFunction;
public class CraftMessageBuilder extends MessageConfigBuilder<CommandSender, TextConfig> {
public CraftMessageBuilder() {
super(CommandSender.class, TextConfig.class);
}
@Override
public @NotNull <M> CraftMessageValueBuilder<M> asValue(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
return new CraftMessageValueBuilder<>(parser);
}
@Override
public @NotNull <M> CraftMessageListBuilder<M> asList(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
return new CraftMessageListBuilder<>(parser);
}
public @NotNull
CraftMessageValueBuilder<String> asStringValue() {
return asValue(defaultParser()).whenSend(CommandSender::sendMessage);
}
public @NotNull
CraftMessageListBuilder<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) -> TextParser.parseText(receiver, message, new HashMap<>());
}
}
@@ -1,34 +0,0 @@
package cc.carm.lib.mineconfiguration.bukkit.builder.message;
import cc.carm.lib.mineconfiguration.bukkit.data.TextConfig;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredMessageList;
import cc.carm.lib.mineconfiguration.common.builder.message.MessageListBuilder;
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.function.BiFunction;
public class CraftMessageListBuilder<M>
extends MessageListBuilder<M, CommandSender, TextConfig, CraftMessageListBuilder<M>> {
public CraftMessageListBuilder(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
super(CommandSender.class, TextConfig::of, parser);
}
@Override
protected @NotNull CraftMessageListBuilder<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
);
}
}
@@ -1,35 +0,0 @@
package cc.carm.lib.mineconfiguration.bukkit.builder.message;
import cc.carm.lib.mineconfiguration.bukkit.data.TextConfig;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredMessage;
import cc.carm.lib.mineconfiguration.common.builder.message.MessageValueBuilder;
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.function.BiFunction;
public class CraftMessageValueBuilder<M>
extends MessageValueBuilder<M, CommandSender, TextConfig, CraftMessageValueBuilder<M>> {
public CraftMessageValueBuilder(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
super(CommandSender.class, TextConfig::new, parser);
}
@Override
protected @NotNull CraftMessageValueBuilder<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
);
}
}
@@ -1,110 +0,0 @@
package cc.carm.lib.mineconfiguration.bukkit.builder.notify;
import cc.carm.lib.configuration.core.builder.CommonConfigBuilder;
import cc.carm.lib.mineconfiguration.bukkit.data.NotifyConfig;
import cc.carm.lib.mineconfiguration.bukkit.data.SoundConfig;
import cc.carm.lib.mineconfiguration.bukkit.data.TitleConfig;
import cc.carm.lib.mineconfiguration.bukkit.value.notify.ConfiguredNotify;
import cc.carm.lib.mineconfiguration.bukkit.value.notify.DefaultNotifyTypes;
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
import org.bukkit.Sound;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Range;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.UnaryOperator;
public class NotifyConfigBuilder extends CommonConfigBuilder<List<NotifyConfig>, NotifyConfigBuilder> {
protected final @NotNull List<NotifyConfig> notifications = new ArrayList<>();
protected @NotNull String[] params = new String[0];
protected @NotNull Function<@NotNull String, @NotNull String> paramFormatter;
public NotifyConfigBuilder() {
this.paramFormatter = ParamsUtils.DEFAULT_PARAM_FORMATTER;
}
public NotifyConfigBuilder defaultMessages(@NotNull String... messages) {
return defaultMessages(Arrays.asList(messages));
}
public NotifyConfigBuilder defaultMessages(@NotNull List<String> messages) {
for (String message : messages) {
notifications.add(NotifyConfig.of(DefaultNotifyTypes.MESSAGE, message));
}
return defaults(this.notifications);
}
public NotifyConfigBuilder defaultActionBar(@NotNull String message) {
notifications.add(NotifyConfig.of(DefaultNotifyTypes.ACTIONBAR, message));
return defaults(this.notifications);
}
public NotifyConfigBuilder defaultSound(@NotNull Sound sound, float volume, float pitch) {
return defaultSound(sound.name(), volume, pitch);
}
public NotifyConfigBuilder defaultSound(@NotNull Sound sound, float volume) {
return defaultSound(sound, volume, 1.0f);
}
public NotifyConfigBuilder defaultSound(@NotNull Sound sound) {
return defaultSound(sound, 1.0f);
}
public NotifyConfigBuilder defaultSound(@NotNull String soundName, float volume, float pitch) {
notifications.add(NotifyConfig.of(DefaultNotifyTypes.SOUND, new SoundConfig(soundName, volume, pitch)));
return defaults(this.notifications);
}
public NotifyConfigBuilder defaultSound(@NotNull String soundName, float volume) {
return defaultSound(soundName, volume, 1.0f);
}
public NotifyConfigBuilder defaultSound(@NotNull String soundName) {
return defaultSound(soundName, 1.0f);
}
public NotifyConfigBuilder defaultTitle(@Nullable String line1, @Nullable String line2,
@Range(from = 0L, to = Integer.MAX_VALUE) int fadeIn,
@Range(from = 0L, to = Integer.MAX_VALUE) int stay,
@Range(from = 0L, to = Integer.MAX_VALUE) int fadeOut) {
notifications.add(NotifyConfig.of(DefaultNotifyTypes.TITLE, TitleConfig.of(line1, line2, fadeIn, stay, fadeOut)));
return defaults(this.notifications);
}
public NotifyConfigBuilder defaultTitle(@Nullable String line1, @Nullable String line2) {
return defaultTitle(line1, line2, 10, 60, 10);
}
public NotifyConfigBuilder params(String... params) {
this.params = params;
return this;
}
public NotifyConfigBuilder params(@NotNull List<String> params) {
return params(params.toArray(new String[0]));
}
public NotifyConfigBuilder formatParam(UnaryOperator<String> paramFormatter) {
this.paramFormatter = paramFormatter;
return this;
}
@Override
protected @NotNull NotifyConfigBuilder getThis() {
return this;
}
@Override
public @NotNull ConfiguredNotify build() {
return new ConfiguredNotify(buildManifest(), ParamsUtils.formatParams(this.paramFormatter, this.params));
}
}
@@ -1,28 +0,0 @@
package cc.carm.lib.mineconfiguration.bukkit.builder.serializable;
import cc.carm.lib.mineconfiguration.bukkit.builder.AbstractCraftBuilder;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredSerializable;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.jetbrains.annotations.NotNull;
public class SerializableBuilder<T extends ConfigurationSerializable>
extends AbstractCraftBuilder<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<>(buildManifest(), valueClass);
}
}
@@ -1,46 +0,0 @@
package cc.carm.lib.mineconfiguration.bukkit.builder.sound;
import cc.carm.lib.mineconfiguration.bukkit.builder.AbstractCraftBuilder;
import cc.carm.lib.mineconfiguration.bukkit.data.SoundConfig;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredSound;
import org.bukkit.Sound;
import org.jetbrains.annotations.NotNull;
public class SoundConfigBuilder extends AbstractCraftBuilder<SoundConfig, SoundConfigBuilder> {
public @NotNull SoundConfigBuilder defaults(@NotNull Sound sound, float volume, float pitch) {
return defaults(new SoundConfig(sound.name(), sound, volume, pitch));
}
public @NotNull SoundConfigBuilder defaults(@NotNull Sound sound, float volume) {
return defaults(sound, volume, 1.0f);
}
public @NotNull SoundConfigBuilder defaults(@NotNull Sound sound) {
return defaults(sound, 1.0f);
}
public @NotNull SoundConfigBuilder defaults(@NotNull String soundName, float volume, float pitch) {
return defaults(new SoundConfig(soundName, volume, pitch));
}
public @NotNull SoundConfigBuilder defaults(@NotNull String soundName, float volume) {
return defaults(soundName, volume, 1.0f);
}
public @NotNull SoundConfigBuilder defaults(@NotNull String soundName) {
return defaults(soundName, 1.0f);
}
@Override
protected @NotNull SoundConfigBuilder getThis() {
return this;
}
@Override
public @NotNull ConfiguredSound build() {
return new ConfiguredSound(buildManifest());
}
}
@@ -1,82 +0,0 @@
package cc.carm.lib.mineconfiguration.bukkit.builder.title;
import cc.carm.lib.mineconfiguration.bukkit.builder.AbstractCraftBuilder;
import cc.carm.lib.mineconfiguration.bukkit.data.TitleConfig;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredTitle;
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Range;
import java.util.List;
import java.util.function.Function;
import java.util.function.UnaryOperator;
public class TitleConfigBuilder extends AbstractCraftBuilder<TitleConfig, TitleConfigBuilder> {
protected @NotNull String[] params = new String[0];
protected @Range(from = 0L, to = Integer.MAX_VALUE) int fadeIn = 10;
protected @Range(from = 0L, to = Integer.MAX_VALUE) int stay = 60;
protected @Range(from = 0L, to = Integer.MAX_VALUE) int fadeOut = 10;
protected @NotNull ConfiguredTitle.TitleConsumer sendConsumer;
protected @NotNull Function<@NotNull String, @NotNull String> paramFormatter;
public TitleConfigBuilder() {
this.sendConsumer = ConfiguredTitle.DEFAULT_TITLE_CONSUMER;
this.paramFormatter = ParamsUtils.DEFAULT_PARAM_FORMATTER;
}
public @NotNull TitleConfigBuilder defaults(@Nullable String line1,
@Nullable String line2) {
return defaults(TitleConfig.of(line1, line2));
}
public @NotNull TitleConfigBuilder whenSend(@NotNull ConfiguredTitle.TitleConsumer consumer) {
this.sendConsumer = consumer;
return this;
}
public TitleConfigBuilder params(String... params) {
this.params = params;
return this;
}
public TitleConfigBuilder params(@NotNull List<String> params) {
return params(params.toArray(new String[0]));
}
public TitleConfigBuilder fadeIn(@Range(from = 0L, to = Integer.MAX_VALUE) int fadeIn) {
this.fadeIn = fadeIn;
return this;
}
public TitleConfigBuilder stay(@Range(from = 0L, to = Integer.MAX_VALUE) int stay) {
this.stay = stay;
return this;
}
public TitleConfigBuilder fadeOut(@Range(from = 0L, to = Integer.MAX_VALUE) int fadeOut) {
this.fadeOut = fadeOut;
return this;
}
public TitleConfigBuilder formatParam(UnaryOperator<String> paramFormatter) {
this.paramFormatter = paramFormatter;
return this;
}
@Override
protected @NotNull TitleConfigBuilder getThis() {
return this;
}
@Override
public @NotNull ConfiguredTitle build() {
return new ConfiguredTitle(
buildManifest(), ParamsUtils.formatParams(this.paramFormatter, this.params),
this.sendConsumer, this.fadeIn, this.stay, this.fadeOut
);
}
}
@@ -1,7 +1,7 @@
package cc.carm.lib.mineconfiguration.bukkit.data; package cc.carm.lib.mineconfiguration.bukkit.data;
import cc.carm.lib.mineconfiguration.bukkit.value.notify.NotifyCache; import cc.carm.lib.mineconfiguration.bukkit.value.notify.type.NotifyCache;
import cc.carm.lib.mineconfiguration.bukkit.value.notify.NotifyType; import cc.carm.lib.mineconfiguration.bukkit.value.notify.type.NotifyType;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -36,12 +36,16 @@ public class SoundConfig {
this.pitch = pitch; this.pitch = pitch;
} }
public void playTo(Player player) { public void playTo(@NotNull Player player) {
if (type == null) return; playTo(player, player.getLocation());
player.playSound(player.getLocation(), type, volume, pitch);
} }
public void playAt(Location location) { public void playTo(@NotNull Player player, @NotNull Location location) {
if (type == null) return;
player.playSound(location, type, volume, pitch);
}
public void playAt(@NotNull Location location) {
if (type == null) return; if (type == null) return;
if (location.getWorld() == null) return; if (location.getWorld() == null) return;
location.getWorld().playSound(location, type, volume, pitch); location.getWorld().playSound(location, type, volume, pitch);
@@ -51,6 +55,10 @@ public class SoundConfig {
Bukkit.getOnlinePlayers().forEach(this::playTo); Bukkit.getOnlinePlayers().forEach(this::playTo);
} }
public void playToAll(@NotNull Location location) {
Bukkit.getOnlinePlayers().forEach(player -> playTo(player, location));
}
public @NotNull String getTypeName() { public @NotNull String getTypeName() {
return typeName; return typeName;
} }
@@ -1,35 +0,0 @@
package cc.carm.lib.mineconfiguration.bukkit.data;
import cc.carm.lib.mineconfiguration.common.data.AbstractText;
import org.bukkit.command.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));
}
}
@@ -1,7 +1,8 @@
package cc.carm.lib.mineconfiguration.bukkit.data; package cc.carm.lib.mineconfiguration.bukkit.data;
import cc.carm.lib.configuration.core.source.ConfigurationWrapper; import cc.carm.lib.configuration.source.section.ConfigureSection;
import cc.carm.lib.mineconfiguration.bukkit.utils.TextParser; import cc.carm.lib.configuration.value.text.PreparedText;
import cc.carm.lib.mineconfiguration.bukkit.utils.MessageUtils;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredTitle; import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredTitle;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -10,47 +11,31 @@ import org.jetbrains.annotations.Range;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional;
public class TitleConfig { public class TitleConfig {
public static @NotNull TitleConfig of(@Nullable String line1, @Nullable String line2) { public static @NotNull TitleConfig of(@Nullable String line1, @Nullable String line2) {
return of(
Optional.ofNullable(line1).map(TextConfig::of).orElse(null),
Optional.ofNullable(line2).map(TextConfig::of).orElse(null)
);
}
public static @NotNull TitleConfig of(@Nullable TextConfig line1, @Nullable TextConfig line2) {
return new TitleConfig(line1, line2); return new TitleConfig(line1, line2);
} }
public static @NotNull TitleConfig of(@Nullable String line1, @Nullable String line2, public static @NotNull TitleConfig of(@Nullable String line1, @Nullable String line2,
int fadeIn, int stay, int fadeOut) { int fadeIn, int stay, int fadeOut) {
return of(
Optional.ofNullable(line1).map(TextConfig::of).orElse(null),
Optional.ofNullable(line2).map(TextConfig::of).orElse(null),
fadeIn, stay, fadeOut
);
}
public static @NotNull TitleConfig of(@Nullable TextConfig line1, @Nullable TextConfig line2,
int fadeIn, int stay, int fadeOut) {
return new TitleConfig(line1, line2, fadeIn, stay, fadeOut); return new TitleConfig(line1, line2, fadeIn, stay, fadeOut);
} }
protected @Nullable TextConfig line1; protected @Nullable String line1;
protected @Nullable TextConfig line2; protected @Nullable String line2;
protected final int fadeIn; protected @Range(from = 0L, to = Integer.MAX_VALUE) int fadeIn = 10;
protected final int stay; protected @Range(from = 0L, to = Integer.MAX_VALUE) int stay = 60;
protected final int fadeOut; protected @Range(from = 0L, to = Integer.MAX_VALUE) int fadeOut = 10;
protected TitleConfig(@Nullable TextConfig line1, @Nullable TextConfig line2) { protected TitleConfig(@Nullable String line1, @Nullable String line2) {
this(line1, line2, -1, -1, -1); this.line1 = line1;
this.line2 = line2;
} }
protected TitleConfig(@Nullable TextConfig line1, @Nullable TextConfig line2, int fadeIn, int stay, int fadeOut) { protected TitleConfig(@Nullable String line1, @Nullable String line2, int fadeIn, int stay, int fadeOut) {
this.line1 = line1; this.line1 = line1;
this.line2 = line2; this.line2 = line2;
this.fadeIn = fadeIn; this.fadeIn = fadeIn;
@@ -58,30 +43,50 @@ public class TitleConfig {
this.fadeOut = fadeOut; this.fadeOut = fadeOut;
} }
public int getFadeIn() { public int stay() {
return stay;
}
public void stay(int stay) {
this.stay = stay;
}
public @Nullable String line1() {
return line1;
}
public void line1(@Nullable String line1) {
this.line1 = line1;
}
public @Nullable String line2() {
return line2;
}
public void line2(@Nullable String line2) {
this.line2 = line2;
}
public int fadeIn() {
return fadeIn; return fadeIn;
} }
public int getFadeOut() { public void fadeIn(int fadeIn) {
this.fadeIn = fadeIn;
}
public int fadeOut() {
return fadeOut; return fadeOut;
} }
public int getStay() { public void fadeOut(int fadeOut) {
return stay; this.fadeOut = fadeOut;
} }
public boolean isStandardTime() { public boolean isStandardTime() {
return this.fadeIn == 10 && this.stay == 60 && this.fadeOut == 10; return this.fadeIn == 10 && this.stay == 60 && this.fadeOut == 10;
} }
public @Nullable TextConfig getLine1() {
return line1;
}
public @Nullable TextConfig getLine2() {
return line2;
}
public void send(@NotNull Player player, public void send(@NotNull Player player,
@NotNull Map<String, Object> placeholders, @NotNull Map<String, Object> placeholders,
@Nullable ConfiguredTitle.TitleConsumer sendConsumer) { @Nullable ConfiguredTitle.TitleConsumer sendConsumer) {
@@ -105,28 +110,28 @@ public class TitleConfig {
if (sendConsumer == null) return; if (sendConsumer == null) return;
sendConsumer.send( sendConsumer.send(
player, fadeIn, stay, fadeOut, player, fadeIn, stay, fadeOut,
parseLine(this.line1, player, placeholders), parseLine(player, this.line1, placeholders),
parseLine(this.line2, player, placeholders) parseLine(player, this.line2, placeholders)
); );
} }
protected @NotNull String parseLine(@Nullable TextConfig text, protected @NotNull String parseLine(@NotNull Player player, @Nullable String text,
@NotNull Player player, @NotNull Map<String, Object> placeholders) { @NotNull Map<String, Object> placeholders) {
if (text == null) return ""; return text == null ? "" : MessageUtils.parseMessage(player, PreparedText.setPlaceholders(text, placeholders));
else return TextParser.parseText(player, text.getMessage(), placeholders);
} }
public @NotNull Map<String, Object> serialize() { public @NotNull Map<String, Object> serialize() {
Map<String, Object> map = new LinkedHashMap<>(); Map<String, Object> map = new LinkedHashMap<>();
if (this.line1 != null) map.put("line1", this.line1.getMessage()); if (this.line1 != null) map.put("line1", this.line1);
if (this.line2 != null) map.put("line2", this.line2.getMessage()); if (this.line2 != null) map.put("line2", this.line2);
if (this.fadeIn > 0) map.put("fadeIn", this.fadeIn); if (this.fadeIn > 0 && this.fadeIn != 10) map.put("fadeIn", this.fadeIn);
if (this.stay > 0) map.put("stay", this.stay); if (this.stay > 0 && this.stay != 60) map.put("stay", this.stay);
if (this.fadeOut > 0) map.put("fadeOut", this.fadeOut); if (this.fadeOut > 0 && this.fadeOut != 10) map.put("fadeOut", this.fadeOut);
return map; return map;
} }
public static @Nullable TitleConfig deserialize(@NotNull ConfigurationWrapper<?> section) { public static @Nullable TitleConfig deserialize(@NotNull ConfigureSection section) {
String line1 = section.getString("line1"); String line1 = section.getString("line1");
String line2 = section.getString("line2"); String line2 = section.getString("line2");
if (line1 == null && line2 == null) return null; if (line1 == null && line2 == null) return null;
@@ -0,0 +1,57 @@
package cc.carm.lib.mineconfiguration.bukkit.source;
import cc.carm.lib.configuration.commentable.Commentable;
import cc.carm.lib.configuration.source.ConfigurationHolder;
import cc.carm.lib.configuration.source.file.FileConfigFactory;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import java.io.File;
public class BukkitConfigFactory extends FileConfigFactory<BukkitSource, ConfigurationHolder<BukkitSource>, BukkitConfigFactory> {
public static BukkitConfigFactory from(@NotNull String path) {
return new BukkitConfigFactory(new File(path));
}
public static BukkitConfigFactory from(@NotNull File file) {
return new BukkitConfigFactory(file);
}
public static BukkitConfigFactory from(@NotNull File parent, @NotNull String configName) {
return new BukkitConfigFactory(new File(parent, configName));
}
public static BukkitConfigFactory from(@NotNull Plugin plugin, @NotNull String configName) {
return from(plugin.getDataFolder(), configName);
}
public BukkitConfigFactory(@NotNull File file) {
super(file);
}
@Override
protected BukkitConfigFactory self() {
return this;
}
@Override
public @NotNull ConfigurationHolder<BukkitSource> build() {
File configFile = this.file;
String sourcePath = this.resourcePath;
Commentable.registerMeta(this.initializer); // Register commentable meta types
return new ConfigurationHolder<BukkitSource>(this.adapters, this.options, this.metadata, this.initializer) {
final @NotNull BukkitSource source = new BukkitSource(this, configFile, sourcePath);
@Override
public @NotNull BukkitSource config() {
return this.source;
}
};
}
}
@@ -1,79 +0,0 @@
package cc.carm.lib.mineconfiguration.bukkit.source;
import cc.carm.lib.configuration.core.ConfigInitializer;
import cc.carm.lib.configuration.core.source.ConfigurationComments;
import cc.carm.lib.yamlcommentupdater.CommentedYAML;
import cc.carm.lib.yamlcommentupdater.CommentedYAMLWriter;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.util.List;
import java.util.Set;
public class BukkitConfigProvider extends CraftConfigProvider implements CommentedYAML {
protected static final char SEPARATOR = '.';
protected @NotNull ConfigurationComments comments = new ConfigurationComments();
public BukkitConfigProvider(@NotNull File file) {
super(file);
}
public void initializeConfig() {
this.configuration = YamlConfiguration.loadConfiguration(file);
this.initializer = new ConfigInitializer<>(this);
}
@Override
public @NotNull CraftSectionWrapper getConfiguration() {
return CraftSectionWrapper.of(this.configuration);
}
@Override
public void save() throws Exception {
try {
CommentedYAMLWriter.writeWithComments(this, this.file);
} catch (Exception ex) {
configuration.save(file);
throw ex;
}
}
@Override
public @NotNull ConfigurationComments getComments() {
return this.comments;
}
@Override
public String serializeValue(@NotNull String key, @NotNull Object value) {
FileConfiguration temp = new YamlConfiguration();
temp.set(key, value);
return temp.saveToString();
}
@Override
public Set<String> getKeys(@Nullable String sectionKey, boolean deep) {
if (sectionKey == null) return configuration.getKeys(deep);
ConfigurationSection section = configuration.getConfigurationSection(sectionKey);
if (section == null) return null;
return section.getKeys(deep);
}
@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);
}
}
@@ -0,0 +1,83 @@
package cc.carm.lib.mineconfiguration.bukkit.source;
import cc.carm.lib.configuration.source.section.ConfigureSection;
import org.bukkit.configuration.ConfigurationSection;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnmodifiableView;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class BukkitSection implements ConfigureSection {
protected final @NotNull BukkitSource source;
protected final @Nullable BukkitSection parent;
protected final @NotNull ConfigurationSection data;
public BukkitSection(@NotNull BukkitSource source, @Nullable BukkitSection parent,
@NotNull ConfigurationSection data) {
this.source = source;
this.parent = parent;
this.data = data;
}
@Override
public @NotNull BukkitSource source() {
return this.source;
}
@Override
public @Nullable BukkitSection parent() {
return this.parent;
}
public @NotNull ConfigurationSection data() {
return this.data;
}
@Override
public @NotNull Set<String> getKeys(boolean deep) {
return data().getKeys(deep);
}
@Override
public @NotNull @UnmodifiableView Map<String, Object> getValues(boolean deep) {
return data().getValues(deep);
}
@Override
public void set(@NotNull String path, @Nullable Object value) {
data().set(path, value);
}
@Override
public boolean contains(@NotNull String path) {
return data().contains(path);
}
@Override
public @Nullable List<?> getList(@NotNull String path) {
return data().getList(path);
}
@Override
public @Nullable ConfigureSection getSection(@NotNull String path) {
Object value = get(path);
if (value instanceof ConfigureSection) {
return (ConfigureSection) value;
}
return null;
}
@Override
public @Nullable Object get(@NotNull String path) {
Object value = data().get(path);
if (value instanceof ConfigurationSection) {
return new BukkitSection(source(), this, (ConfigurationSection) value);
}
return value;
}
}
@@ -0,0 +1,109 @@
package cc.carm.lib.mineconfiguration.bukkit.source;
import cc.carm.lib.configuration.commentable.Commentable;
import cc.carm.lib.configuration.commentable.CommentableOptions;
import cc.carm.lib.configuration.source.ConfigurationHolder;
import cc.carm.lib.configuration.source.file.FileConfigSource;
import cc.carm.lib.configuration.source.section.ConfigureSection;
import cc.carm.lib.yamlcommentupdater.CommentedSection;
import cc.carm.lib.yamlcommentupdater.CommentedYAMLWriter;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.util.List;
import java.util.Objects;
import java.util.Set;
public class BukkitSource extends FileConfigSource<BukkitSection, YamlConfiguration, BukkitSource>
implements CommentedSection {
protected @Nullable BukkitSection rootSection;
public BukkitSource(@NotNull ConfigurationHolder<? extends BukkitSource> holder,
@NotNull File file, @Nullable String resourcePath) {
super(holder, System.currentTimeMillis(), file, resourcePath);
initialize();
}
public void initialize() {
try {
initializeFile();
onReload();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected BukkitSource self() {
return this;
}
@Override
public @NotNull YamlConfiguration original() {
return (YamlConfiguration) section().data(); // #data() of Root Section always returns YamlConfiguration
}
@Override
public @NotNull BukkitSection section() {
return Objects.requireNonNull(rootSection, "Root section has not been initialized");
}
@Override
public void save() throws Exception {
CommentedYAMLWriter writer = new CommentedYAMLWriter(
String.valueOf(this.separator()), 2,
holder.options().get(CommentableOptions.COMMENT_EMPTY_VALUE)
);
try {
fileWriter(w -> w.write(writer.saveToString(this)));
} catch (Exception ex) {
fileWriter(w -> w.write(original().saveToString()));
}
}
@Override
protected void onReload() throws Exception {
YamlConfiguration configuration = fileReader(YamlConfiguration::loadConfiguration);
this.rootSection = new BukkitSection(this, null, configuration);
}
@Override
public String serializeValue(@NotNull String key, @NotNull Object value) {
FileConfiguration temp = new YamlConfiguration();
temp.set(key, value);
return temp.saveToString();
}
@Override
public @Nullable Set<String> getKeys(@Nullable String sectionKey, boolean deep) {
if (sectionKey == null) return section().getKeys(deep);
ConfigureSection sub = section().getSection(sectionKey);
if (sub == null) return null;
return sub.getKeys(deep);
}
@Override
public @Nullable Object getValue(@NotNull String key) {
return get(key);
}
@Override
public @Nullable String getInlineComment(@NotNull String key) {
return Commentable.getInlineComment(holder(), key);
}
@Override
public @Nullable List<String> getHeaderComments(@Nullable String key) {
return Commentable.getHeaderComments(holder(), key);
}
@Override
public @Nullable List<String> getFooterComments(@Nullable String key) {
return Commentable.getFooterComments(holder(), key);
}
}
@@ -1,43 +0,0 @@
package cc.carm.lib.mineconfiguration.bukkit.source;
import cc.carm.lib.configuration.core.ConfigInitializer;
import cc.carm.lib.configuration.core.source.impl.FileConfigProvider;
import org.bukkit.configuration.file.YamlConfiguration;
import org.jetbrains.annotations.NotNull;
import java.io.File;
public abstract class CraftConfigProvider extends FileConfigProvider<CraftSectionWrapper> {
public static final char SEPARATOR = '.';
protected ConfigInitializer<? extends CraftConfigProvider> initializer;
protected YamlConfiguration configuration;
public CraftConfigProvider(@NotNull File file) {
super(file);
}
public abstract void initializeConfig();
@Override
public @NotNull CraftSectionWrapper getConfiguration() {
return CraftSectionWrapper.of(this.configuration);
}
@Override
protected void onReload() throws Exception {
configuration.load(getFile());
}
@Override
public void save() throws Exception {
configuration.save(getFile());
}
@Override
public @NotNull ConfigInitializer<? extends CraftConfigProvider> getInitializer() {
return this.initializer;
}
}
@@ -1,75 +0,0 @@
package cc.carm.lib.mineconfiguration.bukkit.source;
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
import org.bukkit.configuration.ConfigurationSection;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
public class CraftSectionWrapper implements ConfigurationWrapper<ConfigurationSection> {
protected final ConfigurationSection configuration;
protected CraftSectionWrapper(ConfigurationSection configuration) {
this.configuration = configuration;
}
@Override
public @NotNull ConfigurationSection getSource() {
return this.configuration;
}
@Override
public @NotNull Set<String> getKeys(boolean deep) {
return this.configuration.getKeys(deep);
}
@Override
public @NotNull Map<String, Object> getValues(boolean deep) {
return this.configuration.getValues(deep);
}
@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 this.configuration.isList(path);
}
@Override
public @Nullable List<?> getList(@NotNull String path) {
return this.configuration.getList(path);
}
@Override
public boolean isConfigurationSection(@NotNull String path) {
return this.configuration.isConfigurationSection(path);
}
@Override
public @Nullable CraftSectionWrapper getConfigurationSection(@NotNull String path) {
return Optional.ofNullable(configuration.getConfigurationSection(path))
.map(CraftSectionWrapper::of).orElse(null);
}
public static CraftSectionWrapper of(ConfigurationSection section) {
return new CraftSectionWrapper(section);
}
}
@@ -1,7 +1,6 @@
package cc.carm.lib.mineconfiguration.bukkit.utils; package cc.carm.lib.mineconfiguration.bukkit.utils;
import cc.carm.lib.easyplugin.utils.ColorParser; import cc.carm.lib.easyplugin.utils.ColorParser;
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@@ -10,28 +9,26 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class TextParser { public class MessageUtils {
private TextParser() { private MessageUtils() {
} }
@Contract("_,!null,_->!null") @Contract("_,!null->!null")
public static @Nullable String parseText(@Nullable CommandSender sender, @Nullable String message, @NotNull Map<String, Object> placeholders) { public static @Nullable String parseMessage(@Nullable CommandSender sender, @Nullable String message) {
if (message == null) return null; if (message == null) return null;
if (sender instanceof Player && hasPlaceholderAPI()) { if (sender instanceof Player && hasPlaceholderAPI()) {
message = PlaceholderAPIHelper.parseMessages((Player) sender, message); message = PlaceholderAPIHelper.parseMessages((Player) sender, message);
} }
return ColorParser.parse(ParamsUtils.setPlaceholders(message, placeholders)); return ColorParser.parse(message);
} }
public static @NotNull List<String> parseList(@Nullable CommandSender sender, List<String> messages, @NotNull Map<String, Object> placeholders) { public static @NotNull List<String> parseMessage(@Nullable CommandSender sender, @NotNull List<String> messages) {
if (sender instanceof Player && hasPlaceholderAPI()) { if (sender instanceof Player && hasPlaceholderAPI()) {
messages = PlaceholderAPIHelper.parseMessages((Player) sender, messages); messages = PlaceholderAPIHelper.parseMessages((Player) sender, messages);
} }
return ColorParser.parse(messages.stream().map(s -> ParamsUtils.setPlaceholders(s, placeholders)).collect(Collectors.toList())); return ColorParser.parse(messages);
} }
public static boolean hasPlaceholderAPI() { public static boolean hasPlaceholderAPI() {
@@ -1,11 +1,9 @@
package cc.carm.lib.mineconfiguration.bukkit.value; package cc.carm.lib.mineconfiguration.bukkit.value;
import cc.carm.lib.configuration.core.value.ValueManifest; import cc.carm.lib.configuration.value.ValueManifest;
import cc.carm.lib.mineconfiguration.bukkit.CraftConfigValue; import cc.carm.lib.configuration.value.text.ConfiguredText;
import cc.carm.lib.mineconfiguration.bukkit.builder.message.CraftMessageValueBuilder; import cc.carm.lib.configuration.value.text.data.TextContents;
import cc.carm.lib.mineconfiguration.bukkit.data.TextConfig; import cc.carm.lib.mineconfiguration.bukkit.utils.MessageUtils;
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
import cc.carm.lib.mineconfiguration.common.value.ConfigMessage;
import com.cryptomorin.xseries.messages.ActionBar; import com.cryptomorin.xseries.messages.ActionBar;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@@ -13,52 +11,61 @@ import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
public class ConfiguredMessage<M> extends ConfigMessage<M, TextConfig, CommandSender> { public class ConfiguredMessage<M> extends ConfiguredText<M, CommandSender> {
@NotNull @NotNull
public static <M> CraftMessageValueBuilder<@Nullable M> create(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser) { public static <M> ConfiguredMessage.Builder<M> create(
return CraftConfigValue.builder().createMessage().asValue(messageParser); @NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> compiler
) {
return new Builder<M>().compiler(compiler);
} }
public static CraftMessageValueBuilder<String> asString() { public static Builder<String> asString() {
return CraftConfigValue.builder().createMessage().asStringValue(); return create((sender, message) -> message)
.parser(MessageUtils::parseMessage)
.dispatcher((sender, message) -> message.forEach(sender::sendMessage));
} }
public static ConfiguredMessage<String> ofString() { public static ConfiguredMessage<String> ofString() {
return asString().build(); return asString().build();
} }
public static ConfiguredMessage<String> ofString(@NotNull String defaultMessage) { public static ConfiguredMessage<String> ofString(@NotNull String... messages) {
return asString().defaults(defaultMessage).build(); return asString().defaults(messages).build();
} }
public ConfiguredMessage(@NotNull ValueManifest<TextConfig> manifest, @NotNull String[] params, public ConfiguredMessage(@NotNull ValueManifest<TextContents> manifest,
@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser, @NotNull BiFunction<CommandSender, String, String> parser,
@NotNull BiConsumer<@NotNull CommandSender, @NotNull M> sendFunction) { @NotNull BiFunction<CommandSender, String, M> compiler,
super(manifest, TextConfig.class, params, messageParser, sendFunction, TextConfig::of); @NotNull BiConsumer<CommandSender, List<M>> dispatcher,
@NotNull String[] params) {
super(manifest, parser, compiler, dispatcher, params);
} }
public void sendActionBar(Player player, String... values) { public void sendActionBar(Player player, Object... values) {
sendActionBar(player, ParamsUtils.buildParams(this.params, values)); ActionBar.sendActionBar(player, prepare(values).parseLine(player, (sender, message) -> message));
} }
public void sendActionBar(Player player, Map<String, Object> placeholders) { public void print(Object... values) {
ActionBar.sendActionBar(player, parseString(player, placeholders)); prepare(values).to(Bukkit.getConsoleSender());
}
public static class Builder<M> extends ConfiguredText.Builder<M, CommandSender, Builder<M>> {
@Override
public @NotNull ConfiguredMessage<M> build() {
return new ConfiguredMessage<>(buildManifest(), this.parser, this.compiler, this.dispatcher, this.params);
} }
@Override @Override
public @NotNull Collection<CommandSender> getAllReceivers() { public @NotNull ConfiguredMessage.Builder<M> self() {
List<CommandSender> senders = new ArrayList<>(); return this;
senders.add(Bukkit.getConsoleSender()); }
senders.addAll(Bukkit.getOnlinePlayers());
return senders;
} }
} }
@@ -1,47 +0,0 @@
package cc.carm.lib.mineconfiguration.bukkit.value;
import cc.carm.lib.configuration.core.value.ValueManifest;
import cc.carm.lib.mineconfiguration.bukkit.CraftConfigValue;
import cc.carm.lib.mineconfiguration.bukkit.builder.message.CraftMessageListBuilder;
import cc.carm.lib.mineconfiguration.bukkit.data.TextConfig;
import cc.carm.lib.mineconfiguration.common.value.ConfigMessageList;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
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> CraftMessageListBuilder<M> create(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser) {
return CraftConfigValue.builder().createMessage().asList(messageParser);
}
public static CraftMessageListBuilder<String> asStrings() {
return CraftConfigValue.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(Bukkit.getConsoleSender());
senders.addAll(Bukkit.getOnlinePlayers());
return senders;
}
}
@@ -1,50 +0,0 @@
package cc.carm.lib.mineconfiguration.bukkit.value;
import cc.carm.lib.configuration.core.value.ValueManifest;
import cc.carm.lib.mineconfiguration.bukkit.CraftConfigValue;
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 CraftConfigValue<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(@NotNull ValueManifest<T> manifest, @NotNull Class<T> valueClass) {
super(manifest);
this.valueClass = valueClass;
}
@Override
public @Nullable T get() {
if (isExpired()) { // 已过时的数据,需要重新解析一次。
try {
// 若未出现错误,则直接更新缓存并返回。
return updateCache(getBukkitConfig().get(getConfigPath(), getDefaultValue(), valueClass));
} catch (Exception e) {
// 出现了解析错误,提示并返回默认值。
e.printStackTrace();
return getDefaultValue();
}
} else return Optional.ofNullable(getCachedValue()).orElse(defaultValue);
}
@Override
public void set(@Nullable T value) {
updateCache(value);
setValue(value);
}
}
@@ -1,10 +1,12 @@
package cc.carm.lib.mineconfiguration.bukkit.value; package cc.carm.lib.mineconfiguration.bukkit.value;
import cc.carm.lib.configuration.core.function.ConfigValueParser;
import cc.carm.lib.configuration.core.value.ValueManifest; import cc.carm.lib.configuration.adapter.ValueAdapter;
import cc.carm.lib.configuration.core.value.type.ConfiguredValue; import cc.carm.lib.configuration.adapter.ValueType;
import cc.carm.lib.mineconfiguration.bukkit.CraftConfigValue; import cc.carm.lib.configuration.builder.AbstractConfigBuilder;
import cc.carm.lib.mineconfiguration.bukkit.builder.sound.SoundConfigBuilder; import cc.carm.lib.configuration.source.ConfigurationHolder;
import cc.carm.lib.configuration.value.ValueManifest;
import cc.carm.lib.configuration.value.standard.ConfiguredValue;
import cc.carm.lib.mineconfiguration.bukkit.data.SoundConfig; import cc.carm.lib.mineconfiguration.bukkit.data.SoundConfig;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Sound; import org.bukkit.Sound;
@@ -15,47 +17,57 @@ import java.util.Optional;
public class ConfiguredSound extends ConfiguredValue<SoundConfig> { public class ConfiguredSound extends ConfiguredValue<SoundConfig> {
public static @NotNull SoundConfigBuilder create() { public static @NotNull Builder create() {
return CraftConfigValue.builder().createSound(); return new Builder();
} }
public static @NotNull ConfiguredSound of(Sound sound) { public static @NotNull ConfiguredSound of(Sound sound) {
return CraftConfigValue.builder().createSound().defaults(sound).build(); return create().defaults(sound).build();
} }
public static @NotNull ConfiguredSound of(Sound sound, float volume) { public static @NotNull ConfiguredSound of(Sound sound, float volume) {
return CraftConfigValue.builder().createSound().defaults(sound, volume).build(); return create().defaults(sound, volume).build();
} }
public static @NotNull ConfiguredSound of(Sound sound, float volume, float pitch) { public static @NotNull ConfiguredSound of(Sound sound, float volume, float pitch) {
return CraftConfigValue.builder().createSound().defaults(sound, volume, pitch).build(); return create().defaults(sound, volume, pitch).build();
} }
public static @NotNull ConfiguredSound of(String soundName) { public static @NotNull ConfiguredSound of(String soundName) {
return CraftConfigValue.builder().createSound().defaults(soundName).build(); return create().defaults(soundName).build();
} }
public static @NotNull ConfiguredSound of(String soundName, float volume) { public static @NotNull ConfiguredSound of(String soundName, float volume) {
return CraftConfigValue.builder().createSound().defaults(soundName, volume).build(); return create().defaults(soundName, volume).build();
} }
public static @NotNull ConfiguredSound of(String soundName, float volume, float pitch) { public static @NotNull ConfiguredSound of(String soundName, float volume, float pitch) {
return CraftConfigValue.builder().createSound().defaults(soundName, volume, pitch).build(); return create().defaults(soundName, volume, pitch).build();
} }
public ConfiguredSound(@NotNull ValueManifest<SoundConfig> manifest) {
super(manifest, SoundConfig.class, getSoundParser(), SoundConfig::serialize); public static final ValueType<SoundConfig> SOUND_TYPE = ValueType.of(SoundConfig.class);
public static final ValueAdapter<SoundConfig> SOUND_ADAPTER = new ValueAdapter<>(SOUND_TYPE,
(holder, type, value) -> value.serialize(),
(holder, type, value) -> {
String conf = holder.deserialize(String.class, value);
return SoundConfig.deserialize(conf);
}
);
public ConfiguredSound(@NotNull ValueManifest<SoundConfig> manifest, @NotNull ValueAdapter<SoundConfig> adapter) {
super(manifest, adapter);
} }
public void setSound(@NotNull Sound sound) { public void set(@NotNull Sound sound) {
setSound(sound, 1.0f); set(sound, 1.0f);
} }
public void setSound(@NotNull Sound sound, float volume) { public void set(@NotNull Sound sound, float volume) {
setSound(sound, volume, 1.0f); set(sound, volume, 1.0f);
} }
public void setSound(@NotNull Sound sound, float volume, float pitch) { public void set(@NotNull Sound sound, float volume, float pitch) {
set(new SoundConfig(sound.name(), sound, volume, pitch)); set(new SoundConfig(sound.name(), sound, volume, pitch));
} }
@@ -71,8 +83,53 @@ public class ConfiguredSound extends ConfiguredValue<SoundConfig> {
Optional.ofNullable(get()).ifPresent(s -> s.playAt(location)); Optional.ofNullable(get()).ifPresent(s -> s.playAt(location));
} }
public static ConfigValueParser<Object, SoundConfig> getSoundParser() { public static class Builder extends AbstractConfigBuilder<SoundConfig, ConfiguredSound, ConfigurationHolder<?>, Builder> {
return ConfigValueParser.castToString().andThen((s, d) -> SoundConfig.deserialize(s));
protected @NotNull ValueAdapter<SoundConfig> adapter = SOUND_ADAPTER;
protected Builder() {
super(ConfigurationHolder.class, SOUND_TYPE);
} }
public @NotNull Builder adapter(@NotNull ValueAdapter<SoundConfig> adapter) {
this.adapter = adapter;
return this;
}
public @NotNull Builder defaults(@NotNull Sound sound, float volume, float pitch) {
return defaults(new SoundConfig(sound.name(), sound, volume, pitch));
}
public @NotNull Builder defaults(@NotNull Sound sound, float volume) {
return defaults(sound, volume, 1.0f);
}
public @NotNull Builder defaults(@NotNull Sound sound) {
return defaults(sound, 1.0f);
}
public @NotNull Builder defaults(@NotNull String soundName, float volume, float pitch) {
return defaults(new SoundConfig(soundName, volume, pitch));
}
public @NotNull Builder defaults(@NotNull String soundName, float volume) {
return defaults(soundName, volume, 1.0f);
}
public @NotNull Builder defaults(@NotNull String soundName) {
return defaults(soundName, 1.0f);
}
@Override
protected @NotNull Builder self() {
return this;
}
@Override
public @NotNull ConfiguredSound build() {
return new ConfiguredSound(buildManifest(), this.adapter);
}
}
} }
@@ -1,13 +1,14 @@
package cc.carm.lib.mineconfiguration.bukkit.value; package cc.carm.lib.mineconfiguration.bukkit.value;
import cc.carm.lib.configuration.core.function.ConfigValueParser; import cc.carm.lib.configuration.adapter.ValueAdapter;
import cc.carm.lib.configuration.core.source.ConfigurationWrapper; import cc.carm.lib.configuration.adapter.ValueType;
import cc.carm.lib.configuration.core.value.ValueManifest; import cc.carm.lib.configuration.builder.AbstractConfigBuilder;
import cc.carm.lib.configuration.core.value.type.ConfiguredSection; import cc.carm.lib.configuration.source.ConfigurationHolder;
import cc.carm.lib.mineconfiguration.bukkit.CraftConfigValue; import cc.carm.lib.configuration.source.section.ConfigureSection;
import cc.carm.lib.mineconfiguration.bukkit.builder.title.TitleConfigBuilder; import cc.carm.lib.configuration.value.ValueManifest;
import cc.carm.lib.configuration.value.standard.ConfiguredValue;
import cc.carm.lib.configuration.value.text.function.TextParser;
import cc.carm.lib.mineconfiguration.bukkit.data.TitleConfig; import cc.carm.lib.mineconfiguration.bukkit.data.TitleConfig;
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
import com.cryptomorin.xseries.messages.Titles; import com.cryptomorin.xseries.messages.Titles;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@@ -15,17 +16,20 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Range; import org.jetbrains.annotations.Range;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.function.UnaryOperator;
public class ConfiguredTitle extends ConfiguredSection<TitleConfig> { public class ConfiguredTitle extends ConfiguredValue<TitleConfig> {
public static final @NotNull ConfiguredTitle.TitleConsumer DEFAULT_TITLE_CONSUMER = Titles::sendTitle; public static final @NotNull ConfiguredTitle.TitleConsumer DEFAULT_TITLE_CONSUMER = Titles::sendTitle;
public static TitleConfigBuilder create() { public static Builder create() {
return CraftConfigValue.builder().createTitle(); return new Builder();
} }
public static ConfiguredTitle of(@Nullable String line1, @Nullable String line2) { public static ConfiguredTitle of(@Nullable String line1, @Nullable String line2) {
@@ -37,41 +41,53 @@ public class ConfiguredTitle extends ConfiguredSection<TitleConfig> {
return create().defaults(line1, line2).fadeIn(fadeIn).stay(stay).fadeOut(fadeOut).build(); return create().defaults(line1, line2).fadeIn(fadeIn).stay(stay).fadeOut(fadeOut).build();
} }
public static final ValueType<TitleConfig> TITLE_TYPE = ValueType.of(TitleConfig.class);
public static final ValueAdapter<TitleConfig> TITLE_ADAPTER = new ValueAdapter<>(TITLE_TYPE,
(holder, type, value) -> value.serialize(),
(holder, type, value) -> {
ConfigureSection section = holder.deserialize(ConfigureSection.class, value);
return TitleConfig.deserialize(section);
}
);
protected final @NotNull ConfiguredTitle.TitleConsumer sendConsumer; protected final @NotNull ConfiguredTitle.TitleConsumer sendConsumer;
protected final @NotNull UnaryOperator<String> paramBuilder;
protected final @NotNull String[] params; protected final @NotNull String[] params;
protected final int fadeIn; public ConfiguredTitle(@NotNull ValueManifest<TitleConfig> manifest, ValueAdapter<TitleConfig> adapter,
protected final int stay; @NotNull UnaryOperator<String> paramBuilder, @NotNull String[] params,
protected final int fadeOut; @NotNull ConfiguredTitle.TitleConsumer sendConsumer) {
super(manifest, adapter);
public ConfiguredTitle(@NotNull ValueManifest<TitleConfig> manifest, @NotNull String[] params, this.paramBuilder = paramBuilder;
@NotNull ConfiguredTitle.TitleConsumer sendConsumer,
int fadeIn, int stay, int fadeOut) {
super(manifest, TitleConfig.class, getTitleParser(), TitleConfig::serialize);
this.sendConsumer = sendConsumer; this.sendConsumer = sendConsumer;
this.params = params; this.params = params;
this.fadeIn = fadeIn;
this.stay = stay;
this.fadeOut = fadeOut;
} }
@Range(from = 0L, to = Integer.MAX_VALUE) public void set(Consumer<TitleConfig> handler) {
public int getFadeInTicks() { TitleConfig conf = get();
return fadeIn; handler.accept(conf);
set(conf);
} }
@Range(from = 0L, to = Integer.MAX_VALUE) public void setLine1(@Nullable String line1) {
public int getStayTicks() { set(conf -> conf.line1(line1));
return stay;
} }
@Range(from = 0L, to = Integer.MAX_VALUE) public void setLine2(@Nullable String line2) {
public int getFadeOutTicks() { set(conf -> conf.line2(line2));
return fadeOut;
} }
public @NotNull ConfiguredTitle.TitleConsumer getSendConsumer() { public void setFadeIn(int fadeIn) {
return sendConsumer; set(conf -> conf.fadeIn(fadeIn));
}
public void setStay(int stay) {
set(conf -> conf.stay(stay));
}
public void setFadeOut(int fadeOut) {
set(conf -> conf.fadeOut(fadeOut));
} }
public void send(@NotNull Player player, Object... values) { public void send(@NotNull Player player, Object... values) {
@@ -79,14 +95,12 @@ public class ConfiguredTitle extends ConfiguredSection<TitleConfig> {
} }
public void send(@NotNull Player player, @NotNull String[] params, @NotNull Object[] values) { public void send(@NotNull Player player, @NotNull String[] params, @NotNull Object[] values) {
send(player, ParamsUtils.buildParams(params, values)); send(player, TextParser.buildParams(paramBuilder, params, values));
} }
public void send(@NotNull Player player, @NotNull Map<String, Object> placeholders) { public void send(@NotNull Player player, @NotNull Map<String, Object> placeholders) {
TitleConfig config = get(); TitleConfig config = get();
if (config != null) { if (config != null) config.send(player, placeholders, sendConsumer);
config.send(player, fadeIn, stay, fadeOut, placeholders, sendConsumer);
}
} }
public void sendToAll(Object... values) { public void sendToAll(Object... values) {
@@ -94,7 +108,7 @@ public class ConfiguredTitle extends ConfiguredSection<TitleConfig> {
} }
public void sendToAll(@NotNull String[] params, @NotNull Object[] values) { public void sendToAll(@NotNull String[] params, @NotNull Object[] values) {
sendToAll(ParamsUtils.buildParams(params, values)); sendToAll(TextParser.buildParams(paramBuilder, params, values));
} }
public void sendToAll(@NotNull Map<String, Object> placeholders) { public void sendToAll(@NotNull Map<String, Object> placeholders) {
@@ -112,11 +126,7 @@ public class ConfiguredTitle extends ConfiguredSection<TitleConfig> {
@NotNull Function<@NotNull Player, Object[]> eachValues) { @NotNull Function<@NotNull Player, Object[]> eachValues) {
Predicate<Player> predicate = Optional.ofNullable(limiter).orElse(r -> true); Predicate<Player> predicate = Optional.ofNullable(limiter).orElse(r -> true);
Bukkit.getOnlinePlayers().stream().filter(predicate) Bukkit.getOnlinePlayers().stream().filter(predicate)
.forEach(r -> send(r, ParamsUtils.buildParams(params, eachValues.apply(r)))); .forEach(r -> send(r, eachValues.apply(r)));
}
public static ConfigValueParser<ConfigurationWrapper<?>, TitleConfig> getTitleParser() {
return (s, d) -> TitleConfig.deserialize(s);
} }
@FunctionalInterface @FunctionalInterface
@@ -140,4 +150,77 @@ public class ConfiguredTitle extends ConfiguredSection<TitleConfig> {
} }
public static class Builder extends AbstractConfigBuilder<TitleConfig, ConfiguredTitle, ConfigurationHolder<?>, Builder> {
protected @NotNull ValueAdapter<TitleConfig> adapter = TITLE_ADAPTER;
protected @NotNull TitleConfig title = TitleConfig.of(null, null);
protected @NotNull String[] params = new String[0];
protected @NotNull ConfiguredTitle.TitleConsumer sendConsumer = DEFAULT_TITLE_CONSUMER;
protected @NotNull UnaryOperator<String> paramFormatter = s -> "%(" + s + ")";
protected Builder() {
super(ConfigurationHolder.class, TITLE_TYPE);
defaults(() -> this.title);
}
public @NotNull Builder adapter(@NotNull ValueAdapter<TitleConfig> adapter) {
this.adapter = adapter;
return this;
}
public @NotNull Builder defaults(Consumer<TitleConfig> handler) {
handler.accept(this.title);
return this;
}
public @NotNull Builder defaults(@Nullable String line1, @Nullable String line2) {
return line1(line1).line2(line2);
}
public @NotNull Builder line1(@Nullable String line1) {
return defaults(t -> t.line1(line1));
}
public @NotNull Builder line2(@Nullable String line2) {
return defaults(t -> t.line2(line2));
}
public @NotNull Builder fadeIn(@Range(from = 0L, to = Integer.MAX_VALUE) int fadeIn) {
return defaults(t -> t.fadeIn(fadeIn));
}
public @NotNull Builder stay(@Range(from = 0L, to = Integer.MAX_VALUE) int stay) {
return defaults(t -> t.stay(stay));
}
public @NotNull Builder fadeOut(@Range(from = 0L, to = Integer.MAX_VALUE) int fadeOut) {
return defaults(t -> t.fadeOut(fadeOut));
}
public @NotNull Builder params(String... params) {
this.params = params;
return this;
}
public @NotNull Builder params(@NotNull List<String> params) {
return params(params.toArray(new String[0]));
}
public @NotNull Builder whenSend(@NotNull ConfiguredTitle.TitleConsumer consumer) {
this.sendConsumer = consumer;
return this;
}
@Override
protected @NotNull Builder self() {
return this;
}
@Override
public @NotNull ConfiguredTitle build() {
return new ConfiguredTitle(buildManifest(), this.adapter, this.paramFormatter, this.params, this.sendConsumer);
}
}
} }
@@ -1,11 +1,18 @@
package cc.carm.lib.mineconfiguration.bukkit.value.item; package cc.carm.lib.mineconfiguration.bukkit.value.item;
import cc.carm.lib.configuration.core.value.ValueManifest; import cc.carm.lib.configuration.adapter.ValueAdapter;
import cc.carm.lib.configuration.core.value.type.ConfiguredSection; import cc.carm.lib.configuration.adapter.ValueType;
import cc.carm.lib.mineconfiguration.bukkit.builder.item.ItemConfigBuilder; import cc.carm.lib.configuration.builder.AbstractConfigBuilder;
import cc.carm.lib.configuration.source.ConfigurationHolder;
import cc.carm.lib.configuration.value.ValueManifest;
import cc.carm.lib.configuration.value.standard.ConfiguredValue;
import cc.carm.lib.configuration.value.text.function.ContentHandler;
import com.cryptomorin.xseries.XItemStack; import com.cryptomorin.xseries.XItemStack;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -13,50 +20,50 @@ import org.jetbrains.annotations.Nullable;
import java.util.*; import java.util.*;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.UnaryOperator;
public class ConfiguredItem extends ConfiguredSection<ItemStack> { public class ConfiguredItem extends ConfiguredValue<ItemStack> {
public static Builder create() {
public static ItemConfigBuilder create() { return new Builder();
return new ItemConfigBuilder();
} }
public static final @NotNull ValueType<ItemStack> ITEM_TYPE = ValueType.of(ItemStack.class);
public static final ValueAdapter<ItemStack> ITEM_ADAPTER = new ValueAdapter<>(ITEM_TYPE,
(holder, type, value) -> XItemStack.serialize(value),
(holder, type, value) -> {
ConfigurationSection section = (ConfigurationSection) value;
return XItemStack.deserialize(section);
}
);
protected final @NotNull UnaryOperator<String> paramBuilder;
protected final @NotNull String[] params; protected final @NotNull String[] params;
public ConfiguredItem(@NotNull ValueManifest<ItemStack> manifest, @NotNull String[] params) { public ConfiguredItem(@NotNull ValueManifest<ItemStack> manifest, ValueAdapter<ItemStack> adapter,
super( @NotNull UnaryOperator<String> paramBuilder, @NotNull String[] params) {
manifest, ItemStack.class, super(manifest, adapter);
(data, v) -> XItemStack.deserialize((ConfigurationSection) data.getSource()), this.paramBuilder = paramBuilder;
XItemStack::serialize
);
this.params = params; this.params = params;
} }
public @NotNull String[] getParams() {
return params;
}
@Override @Override
public @NotNull Optional<@Nullable ItemStack> getOptional() { public @NotNull Optional<@Nullable ItemStack> optional() {
return Optional.ofNullable(super.get()); return Optional.ofNullable(super.get());
} }
@Override @Override
public @Nullable ItemStack get() { public @Nullable ItemStack get() {
return getOptional().map(ItemStack::clone).orElse(null); return optional().map(ItemStack::clone).orElse(null);
} }
public @Nullable ItemStack get(Consumer<ItemStack> modifier) { public @Nullable ItemStack get(Consumer<ItemStack> modifier) {
return getOptional().map(item -> { return optional().map(item -> {
modifier.accept(item); modifier.accept(item);
return item; return item;
}).orElse(null); }).orElse(null);
} }
public @NotNull PreparedItem prepare(@NotNull Object... values) {
return PreparedItem.of(player -> get()).params(params).values(values);
}
public @Nullable ItemStack get(@Nullable Player player) { public @Nullable ItemStack get(@Nullable Player player) {
return get(player, new HashMap<>()); return get(player, new HashMap<>());
} }
@@ -65,15 +72,18 @@ public class ConfiguredItem extends ConfiguredSection<ItemStack> {
return prepare(values).get(player); return prepare(values).get(player);
} }
public @Nullable ItemStack get(@Nullable Player player, @NotNull String[] params, @NotNull Object[] values) {
return prepare().params(params).values(values).get(player);
}
public @Nullable ItemStack get(@Nullable Player player, public @Nullable ItemStack get(@Nullable Player player,
@NotNull Map<String, Object> placeholders) { @NotNull Map<String, Object> placeholders) {
return prepare().placeholders(placeholders).get(player); return prepare().placeholders(placeholders).get(player);
} }
public @Nullable Map<Integer, ItemStack> give(@NotNull Player player, @NotNull Object... values) {
return prepare(values).give(player);
}
public @NotNull PreparedItem prepare(@NotNull Object... values) {
return PreparedItem.of(player -> get()).params(params).placeholders(values);
}
public void modifyItem(Consumer<ItemStack> modifier) { public void modifyItem(Consumer<ItemStack> modifier) {
ItemStack item = get(); ItemStack item = get();
@@ -103,4 +113,104 @@ public class ConfiguredItem extends ConfiguredSection<ItemStack> {
else setLore(Arrays.asList(lore)); else setLore(Arrays.asList(lore));
} }
public static class Builder extends AbstractConfigBuilder<ItemStack, ConfiguredItem, ConfigurationHolder<?>, Builder> {
protected @Nullable ItemStack item = null;
protected @NotNull String[] params = new String[0];
protected @NotNull UnaryOperator<String> paramFormatter = ContentHandler.DEFAULT_PARAM_BUILDER;
public Builder() {
super(ConfigurationHolder.class, ITEM_TYPE);
defaults(() -> item);
}
@Override
public @NotNull Builder defaults(@Nullable ItemStack item) {
this.item = item;
return this;
}
public Builder defaults(@NotNull Material type) {
return defaults(new ItemStack(type));
}
public Builder defaults(Consumer<ItemStack> consumer) {
if (this.item == null) return self();
consumer.accept(this.item);
return self();
}
public Builder defaultMeta(Consumer<ItemMeta> consumer) {
return defaults(stack -> {
ItemMeta meta = stack.getItemMeta();
consumer.accept(meta);
stack.setItemMeta(meta);
});
}
public Builder defaultType(@NotNull Material type) {
return defaults(new ItemStack(type));
}
public Builder defaultName(@Nullable String name) {
return defaultMeta(meta -> meta.setDisplayName(name));
}
@SuppressWarnings("deprecation")
public Builder defaultDataID(short dataID) {
return defaults(stack -> stack.setDurability(dataID));
}
public Builder defaultLore(@NotNull String... lore) {
return defaultLore(Arrays.asList(lore));
}
public Builder defaultLore(@NotNull List<String> lore) {
return defaultMeta(meta -> meta.setLore(lore));
}
public Builder defaultEnchants(@NotNull Map<Enchantment, Integer> enchants) {
return defaultMeta(meta -> enchants.forEach((enchant, level) -> meta.addEnchant(enchant, level, true)));
}
public Builder defaultEnchant(@NotNull Enchantment enchant, int level) {
return defaultEnchants(Collections.singletonMap(enchant, level));
}
public Builder defaultFlags(@NotNull Set<ItemFlag> flags) {
return defaultMeta(meta -> flags.forEach(meta::addItemFlags));
}
public Builder defaultFlags(@NotNull ItemFlag... flags) {
return defaultFlags(new LinkedHashSet<>(Arrays.asList(flags)));
}
public Builder formatParam(@NotNull UnaryOperator<String> paramFormatter) {
this.paramFormatter = paramFormatter;
return self();
}
public Builder params(@NotNull String... params) {
this.params = params;
return self();
}
public Builder params(@NotNull List<String> params) {
this.params = params.toArray(new String[0]);
return self();
}
@Override
protected @NotNull Builder self() {
return this;
}
@Override
public @NotNull ConfiguredItem build() {
return new ConfiguredItem(buildManifest(), ITEM_ADAPTER, paramFormatter, params);
}
}
} }
@@ -1,13 +1,10 @@
package cc.carm.lib.mineconfiguration.bukkit.value.item; package cc.carm.lib.mineconfiguration.bukkit.value.item;
import cc.carm.lib.configuration.core.value.type.ConfiguredList; import cc.carm.lib.configuration.value.text.data.TextContents;
import cc.carm.lib.mineconfiguration.bukkit.utils.TextParser; import cc.carm.lib.configuration.value.text.function.ContentHandler;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredMessage; import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredMessage;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredMessageList;
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemFlag;
@@ -17,40 +14,34 @@ import org.bukkit.inventory.meta.SkullMeta;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.*; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public abstract class ItemModifier<S extends ItemModifier<S, R>, R> { public abstract class ItemModifier<S extends ItemModifier<S, R>, R>
extends ContentHandler<Player, S> {
public static final @NotNull Pattern LORE_INSERT_PATTERN = Pattern.compile("^(?:\\{(.*)})?#(.*)#(?:\\{(-?\\d+)(?:,(-?\\d+))?})?$"); public static final @NotNull Pattern LORE_INSERT_PATTERN = Pattern.compile("^(?:\\{(.*)})?#(.*)#(?:\\{(-?\\d+)(?:,(-?\\d+))?})?$");
protected final @NotNull Function<@NotNull Player, @Nullable ItemStack> itemProvider; protected final @NotNull Function<@NotNull Player, @Nullable ItemStack> itemProvider;
protected @NotNull Map<String, Object> placeholders = new HashMap<>();
protected @NotNull String[] params;
protected @NotNull Object[] values;
protected final @NotNull Map<String, LoreContent<?>> insertLore = new HashMap<>();
protected @NotNull BiConsumer<ItemStack, Player> itemConsumer; protected @NotNull BiConsumer<ItemStack, Player> itemConsumer;
protected @NotNull BiConsumer<ItemMeta, Player> metaConsumer; protected @NotNull BiConsumer<ItemMeta, Player> metaConsumer;
protected ItemModifier(@NotNull Function<@NotNull Player, @Nullable ItemStack> itemProvider) { protected ItemModifier(@NotNull Function<@NotNull Player, @Nullable ItemStack> itemProvider) {
super();
this.itemProvider = itemProvider; this.itemProvider = itemProvider;
this.params = new String[0]; this.itemConsumer = (item, player) -> {
this.values = new Object[0];
itemConsumer = (item, player) -> {
}; };
metaConsumer = (meta, player) -> { this.metaConsumer = (meta, player) -> {
}; };
this.lineSeparator = " ";
} }
protected abstract @NotNull S getThis();
public abstract @Nullable R get(Player player); public abstract @Nullable R get(Player player);
public void applyTo(@Nullable ItemStack item, @Nullable Player player) { public void applyTo(@Nullable ItemStack item, @Nullable Player player) {
@@ -59,14 +50,12 @@ public abstract class ItemModifier<S extends ItemModifier<S, R>, R> {
ItemMeta meta = item.getItemMeta(); ItemMeta meta = item.getItemMeta();
if (meta == null) return; if (meta == null) return;
Map<String, Object> finalPlaceholders = buildPlaceholders();
String name = meta.getDisplayName(); String name = meta.getDisplayName();
if (!name.isEmpty()) { if (!name.isEmpty()) {
meta.setDisplayName(TextParser.parseText(player, name, finalPlaceholders)); meta.setDisplayName(parse(player, name));
} }
List<String> parsedLore = parseLore(player, meta.getLore(), insertLore, finalPlaceholders); List<String> parsedLore = parseLore(player, meta.getLore());
if (!parsedLore.isEmpty()) { if (!parsedLore.isEmpty()) {
meta.setLore(parsedLore); meta.setLore(parsedLore);
} }
@@ -78,78 +67,17 @@ public abstract class ItemModifier<S extends ItemModifier<S, R>, R> {
public S handleMeta(@NotNull BiConsumer<ItemMeta, Player> modifier) { public S handleMeta(@NotNull BiConsumer<ItemMeta, Player> modifier) {
this.metaConsumer = this.metaConsumer.andThen(modifier); this.metaConsumer = this.metaConsumer.andThen(modifier);
return getThis(); return self();
} }
public S handleItem(@NotNull BiConsumer<ItemStack, Player> modifier) { public S handleItem(@NotNull BiConsumer<ItemStack, Player> modifier) {
this.itemConsumer = this.itemConsumer.andThen(modifier); this.itemConsumer = this.itemConsumer.andThen(modifier);
return getThis(); return self();
} }
public S params(String[] params) { public S insert(@NotNull String key, @NotNull ConfiguredMessage<?> message,
this.params = params; @NotNull Object... values) {
return getThis(); return insert(key, receiver -> message.parse(receiver, values));
}
public S values(Object... values) {
this.values = values;
return getThis();
}
public S placeholders(@NotNull Map<String, Object> placeholders) {
this.placeholders = placeholders;
return getThis();
}
public S placeholders(@NotNull Consumer<Map<String, Object>> consumer) {
Map<String, Object> placeholders = new HashMap<>();
consumer.accept(placeholders);
return placeholders(placeholders);
}
public S insertLore(@NotNull String path, @NotNull LoreContent<?> content) {
insertLore.put(path, content);
return getThis();
}
public S insertLore(@NotNull String path, @NotNull List<String> content) {
return insertLore(path, content, false);
}
public S insertLore(@NotNull String path, @NotNull List<String> content, boolean original) {
return insertLore(path, LoreContent.of(content, original));
}
public S insertLore(@NotNull String path, @NotNull String... content) {
return insertLore(path, Arrays.asList(content));
}
public S insertLore(@NotNull String path, @NotNull ConfiguredList<String> content) {
return insertLore(path, content.copy());
}
public S insertLore(@NotNull String path,
@NotNull ConfiguredMessage<String> content, @NotNull Object... params) {
return insertLore(path, new LoreContent<ConfiguredMessage<String>>(content, false) {
@Override
public List<String> parse(CommandSender receiver) {
String str = getSource().parse(receiver, params);
if (str == null || str.isEmpty()) return Collections.emptyList();
return Collections.singletonList(str);
}
});
}
public S insertLore(@NotNull String path,
@NotNull ConfiguredMessageList<String> content, @NotNull Object... params) {
return insertLore(path, new LoreContent<ConfiguredMessageList<String>>(content, false) {
@Override
public List<String> parse(CommandSender receiver) {
List<String> list = getSource().parse(receiver, params);
if (list == null || list.isEmpty()) return Collections.emptyList();
return list;
}
});
} }
public S amount(int amount) { public S amount(int amount) {
@@ -202,72 +130,12 @@ public abstract class ItemModifier<S extends ItemModifier<S, R>, R> {
}); });
} }
public List<String> parseLore(@Nullable Player player, @Nullable List<String> current) {
protected Map<String, Object> buildPlaceholders() { if (current == null || current.isEmpty()) return new ArrayList<>();
Map<String, Object> finalPlaceholders = new HashMap<>(); List<String> parsed = new ArrayList<>();
finalPlaceholders.putAll(ParamsUtils.buildParams(params, values)); handle(TextContents.of(current, new HashMap<>()), player, parsed::add);
finalPlaceholders.putAll(this.placeholders); return parsed;
return finalPlaceholders;
} }
public static List<String> parseLore(@Nullable Player player, @Nullable List<String> lore,
@NotNull Map<String, LoreContent<?>> insertedLore,
@NotNull Map<String, Object> placeholders) {
List<String> parsedLore = new ArrayList<>();
if (lore == null || lore.isEmpty()) return parsedLore;
for (String line : lore) {
Matcher matcher = LORE_INSERT_PATTERN.matcher(line);
if (!matcher.matches()) {
parsedLore.add(TextParser.parseText(player, line, placeholders));
continue;
}
String path = matcher.group(2);
LoreContent<?> content = insertedLore.get(path);
if (content == null) continue;
String prefix = Optional.ofNullable(matcher.group(1))
.map(s -> TextParser.parseText(player, s, placeholders))
.orElse("");
int offset1 = Optional.ofNullable(matcher.group(3))
.map(Integer::parseInt).orElse(0);
Integer offset2 = Optional.ofNullable(matcher.group(4))
.map(Integer::parseInt).orElse(null);
List<String> inserted = parseLoreLine(
player, content, placeholders, prefix,
offset2 == null ? 0 : offset1, offset2 == null ? offset1 : offset2
);
if (content.isOriginal()) {
parsedLore.addAll(inserted);
} else {
parsedLore.addAll(TextParser.parseList(player, inserted, placeholders));
}
}
return parsedLore;
}
public static List<String> parseLoreLine(@Nullable Player player, @NotNull LoreContent<?> content,
@NotNull Map<String, Object> placeholders,
@NotNull String parsedPrefix, int upOffset, int downOffset) {
List<String> lore = content.parse(player);
if (lore.isEmpty()) return Collections.emptyList();
upOffset = Math.max(0, upOffset);
downOffset = Math.max(0, downOffset);
List<String> finalLore = new ArrayList<>();
for (int i = 0; i < upOffset; i++) finalLore.add(" ");
if (content.isOriginal()) {
lore.stream().map(s -> parsedPrefix + s).forEach(finalLore::add);
} else {
lore.stream().map(s -> parsedPrefix + TextParser.parseText(player, s, placeholders)).forEach(finalLore::add);
}
for (int i = 0; i < downOffset; i++) finalLore.add(" ");
return finalLore;
}
} }
@@ -1,42 +0,0 @@
package cc.carm.lib.mineconfiguration.bukkit.value.item;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public abstract class LoreContent<S> {
public static LoreContent<List<String>> of(@NotNull List<String> content) {
return of(content, false);
}
public static LoreContent<List<String>> of(@NotNull List<String> content, boolean original) {
return new LoreContent<List<String>>(content, original) {
@Override
public List<String> parse(CommandSender receiver) {
return getSource();
}
};
}
protected final @NotNull S source;
protected final boolean original;
public LoreContent(@NotNull S source, boolean original) {
this.source = source;
this.original = original;
}
public @NotNull S getSource() {
return source;
}
public abstract List<String> parse(CommandSender receiver);
public boolean isOriginal() {
return original;
}
}
@@ -5,6 +5,7 @@ import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
public class PreparedItem extends ItemModifier<PreparedItem, ItemStack> { public class PreparedItem extends ItemModifier<PreparedItem, ItemStack> {
@@ -29,9 +30,21 @@ public class PreparedItem extends ItemModifier<PreparedItem, ItemStack> {
return item; return item;
} }
@Override public @Nullable Map<Integer, ItemStack> give(Player player) {
public @NotNull PreparedItem getThis() { @Nullable ItemStack item = get(player);
return this; if (item == null) return null;
return player.getInventory().addItem(item);
} }
public boolean giveOrDrop(Player player) {
@Nullable Map<Integer, ItemStack> left = give(player);
if (left == null) return false;
left.values().forEach(item -> player.getWorld().dropItem(player.getLocation(), item));
return true;
}
@Override
public PreparedItem self() {
return this;
}
} }
@@ -1,40 +1,55 @@
package cc.carm.lib.mineconfiguration.bukkit.value.notify; package cc.carm.lib.mineconfiguration.bukkit.value.notify;
import cc.carm.lib.configuration.core.value.ValueManifest; import cc.carm.lib.configuration.adapter.ValueAdapter;
import cc.carm.lib.configuration.core.value.type.ConfiguredList; import cc.carm.lib.configuration.adapter.ValueType;
import cc.carm.lib.mineconfiguration.bukkit.builder.notify.NotifyConfigBuilder; import cc.carm.lib.configuration.builder.AbstractConfigBuilder;
import cc.carm.lib.configuration.source.ConfigurationHolder;
import cc.carm.lib.configuration.value.ValueManifest;
import cc.carm.lib.configuration.value.standard.ConfiguredList;
import cc.carm.lib.configuration.value.text.function.TextParser;
import cc.carm.lib.mineconfiguration.bukkit.data.NotifyConfig; import cc.carm.lib.mineconfiguration.bukkit.data.NotifyConfig;
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils; import cc.carm.lib.mineconfiguration.bukkit.data.SoundConfig;
import cc.carm.lib.mineconfiguration.bukkit.data.TitleConfig;
import cc.carm.lib.mineconfiguration.bukkit.value.notify.type.NotifyType;
import org.bukkit.Sound;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Range;
import java.util.*; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.UnaryOperator;
public class ConfiguredNotify extends ConfiguredList<NotifyConfig> { public class ConfiguredNotify extends ConfiguredList<NotifyConfig> {
public static NotifyConfigBuilder create() { public static NotifyBuilder create() {
return new NotifyConfigBuilder(); return new NotifyBuilder();
} }
public static final Set<NotifyType<?>> TYPES = new HashSet<>(Arrays.asList(DefaultNotifyTypes.values())); public static final ValueType<NotifyConfig> NOTIFY_TYPE = ValueType.of(NotifyConfig.class);
public static final ValueAdapter<NotifyConfig> NOTIFY_ADAPTER = new ValueAdapter<>(NOTIFY_TYPE,
public static NotifyType<?> getType(@NotNull String key) { (holder, type, value) -> value.serialize(),
return TYPES.stream().filter(type -> type.key.equalsIgnoreCase(key)).findFirst().orElse(null); (holder, type, value) -> {
String conf = holder.deserialize(String.class, value);
return NotifyConfig.deserialize(conf);
} }
);
protected final @NotNull UnaryOperator<String> paramBuilder;
protected final @NotNull String[] params; protected final @NotNull String[] params;
public ConfiguredNotify(@NotNull ValueManifest<List<NotifyConfig>> manifest, @NotNull String[] params) { public ConfiguredNotify(@NotNull ValueManifest<List<NotifyConfig>> manifest,
super(manifest, NotifyConfig.class, obj -> Objects.requireNonNull(NotifyConfig.deserialize(String.valueOf(obj))), NotifyConfig::serialize); @NotNull ValueAdapter<NotifyConfig> adapter,
@NotNull UnaryOperator<String> paramBuilder, @NotNull String[] params) {
super(manifest, ArrayList::new, adapter);
this.paramBuilder = paramBuilder;
this.params = params; this.params = params;
} }
public String[] getParams() {
return params;
}
public @NotNull PreparedNotify prepare(@NotNull Object... values) { public @NotNull PreparedNotify prepare(@NotNull Object... values) {
return new PreparedNotify(getNotNull(), ParamsUtils.buildParams(getParams(), values)); return new PreparedNotify(getNotNull(), TextParser.buildParams(paramBuilder, this.params, values));
} }
public void send(@NotNull Player player, @NotNull Object... values) { public void send(@NotNull Player player, @NotNull Object... values) {
@@ -45,8 +60,105 @@ public class ConfiguredNotify extends ConfiguredList<NotifyConfig> {
prepare(values).to(players); prepare(values).to(players);
} }
public void sendAll(@NotNull String... values) { public void sendAll(@NotNull Object... values) {
prepare(values).toAll(); prepare(values).toAll();
} }
public static class NotifyBuilder extends AbstractConfigBuilder<List<NotifyConfig>, ConfiguredNotify, ConfigurationHolder<?>, NotifyBuilder> {
protected final @NotNull List<NotifyConfig> notifications = new ArrayList<>();
protected @NotNull String[] params = new String[0];
protected @NotNull ValueAdapter<NotifyConfig> adapter = NOTIFY_ADAPTER;
protected @NotNull UnaryOperator<String> paramFormatter = s -> "%(" + s + ")";
protected NotifyBuilder() {
super(ConfigurationHolder.class, new ValueType<List<NotifyConfig>>() {
});
}
@Override
protected @NotNull ConfiguredNotify.NotifyBuilder self() {
return this;
}
@Override
public @NotNull ConfiguredNotify build() {
return new ConfiguredNotify(buildManifest(), this.adapter, this.paramFormatter, this.params);
}
public NotifyBuilder adapter(@NotNull ValueAdapter<NotifyConfig> adapter) {
this.adapter = adapter;
return this;
}
public NotifyBuilder defaultMessages(@NotNull String... messages) {
return defaultMessages(Arrays.asList(messages));
}
public NotifyBuilder defaultMessages(@NotNull List<String> messages) {
for (String message : messages) {
notifications.add(NotifyConfig.of(NotifyType.Standard.MESSAGE, message));
}
return defaults(this.notifications);
}
public NotifyBuilder defaultActionBar(@NotNull String message) {
notifications.add(NotifyConfig.of(NotifyType.Standard.ACTIONBAR, message));
return defaults(this.notifications);
}
public NotifyBuilder defaultSound(@NotNull Sound sound, float volume, float pitch) {
return defaultSound(sound.name(), volume, pitch);
}
public NotifyBuilder defaultSound(@NotNull Sound sound, float volume) {
return defaultSound(sound, volume, 1.0f);
}
public NotifyBuilder defaultSound(@NotNull Sound sound) {
return defaultSound(sound, 1.0f);
}
public NotifyBuilder defaultSound(@NotNull String soundName, float volume, float pitch) {
notifications.add(NotifyConfig.of(NotifyType.Standard.SOUND, new SoundConfig(soundName, volume, pitch)));
return defaults(this.notifications);
}
public NotifyBuilder defaultSound(@NotNull String soundName, float volume) {
return defaultSound(soundName, volume, 1.0f);
}
public NotifyBuilder defaultSound(@NotNull String soundName) {
return defaultSound(soundName, 1.0f);
}
public NotifyBuilder defaultTitle(@Nullable String line1, @Nullable String line2,
@Range(from = 0L, to = Integer.MAX_VALUE) int fadeIn,
@Range(from = 0L, to = Integer.MAX_VALUE) int stay,
@Range(from = 0L, to = Integer.MAX_VALUE) int fadeOut) {
notifications.add(NotifyConfig.of(NotifyType.Standard.TITLE, TitleConfig.of(line1, line2, fadeIn, stay, fadeOut)));
return defaults(this.notifications);
}
public NotifyBuilder defaultTitle(@Nullable String line1, @Nullable String line2) {
return defaultTitle(line1, line2, 10, 60, 10);
}
public NotifyBuilder params(String... params) {
this.params = params;
return this;
}
public NotifyBuilder params(@NotNull List<String> params) {
return params(params.toArray(new String[0]));
}
public NotifyBuilder formatParam(UnaryOperator<String> paramFormatter) {
this.paramFormatter = paramFormatter;
return this;
}
}
} }
@@ -1,30 +0,0 @@
package cc.carm.lib.mineconfiguration.bukkit.value.notify;
import cc.carm.lib.mineconfiguration.bukkit.value.notify.type.SoundNotify;
import cc.carm.lib.mineconfiguration.bukkit.value.notify.type.StringNotify;
import cc.carm.lib.mineconfiguration.bukkit.value.notify.type.TitleNotify;
import com.cryptomorin.xseries.messages.ActionBar;
import org.bukkit.entity.Player;
import java.util.Arrays;
import java.util.Optional;
public interface DefaultNotifyTypes {
StringNotify MESSAGE = StringNotify.of("MESSAGE", Player::sendMessage, content -> Optional.ofNullable(content).orElse(" "));
StringNotify MSG = StringNotify.of("MSG", Player::sendMessage);
StringNotify ACTIONBAR = StringNotify.of("ACTIONBAR", ActionBar::sendActionBar);
TitleNotify TITLE = new TitleNotify("TITLE");
SoundNotify SOUND = new SoundNotify("SOUND");
static NotifyType<?>[] values() {
return new NotifyType<?>[]{MESSAGE, MSG, ACTIONBAR, TITLE, SOUND};
}
static NotifyType<?> valueOf(String name) {
return Arrays.stream(values()).filter(type -> type.key.equalsIgnoreCase(name)).findFirst().orElse(null);
}
}
@@ -1,61 +0,0 @@
package cc.carm.lib.mineconfiguration.bukkit.value.notify;
import cc.carm.lib.mineconfiguration.bukkit.utils.TextParser;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
public abstract class NotifyType<M> {
protected final @NotNull String key;
protected final @NotNull Class<M> metaClass;
protected NotifyType(@NotNull String key, @NotNull Class<M> metaClass) {
this.key = key;
this.metaClass = metaClass;
}
public @NotNull String getKey() {
return key;
}
public @NotNull Class<M> getMetaClass() {
return metaClass;
}
/**
* Parse metadata from string.
* <br> e.g. "[TYPE-KEY@PARAM] CONTENTS..."
*
* @param param The param of the notify config.
* @param content The content of the notify config.
* @return The parsed metadata.
*/
public abstract @Nullable M parseMeta(@Nullable String param, @Nullable String content);
/**
* Serialize the metadata to singleton string to storage on configs.
*
* @param meta The parsed metadata.
* @return The serialized string.
*/
public abstract @NotNull String serializeConfig(@Nullable M meta);
/**
* Execute the notify content to specific player.
*
* @param player The player who receive the notification.
* @param meta The parsed metadata.
*/
public abstract void execute(@NotNull Player player, @Nullable M meta, @NotNull Map<String, Object> placeholders);
@Contract("_, _, null -> null; _, _, !null -> !null")
protected String setPlaceholders(@NotNull Player player, @Nullable String content,
@NotNull Map<String, Object> placeholders) {
return TextParser.parseText(player, content, placeholders);
}
}
@@ -1,4 +1,4 @@
package cc.carm.lib.mineconfiguration.bukkit.value.notify; package cc.carm.lib.mineconfiguration.bukkit.value.notify.type;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -21,7 +21,7 @@ public class NotifyCache<T extends NotifyType<M>, M> {
} }
public static @Nullable NotifyCache<?, ?> of(@NotNull String key, @Nullable String param, @Nullable String content) { public static @Nullable NotifyCache<?, ?> of(@NotNull String key, @Nullable String param, @Nullable String content) {
NotifyType<?> type = ConfiguredNotify.getType(key); NotifyType<?> type = NotifyType.get(key);
if (type == null) return null; if (type == null) return null;
return NotifyCache.of(type, param, content); return NotifyCache.of(type, param, content);
} }
@@ -0,0 +1,91 @@
package cc.carm.lib.mineconfiguration.bukkit.value.notify.type;
import cc.carm.lib.configuration.value.text.function.TextParser;
import cc.carm.lib.mineconfiguration.bukkit.utils.MessageUtils;
import cc.carm.lib.mineconfiguration.bukkit.value.notify.type.standard.SoundNotify;
import cc.carm.lib.mineconfiguration.bukkit.value.notify.type.standard.StringNotify;
import cc.carm.lib.mineconfiguration.bukkit.value.notify.type.standard.TitleNotify;
import com.cryptomorin.xseries.messages.ActionBar;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
public abstract class NotifyType<M> {
public interface Standard {
StringNotify MESSAGE = StringNotify.of("MESSAGE", Player::sendMessage, content -> Optional.ofNullable(content).orElse(" "));
StringNotify MSG = StringNotify.of("MSG", Player::sendMessage);
StringNotify ACTIONBAR = StringNotify.of("ACTIONBAR", ActionBar::sendActionBar);
TitleNotify TITLE = new TitleNotify("TITLE");
SoundNotify SOUND = new SoundNotify("SOUND");
static NotifyType<?>[] values() {
return new NotifyType<?>[]{MESSAGE, MSG, ACTIONBAR, TITLE, SOUND};
}
static NotifyType<?> valueOf(String name) {
return Arrays.stream(values()).filter(type -> type.key.equalsIgnoreCase(name)).findFirst().orElse(null);
}
}
public static final Set<NotifyType<?>> TYPES = new HashSet<>(Arrays.asList(Standard.values()));
public static NotifyType<?> get(@NotNull String key) {
return TYPES.stream().filter(type -> type.key.equalsIgnoreCase(key)).findFirst().orElse(null);
}
protected final @NotNull String key;
protected final @NotNull Class<M> metaClass;
protected NotifyType(@NotNull String key, @NotNull Class<M> metaClass) {
this.key = key;
this.metaClass = metaClass;
}
public @NotNull String getKey() {
return key;
}
public @NotNull Class<M> getMetaClass() {
return metaClass;
}
/**
* Parse metadata from string.
* <br> e.g. "[TYPE-KEY@PARAM] CONTENTS..."
*
* @param param The param of the notify config.
* @param content The content of the notify config.
* @return The parsed metadata.
*/
public abstract @Nullable M parseMeta(@Nullable String param, @Nullable String content);
/**
* Serialize the metadata to singleton string to storage on configs.
*
* @param meta The parsed metadata.
* @return The serialized string.
*/
public abstract @NotNull String serializeConfig(@Nullable M meta);
/**
* Execute the notify content to specific player.
*
* @param player The player who receive the notification.
* @param meta The parsed metadata.
*/
public abstract void execute(@NotNull Player player, @Nullable M meta, @NotNull Map<String, Object> placeholders);
@Contract("_, _, null -> null; _, _, !null -> !null")
protected String setPlaceholders(@NotNull Player player, @NotNull Map<String, Object> placeholders,
@Nullable String content) {
if (content == null) return null;
return MessageUtils.parseMessage(player, TextParser.setPlaceholders(content, placeholders));
}
}
@@ -1,7 +1,7 @@
package cc.carm.lib.mineconfiguration.bukkit.value.notify.type; package cc.carm.lib.mineconfiguration.bukkit.value.notify.type.standard;
import cc.carm.lib.mineconfiguration.bukkit.data.SoundConfig; import cc.carm.lib.mineconfiguration.bukkit.data.SoundConfig;
import cc.carm.lib.mineconfiguration.bukkit.value.notify.NotifyType; import cc.carm.lib.mineconfiguration.bukkit.value.notify.type.NotifyType;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -1,6 +1,6 @@
package cc.carm.lib.mineconfiguration.bukkit.value.notify.type; package cc.carm.lib.mineconfiguration.bukkit.value.notify.type.standard;
import cc.carm.lib.mineconfiguration.bukkit.value.notify.NotifyType; import cc.carm.lib.mineconfiguration.bukkit.value.notify.type.NotifyType;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -46,7 +46,7 @@ public abstract class StringNotify extends NotifyType<String> {
@Override @Override
public void execute(@NotNull Player player, @Nullable String content, @NotNull Map<String, Object> placeholders) { public void execute(@NotNull Player player, @Nullable String content, @NotNull Map<String, Object> placeholders) {
if (content == null) return; if (content == null) return;
send(player, setPlaceholders(player, content, placeholders)); send(player, setPlaceholders(player, placeholders, content));
} }
} }
@@ -1,7 +1,7 @@
package cc.carm.lib.mineconfiguration.bukkit.value.notify.type; package cc.carm.lib.mineconfiguration.bukkit.value.notify.type.standard;
import cc.carm.lib.mineconfiguration.bukkit.data.TitleConfig; import cc.carm.lib.mineconfiguration.bukkit.data.TitleConfig;
import cc.carm.lib.mineconfiguration.bukkit.value.notify.NotifyType; import cc.carm.lib.mineconfiguration.bukkit.value.notify.type.NotifyType;
import com.cryptomorin.xseries.messages.Titles; import com.cryptomorin.xseries.messages.Titles;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -13,8 +13,6 @@ import java.util.regex.Pattern;
public class TitleNotify extends NotifyType<TitleConfig> { public class TitleNotify extends NotifyType<TitleConfig> {
//Content format line1{N}line2
public static final Pattern CONTENT_FORMAT = Pattern.compile("(?<line1>.+?)(?:\\{n}(?<line2>.+))?");
//Param format fadeIn,stay,fadeOut //Param format fadeIn,stay,fadeOut
public static final Pattern PARAM_FORMAT = Pattern.compile("(?<fadeIn>\\d+),(?<stay>\\d+),(?<fadeOut>\\d+)"); public static final Pattern PARAM_FORMAT = Pattern.compile("(?<fadeIn>\\d+),(?<stay>\\d+),(?<fadeOut>\\d+)");
@@ -26,20 +24,23 @@ public class TitleNotify extends NotifyType<TitleConfig> {
public @Nullable TitleConfig parseMeta(@Nullable String param, @Nullable String content) { public @Nullable TitleConfig parseMeta(@Nullable String param, @Nullable String content) {
if (content == null) return null; if (content == null) return null;
Matcher contentMatcher = CONTENT_FORMAT.matcher(content); String[] lines = content.split("\\{n}");
if (!contentMatcher.matches()) return null; if (lines.length == 0) return null;
String line1 = lines[0];
String line2 = lines.length > 1 ? lines[1] : null;
if (param == null) { if (param == null) {
return TitleConfig.of(contentMatcher.group("line1"), contentMatcher.group("line2")); return TitleConfig.of(line1, line2);
} }
Matcher paramMatcher = PARAM_FORMAT.matcher(param); Matcher paramMatcher = PARAM_FORMAT.matcher(param);
if (!paramMatcher.matches()) { if (!paramMatcher.matches()) {
return TitleConfig.of(contentMatcher.group("line1"), contentMatcher.group("line2")); return TitleConfig.of(line1, line2);
} }
return TitleConfig.of( return TitleConfig.of(
contentMatcher.group("line1"), contentMatcher.group("line2"), line1, line2,
Integer.parseInt(paramMatcher.group("fadeIn")), Integer.parseInt(paramMatcher.group("fadeIn")),
Integer.parseInt(paramMatcher.group("stay")), Integer.parseInt(paramMatcher.group("stay")),
Integer.parseInt(paramMatcher.group("fadeOut")) Integer.parseInt(paramMatcher.group("fadeOut"))
@@ -48,15 +49,15 @@ public class TitleNotify extends NotifyType<TitleConfig> {
@Override @Override
public @NotNull String serializeConfig(@Nullable TitleConfig meta) { public @NotNull String serializeConfig(@Nullable TitleConfig meta) {
if (meta == null || (meta.getLine1() == null && meta.getLine2() == null)) return "[" + key + "] "; if (meta == null || (meta.line1() == null && meta.line2() == null)) return "[" + key + "] ";
String line1 = meta.getLine1() == null ? "" : meta.getLine1().getMessage(); String line1 = meta.line1() == null ? "" : meta.line1();
String line2 = meta.getLine2() == null ? "" : meta.getLine2().getMessage(); String line2 = meta.line2() == null ? "" : meta.line2();
if (meta.isStandardTime()) { if (meta.isStandardTime()) {
return "[" + key + "] " + line1 + "{n}" + line1; return "[" + key + "] " + line1 + "{n}" + line2;
} else } else
return "[" + key + "@" + meta.getFadeIn() + "," + meta.getStay() + "," + meta.getFadeOut() + "] " + line1 + "{n}" + line1; return "[" + key + "@" + meta.fadeIn() + "," + meta.stay() + "," + meta.fadeOut() + "] " + line1 + "{n}" + line2;
} }
@Override @Override
@@ -1,55 +0,0 @@
import cc.carm.lib.mineconfiguration.bukkit.value.item.LoreContent;
import cc.carm.lib.mineconfiguration.bukkit.value.item.PreparedItem;
import org.junit.Test;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
public class LoreInsertTest {
@Test
public void insert() {
List<String> original = Arrays.asList(
"测试lore的第一行",
"测试lore的第二行",
"#click-lore#{1,2}",
"测试lore的倒数第二行",
"{--> }#click-lore#{2}",
"测试lore的倒数第一行"
);
List<String> replace = Arrays.asList("> 插入的点击行1", "> 插入的点击行2");
Map<String, LoreContent<?>> inserted = new HashMap<>();
inserted.put("click-lore", LoreContent.of(replace));
PreparedItem.parseLore(null, original, inserted, new HashMap<>()).forEach(System.out::println);
}
@Test
public void parse() {
System.out.println(parse("{LOVE}#click-lore#{1,0}"));
System.out.println(parse("#click-lore#{1,2}"));
System.out.println(parse("#click-lore#{1}"));
System.out.println(parse("#click-lore#{我}"));
}
public static String parse(String line) {
Matcher matcher = PreparedItem.LORE_INSERT_PATTERN.matcher(line);
if (!matcher.matches()) {
return "Failed -> [" + line + "]";
} else {
String prefix = matcher.group(1);
String path = matcher.group(2);
String offset1 = matcher.group(3);
String offset2 = matcher.group(4);
return "Prefix -> [" + prefix + "] Path -> [" + path + "] Offset-> [" + offset1 + "/" + offset2 + "]";
}
}
}
+5 -12
View File
@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>mineconfiguration-parent</artifactId> <artifactId>mineconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>2.9.3</version> <version>3.0.0</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@@ -19,7 +19,7 @@
<packaging>jar</packaging> <packaging>jar</packaging>
<name>MineConfiguration-Bungee</name> <name>MineConfiguration-Bungee</name>
<description>轻松(做)配置,适用于BungeeCord的版本,可用JSON与YAML格式。</description> <description>EasyConfiguration for BungeeCord.</description>
<dependencies> <dependencies>
@@ -32,11 +32,12 @@
<dependency> <dependency>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>yamlcommentwriter</artifactId> <artifactId>easyconfiguration-yaml</artifactId>
<version>${deps.yamlcommentwriter.version}</version> <version>${deps.easyconfifuration.version}</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<!--suppress VulnerableLibrariesLocal -->
<dependency> <dependency>
<groupId>net.md-5</groupId> <groupId>net.md-5</groupId>
<artifactId>bungeecord-api</artifactId> <artifactId>bungeecord-api</artifactId>
@@ -45,14 +46,6 @@
<scope>provided</scope> <scope>provided</scope>
</dependency> </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> </dependencies>
<build> <build>
@@ -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();
}
}
@@ -1,120 +1,52 @@
package cc.carm.lib.mineconfiguration.bungee; package cc.carm.lib.mineconfiguration.bungee;
import cc.carm.lib.configuration.core.Configuration; import cc.carm.lib.configuration.Configuration;
import cc.carm.lib.configuration.core.ConfigurationRoot; import cc.carm.lib.configuration.source.ConfigurationHolder;
import cc.carm.lib.mineconfiguration.bungee.source.BungeeConfigProvider; import cc.carm.lib.configuration.source.yaml.YAMLConfigFactory;
import cc.carm.lib.configuration.source.yaml.YAMLSource;
import cc.carm.lib.mineconfiguration.common.AbstractConfiguration; import cc.carm.lib.mineconfiguration.common.AbstractConfiguration;
import net.md_5.bungee.api.plugin.Plugin; 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 org.jetbrains.annotations.NotNull;
import java.io.File; 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) { public MineConfiguration(@NotNull Plugin plugin) {
super(from(plugin, "config.yml"), from(plugin, "messages.yml")); this(plugin.getDataFolder());
} }
public MineConfiguration(@NotNull Plugin plugin, public MineConfiguration(@NotNull Plugin plugin,
@NotNull Configuration configRoot, @NotNull Configuration configRoot,
@NotNull Configuration messageRoot) { @NotNull Configuration messageRoot) {
this(plugin); this(plugin.getDataFolder(), configRoot, messageRoot);
initializeConfig(configRoot);
initializeMessage(messageRoot);
} }
public MineConfiguration(@NotNull Plugin plugin, public MineConfiguration(@NotNull Plugin plugin,
@NotNull Class<? extends Configuration> configRoot, @NotNull Class<? extends Configuration> configRoot,
@NotNull Class<? extends Configuration> messageRoot) { @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); initializeConfig(configRoot);
initializeMessage(messageRoot); initializeMessage(messageRoot);
} }
@@ -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);
}
}
@@ -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();
}
}
@@ -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);
}
}
@@ -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
);
}
}
@@ -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
);
}
}
@@ -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));
}
}
@@ -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);
}
}
@@ -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));
}
}
@@ -1,52 +1,64 @@
package cc.carm.lib.mineconfiguration.bungee.value; package cc.carm.lib.mineconfiguration.bungee.value;
import cc.carm.lib.configuration.core.value.ValueManifest; import cc.carm.lib.configuration.value.ValueManifest;
import cc.carm.lib.mineconfiguration.bungee.BungeeConfigValue; import cc.carm.lib.configuration.value.text.ConfiguredText;
import cc.carm.lib.mineconfiguration.bungee.builder.message.BungeeMessageValueBuilder; import cc.carm.lib.configuration.value.text.data.TextContents;
import cc.carm.lib.mineconfiguration.bungee.data.TextConfig; import cc.carm.lib.easyplugin.utils.ColorParser;
import cc.carm.lib.mineconfiguration.common.value.ConfigMessage;
import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.ProxyServer; 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.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
public class ConfiguredMessage<M> extends ConfigMessage<M, TextConfig, CommandSender> { public class ConfiguredMessage extends ConfiguredText<BaseComponent[], CommandSender> {
@NotNull @NotNull
public static <M> BungeeMessageValueBuilder<@Nullable M> create(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser) { public static ConfiguredMessage.Builder create() {
return BungeeConfigValue.builder().createMessage().asValue(messageParser); return new Builder();
} }
public static BungeeMessageValueBuilder<String> asString() { public static ConfiguredMessage ofString(@NotNull String... messages) {
return BungeeConfigValue.builder().createMessage().asStringValue(); return create().defaults(messages).build();
} }
public static ConfiguredMessage<String> ofString() { public ConfiguredMessage(@NotNull ValueManifest<TextContents> manifest,
return asString().build(); @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) { public void print(Object... values) {
return asString().defaults(defaultMessage).build(); prepare(values).to(ProxyServer.getInstance().getConsole());
} }
public ConfiguredMessage(@NotNull ValueManifest<TextConfig> manifest, @NotNull String[] params, public static class Builder extends ConfiguredText.Builder<BaseComponent[], CommandSender, Builder> {
@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser,
@NotNull BiConsumer<@NotNull CommandSender, @NotNull M> sendFunction) { public Builder() {
super(manifest, TextConfig.class, params, messageParser, sendFunction, TextConfig::of); 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 @Override
public @NotNull Collection<CommandSender> getAllReceivers() { public @NotNull ConfiguredMessage build() {
List<CommandSender> senders = new ArrayList<>(); return new ConfiguredMessage(buildManifest(), this.parser, this.compiler, this.dispatcher, this.params);
senders.add(ProxyServer.getInstance().getConsole()); }
senders.addAll(ProxyServer.getInstance().getPlayers());
return senders; @Override
public @NotNull ConfiguredMessage.Builder self() {
return this;
}
} }
@@ -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();
}
}
}
+13 -15
View File
@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>mineconfiguration-parent</artifactId> <artifactId>mineconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>2.9.3</version> <version>3.0.0</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@@ -21,6 +21,13 @@
<name>MineConfiguration-Velocity</name> <name>MineConfiguration-Velocity</name>
<description>轻松(做)配置,适用于Velocity的版本,可用JSON与YAML格式。</description> <description>轻松(做)配置,适用于Velocity的版本,可用JSON与YAML格式。</description>
<repositories>
<repository>
<id>papermc</id>
<url>https://papermc.io/repo/repository/maven-public/</url>
</repository>
</repositories>
<dependencies> <dependencies>
<dependency> <dependency>
@@ -32,24 +39,15 @@
<dependency> <dependency>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>yamlcommentwriter</artifactId> <artifactId>easyconfiguration-yaml</artifactId>
<version>${deps.yamlcommentwriter.version}</version> <version>${deps.easyconfifuration.version}</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>net.md-5</groupId> <groupId>com.velocitypowered</groupId>
<artifactId>velocitycord-api</artifactId> <artifactId>velocity-api</artifactId>
<version>1.18-R0.1-SNAPSHOT</version> <version>3.4.0-SNAPSHOT</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.md-5</groupId>
<artifactId>velocitycord-api</artifactId>
<version>1.18-R0.1-SNAPSHOT</version>
<type>javadoc</type>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
@@ -1,31 +0,0 @@
package cc.carm.lib.mineconfiguration.velocity;
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.velocity.builder.VelocityConfigBuilder;
import cc.carm.lib.mineconfiguration.velocity.source.BungeeConfigProvider;
import cc.carm.lib.mineconfiguration.velocity.source.BungeeSectionWrapper;
import org.jetbrains.annotations.NotNull;
public abstract class BungeeConfigValue<T> extends CachedConfigValue<T> {
public static @NotNull VelocityConfigBuilder builder() {
return new VelocityConfigBuilder();
}
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();
}
}
@@ -1,120 +1,35 @@
package cc.carm.lib.mineconfiguration.velocity; package cc.carm.lib.mineconfiguration.velocity;
import cc.carm.lib.configuration.core.Configuration; import cc.carm.lib.configuration.Configuration;
import cc.carm.lib.configuration.core.ConfigurationRoot; import cc.carm.lib.configuration.source.ConfigurationHolder;
import cc.carm.lib.mineconfiguration.velocity.source.BungeeConfigProvider; import cc.carm.lib.configuration.source.yaml.YAMLConfigFactory;
import cc.carm.lib.configuration.source.yaml.YAMLSource;
import cc.carm.lib.mineconfiguration.common.AbstractConfiguration; 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 org.jetbrains.annotations.NotNull;
import java.io.File; import java.io.File;
import java.io.IOException;
public class MineConfiguration extends AbstractConfiguration<BungeeConfigProvider> { public class MineConfiguration extends AbstractConfiguration<ConfigurationHolder<YAMLSource>> {
protected static BungeeConfigProvider create(File file, String source, ConfigurationProvider loader) { public MineConfiguration(@NotNull File pluginDataFolder) {
BungeeConfigProvider provider = new BungeeConfigProvider(file, loader); super(
try { YAMLConfigFactory.from(pluginDataFolder, "config.yml").resourcePath("config.yml").build(),
provider.initializeFile(source); YAMLConfigFactory.from(pluginDataFolder, "messages.yml").resourcePath("messages.yml").build()
provider.initializeConfig(); );
} catch (IOException e) {
e.printStackTrace();
}
return provider;
} }
public static BungeeConfigProvider from(File file, String source) { public MineConfiguration(@NotNull File pluginDataFolder,
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 MineConfiguration(@NotNull Plugin plugin) {
super(from(plugin, "config.yml"), from(plugin, "messages.yml"));
}
public MineConfiguration(@NotNull Plugin plugin,
@NotNull Configuration configRoot, @NotNull Configuration configRoot,
@NotNull Configuration messageRoot) { @NotNull Configuration messageRoot) {
this(plugin); this(pluginDataFolder);
initializeConfig(configRoot); initializeConfig(configRoot);
initializeMessage(messageRoot); initializeMessage(messageRoot);
} }
public MineConfiguration(@NotNull Plugin plugin, public MineConfiguration(@NotNull File pluginDataFolder,
@NotNull Class<? extends Configuration> configRoot, @NotNull Class<? extends Configuration> configRoot,
@NotNull Class<? extends Configuration> messageRoot) { @NotNull Class<? extends Configuration> messageRoot) {
this(plugin); this(pluginDataFolder);
initializeConfig(configRoot); initializeConfig(configRoot);
initializeMessage(messageRoot); initializeMessage(messageRoot);
} }
@@ -1,13 +0,0 @@
package cc.carm.lib.mineconfiguration.velocity.builder;
import cc.carm.lib.mineconfiguration.velocity.source.BungeeConfigProvider;
import cc.carm.lib.configuration.core.builder.AbstractConfigBuilder;
public abstract class AbstractVelocityBuilder<T, B extends AbstractVelocityBuilder<T, B>>
extends AbstractConfigBuilder<T, B, BungeeConfigProvider> {
public AbstractVelocityBuilder() {
super(BungeeConfigProvider.class);
}
}
@@ -1,14 +0,0 @@
package cc.carm.lib.mineconfiguration.velocity.builder;
import cc.carm.lib.mineconfiguration.velocity.builder.message.BungeeMessageBuilder;
import cc.carm.lib.configuration.core.builder.ConfigBuilder;
import org.jetbrains.annotations.NotNull;
public class VelocityConfigBuilder extends ConfigBuilder {
public @NotNull BungeeMessageBuilder createMessage() {
return new BungeeMessageBuilder();
}
}
@@ -1,42 +0,0 @@
package cc.carm.lib.mineconfiguration.velocity.builder.message;
import cc.carm.lib.easyplugin.utils.ColorParser;
import cc.carm.lib.mineconfiguration.velocity.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);
}
}
@@ -1,35 +0,0 @@
package cc.carm.lib.mineconfiguration.velocity.builder.message;
import cc.carm.lib.configuration.core.value.ValueManifest;
import cc.carm.lib.mineconfiguration.velocity.data.TextConfig;
import cc.carm.lib.mineconfiguration.velocity.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.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
);
}
}
@@ -1,35 +0,0 @@
package cc.carm.lib.mineconfiguration.velocity.builder.message;
import cc.carm.lib.mineconfiguration.velocity.data.TextConfig;
import cc.carm.lib.mineconfiguration.velocity.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
);
}
}
@@ -1,35 +0,0 @@
package cc.carm.lib.mineconfiguration.velocity.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));
}
}
@@ -1,107 +0,0 @@
package cc.carm.lib.mineconfiguration.velocity.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);
}
}
@@ -1,94 +0,0 @@
package cc.carm.lib.mineconfiguration.velocity.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.velocity.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));
}
}
@@ -1,52 +1,59 @@
package cc.carm.lib.mineconfiguration.velocity.value; package cc.carm.lib.mineconfiguration.velocity.value;
import cc.carm.lib.configuration.core.value.ValueManifest; import cc.carm.lib.configuration.value.ValueManifest;
import cc.carm.lib.mineconfiguration.velocity.BungeeConfigValue; import cc.carm.lib.configuration.value.text.ConfiguredText;
import cc.carm.lib.mineconfiguration.velocity.builder.message.BungeeMessageValueBuilder; import cc.carm.lib.configuration.value.text.data.TextContents;
import cc.carm.lib.mineconfiguration.velocity.data.TextConfig; import cc.carm.lib.easyplugin.utils.ColorParser;
import cc.carm.lib.mineconfiguration.common.value.ConfigMessage; import net.kyori.adventure.audience.Audience;
import net.md_5.bungee.api.CommandSender; import net.kyori.adventure.text.Component;
import net.md_5.bungee.api.ProxyServer;
import org.jetbrains.annotations.NotNull; 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.List;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
public class ConfiguredMessage<M> extends ConfigMessage<M, TextConfig, CommandSender> { public class ConfiguredMessage extends ConfiguredText<Component, Audience> {
@NotNull @NotNull
public static <M> BungeeMessageValueBuilder<@Nullable M> create(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser) { public static ConfiguredMessage.Builder create() {
return BungeeConfigValue.builder().createMessage().asValue(messageParser); return new Builder();
} }
public static BungeeMessageValueBuilder<String> asString() { public static ConfiguredMessage ofString(@NotNull String... messages) {
return BungeeConfigValue.builder().createMessage().asStringValue(); return create().defaults(messages).build();
} }
public static ConfiguredMessage<String> ofString() { public ConfiguredMessage(@NotNull ValueManifest<TextContents> manifest,
return asString().build(); @NotNull BiFunction<Audience, String, String> parser,
@NotNull BiFunction<Audience, String, Component> compiler,
@NotNull BiConsumer<Audience, List<Component>> dispatcher,
@NotNull String[] params) {
super(manifest, parser, compiler, dispatcher, params);
} }
public static ConfiguredMessage<String> ofString(@NotNull String defaultMessage) { public void sendActionBar(Audience audience, Object... values) {
return asString().defaults(defaultMessage).build(); Component content = prepare(values).parseLine(audience, (sender, message) -> Component.text(message));
if (content != null) audience.sendActionBar(content);
} }
public ConfiguredMessage(@NotNull ValueManifest<TextConfig> manifest, @NotNull String[] params, public static class Builder extends ConfiguredText.Builder<Component, Audience, Builder> {
@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser,
@NotNull BiConsumer<@NotNull CommandSender, @NotNull M> sendFunction) { public Builder() {
super(manifest, TextConfig.class, params, messageParser, sendFunction, TextConfig::of); super();
this.parser = (receiver, message) -> ColorParser.parse(message);
this.compiler = (receiver, message) -> Component.text(message);
this.dispatcher = (receiver, message) -> message.forEach(receiver::sendMessage);
} }
@Override @Override
public @NotNull Collection<CommandSender> getAllReceivers() { public @NotNull ConfiguredMessage build() {
List<CommandSender> senders = new ArrayList<>(); return new ConfiguredMessage(buildManifest(), this.parser, this.compiler, this.dispatcher, this.params);
senders.add(ProxyServer.getInstance().getConsole()); }
senders.addAll(ProxyServer.getInstance().getPlayers());
return senders; @Override
public @NotNull ConfiguredMessage.Builder self() {
return this;
}
} }
@@ -1,47 +0,0 @@
package cc.carm.lib.mineconfiguration.velocity.value;
import cc.carm.lib.configuration.core.value.ValueManifest;
import cc.carm.lib.mineconfiguration.velocity.BungeeConfigValue;
import cc.carm.lib.mineconfiguration.velocity.builder.message.BungeeMessageListBuilder;
import cc.carm.lib.mineconfiguration.velocity.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;
}
}
+4 -4
View File
@@ -10,20 +10,20 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding> <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<deps.easyconfifuration.version>3.9.1</deps.easyconfifuration.version> <deps.easyconfifuration.version>4.0.1</deps.easyconfifuration.version>
<deps.yamlcommentwriter.version>1.2.0</deps.yamlcommentwriter.version>
<deps.easyplugin.version>1.5.12</deps.easyplugin.version> <deps.easyplugin.version>1.5.12</deps.easyplugin.version>
<deps.yamlcommentwriter.version>1.0.1</deps.yamlcommentwriter.version>
</properties> </properties>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>mineconfiguration-parent</artifactId> <artifactId>mineconfiguration-parent</artifactId>
<version>2.9.3</version> <version>3.0.0</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<modules> <modules>
<module>common</module> <module>common</module>
<module>platform/bukkit</module>
<module>platform/bungee</module> <module>platform/bungee</module>
<module>platform/velocity</module> <module>platform/velocity</module>
<module>platform/bukkit</module>
</modules> </modules>
<name>MineConfiguration</name> <name>MineConfiguration</name>