1
mirror of https://github.com/CarmJos/MineConfiguration.git synced 2024-09-19 20:05:49 +00: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:
Carm Jos 2022-05-19 01:45:16 +08:00
parent af614deae6
commit 98ee47f676
51 changed files with 895 additions and 370 deletions

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>

View File

@ -1,6 +1,7 @@
package cc.carm.lib.configuration;
import cc.carm.lib.configuration.bukkit.source.BukkitConfigProvider;
import org.bukkit.plugin.Plugin;
import java.io.File;
import java.io.IOException;
@ -30,4 +31,11 @@ public class MineConfiguration {
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);
}
}

View File

@ -1,12 +1,12 @@
package cc.carm.lib.configuration.bukkit.source;
import cc.carm.lib.configuration.core.ConfigInitializer;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import cc.carm.lib.configuration.craft.source.CraftConfigProvider;
import cc.carm.lib.configuration.craft.source.CraftSectionWrapper;
import org.bukkit.configuration.file.YamlConfiguration;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.io.BufferedWriter;
import java.io.File;
@ -14,6 +14,7 @@ import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
public class BukkitConfigProvider extends CraftConfigProvider {
@ -55,14 +56,24 @@ public class BukkitConfigProvider extends CraftConfigProvider {
}
@Override
public void setComment(@Nullable String path, @Nullable ConfigCommentInfo comment) {
this.bukkitComments.set(path, comment);
public void setHeaderComment(@Nullable String path, @Nullable List<String> comments) {
this.bukkitComments.setHeaderComments(path, comments);
}
@Override
public @Nullable ConfigCommentInfo getComment(@Nullable String path) {
return this.bukkitComments.get(path);
public void setInlineComment(@NotNull String path, @Nullable String comment) {
this.bukkitComments.setInlineComment(path, comment);
}
@Override
@Nullable
@Unmodifiable
public List<String> getHeaderComment(@Nullable String path) {
return this.bukkitComments.getHeaderComment(path);
}
@Override
public @Nullable String getInlineComment(@NotNull String path) {
return this.bukkitComments.getInlineComment(path);
}
}

View File

@ -1,17 +1,15 @@
package cc.carm.lib.configuration.bukkit.source;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
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 org.jetbrains.annotations.Unmodifiable;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.StringJoiner;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@ -19,38 +17,55 @@ import static cc.carm.lib.configuration.craft.source.CraftConfigProvider.SEPARAT
public class BukkitYAMLComments {
Map<String, ConfigCommentInfo> comments = new HashMap<>();
protected final @NotNull Map<String, List<String>> headerComments = new HashMap<>();
protected final @NotNull Map<String, String> inlineComments = new HashMap<>();
protected Map<String, ConfigCommentInfo> getComments() {
return comments;
protected @NotNull Map<String, List<String>> getHeaderComments() {
return headerComments;
}
public void set(@Nullable String path, @Nullable ConfigCommentInfo comments) {
protected @NotNull Map<String, String> getInlineComments() {
return inlineComments;
}
public void setHeaderComments(@Nullable String path, @Nullable List<String> comments) {
if (comments == null) {
getComments().remove(path);
getHeaderComments().remove(path);
} else {
getComments().put(path, comments);
getHeaderComments().put(path, comments);
}
}
public @NotNull ConfigCommentInfo get(@Nullable String path) {
return getComments().getOrDefault(path, ConfigCommentInfo.defaults());
public void setInlineComment(@NotNull String path, @Nullable String comment) {
if (comment == null) {
getInlineComments().remove(path);
} else {
getInlineComments().put(path, comment);
}
}
public @Nullable String buildComments(@NotNull String indents, @Nullable String path) {
ConfigCommentInfo comments = get(path);
if (!String.join("", comments.getComments()).isEmpty()) {
String prefix = comments.startWrap() ? "\n" : "";
String suffix = comments.endWrap() ? "\n" : "";
StringJoiner joiner = new StringJoiner("\n", prefix, suffix);
for (String comment : comments.getComments()) {
if (comment.length() == 0) joiner.add(" ");
else joiner.add(indents + "# " + comment);
}
return joiner + "\n";
} else {
return comments.startWrap() || comments.endWrap() ? "\n" : null;
@Nullable
@Unmodifiable
public List<String> getHeaderComment(@Nullable String path) {
return Optional.ofNullable(getHeaderComments().get(path)).map(Collections::unmodifiableList).orElse(null);
}
public @Nullable String getInlineComment(@NotNull String path) {
return getInlineComments().get(path);
}
public @Nullable String buildHeaderComments(@Nullable String path, @NotNull String indents) {
List<String> comments = getHeaderComment(path);
if (comments == null || comments.size() == 0) return null;
StringJoiner joiner = new StringJoiner("\n");
for (String comment : comments) {
if (comment.length() == 0) joiner.add(" ");
else joiner.add(indents + "# " + comment);
}
return joiner + "\n";
}
/**
@ -64,37 +79,56 @@ public class BukkitYAMLComments {
public void writeComments(@NotNull YamlConfiguration source, @NotNull BufferedWriter writer) throws IOException {
FileConfiguration temp = new YamlConfiguration(); // 该对象用于临时记录配置内容
for (String fullKey : source.getKeys(true)) {
String indents = getIndents(fullKey);
String comment = buildComments(indents, fullKey);
if (comment != null) writer.write(comment);
String configHeader = buildHeaderComments(null, "");
if (configHeader != null) writer.write(configHeader);
for (String fullKey : source.getKeys(true)) {
Object currentValue = source.get(fullKey);
String indents = getIndents(fullKey);
String headerComments = buildHeaderComments(fullKey, indents);
String inlineComment = getInlineComment(fullKey);
if (headerComments != null) writer.write(headerComments);
String[] splitFullKey = fullKey.split("[" + SEPARATOR + "]");
String trailingKey = splitFullKey[splitFullKey.length - 1];
if (currentValue instanceof ConfigurationSection) {
ConfigurationSection section = (ConfigurationSection) currentValue;
writer.write(indents + trailingKey + ":");
if (!((ConfigurationSection) currentValue).getKeys(false).isEmpty()) {
if (inlineComment != null && inlineComment.length() > 0) {
writer.write(" # " + inlineComment);
}
if (!section.getKeys(false).isEmpty()) {
writer.write("\n");
} else {
writer.write(" {}\n");
if (indents.length() == 0) writer.write("\n");
}
continue;
}
temp.set(trailingKey, currentValue);
String yaml = temp.saveToString();
yaml = yaml.substring(0, yaml.length() - 1).replace("\n", "\n" + indents);
String toWrite = indents + yaml + "\n";
temp.set(trailingKey, null);
writer.write(toWrite);
}
yaml = yaml.substring(0, yaml.length() - 1);
String endComment = buildComments("", null);
if (endComment != null) writer.write(endComment);
if (inlineComment != null && inlineComment.length() > 0) {
if (yaml.contains("\n")) {
// section为多行内容需要 InlineComment 加在首行末尾
String[] splitLine = yaml.split("\n", 2);
yaml = splitLine[0] + " # " + inlineComment + "\n" + splitLine[1];
} else {
// 其他情况下就直接加载后面就好
yaml += " # " + inlineComment;
}
}
writer.write(indents + yaml.replace("\n", "\n" + indents) + "\n");
if (indents.length() == 0) writer.write("\n");
}
writer.close();
}
@ -107,9 +141,8 @@ public class BukkitYAMLComments {
* @return 该键的缩进文本
*/
protected static String getIndents(String key) {
String[] splitKey = key.split("[" + SEPARATOR + "]");
String[] splitKey = key.split("[" + BukkitConfigProvider.SEPARATOR + "]");
return IntStream.range(1, splitKey.length).mapToObj(i -> " ").collect(Collectors.joining());
}
}

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>

View File

@ -1,6 +1,7 @@
package cc.carm.lib.configuration;
import cc.carm.lib.configuration.bungee.source.BungeeConfigProvider;
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;
@ -37,6 +38,14 @@ public class MineConfiguration {
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));
}
@ -53,6 +62,13 @@ public class MineConfiguration {
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));
@ -70,5 +86,12 @@ public class MineConfiguration {
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);
}
}

View File

@ -3,21 +3,23 @@ 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;
import java.util.List;
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 BungeeConfigValue(@Nullable BungeeConfigProvider provider, @Nullable String configPath,
@Nullable List<String> headerComments, @Nullable String inlineComments,
@Nullable T defaultValue) {
super(provider, configPath, headerComments, inlineComments, defaultValue);
}
public BungeeConfigProvider getBukkitProvider() {

View File

@ -3,6 +3,7 @@ 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 cc.carm.lib.configuration.common.utils.ParamsUtils;
import net.md_5.bungee.api.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -26,9 +27,10 @@ public class BungeeMessageListBuilder<M>
@Override
public @NotNull ConfiguredMessageList<M> build() {
return new ConfiguredMessageList<>(
this.provider, this.path, buildComments(),
this.provider, this.path, this.headerComments, this.inlineComment,
Optional.ofNullable(this.defaultValue).orElse(MessageText.of(new ArrayList<>())),
buildParams(), this.messageParser, this.sendFunction
ParamsUtils.formatParams(this.paramFormatter, this.params),
this.messageParser, this.sendFunction
);
}
}

View File

@ -3,6 +3,7 @@ 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 cc.carm.lib.configuration.common.utils.ParamsUtils;
import net.md_5.bungee.api.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -25,9 +26,10 @@ public class BungeeMessageValueBuilder<M>
@Override
public @NotNull ConfiguredMessage<M> build() {
return new ConfiguredMessage<>(
this.provider, this.path, buildComments(),
this.provider, this.path, this.headerComments, this.inlineComment,
Optional.ofNullable(this.defaultValue).orElse(MessageText.of("")),
buildParams(), this.messageParser, this.sendHandler
ParamsUtils.formatParams(this.paramFormatter, this.params),
this.messageParser, this.sendHandler
);
}

View File

@ -1,15 +1,16 @@
package cc.carm.lib.configuration.bungee.source;
import cc.carm.lib.configuration.core.ConfigInitializer;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import cc.carm.lib.configuration.core.source.impl.FileConfigProvider;
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 org.jetbrains.annotations.Unmodifiable;
import java.io.File;
import java.io.IOException;
import java.util.List;
public class BungeeConfigProvider extends FileConfigProvider<BungeeSectionWrapper> {
@ -43,13 +44,23 @@ public class BungeeConfigProvider extends FileConfigProvider<BungeeSectionWrappe
}
@Override
public void setComment(@Nullable String path, @Nullable ConfigCommentInfo comment) {
// BungeeCord version doesn't support comments
public void setHeaderComment(@Nullable String path, @Nullable List<String> comments) {
}
@Override
public @Nullable ConfigCommentInfo getComment(@Nullable String path) {
return null; // BungeeCord version doesn't support comments
public void setInlineComment(@NotNull String path, @Nullable String comment) {
}
@Override
public @Nullable @Unmodifiable List<String> getHeaderComment(@Nullable String path) {
return null;
}
@Override
public @Nullable String getInlineComment(@NotNull String path) {
return null;
}
@Override

View File

@ -4,13 +4,13 @@ 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.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
@ -34,15 +34,15 @@ public class ConfiguredMessage<M> extends ConfigMessage<M, MessageText, CommandS
return asString().defaults(defaultMessage).build();
}
public ConfiguredMessage(@Nullable ConfigurationProvider<?> provider,
@Nullable String sectionPath, @Nullable ConfigCommentInfo comments,
public ConfiguredMessage(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
@Nullable List<String> headerComments, @Nullable String inlineComments,
@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);
super(provider, sectionPath, headerComments, inlineComments, MessageText.class, defaultMessage, params, messageParser, sendFunction, MessageText::of);
}
@Override
public void broadcast(@NotNull Map<String, Object> placeholders) {
ProxyServer.getInstance().getPlayers().forEach(pl -> send(pl, placeholders));
send(ProxyServer.getInstance().getConsole(), placeholders);

View File

@ -4,7 +4,6 @@ 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;
@ -31,12 +30,12 @@ public class ConfiguredMessageList<M> extends ConfigMessageList<M, MessageText,
return asStrings().defaults(defaultMessages).build();
}
public ConfiguredMessageList(@Nullable ConfigurationProvider<?> provider,
@Nullable String sectionPath, @Nullable ConfigCommentInfo comments,
public ConfiguredMessageList(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
@Nullable List<String> headerComments, @Nullable String inlineComments,
@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);
super(provider, sectionPath, headerComments, inlineComments, MessageText.class, messages, params, messageParser, sendFunction, MessageText::of);
}
public void broadcast(@NotNull Map<String, Object> placeholders) {

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>

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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));
}

View File

@ -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", ""));
}
}

View File

@ -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;
}
}

View File

@ -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);

View File

@ -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);

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>
@ -45,6 +45,13 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.comphenix.protocol</groupId>
<artifactId>ProtocolLib</artifactId>
<version>4.8.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>

View File

@ -1,23 +1,25 @@
package cc.carm.lib.configuration.craft;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
import cc.carm.lib.configuration.craft.builder.CraftConfigBuilder;
import cc.carm.lib.configuration.craft.source.CraftConfigProvider;
import cc.carm.lib.configuration.craft.source.CraftSectionWrapper;
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;
import java.util.List;
public abstract class CraftConfigValue<T> extends CachedConfigValue<T> {
public static @NotNull CraftConfigBuilder builder() {
return new CraftConfigBuilder();
}
public CraftConfigValue(@Nullable CraftConfigProvider provider,
@Nullable String configPath, @Nullable ConfigCommentInfo comments, @Nullable T defaultValue) {
super(provider, configPath, comments, defaultValue);
public CraftConfigValue(@Nullable CraftConfigProvider provider, @Nullable String sectionPath,
@Nullable List<String> headerComments, @Nullable String inlineComments,
@Nullable T defaultValue) {
super(provider, sectionPath, headerComments, inlineComments, defaultValue);
}
public CraftConfigProvider getBukkitProvider() {

View File

@ -5,6 +5,7 @@ import cc.carm.lib.configuration.craft.builder.item.ItemConfigBuilder;
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.builder.title.TitleConfigBuilder;
import cc.carm.lib.configuration.craft.data.ItemConfig;
import cc.carm.lib.configuration.craft.value.ConfiguredItem;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
@ -25,6 +26,10 @@ public class CraftConfigBuilder extends ConfigBuilder {
return new CraftMessageBuilder();
}
public @NotNull TitleConfigBuilder createTitle() {
return new TitleConfigBuilder();
}
public <V extends ConfigurationSerializable> @NotNull SerializableBuilder<V> ofSerializable(@NotNull Class<V> valueClass) {
return new SerializableBuilder<>(valueClass);
}

View File

@ -1,5 +1,6 @@
package cc.carm.lib.configuration.craft.builder.item;
import cc.carm.lib.configuration.common.utils.ParamsUtils;
import cc.carm.lib.configuration.craft.builder.AbstractCraftBuilder;
import cc.carm.lib.configuration.craft.data.ItemConfig;
import cc.carm.lib.configuration.craft.value.ConfiguredItem;
@ -11,6 +12,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
public class ItemConfigBuilder extends AbstractCraftBuilder<ItemConfig, ItemConfigBuilder> {
@ -19,6 +21,9 @@ public class ItemConfigBuilder extends AbstractCraftBuilder<ItemConfig, ItemConf
protected String name;
protected List<String> lore = new ArrayList<>();
protected @NotNull String[] params;
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));
@ -58,6 +63,21 @@ public class ItemConfigBuilder extends AbstractCraftBuilder<ItemConfig, ItemConf
return this;
}
public ItemConfigBuilder formatParam(@NotNull Function<@NotNull String, @NotNull 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;
@ -71,7 +91,11 @@ public class ItemConfigBuilder extends AbstractCraftBuilder<ItemConfig, ItemConf
@Override
public @NotNull ConfiguredItem build() {
ItemConfig defaultItem = Optional.ofNullable(this.defaultValue).orElse(buildDefault());
return new ConfiguredItem(this.provider, this.path, buildComments(), defaultItem);
return new ConfiguredItem(this.provider, this.path, this.headerComments, this.inlineComment, defaultItem, buildParams());
}
protected final String[] buildParams() {
return Arrays.stream(params).map(param -> paramFormatter.apply(param)).toArray(String[]::new);
}
}

View File

@ -1,22 +1,20 @@
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 cc.carm.lib.configuration.craft.data.TextConfig;
import cc.carm.lib.configuration.craft.utils.TextParser;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
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, MessageText> {
public class CraftMessageBuilder extends MessageConfigBuilder<CommandSender, TextConfig> {
public CraftMessageBuilder() {
super(CommandSender.class, MessageText.class);
super(CommandSender.class, TextConfig.class);
}
@Override
@ -40,16 +38,7 @@ public class CraftMessageBuilder extends MessageConfigBuilder<CommandSender, Mes
}
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;
return (receiver, message) -> TextParser.parseText(receiver, message, new HashMap<>());
}
}

View File

@ -1,7 +1,8 @@
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.common.utils.ParamsUtils;
import cc.carm.lib.configuration.craft.data.TextConfig;
import cc.carm.lib.configuration.craft.value.ConfiguredMessageList;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
@ -12,10 +13,10 @@ import java.util.Optional;
import java.util.function.BiFunction;
public class CraftMessageListBuilder<M>
extends MessageListBuilder<M, CommandSender, MessageText, CraftMessageListBuilder<M>> {
extends MessageListBuilder<M, CommandSender, TextConfig, CraftMessageListBuilder<M>> {
public CraftMessageListBuilder(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
super(CommandSender.class, MessageText::of, parser);
super(CommandSender.class, TextConfig::of, parser);
}
@Override
@ -26,9 +27,10 @@ public class CraftMessageListBuilder<M>
@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
this.provider, this.path, this.headerComments, this.inlineComment,
Optional.ofNullable(this.defaultValue).orElse(TextConfig.of(new ArrayList<>())),
ParamsUtils.formatParams(this.paramFormatter, this.params),
this.messageParser, this.sendFunction
);
}
}

View File

@ -1,7 +1,8 @@
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.common.utils.ParamsUtils;
import cc.carm.lib.configuration.craft.data.TextConfig;
import cc.carm.lib.configuration.craft.value.ConfiguredMessage;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
@ -11,10 +12,10 @@ import java.util.Optional;
import java.util.function.BiFunction;
public class CraftMessageValueBuilder<M>
extends MessageValueBuilder<M, CommandSender, MessageText, CraftMessageValueBuilder<M>> {
extends MessageValueBuilder<M, CommandSender, TextConfig, CraftMessageValueBuilder<M>> {
public CraftMessageValueBuilder(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
super(CommandSender.class, MessageText::new, parser);
super(CommandSender.class, TextConfig::new, parser);
}
@Override
@ -25,9 +26,10 @@ public class CraftMessageValueBuilder<M>
@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
this.provider, this.path, this.headerComments, this.inlineComment,
Optional.ofNullable(this.defaultValue).orElse(TextConfig.of("")),
ParamsUtils.formatParams(this.paramFormatter, this.params),
this.messageParser, this.sendHandler
);
}

View File

@ -21,7 +21,7 @@ public class SerializableBuilder<T extends ConfigurationSerializable>
@Override
public @NotNull ConfiguredSerializable<T> build() {
return new ConfiguredSerializable<>(this.provider, this.path, buildComments(), this.valueClass, this.defaultValue);
return new ConfiguredSerializable<>(this.provider, this.path, this.headerComments, this.inlineComment, this.valueClass, this.defaultValue);
}
}

View File

@ -10,7 +10,7 @@ public class SoundConfigBuilder extends AbstractCraftBuilder<SoundConfig, SoundC
public @NotNull SoundConfigBuilder defaults(@NotNull Sound sound, float volume, float pitch) {
return defaults(new SoundConfig(sound, volume, pitch));
return defaults(new SoundConfig(sound.name(), sound, volume, pitch));
}
public @NotNull SoundConfigBuilder defaults(@NotNull Sound sound, float volume) {
@ -21,6 +21,18 @@ public class SoundConfigBuilder extends AbstractCraftBuilder<SoundConfig, SoundC
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;
@ -28,7 +40,7 @@ public class SoundConfigBuilder extends AbstractCraftBuilder<SoundConfig, SoundC
@Override
public @NotNull ConfiguredSound build() {
return new ConfiguredSound(this.provider, this.path, buildComments(), this.defaultValue);
return new ConfiguredSound(this.provider, this.path, this.headerComments, this.inlineComment, this.defaultValue);
}
}

View File

@ -0,0 +1,97 @@
package cc.carm.lib.configuration.craft.builder.title;
import cc.carm.lib.configuration.common.utils.ParamsUtils;
import cc.carm.lib.configuration.craft.builder.AbstractCraftBuilder;
import cc.carm.lib.configuration.craft.data.TitleConfig;
import cc.carm.lib.configuration.craft.function.TitleSendConsumer;
import cc.carm.lib.configuration.craft.utils.ProtocolLibHelper;
import cc.carm.lib.configuration.craft.value.ConfiguredTitle;
import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Range;
import java.util.List;
import java.util.function.Function;
public class TitleConfigBuilder extends AbstractCraftBuilder<TitleConfig, TitleConfigBuilder> {
@SuppressWarnings("deprecation")
protected static @NotNull TitleSendConsumer DEFAULT_TITLE_CONSUMER = (player, fadeIn, stay, fadeOut, line1, line2) -> {
if (Bukkit.getPluginManager().isPluginEnabled("ProtocolLib")) {
try {
ProtocolLibHelper.sendTitle(player, fadeIn, stay, fadeOut, line1, line2);
} catch (Exception ignored) {
}
} else {
player.sendTitle(line1, line2);
}
};
protected @NotNull String[] params;
protected @Range(from = 0L, to = Integer.MAX_VALUE) long fadeIn = 10;
protected @Range(from = 0L, to = Integer.MAX_VALUE) long stay = 60;
protected @Range(from = 0L, to = Integer.MAX_VALUE) long fadeOut = 10;
protected @NotNull TitleSendConsumer sendConsumer;
protected @NotNull Function<@NotNull String, @NotNull String> paramFormatter;
public TitleConfigBuilder() {
this.sendConsumer = TitleConfigBuilder.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 TitleSendConsumer 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) long fadeIn) {
this.fadeIn = fadeIn;
return this;
}
public TitleConfigBuilder stay(@Range(from = 0L, to = Integer.MAX_VALUE) long stay) {
this.stay = stay;
return this;
}
public TitleConfigBuilder fadeOut(@Range(from = 0L, to = Integer.MAX_VALUE) long fadeOut) {
this.fadeOut = fadeOut;
return this;
}
public TitleConfigBuilder formatParam(Function<String, String> paramFormatter) {
this.paramFormatter = paramFormatter;
return this;
}
@Override
protected @NotNull TitleConfigBuilder getThis() {
return this;
}
@Override
public @NotNull ConfiguredTitle build() {
return new ConfiguredTitle(
this.provider, this.path, this.headerComments, this.inlineComment,
this.defaultValue, this.params, this.sendConsumer,
this.fadeIn, this.stay, this.fadeOut
);
}
}

View File

@ -1,9 +1,7 @@
package cc.carm.lib.configuration.craft.data;
import cc.carm.lib.configuration.common.utils.ColorParser;
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
import cc.carm.lib.configuration.craft.utils.PAPIHelper;
import org.bukkit.Bukkit;
import cc.carm.lib.configuration.craft.utils.TextParser;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
@ -11,11 +9,7 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.*;
public class ItemConfig {
@ -44,28 +38,43 @@ public class ItemConfig {
return name;
}
public @Nullable String getName(@Nullable Player player, @NotNull Map<String, Object> placeholders) {
return Optional.ofNullable(getName())
.map(name -> TextParser.parseText(player, name, placeholders))
.orElse(null);
}
public @NotNull List<String> getLore() {
return lore;
}
public @Nullable List<String> getLore(@Nullable Player player, @NotNull Map<String, Object> placeholders) {
if (getLore().isEmpty()) return null;
else return TextParser.parseList(player, getLore(), placeholders);
}
public final @NotNull ItemStack getItemStack() {
return getItemStack(1);
}
public @NotNull ItemStack getItemStack(int amount) {
return getItemStack(null, amount);
return getItemStack(null, amount, new HashMap<>());
}
public @NotNull ItemStack getItemStack(@Nullable Player player) {
return getItemStack(player, 1);
return getItemStack(player, new HashMap<>());
}
public @NotNull ItemStack getItemStack(@Nullable Player player, int amount) {
public @NotNull ItemStack getItemStack(@Nullable Player player, @NotNull Map<String, Object> placeholders) {
return getItemStack(player, 1, placeholders);
}
public @NotNull ItemStack getItemStack(@Nullable Player player, int amount, @NotNull Map<String, Object> placeholders) {
ItemStack item = new ItemStack(type, amount, data);
ItemMeta meta = item.getItemMeta();
if (meta == null) return item;
if (getName() != null) meta.setDisplayName(parseName(player, getName()));
if (!getLore().isEmpty()) meta.setLore(parseLore(player, getLore()));
Optional.ofNullable(getName(player, placeholders)).ifPresent(meta::setDisplayName);
Optional.ofNullable(getLore(player, placeholders)).ifPresent(meta::setLore);
item.setItemMeta(meta);
return item;
}
@ -89,31 +98,8 @@ public class ItemConfig {
else return new ItemConfig(
type, section.getShort("data", (short) 0),
section.getString("name"),
parseStringList(section.getList("lore"))
section.getStringList("lore")
);
}
private static List<String> parseStringList(@Nullable List<?> data) {
if (data == null) return new ArrayList<>();
else return data.stream()
.map(o -> o instanceof String ? (String) o : o.toString())
.collect(Collectors.toList());
}
protected static @NotNull String parseName(@Nullable Player player, String message) {
if (player != null && hasPlaceholderAPI()) message = PAPIHelper.parseMessages(player, message);
return ColorParser.parse(message);
}
protected static @NotNull List<String> parseLore(@Nullable Player player, List<String> messages) {
if (player != null && hasPlaceholderAPI()) messages = PAPIHelper.parseMessages(player, messages);
return ColorParser.parse(messages);
}
public static boolean hasPlaceholderAPI() {
return Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null;
}
}

View File

@ -7,27 +7,36 @@ import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
public class SoundConfig {
protected @NotNull Sound type;
protected @NotNull String typeName;
protected @Nullable Sound type;
protected float volume;
protected float pitch;
public SoundConfig(@NotNull Sound type) {
this(type, 1, 1);
public SoundConfig(@NotNull String typeName) {
this(typeName, 1, 1);
}
public SoundConfig(@NotNull Sound type, float volume) {
this(type, volume, 1);
public SoundConfig(@NotNull String typeName, float volume) {
this(typeName, volume, 1);
}
public SoundConfig(@NotNull Sound type, float volume, float pitch) {
public SoundConfig(@NotNull String typeName, float volume, float pitch) {
this(typeName, Arrays.stream(Sound.values()).filter(s -> s.name().equalsIgnoreCase(typeName)).findFirst().orElse(null), volume, pitch);
}
public SoundConfig(@NotNull String typeName, @Nullable Sound type, float volume, float pitch) {
this.typeName = typeName;
this.type = type;
this.volume = volume;
this.pitch = pitch;
}
public void playTo(Player player) {
if (type == null) return;
player.playSound(player.getLocation(), type, volume, pitch);
}
@ -35,7 +44,11 @@ public class SoundConfig {
Bukkit.getOnlinePlayers().forEach(this::playTo);
}
public @NotNull Sound getType() {
public @NotNull String getTypeName() {
return typeName;
}
public @Nullable Sound getType() {
return type;
}
@ -48,6 +61,7 @@ public class SoundConfig {
}
public void setType(@NotNull Sound type) {
this.typeName = type.name();
this.type = type;
}
@ -61,11 +75,11 @@ public class SoundConfig {
public @NotNull String serialize() {
if (pitch != 1) {
return type.name() + ":" + volume + ":" + pitch;
return typeName + ":" + volume + ":" + pitch;
} else if (volume != 1) {
return type.name() + ":" + volume;
return typeName + ":" + volume;
} else {
return type.name();
return typeName;
}
}
@ -78,6 +92,7 @@ public class SoundConfig {
try {
return new SoundConfig(
args[0],
Sound.valueOf(args[0]),
(args.length >= 2) ? Float.parseFloat(args[1]) : 1,
(args.length >= 3) ? Float.parseFloat(args[2]) : 1

View File

@ -11,24 +11,24 @@ import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class MessageText extends AbstractText<CommandSender> {
public class TextConfig extends AbstractText<CommandSender> {
public MessageText(@NotNull String message) {
public TextConfig(@NotNull String message) {
super(CommandSender.class, message);
}
@Contract("!null,-> !null")
public static @Nullable MessageText of(@Nullable String message) {
public static @Nullable TextConfig of(@Nullable String message) {
if (message == null) return null;
else return new MessageText(message);
else return new TextConfig(message);
}
public static @NotNull List<MessageText> of(@Nullable List<String> messages) {
public static @NotNull List<TextConfig> of(@Nullable List<String> messages) {
if (messages == null || messages.isEmpty()) return new ArrayList<>();
else return messages.stream().map(MessageText::of).collect(Collectors.toList());
else return messages.stream().map(TextConfig::of).collect(Collectors.toList());
}
public static @NotNull List<MessageText> of(@NotNull String... messages) {
public static @NotNull List<TextConfig> of(@NotNull String... messages) {
return of(Arrays.asList(messages));
}

View File

@ -0,0 +1,68 @@
package cc.carm.lib.configuration.craft.data;
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
import cc.carm.lib.configuration.craft.function.TitleSendConsumer;
import cc.carm.lib.configuration.craft.utils.TextParser;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Range;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
public class TitleConfig {
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);
}
protected @Nullable TextConfig line1;
protected @Nullable TextConfig line2;
protected TitleConfig(@Nullable TextConfig line1, @Nullable TextConfig line2) {
this.line1 = line1;
this.line2 = line2;
}
public void send(@NotNull Player player,
@Range(from = 0L, to = Long.MAX_VALUE) long fadeIn,
@Range(from = 0L, to = Long.MAX_VALUE) long stay,
@Range(from = 0L, to = Long.MAX_VALUE) long fadeOut,
@NotNull Map<String, Object> placeholders,
@Nullable TitleSendConsumer sendConsumer) {
if (this.line1 == null && this.line2 == null) return;
if (sendConsumer == null) return;
sendConsumer.send(
player, fadeIn, stay, fadeOut,
parseLine(this.line1, player, placeholders),
parseLine(this.line2, player, placeholders)
);
}
protected @NotNull String parseLine(@Nullable TextConfig text,
@NotNull Player player, @NotNull Map<String, Object> placeholders) {
if (text == null) return "";
else return TextParser.parseText(player, text.getMessage(), placeholders);
}
public @NotNull Map<String, Object> serialize() {
Map<String, Object> map = new LinkedHashMap<>();
if (this.line1 != null) map.put("line1", this.line1.getMessage());
if (this.line2 != null) map.put("line2", this.line2.getMessage());
return map;
}
public static @NotNull TitleConfig deserialize(@NotNull ConfigurationWrapper section) {
return of(section.getString("line1"), section.getString("line2"));
}
}

View File

@ -0,0 +1,26 @@
package cc.carm.lib.configuration.craft.function;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Range;
@FunctionalInterface
public interface TitleSendConsumer {
/**
* 向目标玩家发送标题文字
*
* @param player 目标玩家
* @param fadeIn 淡入时间 (ticks)
* @param stay 保留时间 (ticks)
* @param fadeOut 淡出时间 (ticks)
* @param line1 第一行文字
* @param line2 第二行文字
*/
void send(@NotNull Player player,
@Range(from = 0L, to = Integer.MAX_VALUE) long fadeIn,
@Range(from = 0L, to = Integer.MAX_VALUE) long stay,
@Range(from = 0L, to = Integer.MAX_VALUE) long fadeOut,
@NotNull String line1, @NotNull String line2);
}

View File

@ -1,49 +0,0 @@
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 <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

@ -1,19 +0,0 @@
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

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

View File

@ -0,0 +1,41 @@
package cc.carm.lib.configuration.craft.utils;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.EnumWrappers;
import com.comphenix.protocol.wrappers.WrappedChatComponent;
import org.bukkit.entity.Player;
public class ProtocolLibHelper {
@SuppressWarnings("deprecation")
public static void sendTitle(Player player, long fadeIn, long stay, long fadeOut, String line1, String line2) throws Exception {
ProtocolManager pm = ProtocolLibrary.getProtocolManager();
if (line1 != null) {
PacketContainer packet = pm.createPacket(PacketType.Play.Server.TITLE);
packet.getTitleActions().write(0, EnumWrappers.TitleAction.TITLE);
packet.getChatComponents().write(0, WrappedChatComponent.fromText(line1));
pm.sendServerPacket(player, packet, false);
}
if (line2 != null) {
PacketContainer packet = pm.createPacket(PacketType.Play.Server.TITLE);
packet.getTitleActions().write(0, EnumWrappers.TitleAction.SUBTITLE);
packet.getChatComponents().write(0, WrappedChatComponent.fromText(line2));
pm.sendServerPacket(player, packet, false);
}
PacketContainer timePacket = pm.createPacket(PacketType.Play.Server.TITLE);
timePacket.getTitleActions().write(0, EnumWrappers.TitleAction.TIMES);
timePacket.getIntegers()
.write(0, Math.toIntExact(fadeIn))
.write(1, Math.toIntExact(stay))
.write(2, Math.toIntExact(fadeOut));
pm.sendServerPacket(player, timePacket, false);
}
}

View File

@ -0,0 +1,38 @@
package cc.carm.lib.configuration.craft.utils;
import cc.carm.lib.configuration.common.utils.ColorParser;
import cc.carm.lib.configuration.common.utils.ParamsUtils;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class TextParser {
@Contract("_,!null,_->!null")
public static @Nullable String parseText(@Nullable CommandSender sender, @Nullable String message, @NotNull Map<String, Object> placeholders) {
if (message == null) return null;
if (sender instanceof Player && hasPlaceholderAPI()) {
message = PlaceholderAPIHelper.parseMessages((Player) sender, message);
}
return ColorParser.parse(ParamsUtils.setPlaceholders(message, placeholders));
}
public static @NotNull List<String> parseList(@Nullable CommandSender sender, List<String> messages, @NotNull Map<String, Object> placeholders) {
if (sender instanceof Player && hasPlaceholderAPI()) {
messages = PlaceholderAPIHelper.parseMessages((Player) sender, messages);
}
return ColorParser.parse(messages.stream().map(s -> ParamsUtils.setPlaceholders(s, placeholders)).collect(Collectors.toList()));
}
public static boolean hasPlaceholderAPI() {
return Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null;
}
}

View File

@ -1,15 +1,22 @@
package cc.carm.lib.configuration.craft.value;
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.source.ConfigurationWrapper;
import cc.carm.lib.configuration.core.value.type.ConfiguredSection;
import cc.carm.lib.configuration.craft.CraftConfigValue;
import cc.carm.lib.configuration.craft.builder.item.ItemConfigBuilder;
import cc.carm.lib.configuration.craft.data.ItemConfig;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ConfiguredItem extends ConfiguredSection<ItemConfig> {
public static ItemConfigBuilder create() {
@ -24,14 +31,45 @@ public class ConfiguredItem extends ConfiguredSection<ItemConfig> {
return CraftConfigValue.builder().ofItem(defaultItem);
}
public ConfiguredItem(@Nullable ConfigurationProvider<?> provider,
@Nullable String sectionPath, @Nullable ConfigCommentInfo comments,
@Nullable ItemConfig defaultValue) {
super(provider, sectionPath, comments, ItemConfig.class, defaultValue, getItemParser(), ItemConfig::serialize);
protected final @NotNull String[] params;
public ConfiguredItem(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
@Nullable List<String> headerComments, @Nullable String inlineComments,
@Nullable ItemConfig defaultValue, @NotNull String[] params) {
super(provider, sectionPath, headerComments, inlineComments, ItemConfig.class, defaultValue, getItemParser(), ItemConfig::serialize);
this.params = params;
}
public static ConfigValueParser<ConfigurationWrapper, ItemConfig> getItemParser() {
return (s, d) -> ItemConfig.deserialize(s);
}
public @NotNull String[] getParams() {
return params;
}
public @Nullable ItemStack getItem(@Nullable Player player) {
return getItem(player, 1);
}
public @Nullable ItemStack getItem(@Nullable Player player, int amount) {
return getItem(player, amount, new HashMap<>());
}
public @Nullable ItemStack getItem(@Nullable Player player, @NotNull Object... values) {
return getItem(player, 1, values);
}
public @Nullable ItemStack getItem(@Nullable Player player, int amount, @NotNull Object... values) {
return getItem(player, amount, ParamsUtils.buildParams(params, values));
}
public @Nullable ItemStack getItem(@Nullable Player player, int amount, @NotNull String[] params, @NotNull Object[] values) {
return getItem(player, amount, ParamsUtils.buildParams(params, values));
}
public @Nullable ItemStack getItem(@Nullable Player player, int amount, @NotNull Map<String, Object> placeholders) {
return getOptional().map(item -> item.getItemStack(player, amount, placeholders)).orElse(null);
}
}

View File

@ -1,21 +1,21 @@
package cc.carm.lib.configuration.craft.value;
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.craft.CraftConfigValue;
import cc.carm.lib.configuration.craft.builder.message.CraftMessageValueBuilder;
import cc.carm.lib.configuration.craft.data.MessageText;
import cc.carm.lib.configuration.craft.data.TextConfig;
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;
public class ConfiguredMessage<M> extends ConfigMessage<M, MessageText, CommandSender> {
public class ConfiguredMessage<M> extends ConfigMessage<M, TextConfig, CommandSender> {
@NotNull
public static <M> CraftMessageValueBuilder<@Nullable M> create(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser) {
@ -34,15 +34,15 @@ public class ConfiguredMessage<M> extends ConfigMessage<M, MessageText, CommandS
return asString().defaults(defaultMessage).build();
}
public ConfiguredMessage(@Nullable ConfigurationProvider<?> provider,
@Nullable String sectionPath, @Nullable ConfigCommentInfo comments,
@NotNull MessageText defaultMessage, @NotNull String[] params,
public ConfiguredMessage(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
@Nullable List<String> headerComments, @Nullable String inlineComments,
@NotNull TextConfig 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);
super(provider, sectionPath, headerComments, inlineComments, TextConfig.class, defaultMessage, params, messageParser, sendFunction, TextConfig::of);
}
@Override
public void broadcast(@NotNull Map<String, Object> placeholders) {
Bukkit.getOnlinePlayers().forEach(pl -> send(pl, placeholders));
send(Bukkit.getConsoleSender(), placeholders);

View File

@ -1,11 +1,10 @@
package cc.carm.lib.configuration.craft.value;
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.craft.CraftConfigValue;
import cc.carm.lib.configuration.craft.builder.message.CraftMessageListBuilder;
import cc.carm.lib.configuration.craft.data.MessageText;
import cc.carm.lib.configuration.craft.data.TextConfig;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
@ -16,7 +15,7 @@ import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
public class ConfiguredMessageList<M> extends ConfigMessageList<M, MessageText, CommandSender> {
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) {
@ -31,12 +30,12 @@ public class ConfiguredMessageList<M> extends ConfigMessageList<M, MessageText,
return asStrings().defaults(defaultMessages).build();
}
public ConfiguredMessageList(@Nullable ConfigurationProvider<?> provider,
@Nullable String sectionPath, @Nullable ConfigCommentInfo comments,
@NotNull List<MessageText> messages, @NotNull String[] params,
public ConfiguredMessageList(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
@Nullable List<String> headerComments, @Nullable String inlineComments,
@NotNull List<TextConfig> 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);
super(provider, sectionPath, headerComments, inlineComments, TextConfig.class, messages, params, messageParser, sendFunction, TextConfig::of);
}
public void broadcast(@NotNull Map<String, Object> placeholders) {

View File

@ -2,11 +2,11 @@ package cc.carm.lib.configuration.craft.value;
import cc.carm.lib.configuration.craft.CraftConfigValue;
import cc.carm.lib.configuration.craft.source.CraftConfigProvider;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Optional;
public class ConfiguredSerializable<T extends ConfigurationSerializable> extends CraftConfigValue<T> {
@ -22,10 +22,10 @@ public class ConfiguredSerializable<T extends ConfigurationSerializable> extends
protected final @NotNull Class<T> valueClass;
public ConfiguredSerializable(@Nullable CraftConfigProvider provider,
@Nullable String configPath, @Nullable ConfigCommentInfo comments,
public ConfiguredSerializable(@Nullable CraftConfigProvider provider, @Nullable String sectionPath,
@Nullable List<String> headerComments, @Nullable String inlineComments,
@NotNull Class<T> valueClass, @Nullable T defaultValue) {
super(provider, configPath, comments, defaultValue);
super(provider, sectionPath, headerComments, inlineComments, defaultValue);
this.valueClass = valueClass;
}

View File

@ -1,7 +1,6 @@
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;
@ -12,6 +11,7 @@ import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Optional;
public class ConfiguredSound extends ConfiguredValue<SoundConfig> {
@ -32,10 +32,22 @@ public class ConfiguredSound extends ConfiguredValue<SoundConfig> {
return CraftConfigValue.builder().createSound().defaults(sound, volume, pitch).build();
}
public ConfiguredSound(@Nullable ConfigurationProvider<?> provider,
@Nullable String sectionPath, @Nullable ConfigCommentInfo comments,
public static @NotNull ConfiguredSound of(String soundName) {
return CraftConfigValue.builder().createSound().defaults(soundName).build();
}
public static @NotNull ConfiguredSound of(String soundName, float volume) {
return CraftConfigValue.builder().createSound().defaults(soundName, volume).build();
}
public static @NotNull ConfiguredSound of(String soundName, float volume, float pitch) {
return CraftConfigValue.builder().createSound().defaults(soundName, volume, pitch).build();
}
public ConfiguredSound(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
@Nullable List<String> headerComments, @Nullable String inlineComments,
@Nullable SoundConfig defaultValue) {
super(provider, sectionPath, comments, SoundConfig.class, defaultValue, getSoundParser(), SoundConfig::serialize);
super(provider, sectionPath, headerComments, inlineComments, SoundConfig.class, defaultValue, getSoundParser(), SoundConfig::serialize);
}
public void setSound(@NotNull Sound sound) {
@ -47,7 +59,7 @@ public class ConfiguredSound extends ConfiguredValue<SoundConfig> {
}
public void setSound(@NotNull Sound sound, float volume, float pitch) {
set(new SoundConfig(sound, volume, pitch));
set(new SoundConfig(sound.name(), sound, volume, pitch));
}
public void playTo(@NotNull Player player) {

View File

@ -0,0 +1,109 @@
package cc.carm.lib.configuration.craft.value;
import cc.carm.lib.configuration.common.utils.ParamsUtils;
import cc.carm.lib.configuration.core.function.ConfigValueParser;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
import cc.carm.lib.configuration.core.value.type.ConfiguredSection;
import cc.carm.lib.configuration.craft.CraftConfigValue;
import cc.carm.lib.configuration.craft.builder.title.TitleConfigBuilder;
import cc.carm.lib.configuration.craft.data.TitleConfig;
import cc.carm.lib.configuration.craft.function.TitleSendConsumer;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Range;
import java.util.List;
import java.util.Map;
public class ConfiguredTitle extends ConfiguredSection<TitleConfig> {
public static TitleConfigBuilder create() {
return CraftConfigValue.builder().createTitle();
}
public static ConfiguredTitle of(@Nullable String line1, @Nullable String line2) {
return create().defaults(line1, line2).build();
}
public static ConfiguredTitle of(@Nullable String line1, @Nullable String line2,
long fadeIn, long stay, long fadeOut) {
return create().defaults(line1, line2).fadeIn(fadeIn).stay(stay).fadeOut(fadeOut).build();
}
protected final @NotNull TitleSendConsumer sendConsumer;
protected final @NotNull String[] params;
protected final long fadeIn;
protected final long stay;
protected final long fadeOut;
public ConfiguredTitle(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
@Nullable List<String> headerComments, @Nullable String inlineComments,
@Nullable TitleConfig defaultValue, @NotNull String[] params,
@NotNull TitleSendConsumer sendConsumer,
long fadeIn, long stay, long fadeOut) {
super(provider, sectionPath, headerComments, inlineComments, TitleConfig.class, defaultValue, getTitleParser(), TitleConfig::serialize);
this.sendConsumer = sendConsumer;
this.params = params;
this.fadeIn = fadeIn;
this.stay = stay;
this.fadeOut = fadeOut;
}
@Range(from = 0L, to = Long.MAX_VALUE)
public long getFadeInTicks() {
return fadeIn;
}
@Range(from = 0L, to = Long.MAX_VALUE)
public long getStayTicks() {
return stay;
}
@Range(from = 0L, to = Long.MAX_VALUE)
public long getFadeOutTicks() {
return fadeOut;
}
public @NotNull TitleSendConsumer getSendConsumer() {
return sendConsumer;
}
public void send(@NotNull Player player, Object... values) {
send(player, this.params, values);
}
public void send(@NotNull Player player, @NotNull String[] params, @NotNull Object[] values) {
send(player, ParamsUtils.buildParams(params, values));
}
public void send(@NotNull Player player, @NotNull Map<String, Object> placeholders) {
TitleConfig config = get();
if (config != null) {
config.send(player, fadeIn, stay, fadeOut, placeholders, sendConsumer);
}
}
public void sendAll(Object... values) {
sendAll(this.params, values);
}
public void sendAll(@NotNull String[] params, @NotNull Object[] values) {
sendAll(ParamsUtils.buildParams(params, values));
}
public void sendAll(@NotNull Map<String, Object> placeholders) {
TitleConfig config = get();
if (config == null) return;
Bukkit.getOnlinePlayers().forEach(onlinePlayer -> send(onlinePlayer, placeholders));
}
public static ConfigValueParser<ConfigurationWrapper, TitleConfig> getTitleParser() {
return (s, d) -> TitleConfig.deserialize(s);
}
}

12
pom.xml
View File

@ -18,11 +18,11 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<easyconfiguration.version>2.3.0</easyconfiguration.version>
<easyconfiguration.version>3.0.0</easyconfiguration.version>
</properties>
<groupId>cc.carm.lib</groupId>
<artifactId>mineconfiguration-parent</artifactId>
<version>1.2.2</version>
<version>1.3.0</version>
<packaging>pom</packaging>
<name>MineConfiguration</name>
@ -86,12 +86,6 @@
<url>https://repo1.maven.org/maven2/</url>
</repository>
<repository>
<id>github</id>
<name>GitHub Packages</name>
<url>https://maven.pkg.github.com/CarmJos/*</url>
</repository>
</repositories>
<distributionManagement>
@ -295,7 +289,7 @@
</distributionManagement>
</profile>
</profiles>
</project>

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>

View File

@ -1,6 +1,7 @@
package cc.carm.lib.configuration;
import cc.carm.lib.configuration.spigot.source.SpigotConfigProvider;
import org.bukkit.plugin.Plugin;
import java.io.File;
import java.io.IOException;
@ -30,4 +31,12 @@ public class MineConfiguration {
return from(new File(fileName), source);
}
public static SpigotConfigProvider from(Plugin plugin, String fileName) {
return from(plugin, fileName, fileName);
}
public static SpigotConfigProvider from(Plugin plugin, String fileName, String source) {
return from(new File(plugin.getDataFolder(), fileName), source);
}
}

View File

@ -1,15 +1,14 @@
package cc.carm.lib.configuration.spigot.source;
import cc.carm.lib.configuration.core.ConfigInitializer;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import cc.carm.lib.configuration.craft.source.CraftConfigProvider;
import org.bukkit.configuration.file.YamlConfiguration;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class SpigotConfigProvider extends CraftConfigProvider {
@ -25,33 +24,31 @@ public class SpigotConfigProvider extends CraftConfigProvider {
}
@Override
public void setComment(@Nullable String path, @Nullable ConfigCommentInfo commentInfo) {
public void setHeaderComment(@Nullable String path, @Nullable List<String> comments) {
if (path == null) {
if (commentInfo == null) this.configuration.options().setFooter(null);
else if (!String.join("", commentInfo.getComments()).isEmpty()) {
this.configuration.options().setFooter(Arrays.asList(commentInfo.getComments()));
}
this.configuration.options().setHeader(comments);
} else {
if (commentInfo == null) this.configuration.setComments(path, null);
else {
List<String> comments = new ArrayList<>();
if (!String.join("", commentInfo.getComments()).isEmpty()) {
if (commentInfo.startWrap()) comments.add("");
comments.addAll(Arrays.asList(commentInfo.getComments()));
if (commentInfo.endWrap()) comments.add("");
} else if (commentInfo.startWrap() || commentInfo.endWrap()) {
comments.add("");
}
this.configuration.setComments(path, comments);
}
this.configuration.setComments(path, comments);
}
}
@Override
public @Nullable ConfigCommentInfo getComment(@Nullable String path) {
return null;
public void setInlineComment(@NotNull String path, @Nullable String comment) {
if (comment == null) {
this.configuration.setInlineComments(path, null);
} else {
this.configuration.setComments(path, Collections.singletonList(comment));
}
}
@Override
public @Nullable @Unmodifiable List<String> getHeaderComment(@Nullable String path) {
if (path == null) return Collections.unmodifiableList(this.configuration.options().getHeader());
else return this.configuration.getComments(path);
}
@Override
public @Nullable String getInlineComment(@NotNull String path) {
return String.join(" ", this.configuration.getInlineComments(path));
}
}