1
mirror of https://github.com/CarmJos/MineConfiguration.git synced 2024-09-19 20:05:49 +00:00
- [A] 令 ConfiguredMessage 同时支持Bungee、Bukkit平台。
- [A] 为Bukkit相关平台原生支持PlaceholderAPI。
This commit is contained in:
Carm Jos 2022-04-29 04:17:38 +08:00
parent 4850514a8a
commit 0cd61842e5
36 changed files with 1054 additions and 380 deletions

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>mineconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.1.7</version>
<version>1.2.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>mineconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.1.7</version>
<version>1.2.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
@ -40,6 +40,13 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>mineconfiguration-common</artifactId>
<version>${project.parent.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>

View File

@ -1,6 +1,6 @@
package cc.carm.lib.configuration;
import cc.carm.lib.configuration.bungee.BungeeConfigProvider;
import cc.carm.lib.configuration.bungee.source.BungeeConfigProvider;
import net.md_5.bungee.config.ConfigurationProvider;
import net.md_5.bungee.config.JsonConfiguration;
import net.md_5.bungee.config.YamlConfiguration;

View File

@ -0,0 +1,33 @@
package cc.carm.lib.configuration.bungee;
import cc.carm.lib.configuration.bungee.builder.BungeeConfigBuilder;
import cc.carm.lib.configuration.bungee.source.BungeeConfigProvider;
import cc.carm.lib.configuration.bungee.source.BungeeSectionWrapper;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public abstract class BungeeConfigValue<T> extends CachedConfigValue<T> {
public static @NotNull BungeeConfigBuilder builder() {
return new BungeeConfigBuilder();
}
public BungeeConfigValue(@Nullable BungeeConfigProvider provider,
@Nullable String configPath, @Nullable ConfigCommentInfo comments, @Nullable T defaultValue) {
super(provider, configPath, comments, defaultValue);
}
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();
}
}

View File

@ -0,0 +1,13 @@
package cc.carm.lib.configuration.bungee.builder;
import cc.carm.lib.configuration.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);
}
}

View File

@ -0,0 +1,14 @@
package cc.carm.lib.configuration.bungee.builder;
import cc.carm.lib.configuration.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();
}
}

View File

@ -0,0 +1,43 @@
package cc.carm.lib.configuration.bungee.builder.message;
import cc.carm.lib.configuration.bungee.data.MessageText;
import cc.carm.lib.configuration.common.builder.message.MessageConfigBuilder;
import cc.carm.lib.configuration.common.utils.ColorParser;
import net.md_5.bungee.api.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.function.BiFunction;
public class BungeeMessageBuilder extends MessageConfigBuilder<CommandSender, MessageText> {
public BungeeMessageBuilder() {
super(CommandSender.class, MessageText.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);
}
}

View File

@ -0,0 +1,34 @@
package cc.carm.lib.configuration.bungee.builder.message;
import cc.carm.lib.configuration.bungee.data.MessageText;
import cc.carm.lib.configuration.bungee.value.ConfiguredMessageList;
import cc.carm.lib.configuration.common.builder.message.MessageListBuilder;
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, MessageText, BungeeMessageListBuilder<M>> {
public BungeeMessageListBuilder(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
super(CommandSender.class, MessageText::of, parser);
}
@Override
protected @NotNull BungeeMessageListBuilder<M> getThis() {
return this;
}
@Override
public @NotNull ConfiguredMessageList<M> build() {
return new ConfiguredMessageList<>(
this.provider, this.path, buildComments(),
Optional.ofNullable(this.defaultValue).orElse(MessageText.of(new ArrayList<>())),
buildParams(), this.messageParser, this.sendFunction
);
}
}

View File

@ -0,0 +1,35 @@
package cc.carm.lib.configuration.bungee.builder.message;
import cc.carm.lib.configuration.bungee.data.MessageText;
import cc.carm.lib.configuration.bungee.value.ConfiguredMessage;
import cc.carm.lib.configuration.common.builder.message.MessageValueBuilder;
import net.md_5.bungee.api.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Optional;
import java.util.function.BiFunction;
public class BungeeMessageValueBuilder<M>
extends MessageValueBuilder<M, CommandSender, MessageText, BungeeMessageValueBuilder<M>> {
public BungeeMessageValueBuilder(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
super(CommandSender.class, MessageText::new, parser);
}
@Override
protected @NotNull BungeeMessageValueBuilder<M> getThis() {
return this;
}
@Override
public @NotNull ConfiguredMessage<M> build() {
return new ConfiguredMessage<>(
this.provider, this.path, buildComments(),
Optional.ofNullable(this.defaultValue).orElse(MessageText.of("")),
buildParams(), this.messageParser, this.sendHandler
);
}
}

View File

@ -0,0 +1,35 @@
package cc.carm.lib.configuration.bungee.data;
import cc.carm.lib.configuration.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 MessageText extends AbstractText<CommandSender> {
public MessageText(@NotNull String message) {
super(CommandSender.class, message);
}
@Contract("!null,-> !null")
public static @Nullable MessageText of(@Nullable String message) {
if (message == null) return null;
else return new MessageText(message);
}
public static @NotNull List<MessageText> of(@Nullable List<String> messages) {
if (messages == null || messages.isEmpty()) return new ArrayList<>();
else return messages.stream().map(MessageText::of).collect(Collectors.toList());
}
public static @NotNull List<MessageText> of(@NotNull String... messages) {
return of(Arrays.asList(messages));
}
}

View File

@ -1,4 +1,4 @@
package cc.carm.lib.configuration.bungee;
package cc.carm.lib.configuration.bungee.source;
import cc.carm.lib.configuration.core.ConfigInitializer;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;

View File

@ -1,4 +1,4 @@
package cc.carm.lib.configuration.bungee;
package cc.carm.lib.configuration.bungee.source;
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
import net.md_5.bungee.config.Configuration;

View File

@ -0,0 +1,52 @@
package cc.carm.lib.configuration.bungee.value;
import cc.carm.lib.configuration.bungee.BungeeConfigValue;
import cc.carm.lib.configuration.bungee.builder.message.BungeeMessageValueBuilder;
import cc.carm.lib.configuration.bungee.data.MessageText;
import cc.carm.lib.configuration.common.value.ConfigMessage;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
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.Map;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
public class ConfiguredMessage<M> extends ConfigMessage<M, MessageText, CommandSender> {
@NotNull
public static <M> BungeeMessageValueBuilder<@Nullable M> create(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser) {
return BungeeConfigValue.builder().createMessage().asValue(messageParser);
}
public static BungeeMessageValueBuilder<String> asString() {
return BungeeConfigValue.builder().createMessage().asStringValue();
}
public static ConfiguredMessage<String> ofString() {
return asString().build();
}
public static ConfiguredMessage<String> ofString(@NotNull String defaultMessage) {
return asString().defaults(defaultMessage).build();
}
public ConfiguredMessage(@Nullable ConfigurationProvider<?> provider,
@Nullable String sectionPath, @Nullable ConfigCommentInfo comments,
@NotNull MessageText defaultMessage, @NotNull String[] params,
@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser,
@NotNull BiConsumer<@NotNull CommandSender, @NotNull M> sendFunction) {
super(provider, sectionPath, comments, MessageText.class, defaultMessage, params, messageParser, sendFunction, MessageText::of);
}
public void broadcast(@NotNull Map<String, Object> placeholders) {
ProxyServer.getInstance().getPlayers().forEach(pl -> send(pl, placeholders));
send(ProxyServer.getInstance().getConsole(), placeholders);
}
}

View File

@ -0,0 +1,47 @@
package cc.carm.lib.configuration.bungee.value;
import cc.carm.lib.configuration.bungee.BungeeConfigValue;
import cc.carm.lib.configuration.bungee.builder.message.BungeeMessageListBuilder;
import cc.carm.lib.configuration.bungee.data.MessageText;
import cc.carm.lib.configuration.common.value.ConfigMessageList;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
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.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
public class ConfiguredMessageList<M> extends ConfigMessageList<M, MessageText, 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(@Nullable ConfigurationProvider<?> provider,
@Nullable String sectionPath, @Nullable ConfigCommentInfo comments,
@NotNull List<MessageText> messages, @NotNull String[] params,
@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser,
@NotNull BiConsumer<@NotNull CommandSender, @NotNull List<M>> sendFunction) {
super(provider, sectionPath, comments, MessageText.class, messages, params, messageParser, sendFunction, MessageText::of);
}
public void broadcast(@NotNull Map<String, Object> placeholders) {
ProxyServer.getInstance().getPlayers().forEach(pl -> send(pl, placeholders));
send(ProxyServer.getInstance().getConsole(), placeholders);
}
}

65
common/pom.xml Normal file
View File

@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mineconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.2.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
<artifactId>mineconfiguration-common</artifactId>
<dependencies>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>easyconfiguration-core</artifactId>
<version>${easyconfiguration.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>de.themoep</groupId>
<artifactId>minedown</artifactId>
<version>1.7.1-SNAPSHOT</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>net.md-5</groupId>
<artifactId>bungeecord-chat</artifactId>
<version>1.16-R0.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,45 @@
package cc.carm.lib.configuration.common.builder.message;
import cc.carm.lib.configuration.common.data.AbstractText;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.function.BiFunction;
import java.util.function.Function;
public abstract class MessageConfigBuilder<R, T extends AbstractText<R>> {
/**
* 默认的变量格式为 {@code %(变量名)}
*/
public static Function<@NotNull String, @NotNull String> DEFAULT_PARAM_FORMATTER = (s) -> "%(" + s + ")";
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);
}

View File

@ -0,0 +1,75 @@
package cc.carm.lib.configuration.common.builder.message;
import cc.carm.lib.configuration.common.data.AbstractText;
import cc.carm.lib.configuration.common.value.ConfigMessageList;
import cc.carm.lib.configuration.core.builder.CommonConfigBuilder;
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.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
import static cc.carm.lib.configuration.common.builder.message.MessageConfigBuilder.DEFAULT_PARAM_FORMATTER;
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 = 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 final String[] buildParams() {
return Arrays.stream(params).map(param -> paramFormatter.apply(param)).toArray(String[]::new);
}
}

View File

@ -0,0 +1,73 @@
package cc.carm.lib.configuration.common.builder.message;
import cc.carm.lib.configuration.common.data.AbstractText;
import cc.carm.lib.configuration.common.value.ConfigMessage;
import cc.carm.lib.configuration.core.builder.CommonConfigBuilder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
import static cc.carm.lib.configuration.common.builder.message.MessageConfigBuilder.DEFAULT_PARAM_FORMATTER;
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 = 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 final String[] buildParams() {
return Arrays.stream(params).map(param -> paramFormatter.apply(param)).toArray(String[]::new);
}
}

View File

@ -0,0 +1,65 @@
package cc.carm.lib.configuration.common.data;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
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> @Nullable M parse(@NotNull BiFunction<@Nullable R, @NotNull String, @NotNull M> parser,
@Nullable R receiver, @Nullable String[] params, @Nullable Object[] values) {
return parse(parser, receiver, buildParams(params, values));
}
public <M> @Nullable M parse(@NotNull BiFunction<@Nullable R, @NotNull String, @NotNull M> parser,
@Nullable R receiver, @NotNull Map<String, Object> placeholders) {
String message = getMessage();
if (message.isEmpty()) return null; // No further processing
else return parser.apply(receiver, setPlaceholders(message, placeholders));
}
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;
}
}

View File

@ -0,0 +1,48 @@
package cc.carm.lib.configuration.common.utils;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class ColorParser {
public static String parse(String text) {
return parseBaseColor(parseHexColor(text));
}
public static String[] parse(String... texts) {
return parse(Arrays.asList(texts)).toArray(new String[0]);
}
public static List<String> parse(List<String> texts) {
return texts.stream().map(ColorParser::parse).collect(Collectors.toList());
}
public static String parseBaseColor(final String text) {
return text.replaceAll("&", "§").replace("§§", "&");
}
/**
* Parse HEXColor code like <blockquote><pre>&amp;(#000000)</pre></blockquote> to minecraft colored text.
*
* @param text the text to parse
* @return color parsed
*/
public static String parseHexColor(String text) {
Pattern pattern = Pattern.compile("&\\((&?#[0-9a-fA-F]{6})\\)");
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
String hexColor = text.substring(matcher.start() + 2, matcher.end() - 1);
hexColor = hexColor.replace("&", "");
StringBuilder bukkitColorCode = new StringBuilder('§' + "x");
for (int i = 1; i < hexColor.length(); i++) {
bukkitColorCode.append('§').append(hexColor.charAt(i));
}
text = text.replaceAll("&\\(" + hexColor + "\\)", bukkitColorCode.toString().toLowerCase());
matcher.reset(text);
}
return text;
}
}

View File

@ -0,0 +1,74 @@
package cc.carm.lib.configuration.common.value;
import cc.carm.lib.configuration.common.data.AbstractText;
import cc.carm.lib.configuration.core.function.ConfigValueParser;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
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> {
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(@Nullable ConfigurationProvider<?> provider,
@Nullable String sectionPath, @Nullable ConfigCommentInfo comments,
@NotNull Class<T> textClazz, @NotNull T defaultMessage, @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(provider, sectionPath, comments, textClazz, defaultMessage,
ConfigValueParser.castToString().andThen((s, d) -> textBuilder.apply(s)), AbstractText::getMessage
);
this.params = params;
this.messageParser = messageParser;
this.sendFunction = sendFunction;
this.textBuilder = textBuilder;
}
public @Nullable M parse(@Nullable R sender, @Nullable Object... values) {
return parse(sender, AbstractText.buildParams(params, values));
}
public @Nullable M parse(@Nullable R sender, @NotNull Map<String, Object> placeholders) {
T value = get();
if (value == null || value.getMessage().isEmpty()) return null;
else return value.parse(this.messageParser, sender, placeholders);
}
public void send(@Nullable R receiver, @Nullable Object... values) {
send(receiver, AbstractText.buildParams(params, values));
}
public void send(@Nullable R receiver, @NotNull Map<String, Object> placeholders) {
if (receiver == null) return;
M parsed = parse(receiver, placeholders);
if (parsed == null) return;
sendFunction.accept(receiver, parsed);
}
public void broadcast(@Nullable Object... values) {
broadcast(AbstractText.buildParams(params, values));
}
public abstract void broadcast(@NotNull Map<String, Object> placeholders);
public void set(@Nullable String value) {
this.set(value == null ? null : buildText(value));
}
protected T buildText(String value) {
return textBuilder.apply(value);
}
}

View File

@ -0,0 +1,93 @@
package cc.carm.lib.configuration.common.value;
import cc.carm.lib.configuration.common.data.AbstractText;
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.value.type.ConfiguredList;
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> {
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;
@SuppressWarnings("NullableProblems")
public ConfigMessageList(@Nullable ConfigurationProvider<?> provider,
@Nullable String sectionPath, @Nullable ConfigCommentInfo comments,
@NotNull Class<T> textClazz, @NotNull List<T> messages, @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(
provider, sectionPath, comments, textClazz, messages,
ConfigDataFunction.castToString().andThen(textBuilder::apply), AbstractText::getMessage
);
this.params = params;
this.messageParser = messageParser;
this.sendFunction = sendFunction;
this.textBuilder = textBuilder;
}
public @Nullable List<M> parse(@Nullable R sender, @Nullable Object... values) {
return parse(sender, T.buildParams(params, values));
}
public @Nullable List<M> parse(@Nullable R sender, @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, sender, placeholders))
.collect(Collectors.toList());
}
public void send(@Nullable R receiver, @Nullable Object... values) {
send(receiver, T.buildParams(params, values));
}
public void send(@Nullable R receiver, @NotNull Map<String, Object> placeholders) {
if (receiver == null) return;
List<M> parsed = parse(receiver, placeholders);
if (parsed == null) return;
sendFunction.accept(receiver, parsed);
}
public void broadcast(@Nullable Object... values) {
broadcast(T.buildParams(params, values));
}
public abstract void broadcast(@NotNull Map<String, Object> 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 || values.isEmpty()) {
set(null);
} else {
set(buildText(values));
}
}
protected List<T> buildText(List<String> values) {
return values.stream().map(textBuilder).collect(Collectors.toList());
}
}

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>mineconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.1.7</version>
<version>1.2.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
@ -24,6 +24,13 @@
<scope>compile</scope>
</dependency>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>mineconfiguration-common</artifactId>
<version>${project.parent.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
@ -31,6 +38,13 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.10.9</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>

View File

@ -2,7 +2,7 @@ package cc.carm.lib.configuration.craft.builder;
import cc.carm.lib.configuration.core.builder.ConfigBuilder;
import cc.carm.lib.configuration.craft.builder.item.ItemConfigBuilder;
import cc.carm.lib.configuration.craft.builder.message.MessageConfigBuilder;
import cc.carm.lib.configuration.craft.builder.message.CraftMessageBuilder;
import cc.carm.lib.configuration.craft.builder.serializable.SerializableBuilder;
import cc.carm.lib.configuration.craft.builder.sound.SoundConfigBuilder;
import cc.carm.lib.configuration.craft.data.ItemConfig;
@ -21,8 +21,8 @@ public class CraftConfigBuilder extends ConfigBuilder {
return new ItemConfigBuilder();
}
public @NotNull MessageConfigBuilder createMessage() {
return new MessageConfigBuilder();
public @NotNull CraftMessageBuilder createMessage() {
return new CraftMessageBuilder();
}
public <V extends ConfigurationSerializable> @NotNull SerializableBuilder<V> ofSerializable(@NotNull Class<V> valueClass) {

View File

@ -0,0 +1,58 @@
package cc.carm.lib.configuration.craft.builder.message;
import cc.carm.lib.configuration.common.builder.message.MessageConfigBuilder;
import cc.carm.lib.configuration.common.utils.ColorParser;
import cc.carm.lib.configuration.craft.data.MessageText;
import cc.carm.lib.configuration.craft.utils.PAPIHelper;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.function.BiFunction;
public class CraftMessageBuilder extends MessageConfigBuilder<CommandSender, MessageText> {
public CraftMessageBuilder() {
super(CommandSender.class, MessageText.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) -> {
if (receiver instanceof Player && hasPlaceholderAPI()) {
message = PAPIHelper.parseMessages((Player) receiver, message);
}
return ColorParser.parse(message);
};
}
public static boolean hasPlaceholderAPI() {
return Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null;
}
}

View File

@ -0,0 +1,34 @@
package cc.carm.lib.configuration.craft.builder.message;
import cc.carm.lib.configuration.common.builder.message.MessageListBuilder;
import cc.carm.lib.configuration.craft.data.MessageText;
import cc.carm.lib.configuration.craft.value.ConfiguredMessageList;
import org.bukkit.command.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 CraftMessageListBuilder<M>
extends MessageListBuilder<M, CommandSender, MessageText, CraftMessageListBuilder<M>> {
public CraftMessageListBuilder(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
super(CommandSender.class, MessageText::of, parser);
}
@Override
protected @NotNull CraftMessageListBuilder<M> getThis() {
return this;
}
@Override
public @NotNull ConfiguredMessageList<M> build() {
return new ConfiguredMessageList<>(
this.provider, this.path, buildComments(),
Optional.ofNullable(this.defaultValue).orElse(MessageText.of(new ArrayList<>())),
buildParams(), this.messageParser, this.sendFunction
);
}
}

View File

@ -0,0 +1,35 @@
package cc.carm.lib.configuration.craft.builder.message;
import cc.carm.lib.configuration.common.builder.message.MessageValueBuilder;
import cc.carm.lib.configuration.craft.data.MessageText;
import cc.carm.lib.configuration.craft.value.ConfiguredMessage;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Optional;
import java.util.function.BiFunction;
public class CraftMessageValueBuilder<M>
extends MessageValueBuilder<M, CommandSender, MessageText, CraftMessageValueBuilder<M>> {
public CraftMessageValueBuilder(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
super(CommandSender.class, MessageText::new, parser);
}
@Override
protected @NotNull CraftMessageValueBuilder<M> getThis() {
return this;
}
@Override
public @NotNull ConfiguredMessage<M> build() {
return new ConfiguredMessage<>(
this.provider, this.path, buildComments(),
Optional.ofNullable(this.defaultValue).orElse(MessageText.of("")),
buildParams(), this.messageParser, this.sendHandler
);
}
}

View File

@ -1,45 +0,0 @@
package cc.carm.lib.configuration.craft.builder.message;
import cc.carm.lib.configuration.craft.utils.ColorParser;
import cc.carm.lib.configuration.craft.value.ConfiguredMessage;
import cc.carm.lib.configuration.craft.value.ConfiguredMessageList;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.function.BiFunction;
public class MessageConfigBuilder {
public <M> @NotNull MessageValueBuilder<M> asValue(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser) {
return new MessageValueBuilder<>(messageParser);
}
public <M> @NotNull MessageListBuilder<M> asList(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser) {
return new MessageListBuilder<>(messageParser);
}
public @NotNull MessageValueBuilder<String> asStringValue() {
return asValue((sender, message) -> ColorParser.parseColor(message))
.whenSend(CommandSender::sendMessage);
}
public @NotNull ConfiguredMessage<String> valueOfString() {
return valueOfString("");
}
public @NotNull ConfiguredMessage<String> valueOfString(@NotNull String defaultMessage) {
return asStringValue().defaults(defaultMessage).build();
}
public @NotNull MessageListBuilder<String> asStringList() {
return asList((sender, message) -> ColorParser.parseColor(message))
.whenSend((sender, messages) -> messages.forEach(sender::sendMessage));
}
public @NotNull ConfiguredMessageList<String> listOfString(@NotNull String... defaultMessages) {
return asStringList().defaults(defaultMessages).build();
}
}

View File

@ -1,81 +0,0 @@
package cc.carm.lib.configuration.craft.builder.message;
import cc.carm.lib.configuration.core.builder.CommonConfigBuilder;
import cc.carm.lib.configuration.craft.data.MessageText;
import cc.carm.lib.configuration.craft.value.ConfiguredMessageList;
import org.bukkit.command.CommandSender;
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;
public class MessageListBuilder<M>
extends CommonConfigBuilder<List<MessageText>, MessageListBuilder<M>> {
protected @NotNull String[] params;
protected @NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser;
protected @NotNull BiConsumer<@NotNull CommandSender, @NotNull List<M>> sendFunction;
protected @NotNull Function<@NotNull String, @NotNull String> paramFormatter;
public MessageListBuilder(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
this.params = new String[0];
this.messageParser = parser;
this.paramFormatter = MessageValueBuilder.DEFAULT_PARAM_FORMATTER;
this.sendFunction = (sender, M) -> {
};
}
public MessageListBuilder<M> defaults(@NotNull String... messages) {
return defaults(Arrays.asList(messages));
}
public MessageListBuilder<M> defaults(@NotNull List<String> messages) {
return defaults(new ArrayList<>(MessageText.of(messages)));
}
public MessageListBuilder<M> params(@NotNull String... params) {
this.params = params;
return this;
}
public MessageListBuilder<M> params(@NotNull List<String> params) {
this.params = params.toArray(new String[0]);
return this;
}
public MessageListBuilder<M> formatParam(@NotNull Function<@NotNull String, @NotNull String> paramFormatter) {
this.paramFormatter = paramFormatter;
return this;
}
public MessageListBuilder<M> whenSend(@NotNull BiConsumer<@NotNull CommandSender, @NotNull List<M>> sendFunction) {
this.sendFunction = sendFunction;
return this;
}
@Override
protected @NotNull MessageListBuilder<M> getThis() {
return this;
}
@Override
public @NotNull ConfiguredMessageList<M> build() {
return new ConfiguredMessageList<>(
this.provider, this.path, buildComments(),
Optional.ofNullable(this.defaultValue).orElse(new ArrayList<>()),
buildParams(), this.messageParser, this.sendFunction
);
}
protected final String[] buildParams() {
return Arrays.stream(params).map(param -> paramFormatter.apply(param)).toArray(String[]::new);
}
}

View File

@ -1,79 +0,0 @@
package cc.carm.lib.configuration.craft.builder.message;
import cc.carm.lib.configuration.core.builder.CommonConfigBuilder;
import cc.carm.lib.configuration.craft.data.MessageText;
import cc.carm.lib.configuration.craft.value.ConfiguredMessage;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
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;
public class MessageValueBuilder<M>
extends CommonConfigBuilder<MessageText, MessageValueBuilder<M>> {
public static Function<@NotNull String, @NotNull String> DEFAULT_PARAM_FORMATTER = (s) -> "%(" + s + ")";
protected @NotNull String[] params;
protected @NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser;
protected @NotNull BiConsumer<@NotNull CommandSender, @NotNull M> sendHandler;
protected @NotNull Function<@NotNull String, @NotNull String> paramFormatter;
public MessageValueBuilder(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
this.params = new String[0];
this.paramFormatter = DEFAULT_PARAM_FORMATTER;
this.messageParser = parser;
this.sendHandler = (sender, M) -> {
};
}
public MessageValueBuilder<M> defaults(@NotNull String message) {
return defaults(MessageText.of(message));
}
public MessageValueBuilder<M> params(@NotNull String... params) {
this.params = params;
return this;
}
public MessageValueBuilder<M> params(@NotNull List<String> params) {
this.params = params.toArray(new String[0]);
return this;
}
public MessageValueBuilder<M> formatParam(@NotNull Function<@NotNull String, @NotNull String> paramFormatter) {
this.paramFormatter = paramFormatter;
return this;
}
public MessageValueBuilder<M> whenSend(@NotNull BiConsumer<@NotNull CommandSender, @NotNull M> sendFunction) {
this.sendHandler = sendFunction;
return this;
}
@Override
protected @NotNull MessageValueBuilder<M> getThis() {
return this;
}
@Override
public @NotNull ConfiguredMessage<M> build() {
return new ConfiguredMessage<>(
this.provider, this.path, buildComments(),
Optional.ofNullable(this.defaultValue).orElse(MessageText.of("")),
buildParams(), this.messageParser, this.sendHandler
);
}
protected final String[] buildParams() {
return Arrays.stream(params).map(param -> paramFormatter.apply(param)).toArray(String[]::new);
}
}

View File

@ -1,15 +1,21 @@
package cc.carm.lib.configuration.craft.data;
import cc.carm.lib.configuration.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.*;
import java.util.function.BiFunction;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class MessageText {
public class MessageText extends AbstractText<CommandSender> {
public MessageText(@NotNull String message) {
super(CommandSender.class, message);
}
@Contract("!null,-> !null")
public static @Nullable MessageText of(@Nullable String message) {
@ -26,49 +32,4 @@ public class MessageText {
return of(Arrays.asList(messages));
}
protected @NotNull String message;
public MessageText(@NotNull String message) {
this.message = message;
}
public @NotNull String getMessage() {
return this.message;
}
public <M> @Nullable M parse(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @NotNull M> parser,
@Nullable CommandSender sender, @Nullable String[] params, @Nullable Object[] values) {
return parse(parser, sender, buildParams(params, values));
}
public <M> @Nullable M parse(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @NotNull M> parser,
@Nullable CommandSender sender, @NotNull Map<String, Object> placeholders) {
String message = getMessage();
if (message.isEmpty()) return null; // No further processing
else return parser.apply(sender, setPlaceholders(message, placeholders));
}
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;
}
}

View File

@ -0,0 +1,19 @@
package cc.carm.lib.configuration.craft.utils;
import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.entity.Player;
import java.util.List;
public class PAPIHelper {
public static String parseMessages(Player player, String message) {
return PlaceholderAPI.setPlaceholders(player, message);
}
public static List<String> parseMessages(Player player, List<String> messages) {
return PlaceholderAPI.setPlaceholders(player, messages);
}
}

View File

@ -1,11 +1,10 @@
package cc.carm.lib.configuration.craft.value;
import cc.carm.lib.configuration.core.function.ConfigValueParser;
import cc.carm.lib.configuration.common.value.ConfigMessage;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
import cc.carm.lib.configuration.craft.CraftConfigValue;
import cc.carm.lib.configuration.craft.builder.message.MessageValueBuilder;
import cc.carm.lib.configuration.craft.builder.message.CraftMessageValueBuilder;
import cc.carm.lib.configuration.craft.data.MessageText;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
@ -16,75 +15,38 @@ import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
public class ConfiguredMessage<M> extends ConfiguredValue<MessageText> {
public class ConfiguredMessage<M> extends ConfigMessage<M, MessageText, CommandSender> {
@NotNull
public static <M> MessageValueBuilder<@Nullable M> create(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser) {
public static <M> CraftMessageValueBuilder<@Nullable M> create(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser) {
return CraftConfigValue.builder().createMessage().asValue(messageParser);
}
public static MessageValueBuilder<String> fromString() {
public static CraftMessageValueBuilder<String> asString() {
return CraftConfigValue.builder().createMessage().asStringValue();
}
public static ConfiguredMessage<String> ofString() {
return CraftConfigValue.builder().createMessage().valueOfString();
return asString().build();
}
public static ConfiguredMessage<String> ofString(@NotNull String defaultMessage) {
return CraftConfigValue.builder().createMessage().valueOfString(defaultMessage);
return asString().defaults(defaultMessage).build();
}
protected final @NotNull String[] params;
protected final @NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser;
protected final @NotNull BiConsumer<@NotNull CommandSender, @NotNull M> sendFunction;
public ConfiguredMessage(@Nullable ConfigurationProvider<?> provider,
@Nullable String sectionPath, @Nullable ConfigCommentInfo comments,
@NotNull MessageText message, @NotNull String[] params,
@NotNull MessageText defaultMessage, @NotNull String[] params,
@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser,
@NotNull BiConsumer<@NotNull CommandSender, @NotNull M> sendFunction) {
super(provider, sectionPath, comments, MessageText.class, message,
ConfigValueParser.castToString().andThen((s, d) -> MessageText.of(s)),
MessageText::getMessage
);
this.params = params;
this.messageParser = messageParser;
this.sendFunction = sendFunction;
super(provider, sectionPath, comments, MessageText.class, defaultMessage, params, messageParser, sendFunction, MessageText::of);
}
public @Nullable M parse(@Nullable CommandSender sender, @Nullable Object... values) {
return parse(sender, MessageText.buildParams(params, values));
}
public @Nullable M parse(@Nullable CommandSender sender, @NotNull Map<String, Object> placeholders) {
MessageText value = get();
if (value == null || value.getMessage().isEmpty()) return null;
else return value.parse(this.messageParser, sender, placeholders);
}
public void send(@Nullable CommandSender receiver, @Nullable Object... values) {
send(receiver, MessageText.buildParams(params, values));
}
public void send(@Nullable CommandSender receiver, @NotNull Map<String, Object> placeholders) {
if (receiver == null) return;
M parsed = parse(receiver, placeholders);
if (parsed == null) return;
sendFunction.accept(receiver, parsed);
}
public void broadcast(@Nullable Object... values) {
broadcast(MessageText.buildParams(params, values));
}
public void broadcast(@NotNull Map<String, Object> placeholders) {
Bukkit.getOnlinePlayers().forEach(pl -> send(pl, placeholders));
send(Bukkit.getConsoleSender(), placeholders);
}
public void set(@Nullable String value) {
this.set(value == null ? null : new MessageText(value));
}
}

View File

@ -1,11 +1,10 @@
package cc.carm.lib.configuration.craft.value;
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
import cc.carm.lib.configuration.common.value.ConfigMessageList;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.value.type.ConfiguredList;
import cc.carm.lib.configuration.craft.CraftConfigValue;
import cc.carm.lib.configuration.craft.builder.message.MessageListBuilder;
import cc.carm.lib.configuration.craft.builder.message.CraftMessageListBuilder;
import cc.carm.lib.configuration.craft.data.MessageText;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
@ -16,68 +15,28 @@ import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
public class ConfiguredMessageList<M> extends ConfiguredList<MessageText> {
public class ConfiguredMessageList<M> extends ConfigMessageList<M, MessageText, CommandSender> {
@NotNull
public static <M> MessageListBuilder<M> create(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser) {
public static <M> CraftMessageListBuilder<M> create(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser) {
return CraftConfigValue.builder().createMessage().asList(messageParser);
}
public static MessageListBuilder<String> fromString() {
public static CraftMessageListBuilder<String> asStrings() {
return CraftConfigValue.builder().createMessage().asStringList();
}
public static ConfiguredMessageList<String> ofString(@NotNull String... defaultMessages) {
return CraftConfigValue.builder().createMessage().listOfString(defaultMessages);
public static ConfiguredMessageList<String> ofStrings(@NotNull String... defaultMessages) {
return asStrings().defaults(defaultMessages).build();
}
protected final @NotNull String[] params;
protected final @NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser;
protected final @NotNull BiConsumer<@NotNull CommandSender, @NotNull List<M>> sendFunction;
public ConfiguredMessageList(@Nullable ConfigurationProvider<?> provider,
@Nullable String sectionPath, @Nullable ConfigCommentInfo comments,
@NotNull List<MessageText> messages, @NotNull String[] params,
@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser,
@NotNull BiConsumer<@NotNull CommandSender, @NotNull List<M>> sendFunction) {
super(provider, sectionPath, comments, MessageText.class, messages,
ConfigDataFunction.castToString().andThen(MessageText::new), MessageText::getMessage
);
this.params = params;
this.messageParser = messageParser;
this.sendFunction = sendFunction;
}
public @Nullable List<M> parse(@Nullable CommandSender sender, @Nullable Object... values) {
return parse(sender, MessageText.buildParams(params, values));
}
public @Nullable List<M> parse(@Nullable CommandSender sender, @NotNull Map<String, Object> placeholders) {
List<MessageText> list = get();
if (list.isEmpty()) return null;
List<String> messages = list.stream().map(MessageText::getMessage).collect(Collectors.toList());
if (String.join("", messages).isEmpty()) return null;
return list.stream().map(value -> value.parse(this.messageParser, sender, placeholders))
.collect(Collectors.toList());
}
public void send(@Nullable CommandSender receiver, @Nullable Object... values) {
send(receiver, MessageText.buildParams(params, values));
}
public void send(@Nullable CommandSender receiver, @NotNull Map<String, Object> placeholders) {
if (receiver == null) return;
List<M> parsed = parse(receiver, placeholders);
if (parsed == null) return;
sendFunction.accept(receiver, parsed);
}
public void broadcast(@Nullable Object... values) {
broadcast(MessageText.buildParams(params, values));
super(provider, sectionPath, comments, MessageText.class, messages, params, messageParser, sendFunction, MessageText::of);
}
public void broadcast(@NotNull Map<String, Object> placeholders) {
@ -85,24 +44,4 @@ public class ConfiguredMessageList<M> extends ConfiguredList<MessageText> {
send(Bukkit.getConsoleSender(), placeholders);
}
public void setNull() {
set(null);
}
public void setMessages(@NotNull String... values) {
if (values.length == 0) {
setNull();
return;
}
set(MessageText.of(values));
}
public void setMessages(@Nullable List<String> values) {
if (values == null || values.isEmpty()) {
setNull();
return;
}
set(MessageText.of(values));
}
}

View File

@ -9,6 +9,7 @@
<module>craftbukkit</module>
<module>bukkit</module>
<module>spigot</module>
<module>common</module>
</modules>
<properties>
<java.version>1.8</java.version>
@ -21,7 +22,7 @@
</properties>
<groupId>cc.carm.lib</groupId>
<artifactId>mineconfiguration-parent</artifactId>
<version>1.1.7</version>
<version>1.2.0</version>
<packaging>pom</packaging>
<name>MineConfiguration</name>
@ -70,6 +71,11 @@
<url>https://repo.carm.cc/repository/maven-public/</url>
</repository>
<repository>
<id>minebench-repo</id>
<url>https://repo.minebench.de/</url>
</repository>
<repository>
<id>nexus</id>
<url>https://mvn.lumine.io/repository/maven-public/</url>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>mineconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.1.7</version>
<version>1.2.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>