1
mirror of https://github.com/CarmJos/MineConfiguration.git synced 2026-06-04 13:55:03 +08:00

[1.3.0] 版本更新

1. 更新 EasyConfiguration 到 3.0.0 (破坏性更新)。
2. 添加 ConfiguredTitle 用于快捷向玩家发送title消息。
3. 为 ConfiguredItem 添加name和lore的原生params变量支持。
4. 添加 ProtocolLibHelper 用于支持多版本情况下的部分功能。(如需使用请安装 ProtocolLib 插件)
This commit is contained in:
2022-05-19 01:45:16 +08:00
parent af614deae6
commit 98ee47f676
51 changed files with 895 additions and 370 deletions
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>mineconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.2.2</version>
<version>1.3.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
@@ -9,11 +9,6 @@ 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;
@@ -2,6 +2,7 @@ package cc.carm.lib.configuration.common.builder.message;
import cc.carm.lib.configuration.common.data.AbstractText;
import cc.carm.lib.configuration.common.utils.ParamsUtils;
import cc.carm.lib.configuration.common.value.ConfigMessageList;
import cc.carm.lib.configuration.core.builder.CommonConfigBuilder;
import org.jetbrains.annotations.NotNull;
@@ -15,8 +16,6 @@ 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> {
@@ -36,7 +35,7 @@ public abstract class MessageListBuilder<M, R, T extends AbstractText<R>, B exte
this.textBuilder = textBuilder;
this.params = new String[0];
this.messageParser = parser;
this.paramFormatter = DEFAULT_PARAM_FORMATTER;
this.paramFormatter = ParamsUtils.DEFAULT_PARAM_FORMATTER;
this.sendFunction = (sender, M) -> {
};
}
@@ -68,8 +67,4 @@ public abstract class MessageListBuilder<M, R, T extends AbstractText<R>, B exte
@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);
}
}
@@ -1,19 +1,17 @@
package cc.carm.lib.configuration.common.builder.message;
import cc.carm.lib.configuration.common.data.AbstractText;
import cc.carm.lib.configuration.common.utils.ParamsUtils;
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> {
@@ -32,7 +30,7 @@ public abstract class MessageValueBuilder<M, R, T extends AbstractText<R>, B ext
@NotNull BiFunction<@Nullable R, @NotNull String, @Nullable M> parser) {
this.receiverClazz = receiverClazz;
this.params = new String[0];
this.paramFormatter = DEFAULT_PARAM_FORMATTER;
this.paramFormatter = ParamsUtils.DEFAULT_PARAM_FORMATTER;
this.textBuilder = textBuilder;
this.messageParser = parser;
this.sendHandler = (receiver, M) -> {
@@ -66,8 +64,4 @@ public abstract class MessageValueBuilder<M, R, T extends AbstractText<R>, B ext
@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);
}
}
@@ -1,9 +1,9 @@
package cc.carm.lib.configuration.common.data;
import cc.carm.lib.configuration.common.utils.ParamsUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;
@@ -30,35 +30,14 @@ public abstract class AbstractText<R> {
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));
return parse(parser, receiver, ParamsUtils.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;
else return parser.apply(receiver, ParamsUtils.setPlaceholders(message, placeholders));
}
@@ -8,6 +8,8 @@ import java.util.stream.Collectors;
public class ColorParser {
public static Pattern HEX_PATTERN = Pattern.compile("&\\(&?#([0-9a-fA-F]{6})\\)");
public static String parse(String text) {
return parseBaseColor(parseHexColor(text));
}
@@ -24,25 +26,19 @@ public class ColorParser {
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);
Matcher matcher = HEX_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());
text = matcher.replaceFirst(buildHexColor(matcher.group(1)).toLowerCase());
matcher.reset(text);
}
return text;
}
private static String buildHexColor(String hexCode) {
return Arrays.stream(hexCode.split(""))
.map(s -> '§' + s)
.collect(Collectors.joining("", '§' + "x", ""));
}
}
@@ -0,0 +1,43 @@
package cc.carm.lib.configuration.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,13 +1,14 @@
package cc.carm.lib.configuration.common.value;
import cc.carm.lib.configuration.common.data.AbstractText;
import cc.carm.lib.configuration.common.utils.ParamsUtils;
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.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
@@ -18,27 +19,27 @@ public abstract class ConfigMessage<M, T extends AbstractText<R>, R>
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 BiConsumer<@NotNull R, @NotNull M> messageConsumer;
protected final @NotNull Function<String, T> textBuilder;
public ConfigMessage(@Nullable ConfigurationProvider<?> provider,
@Nullable String sectionPath, @Nullable ConfigCommentInfo comments,
public ConfigMessage(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
@Nullable List<String> headerComments, @Nullable String inlineComments,
@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 BiConsumer<@NotNull R, @NotNull M> messageConsumer,
@NotNull Function<String, T> textBuilder) {
super(provider, sectionPath, comments, textClazz, defaultMessage,
super(provider, sectionPath, headerComments, inlineComments, textClazz, defaultMessage,
ConfigValueParser.castToString().andThen((s, d) -> textBuilder.apply(s)), AbstractText::getMessage
);
this.params = params;
this.messageParser = messageParser;
this.sendFunction = sendFunction;
this.messageConsumer = messageConsumer;
this.textBuilder = textBuilder;
}
public @Nullable M parse(@Nullable R sender, @Nullable Object... values) {
return parse(sender, AbstractText.buildParams(params, values));
return parse(sender, ParamsUtils.buildParams(params, values));
}
public @Nullable M parse(@Nullable R sender, @NotNull Map<String, Object> placeholders) {
@@ -48,18 +49,18 @@ public abstract class ConfigMessage<M, T extends AbstractText<R>, R>
}
public void send(@Nullable R receiver, @Nullable Object... values) {
send(receiver, AbstractText.buildParams(params, values));
send(receiver, ParamsUtils.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);
messageConsumer.accept(receiver, parsed);
}
public void broadcast(@Nullable Object... values) {
broadcast(AbstractText.buildParams(params, values));
broadcast(ParamsUtils.buildParams(params, values));
}
public abstract void broadcast(@NotNull Map<String, Object> placeholders);
@@ -1,8 +1,8 @@
package cc.carm.lib.configuration.common.value;
import cc.carm.lib.configuration.common.data.AbstractText;
import cc.carm.lib.configuration.common.utils.ParamsUtils;
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;
@@ -25,14 +25,14 @@ public abstract class ConfigMessageList<M, T extends AbstractText<R>, R> extends
protected final @NotNull Function<String, T> textBuilder;
@SuppressWarnings("NullableProblems")
public ConfigMessageList(@Nullable ConfigurationProvider<?> provider,
@Nullable String sectionPath, @Nullable ConfigCommentInfo comments,
public ConfigMessageList(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
@Nullable List<String> headerComments, @Nullable String inlineComments,
@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,
provider, sectionPath, headerComments, inlineComments, textClazz, messages,
ConfigDataFunction.castToString().andThen(textBuilder::apply), AbstractText::getMessage
);
this.params = params;
@@ -42,7 +42,7 @@ public abstract class ConfigMessageList<M, T extends AbstractText<R>, R> extends
}
public @Nullable List<M> parse(@Nullable R sender, @Nullable Object... values) {
return parse(sender, T.buildParams(params, values));
return parse(sender, ParamsUtils.buildParams(params, values));
}
public @Nullable List<M> parse(@Nullable R sender, @NotNull Map<String, Object> placeholders) {
@@ -57,7 +57,7 @@ public abstract class ConfigMessageList<M, T extends AbstractText<R>, R> extends
}
public void send(@Nullable R receiver, @Nullable Object... values) {
send(receiver, T.buildParams(params, values));
send(receiver, ParamsUtils.buildParams(params, values));
}
public void send(@Nullable R receiver, @NotNull Map<String, Object> placeholders) {
@@ -68,7 +68,7 @@ public abstract class ConfigMessageList<M, T extends AbstractText<R>, R> extends
}
public void broadcast(@Nullable Object... values) {
broadcast(T.buildParams(params, values));
broadcast(ParamsUtils.buildParams(params, values));
}
public abstract void broadcast(@NotNull Map<String, Object> placeholders);