1
mirror of https://github.com/CarmJos/MineConfiguration.git synced 2024-09-19 20:05:49 +00:00

[1.1.0-SNAPSHOT] 添加 ConfiguredMessage 便于快速制作消息类配置文件。

This commit is contained in:
Carm Jos 2022-04-25 08:13:08 +08:00
parent 1c0883534f
commit d567bfb4af
18 changed files with 555 additions and 15 deletions

View File

@ -39,7 +39,7 @@ jobs:
central-deploy:
name: "Deploy Project (Central Repository)"
runs-on: ubuntu-latest
if: '!contains( {{ env.GITHUB_REF }} , "SNAPSHOT" )'
steps:
- uses: actions/checkout@v2
- name: "Set up JDK"

View File

@ -8,6 +8,10 @@ on:
workflow_dispatch:
pull_request:
push:
paths-ignore:
- ".github/**"
- "README.md"
- "LICENCE"
jobs:
build:

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>mineconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.0.2</version>
<version>1.1.0-SNAPSHOT</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.0.2</version>
<version>1.1.0-SNAPSHOT</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.0.2</version>
<version>1.1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>

View File

@ -2,6 +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.serializable.SerializableBuilder;
import cc.carm.lib.configuration.craft.builder.sound.SoundConfigBuilder;
import cc.carm.lib.configuration.craft.data.ItemConfig;
@ -20,6 +21,10 @@ public class CraftConfigBuilder extends ConfigBuilder {
return new ItemConfigBuilder();
}
public @NotNull MessageConfigBuilder createMessage() {
return new MessageConfigBuilder();
}
public <V extends ConfigurationSerializable> @NotNull SerializableBuilder<V> ofSerializable(@NotNull Class<V> valueClass) {
return new SerializableBuilder<>(valueClass);
}

View File

@ -31,7 +31,7 @@ public class ItemConfigBuilder extends AbstractCraftBuilder<ItemConfig, ItemConf
public ItemConfigBuilder defaults(@NotNull Material type, short data,
@Nullable String name, @NotNull List<String> lore) {
return defaults(new ItemConfig(type, data, name, lore));
return defaultType(type).defaultDataID(data).defaultName(name).defaultLore(lore);
}
public ItemConfigBuilder defaultType(@NotNull Material type) {
@ -39,7 +39,7 @@ public class ItemConfigBuilder extends AbstractCraftBuilder<ItemConfig, ItemConf
return this;
}
public ItemConfigBuilder defaultName(@NotNull String name) {
public ItemConfigBuilder defaultName(@Nullable String name) {
this.name = name;
return this;
}
@ -50,7 +50,11 @@ public class ItemConfigBuilder extends AbstractCraftBuilder<ItemConfig, ItemConf
}
public ItemConfigBuilder defaultLore(@NotNull String... lore) {
this.lore = new ArrayList<>(Arrays.asList(lore));
return defaultLore(Arrays.asList(lore));
}
public ItemConfigBuilder defaultLore(@NotNull List<String> lore) {
this.lore = new ArrayList<>(lore);
return this;
}

View File

@ -0,0 +1,45 @@
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().content(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().contents(defaultMessages).build();
}
}

View File

@ -0,0 +1,81 @@
package cc.carm.lib.configuration.craft.builder.message;
import cc.carm.lib.configuration.core.builder.CommonConfigBuilder;
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.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
public class MessageListBuilder<M>
extends CommonConfigBuilder<String, MessageListBuilder<M>> {
protected @NotNull List<String> messages;
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.messages = new ArrayList<>();
this.params = new String[0];
this.messageParser = parser;
this.paramFormatter = MessageValueBuilder.DEFAULT_PARAM_FORMATTER;
this.sendFunction = (sender, M) -> {
};
}
public MessageListBuilder<M> contents(@NotNull String... messages) {
return contents(Arrays.asList(messages));
}
public MessageListBuilder<M> contents(@NotNull List<String> messages) {
this.messages = new ArrayList<>(messages);
return this;
}
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(),
this.messages, buildParams(), this.messageParser, this.sendFunction
);
}
protected final String[] buildParams() {
return Arrays.stream(params).map(param -> paramFormatter.apply(param)).toArray(String[]::new);
}
}

View File

@ -0,0 +1,79 @@
package cc.carm.lib.configuration.craft.builder.message;
import cc.carm.lib.configuration.core.builder.CommonConfigBuilder;
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.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
public class MessageValueBuilder<M>
extends CommonConfigBuilder<String, MessageValueBuilder<M>> {
public static Function<@NotNull String, @NotNull String> DEFAULT_PARAM_FORMATTER = (s) -> "%(" + s + ")";
protected @NotNull String message;
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.message = "";
this.params = new String[0];
this.paramFormatter = DEFAULT_PARAM_FORMATTER;
this.messageParser = parser;
this.sendHandler = (sender, M) -> {
};
}
public MessageValueBuilder<M> content(@NotNull String message) {
this.message = message;
return this;
}
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(),
this.message, buildParams(), this.messageParser, this.sendHandler
);
}
protected final String[] buildParams() {
return Arrays.stream(params).map(param -> paramFormatter.apply(param)).toArray(String[]::new);
}
}

View File

@ -15,10 +15,10 @@ import java.util.stream.Collectors;
public class ItemConfig {
@NotNull Material type;
short data;
@Nullable String name;
@NotNull List<String> lore;
protected @NotNull Material type;
protected short data;
protected @Nullable String name;
protected @NotNull List<String> lore;
public ItemConfig(@NotNull Material type, short damage,
@Nullable String name, @NotNull List<String> lore) {
@ -44,7 +44,7 @@ public class ItemConfig {
return lore;
}
public @NotNull ItemStack getItemStack() {
public final @NotNull ItemStack getItemStack() {
return getItemStack(1);
}

View File

@ -0,0 +1,75 @@
package cc.carm.lib.configuration.craft.data;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
public class MessageText {
@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(@NotNull List<String> messages) {
return messages.stream().map(MessageText::of).collect(Collectors.toList());
}
public static @NotNull List<MessageText> of(@NotNull String... messages) {
return Arrays.stream(messages).map(MessageText::of).collect(Collectors.toList());
}
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));
}
protected 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;
}
protected 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,49 @@
package cc.carm.lib.configuration.craft.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) {
text = parseHexColor(text);
return parseColor(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 parseColor(final String text) {
return text.replaceAll("&", "§").replace("§§", "&");
}
/**
* Parse HEXColor code like &(#000000) 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

@ -15,7 +15,7 @@ public class ConfiguredItem extends ConfiguredSection<ItemConfig> {
public static ItemConfigBuilder create() {
return CraftConfigValue.builder().createItem();
}
public static ConfiguredItem of() {
return CraftConfigValue.builder().ofItem();
}

View File

@ -0,0 +1,92 @@
package cc.carm.lib.configuration.craft.value;
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 cc.carm.lib.configuration.craft.CraftConfigValue;
import cc.carm.lib.configuration.craft.builder.message.MessageConfigBuilder;
import cc.carm.lib.configuration.craft.data.MessageText;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
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 ConfiguredValue<MessageText> {
public static MessageConfigBuilder create() {
return CraftConfigValue.builder().createMessage();
}
public static ConfiguredMessage<String> ofString() {
return create().valueOfString();
}
public static ConfiguredMessage<String> ofString(@NotNull String defaultMessage) {
return create().valueOfString(defaultMessage);
}
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 String message, @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, MessageText.of(message),
ConfigValueParser.castToString().andThen((s, d) -> MessageText.of(s)),
MessageText::getMessage
);
this.params = params;
this.messageParser = messageParser;
this.sendFunction = sendFunction;
}
public @Nullable M parse(@Nullable CommandSender sender, @Nullable Object... values) {
MessageText value = get();
if (value == null) return null;
else return value.parse(this.messageParser, sender, this.params, values);
}
public @Nullable M parse(@Nullable CommandSender sender, @NotNull Map<String, Object> placeholders) {
MessageText value = get();
if (value == null) return null;
else return value.parse(this.messageParser, sender, placeholders);
}
public void send(@Nullable CommandSender receiver, @Nullable Object... values) {
if (receiver == null) return;
M parsed = parse(receiver, values);
if (parsed == null) return;
sendFunction.accept(receiver, parsed);
}
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) {
Bukkit.getOnlinePlayers().forEach(pl -> send(pl, values));
send(Bukkit.getConsoleSender(), 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

@ -0,0 +1,106 @@
package cc.carm.lib.configuration.craft.value;
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 cc.carm.lib.configuration.craft.CraftConfigValue;
import cc.carm.lib.configuration.craft.builder.message.MessageConfigBuilder;
import cc.carm.lib.configuration.craft.data.MessageText;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
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;
import java.util.stream.Collectors;
public class ConfiguredMessageList<M> extends ConfiguredList<MessageText> {
public static MessageConfigBuilder create() {
return CraftConfigValue.builder().createMessage();
}
public static ConfiguredMessageList<String> ofString(@NotNull String... defaultMessages) {
return create().listOfString(defaultMessages);
}
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<String> 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, MessageText.of(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) {
List<MessageText> list = get();
if (list.isEmpty()) return null;
return list.stream().map(value -> value.parse(this.messageParser, sender, this.params, values))
.collect(Collectors.toList());
}
public @Nullable List<M> parse(@Nullable CommandSender sender, @NotNull Map<String, Object> placeholders) {
List<MessageText> list = get();
if (list.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) {
if (receiver == null) return;
List<M> parsed = parse(receiver, values);
if (parsed == null) return;
sendFunction.accept(receiver, parsed);
}
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) {
Bukkit.getOnlinePlayers().forEach(pl -> send(pl, values));
send(Bukkit.getConsoleSender(), values);
}
public void broadcast(@NotNull Map<String, Object> placeholders) {
Bukkit.getOnlinePlayers().forEach(pl -> send(pl, placeholders));
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

@ -21,7 +21,7 @@
</properties>
<groupId>cc.carm.lib</groupId>
<artifactId>mineconfiguration-parent</artifactId>
<version>1.0.2</version>
<version>1.1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>MineConfiguration</name>

View File

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