1
mirror of https://github.com/CarmJos/MineConfiguration.git synced 2026-06-05 06:51:49 +08:00

Compare commits

...

18 Commits

Author SHA1 Message Date
carm 40ec299c64 docs(parent): Update dependency documents 2022-05-20 21:59:56 +08:00
carm 483fbc7c4d [1.4.0]refactor(all): project reformatted.
BREAKING CHANGE: reformatted projects including project settings and package paths.
2022-05-20 21:54:46 +08:00
carm 65d7a32108 [1.4.0]refactor(all): project reformatted.
BREAKING CHANGE: reformatted projects including project settings and package paths.
2022-05-20 21:50:00 +08:00
carm 6aa220924d refactor(common): packages renamed. 2022-05-20 18:49:35 +08:00
carm f4a55f216b fix(impl): configuration reload problem 2022-05-20 18:39:14 +08:00
carm 34a51c42a5 fix(impl): configuration reload problem 2022-05-20 18:26:48 +08:00
carm c50df3bc56 修复依赖 2022-05-19 02:15:25 +08:00
carm 9a95e3d105 [1.3.0] 版本更新
1. 更新 EasyConfiguration 到 3.0.0 (破坏性更新)。
2. 添加 ConfiguredTitle 用于快捷向玩家发送title消息。
3. 为 ConfiguredItem 添加name和lore的原生params变量支持。
4. 添加 ProtocolLibHelper 用于支持多版本情况下的部分功能。(如需使用请安装 ProtocolLib 插件)
2022-05-19 02:04:57 +08:00
carm 98ee47f676 [1.3.0] 版本更新
1. 更新 EasyConfiguration 到 3.0.0 (破坏性更新)。
2. 添加 ConfiguredTitle 用于快捷向玩家发送title消息。
3. 为 ConfiguredItem 添加name和lore的原生params变量支持。
4. 添加 ProtocolLibHelper 用于支持多版本情况下的部分功能。(如需使用请安装 ProtocolLib 插件)
2022-05-19 01:45:16 +08:00
carm af614deae6 Merge remote-tracking branch 'origin/master' 2022-05-01 17:38:15 +08:00
carm cf80291e51 [1.2.2] 令物品配置原生支持PlaceholderAPI 2022-05-01 17:38:04 +08:00
carm 53ba56b0ad Merge pull request #5 from CarmJos/dependabot/maven/me.clip-placeholderapi-2.11.1
Bump placeholderapi from 2.10.9 to 2.11.1
2022-04-29 08:32:15 +08:00
dependabot[bot] da2023aad0 Bump placeholderapi from 2.10.9 to 2.11.1
Bumps placeholderapi from 2.10.9 to 2.11.1.

---
updated-dependencies:
- dependency-name: me.clip:placeholderapi
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-04-29 00:27:51 +00:00
carm 29526223e4 修复编译缺失 2022-04-29 05:12:35 +08:00
carm e8044c112e 修改Javadoc编译方式 2022-04-29 04:36:19 +08:00
carm a552938858 修改Javadoc编译方式 2022-04-29 04:27:38 +08:00
carm b61baed8bc [1.2.0]
- [A] 令 ConfiguredMessage 同时支持Bungee、Bukkit平台。
- [A] 为Bukkit相关平台原生支持PlaceholderAPI。
2022-04-29 04:19:08 +08:00
carm 0cd61842e5 [1.2.0]
- [A] 令 ConfiguredMessage 同时支持Bungee、Bukkit平台。
- [A] 为Bukkit相关平台原生支持PlaceholderAPI。
2022-04-29 04:17:38 +08:00
70 changed files with 2162 additions and 920 deletions
+23 -11
View File
@@ -20,9 +20,20 @@ EasyConfiguration for MineCraft!
## 项目结构
### **MineConfiguration-CraftBukkit**
### **MineConfiguration-Common**
CraftBukkit系通用依赖,不包含实现部分,请使用 **MineConfiguration-Bukkit****MineConfiguration-Spigot**
全部版本的共用部分,包括
- `ConfigMessage` (实现类为 `ConfiguredMessage<M>`)
- `ConfigMessageList` (实现类为 `ConfiguredMessageList<M>`)
如要使用,请访问对应实现类的builder() 方法来快速创建。
### MineConfiguration-Bukkit
#### MineConfiguration-Bukkit-Base
Bukkit系通用依赖,不包含实现部分,请使用 **MineConfiguration-Bukkit-General****MineConfiguration-Bukkit-Native**
相较于基础版本,额外提供了以下功能:
@@ -32,13 +43,13 @@ CraftBukkit系通用依赖,不包含实现部分,请使用 **MineConfigurati
以上类型可以通过 `CraftConfigValue.builder()` 来创建,部分类型提供了 `of(...);` 方法来快速创建。
### **MineConfiguration-Bukkit**
#### **MineConfiguration-Bukkit-General** _(推荐)_
适用于Bukkit的版本,包含以Bukkit为基础的其他服务端 *(如Spigot、Paper、CatServer)*
### **MineConfiguration-Spigot**
#### **MineConfiguration-Bukkit-Native**
适用于 Spigot(1.18+) 的版本,适配了1.18与更新版本自带的配置文件注释功能,随Spigot更新而优化,安全稳定。
适用于 Spigot(1.18+) 的版本,适配了1.18及以后版本Spigot原生自带的配置文件注释功能,随Spigot更新而优化,安全稳定。
### **MineConfiguration-Bungee**
@@ -48,7 +59,8 @@ CraftBukkit系通用依赖,不包含实现部分,请使用 **MineConfigurati
## 开发
请详见 [EasyConfiguration](https://github.com/CarmJos/EasyConfiguration) 的 [开发介绍](https://github.com/CarmJos/EasyConfiguration/tree/master/.documentation) 。
请详见 [EasyConfiguration](https://github.com/CarmJos/EasyConfiguration)
的 [开发介绍](https://github.com/CarmJos/EasyConfiguration/tree/master/.documentation) 。
### 依赖方式
@@ -99,14 +111,14 @@ CraftBukkit系通用依赖,不包含实现部分,请使用 **MineConfigurati
<dependency>
<groupId>cc.carm.lib</groupId>
<artifactId>mineconfiguration-bukkit</artifactId>
<artifactId>mineconfiguration-bukkit-general</artifactId>
<version>[LATEST RELEASE]</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cc.carm.lib</groupId>
<artifactId>mineconfiguration-spigot</artifactId>
<artifactId>mineconfiguration-bukkit-native</artifactId>
<version>[LATEST RELEASE]</version>
<scope>compile</scope>
</dependency>
@@ -152,9 +164,9 @@ repositories {
dependencies {
api "cc.carm.lib:mineconfiguration-bukkit:[LATEST RELEASE]"
api "cc.carm.lib:mineconfiguration-bukkit-general:[LATEST RELEASE]"
api "cc.carm.lib:mineconfiguration-spigot:[LATEST RELEASE]"
api "cc.carm.lib:mineconfiguration-bukkit-native:[LATEST RELEASE]"
api "cc.carm.lib:mineconfiguration-bungee:[LATEST RELEASE]"
@@ -1,115 +0,0 @@
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 java.io.BufferedWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.StringJoiner;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static cc.carm.lib.configuration.craft.source.CraftConfigProvider.SEPARATOR;
public class BukkitYAMLComments {
Map<String, ConfigCommentInfo> comments = new HashMap<>();
protected Map<String, ConfigCommentInfo> getComments() {
return comments;
}
public void set(@Nullable String path, @Nullable ConfigCommentInfo comments) {
if (comments == null) {
getComments().remove(path);
} else {
getComments().put(path, comments);
}
}
public @NotNull ConfigCommentInfo get(@Nullable String path) {
return getComments().getOrDefault(path, ConfigCommentInfo.defaults());
}
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;
}
}
/**
* 从一个文件读取配置并写入注释到某个写入器中。
* 该方法的部分源代码借鉴自 tchristofferson/ConfigUpdater 项目。
*
* @param source 源配置文件
* @param writer 配置写入器
* @throws IOException 当写入发生错误时抛出
*/
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);
Object currentValue = source.get(fullKey);
String[] splitFullKey = fullKey.split("[" + SEPARATOR + "]");
String trailingKey = splitFullKey[splitFullKey.length - 1];
if (currentValue instanceof ConfigurationSection) {
writer.write(indents + trailingKey + ":");
if (!((ConfigurationSection) currentValue).getKeys(false).isEmpty()) {
writer.write("\n");
} else {
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);
}
String endComment = buildComments("", null);
if (endComment != null) writer.write(endComment);
writer.close();
}
/**
* 得到一个键的缩进。
* 该方法的源代码来自 tchristofferson/ConfigUpdater 项目。
*
* @param key 键
* @return 该键的缩进文本
*/
protected static String getIndents(String key) {
String[] splitKey = key.split("[" + SEPARATOR + "]");
return IntStream.range(1, splitKey.length).mapToObj(i -> " ").collect(Collectors.joining());
}
}
+10 -9
View File
@@ -5,15 +5,14 @@
<parent>
<artifactId>mineconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.1.7</version>
<version>1.4.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
<artifactId>mineconfiguration-bukkit</artifactId>
<packaging>jar</packaging>
<artifactId>mineconfiguration-common</artifactId>
<dependencies>
@@ -25,21 +24,23 @@
</dependency>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>mineconfiguration-craftbukkit</artifactId>
<version>${project.parent.version}</version>
<groupId>de.themoep</groupId>
<artifactId>minedown</artifactId>
<version>1.7.1-SNAPSHOT</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.8.8-R0.1-SNAPSHOT</version>
<groupId>net.md-5</groupId>
<artifactId>bungeecord-chat</artifactId>
<version>1.16-R0.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
@@ -0,0 +1,39 @@
package cc.carm.lib.mineconfiguration.common.builder.message;
import cc.carm.lib.mineconfiguration.common.data.AbstractText;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.function.BiFunction;
public abstract class MessageConfigBuilder<R, T extends AbstractText<R>> {
protected final @NotNull Class<R> receiverClazz;
protected final @NotNull Class<T> textClazz;
public MessageConfigBuilder(@NotNull Class<R> receiverClazz,
@NotNull Class<T> textClazz) {
this.receiverClazz = receiverClazz;
this.textClazz = textClazz;
}
/**
* 以单条消息为目标,构建一个消息配置。
*
* @param parser 消息解析器,负责将String转换为目标消息类型。
* @param <M> 消息类型
* @return 单条消息构建器
*/
public abstract <M> @NotNull MessageValueBuilder<M, R, T, ?> asValue(@NotNull BiFunction<@Nullable R, @NotNull String, @Nullable M> parser);
/**
* 以多行消息为目标,构建一个消息配置。
*
* @param parser 消息解析器
* @param <M> 消息类型
* @return 多行消息构建器
*/
public abstract <M> @NotNull MessageListBuilder<M, R, T, ?> asList(@NotNull BiFunction<@Nullable R, @NotNull String, @Nullable M> parser);
}
@@ -0,0 +1,70 @@
package cc.carm.lib.mineconfiguration.common.builder.message;
import cc.carm.lib.mineconfiguration.common.data.AbstractText;
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
import cc.carm.lib.mineconfiguration.common.value.ConfigMessageList;
import cc.carm.lib.configuration.core.builder.CommonConfigBuilder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
public abstract class MessageListBuilder<M, R, T extends AbstractText<R>, B extends MessageListBuilder<M, R, T, B>>
extends CommonConfigBuilder<List<T>, B> {
protected final @NotNull Class<R> receiverClazz;
protected @NotNull String[] params;
protected @NotNull BiFunction<@Nullable R, @NotNull String, @Nullable M> messageParser;
protected @NotNull BiConsumer<@NotNull R, @NotNull List<M>> sendFunction;
protected @NotNull Function<@NotNull String, @NotNull String> paramFormatter;
protected final @NotNull Function<String, T> textBuilder;
public MessageListBuilder(@NotNull Class<R> receiverClazz,
@NotNull Function<String, T> textBuilder,
@NotNull BiFunction<@Nullable R, @NotNull String, @Nullable M> parser) {
this.receiverClazz = receiverClazz;
this.textBuilder = textBuilder;
this.params = new String[0];
this.messageParser = parser;
this.paramFormatter = ParamsUtils.DEFAULT_PARAM_FORMATTER;
this.sendFunction = (sender, M) -> {
};
}
public B defaults(@NotNull String... messages) {
return defaults(new ArrayList<>(Arrays.stream(messages).map(textBuilder).collect(Collectors.toList())));
}
public B params(@NotNull String... params) {
this.params = params;
return getThis();
}
public B params(@NotNull List<String> params) {
this.params = params.toArray(new String[0]);
return getThis();
}
public B formatParam(@NotNull Function<@NotNull String, @NotNull String> paramFormatter) {
this.paramFormatter = paramFormatter;
return getThis();
}
public B whenSend(@NotNull BiConsumer<@NotNull R, @NotNull List<M>> sendFunction) {
this.sendFunction = sendFunction;
return getThis();
}
@Override
public abstract @NotNull ConfigMessageList<M, T, R> build();
}
@@ -0,0 +1,67 @@
package cc.carm.lib.mineconfiguration.common.builder.message;
import cc.carm.lib.mineconfiguration.common.data.AbstractText;
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
import cc.carm.lib.mineconfiguration.common.value.ConfigMessage;
import cc.carm.lib.configuration.core.builder.CommonConfigBuilder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
public abstract class MessageValueBuilder<M, R, T extends AbstractText<R>, B extends MessageValueBuilder<M, R, T, B>>
extends CommonConfigBuilder<T, B> {
protected final @NotNull Class<R> receiverClazz;
protected @NotNull String[] params;
protected @NotNull BiFunction<@Nullable R, @NotNull String, @Nullable M> messageParser;
protected @NotNull BiConsumer<@NotNull R, @NotNull M> sendHandler;
protected @NotNull Function<@NotNull String, @NotNull String> paramFormatter;
protected final @NotNull Function<String, T> textBuilder;
public MessageValueBuilder(@NotNull Class<R> receiverClazz,
@NotNull Function<String, T> textBuilder,
@NotNull BiFunction<@Nullable R, @NotNull String, @Nullable M> parser) {
this.receiverClazz = receiverClazz;
this.params = new String[0];
this.paramFormatter = ParamsUtils.DEFAULT_PARAM_FORMATTER;
this.textBuilder = textBuilder;
this.messageParser = parser;
this.sendHandler = (receiver, M) -> {
};
}
public B defaults(@NotNull String message) {
return defaults(this.textBuilder.apply(message));
}
public B params(@NotNull String... params) {
this.params = params;
return getThis();
}
public B params(@NotNull List<String> params) {
this.params = params.toArray(new String[0]);
return getThis();
}
public B formatParam(@NotNull Function<@NotNull String, @NotNull String> paramFormatter) {
this.paramFormatter = paramFormatter;
return getThis();
}
public B whenSend(@NotNull BiConsumer<@NotNull R, @NotNull M> sendFunction) {
this.sendHandler = sendFunction;
return getThis();
}
@Override
public abstract @NotNull ConfigMessage<M, T, R> build();
}
@@ -0,0 +1,44 @@
package cc.carm.lib.mineconfiguration.common.data;
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
import java.util.function.BiFunction;
/**
* @param <R> Receiver type
*/
public abstract class AbstractText<R> {
private final @NotNull Class<R> receiverClazz;
protected @NotNull String message;
public AbstractText(@NotNull Class<R> receiverClazz, @NotNull String message) {
this.receiverClazz = receiverClazz;
this.message = message;
}
public @NotNull Class<R> getReceiverClazz() {
return receiverClazz;
}
public @NotNull String getMessage() {
return this.message;
}
public <M> @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, 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, ParamsUtils.setPlaceholders(message, placeholders));
}
}
@@ -0,0 +1,44 @@
package cc.carm.lib.mineconfiguration.common.utils;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class ColorParser {
public static Pattern HEX_PATTERN = Pattern.compile("&\\(&?#([0-9a-fA-F]{6})\\)");
public static String parse(String text) {
return parseBaseColor(parseHexColor(text));
}
public static String[] parse(String... texts) {
return parse(Arrays.asList(texts)).toArray(new String[0]);
}
public static List<String> parse(List<String> texts) {
return texts.stream().map(ColorParser::parse).collect(Collectors.toList());
}
public static String parseBaseColor(final String text) {
return text.replaceAll("&", "§").replace("§§", "&");
}
public static String parseHexColor(String text) {
Matcher matcher = HEX_PATTERN.matcher(text);
while (matcher.find()) {
text = matcher.replaceFirst(buildHexColor(matcher.group(1)).toLowerCase());
matcher.reset(text);
}
return text;
}
private static String buildHexColor(String hexCode) {
return Arrays.stream(hexCode.split(""))
.map(s -> '§' + s)
.collect(Collectors.joining("", '§' + "x", ""));
}
}
@@ -0,0 +1,43 @@
package cc.carm.lib.mineconfiguration.common.utils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
public class ParamsUtils {
/**
* 默认的变量格式为 {@code %(变量名)}。
*/
public static Function<@NotNull String, @NotNull String> DEFAULT_PARAM_FORMATTER = (s) -> "%(" + s + ")";
public static String[] formatParams(@NotNull Function<String, String> formatter, @NotNull String[] params) {
return Arrays.stream(params).map(formatter).toArray(String[]::new);
}
public static Map<String, Object> buildParams(@Nullable String[] params, @Nullable Object[] values) {
Map<String, Object> map = new HashMap<>();
if (params == null || params.length == 0) return map;
for (int i = 0; i < params.length; i++) {
map.put(params[i], values.length > i ? values[i] : "?");
}
return map;
}
public static String setPlaceholders(@NotNull String messages, @NotNull Map<String, Object> placeholders) {
if (messages.isEmpty()) return messages;
String parsed = messages;
for (Map.Entry<String, Object> entry : placeholders.entrySet()) {
Object value = entry.getValue();
parsed = parsed.replace(entry.getKey(), value == null ? "" : value.toString());
}
return parsed;
}
}
@@ -0,0 +1,75 @@
package cc.carm.lib.mineconfiguration.common.value;
import cc.carm.lib.mineconfiguration.common.data.AbstractText;
import cc.carm.lib.mineconfiguration.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.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;
import java.util.function.Function;
public abstract class ConfigMessage<M, T extends AbstractText<R>, R>
extends ConfiguredValue<T> {
protected final @NotNull String[] params;
protected final @NotNull BiFunction<@Nullable R, @NotNull String, @Nullable M> messageParser;
protected final @NotNull BiConsumer<@NotNull R, @NotNull M> messageConsumer;
protected final @NotNull Function<String, T> textBuilder;
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> messageConsumer,
@NotNull Function<String, T> textBuilder) {
super(provider, sectionPath, headerComments, inlineComments, textClazz, defaultMessage,
ConfigValueParser.castToString().andThen((s, d) -> textBuilder.apply(s)), AbstractText::getMessage
);
this.params = params;
this.messageParser = messageParser;
this.messageConsumer = messageConsumer;
this.textBuilder = textBuilder;
}
public @Nullable M parse(@Nullable R sender, @Nullable Object... values) {
return parse(sender, ParamsUtils.buildParams(params, values));
}
public @Nullable M parse(@Nullable R sender, @NotNull Map<String, Object> placeholders) {
T value = get();
if (value == null || value.getMessage().isEmpty()) return null;
else return value.parse(this.messageParser, sender, placeholders);
}
public void send(@Nullable R receiver, @Nullable Object... values) {
send(receiver, 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;
messageConsumer.accept(receiver, parsed);
}
public void broadcast(@Nullable Object... values) {
broadcast(ParamsUtils.buildParams(params, values));
}
public abstract void broadcast(@NotNull Map<String, Object> placeholders);
public void set(@Nullable String value) {
this.set(value == null ? null : buildText(value));
}
protected T buildText(String value) {
return textBuilder.apply(value);
}
}
@@ -0,0 +1,93 @@
package cc.carm.lib.mineconfiguration.common.value;
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
import cc.carm.lib.mineconfiguration.common.data.AbstractText;
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.value.type.ConfiguredList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
public abstract class ConfigMessageList<M, T extends AbstractText<R>, R> extends ConfiguredList<T> {
protected final @NotNull String[] params;
protected final @NotNull BiFunction<@Nullable R, @NotNull String, @Nullable M> messageParser;
protected final @NotNull BiConsumer<@NotNull R, @NotNull List<M>> sendFunction;
protected final @NotNull Function<String, T> textBuilder;
@SuppressWarnings("NullableProblems")
public ConfigMessageList(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
@Nullable 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, headerComments, inlineComments, textClazz, messages,
ConfigDataFunction.castToString().andThen(textBuilder::apply), AbstractText::getMessage
);
this.params = params;
this.messageParser = messageParser;
this.sendFunction = sendFunction;
this.textBuilder = textBuilder;
}
public @Nullable List<M> parse(@Nullable R sender, @Nullable Object... values) {
return parse(sender, ParamsUtils.buildParams(params, values));
}
public @Nullable List<M> parse(@Nullable R sender, @NotNull Map<String, Object> placeholders) {
List<T> list = get();
if (list.isEmpty()) return null;
List<String> messages = list.stream().map(T::getMessage).collect(Collectors.toList());
if (String.join("", messages).isEmpty()) return null;
return list.stream().map(value -> value.parse(this.messageParser, sender, placeholders))
.collect(Collectors.toList());
}
public void send(@Nullable R receiver, @Nullable Object... values) {
send(receiver, ParamsUtils.buildParams(params, values));
}
public void send(@Nullable R receiver, @NotNull Map<String, Object> placeholders) {
if (receiver == null) return;
List<M> parsed = parse(receiver, placeholders);
if (parsed == null) return;
sendFunction.accept(receiver, parsed);
}
public void broadcast(@Nullable Object... values) {
broadcast(ParamsUtils.buildParams(params, values));
}
public abstract void broadcast(@NotNull Map<String, Object> placeholders);
public void setMessages(@NotNull String... values) {
setMessages(values.length == 0 ? null : Arrays.asList(values));
}
public void setMessages(@Nullable List<String> values) {
if (values == null || values.isEmpty()) {
set(null);
} else {
set(buildText(values));
}
}
protected List<T> buildText(List<String> values) {
return values.stream().map(textBuilder).collect(Collectors.toList());
}
}
@@ -1,45 +0,0 @@
package cc.carm.lib.configuration.craft.builder.message;
import cc.carm.lib.configuration.craft.utils.ColorParser;
import cc.carm.lib.configuration.craft.value.ConfiguredMessage;
import cc.carm.lib.configuration.craft.value.ConfiguredMessageList;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.function.BiFunction;
public class MessageConfigBuilder {
public <M> @NotNull MessageValueBuilder<M> asValue(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser) {
return new MessageValueBuilder<>(messageParser);
}
public <M> @NotNull MessageListBuilder<M> asList(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser) {
return new MessageListBuilder<>(messageParser);
}
public @NotNull MessageValueBuilder<String> asStringValue() {
return asValue((sender, message) -> ColorParser.parseColor(message))
.whenSend(CommandSender::sendMessage);
}
public @NotNull ConfiguredMessage<String> valueOfString() {
return valueOfString("");
}
public @NotNull ConfiguredMessage<String> valueOfString(@NotNull String defaultMessage) {
return asStringValue().defaults(defaultMessage).build();
}
public @NotNull MessageListBuilder<String> asStringList() {
return asList((sender, message) -> ColorParser.parseColor(message))
.whenSend((sender, messages) -> messages.forEach(sender::sendMessage));
}
public @NotNull ConfiguredMessageList<String> listOfString(@NotNull String... defaultMessages) {
return asStringList().defaults(defaultMessages).build();
}
}
@@ -1,81 +0,0 @@
package cc.carm.lib.configuration.craft.builder.message;
import cc.carm.lib.configuration.core.builder.CommonConfigBuilder;
import cc.carm.lib.configuration.craft.data.MessageText;
import cc.carm.lib.configuration.craft.value.ConfiguredMessageList;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
public class MessageListBuilder<M>
extends CommonConfigBuilder<List<MessageText>, MessageListBuilder<M>> {
protected @NotNull String[] params;
protected @NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser;
protected @NotNull BiConsumer<@NotNull CommandSender, @NotNull List<M>> sendFunction;
protected @NotNull Function<@NotNull String, @NotNull String> paramFormatter;
public MessageListBuilder(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
this.params = new String[0];
this.messageParser = parser;
this.paramFormatter = MessageValueBuilder.DEFAULT_PARAM_FORMATTER;
this.sendFunction = (sender, M) -> {
};
}
public MessageListBuilder<M> defaults(@NotNull String... messages) {
return defaults(Arrays.asList(messages));
}
public MessageListBuilder<M> defaults(@NotNull List<String> messages) {
return defaults(new ArrayList<>(MessageText.of(messages)));
}
public MessageListBuilder<M> params(@NotNull String... params) {
this.params = params;
return this;
}
public MessageListBuilder<M> params(@NotNull List<String> params) {
this.params = params.toArray(new String[0]);
return this;
}
public MessageListBuilder<M> formatParam(@NotNull Function<@NotNull String, @NotNull String> paramFormatter) {
this.paramFormatter = paramFormatter;
return this;
}
public MessageListBuilder<M> whenSend(@NotNull BiConsumer<@NotNull CommandSender, @NotNull List<M>> sendFunction) {
this.sendFunction = sendFunction;
return this;
}
@Override
protected @NotNull MessageListBuilder<M> getThis() {
return this;
}
@Override
public @NotNull ConfiguredMessageList<M> build() {
return new ConfiguredMessageList<>(
this.provider, this.path, buildComments(),
Optional.ofNullable(this.defaultValue).orElse(new ArrayList<>()),
buildParams(), this.messageParser, this.sendFunction
);
}
protected final String[] buildParams() {
return Arrays.stream(params).map(param -> paramFormatter.apply(param)).toArray(String[]::new);
}
}
@@ -1,79 +0,0 @@
package cc.carm.lib.configuration.craft.builder.message;
import cc.carm.lib.configuration.core.builder.CommonConfigBuilder;
import cc.carm.lib.configuration.craft.data.MessageText;
import cc.carm.lib.configuration.craft.value.ConfiguredMessage;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
public class MessageValueBuilder<M>
extends CommonConfigBuilder<MessageText, MessageValueBuilder<M>> {
public static Function<@NotNull String, @NotNull String> DEFAULT_PARAM_FORMATTER = (s) -> "%(" + s + ")";
protected @NotNull String[] params;
protected @NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser;
protected @NotNull BiConsumer<@NotNull CommandSender, @NotNull M> sendHandler;
protected @NotNull Function<@NotNull String, @NotNull String> paramFormatter;
public MessageValueBuilder(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
this.params = new String[0];
this.paramFormatter = DEFAULT_PARAM_FORMATTER;
this.messageParser = parser;
this.sendHandler = (sender, M) -> {
};
}
public MessageValueBuilder<M> defaults(@NotNull String message) {
return defaults(MessageText.of(message));
}
public MessageValueBuilder<M> params(@NotNull String... params) {
this.params = params;
return this;
}
public MessageValueBuilder<M> params(@NotNull List<String> params) {
this.params = params.toArray(new String[0]);
return this;
}
public MessageValueBuilder<M> formatParam(@NotNull Function<@NotNull String, @NotNull String> paramFormatter) {
this.paramFormatter = paramFormatter;
return this;
}
public MessageValueBuilder<M> whenSend(@NotNull BiConsumer<@NotNull CommandSender, @NotNull M> sendFunction) {
this.sendHandler = sendFunction;
return this;
}
@Override
protected @NotNull MessageValueBuilder<M> getThis() {
return this;
}
@Override
public @NotNull ConfiguredMessage<M> build() {
return new ConfiguredMessage<>(
this.provider, this.path, buildComments(),
Optional.ofNullable(this.defaultValue).orElse(MessageText.of("")),
buildParams(), this.messageParser, this.sendHandler
);
}
protected final String[] buildParams() {
return Arrays.stream(params).map(param -> paramFormatter.apply(param)).toArray(String[]::new);
}
}
@@ -1,34 +0,0 @@
package cc.carm.lib.configuration.craft.builder.sound;
import cc.carm.lib.configuration.craft.builder.AbstractCraftBuilder;
import cc.carm.lib.configuration.craft.data.SoundConfig;
import cc.carm.lib.configuration.craft.value.ConfiguredSound;
import org.bukkit.Sound;
import org.jetbrains.annotations.NotNull;
public class SoundConfigBuilder extends AbstractCraftBuilder<SoundConfig, SoundConfigBuilder> {
public @NotNull SoundConfigBuilder defaults(@NotNull Sound sound, float volume, float pitch) {
return defaults(new SoundConfig(sound, volume, pitch));
}
public @NotNull SoundConfigBuilder defaults(@NotNull Sound sound, float volume) {
return defaults(sound, volume, 1.0f);
}
public @NotNull SoundConfigBuilder defaults(@NotNull Sound sound) {
return defaults(sound, 1.0f);
}
@Override
protected @NotNull SoundConfigBuilder getThis() {
return this;
}
@Override
public @NotNull ConfiguredSound build() {
return new ConfiguredSound(this.provider, this.path, buildComments(), this.defaultValue);
}
}
@@ -1,74 +0,0 @@
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.*;
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(@Nullable List<String> messages) {
if (messages == null || messages.isEmpty()) return new ArrayList<>();
else return messages.stream().map(MessageText::of).collect(Collectors.toList());
}
public static @NotNull List<MessageText> of(@NotNull String... messages) {
return of(Arrays.asList(messages));
}
protected @NotNull String message;
public MessageText(@NotNull String message) {
this.message = message;
}
public @NotNull String getMessage() {
return this.message;
}
public <M> @Nullable M parse(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @NotNull M> parser,
@Nullable CommandSender sender, @Nullable String[] params, @Nullable Object[] values) {
return parse(parser, sender, buildParams(params, values));
}
public <M> @Nullable M parse(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @NotNull M> parser,
@Nullable CommandSender sender, @NotNull Map<String, Object> placeholders) {
String message = getMessage();
if (message.isEmpty()) return null; // No further processing
else return parser.apply(sender, setPlaceholders(message, placeholders));
}
public static Map<String, Object> buildParams(@Nullable String[] params, @Nullable Object[] values) {
Map<String, Object> map = new HashMap<>();
if (params == null || params.length == 0) return map;
for (int i = 0; i < params.length; i++) {
map.put(params[i], values.length > i ? values[i] : "?");
}
return map;
}
public static String setPlaceholders(@NotNull String messages, @NotNull Map<String, Object> placeholders) {
if (messages.isEmpty()) return messages;
String parsed = messages;
for (Map.Entry<String, Object> entry : placeholders.entrySet()) {
Object value = entry.getValue();
parsed = parsed.replace(entry.getKey(), value == null ? "" : value.toString());
}
return parsed;
}
}
@@ -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;
}
}
@@ -1,37 +0,0 @@
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.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.jetbrains.annotations.Nullable;
public class ConfiguredItem extends ConfiguredSection<ItemConfig> {
public static ItemConfigBuilder create() {
return CraftConfigValue.builder().createItem();
}
public static ConfiguredItem of() {
return CraftConfigValue.builder().ofItem();
}
public static ConfiguredItem of(@Nullable ItemConfig defaultItem) {
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);
}
public static ConfigValueParser<ConfigurationWrapper, ItemConfig> getItemParser() {
return (s, d) -> ItemConfig.deserialize(s);
}
}
@@ -1,90 +0,0 @@
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.MessageValueBuilder;
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> {
@NotNull
public static <M> MessageValueBuilder<@Nullable M> create(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser) {
return CraftConfigValue.builder().createMessage().asValue(messageParser);
}
public static MessageValueBuilder<String> fromString() {
return CraftConfigValue.builder().createMessage().asStringValue();
}
public static ConfiguredMessage<String> ofString() {
return CraftConfigValue.builder().createMessage().valueOfString();
}
public static ConfiguredMessage<String> ofString(@NotNull String defaultMessage) {
return CraftConfigValue.builder().createMessage().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 MessageText 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, 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) {
return parse(sender, MessageText.buildParams(params, values));
}
public @Nullable M parse(@Nullable CommandSender sender, @NotNull Map<String, Object> placeholders) {
MessageText value = get();
if (value == null || value.getMessage().isEmpty()) return null;
else return value.parse(this.messageParser, sender, placeholders);
}
public void send(@Nullable CommandSender receiver, @Nullable Object... values) {
send(receiver, MessageText.buildParams(params, values));
}
public void send(@Nullable CommandSender receiver, @NotNull Map<String, Object> placeholders) {
if (receiver == null) return;
M parsed = parse(receiver, placeholders);
if (parsed == null) return;
sendFunction.accept(receiver, parsed);
}
public void broadcast(@Nullable Object... values) {
broadcast(MessageText.buildParams(params, values));
}
public void broadcast(@NotNull Map<String, Object> placeholders) {
Bukkit.getOnlinePlayers().forEach(pl -> send(pl, placeholders));
send(Bukkit.getConsoleSender(), placeholders);
}
public void set(@Nullable String value) {
this.set(value == null ? null : new MessageText(value));
}
}
@@ -1,108 +0,0 @@
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.MessageListBuilder;
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> {
@NotNull
public static <M> MessageListBuilder<M> create(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser) {
return CraftConfigValue.builder().createMessage().asList(messageParser);
}
public static MessageListBuilder<String> fromString() {
return CraftConfigValue.builder().createMessage().asStringList();
}
public static ConfiguredMessageList<String> ofString(@NotNull String... defaultMessages) {
return CraftConfigValue.builder().createMessage().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<MessageText> messages, @NotNull String[] params,
@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser,
@NotNull BiConsumer<@NotNull CommandSender, @NotNull List<M>> sendFunction) {
super(provider, sectionPath, comments, MessageText.class, messages,
ConfigDataFunction.castToString().andThen(MessageText::new), MessageText::getMessage
);
this.params = params;
this.messageParser = messageParser;
this.sendFunction = sendFunction;
}
public @Nullable List<M> parse(@Nullable CommandSender sender, @Nullable Object... values) {
return parse(sender, MessageText.buildParams(params, values));
}
public @Nullable List<M> parse(@Nullable CommandSender sender, @NotNull Map<String, Object> placeholders) {
List<MessageText> list = get();
if (list.isEmpty()) return null;
List<String> messages = list.stream().map(MessageText::getMessage).collect(Collectors.toList());
if (String.join("", messages).isEmpty()) return null;
return list.stream().map(value -> value.parse(this.messageParser, sender, placeholders))
.collect(Collectors.toList());
}
public void send(@Nullable CommandSender receiver, @Nullable Object... values) {
send(receiver, MessageText.buildParams(params, values));
}
public void send(@Nullable CommandSender receiver, @NotNull Map<String, Object> placeholders) {
if (receiver == null) return;
List<M> parsed = parse(receiver, placeholders);
if (parsed == null) return;
sendFunction.accept(receiver, parsed);
}
public void broadcast(@Nullable Object... values) {
broadcast(MessageText.buildParams(params, values));
}
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));
}
}
@@ -3,16 +3,16 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mineconfiguration-parent</artifactId>
<artifactId>mineconfiguration-bukkit</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.1.7</version>
<version>1.4.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
<artifactId>mineconfiguration-craftbukkit</artifactId>
<artifactId>mineconfiguration-bukkit-base</artifactId>
<packaging>jar</packaging>
<dependencies>
@@ -25,12 +25,12 @@
</dependency>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.8.8-R0.1-SNAPSHOT</version>
<scope>provided</scope>
<groupId>${project.parent.groupId}</groupId>
<artifactId>mineconfiguration-common</artifactId>
<version>${project.parent.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
@@ -1,23 +1,25 @@
package cc.carm.lib.configuration.craft;
package cc.carm.lib.mineconfiguration.bukkit;
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 cc.carm.lib.mineconfiguration.bukkit.builder.CraftConfigBuilder;
import cc.carm.lib.mineconfiguration.bukkit.source.CraftConfigProvider;
import cc.carm.lib.mineconfiguration.bukkit.source.CraftSectionWrapper;
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() {
@@ -1,6 +1,6 @@
package cc.carm.lib.configuration.craft.builder;
package cc.carm.lib.mineconfiguration.bukkit.builder;
import cc.carm.lib.configuration.craft.source.CraftConfigProvider;
import cc.carm.lib.mineconfiguration.bukkit.source.CraftConfigProvider;
import cc.carm.lib.configuration.core.builder.AbstractConfigBuilder;
public abstract class AbstractCraftBuilder<T, B extends AbstractCraftBuilder<T, B>>
@@ -1,12 +1,13 @@
package cc.carm.lib.configuration.craft.builder;
package cc.carm.lib.mineconfiguration.bukkit.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;
import cc.carm.lib.configuration.craft.value.ConfiguredItem;
import cc.carm.lib.mineconfiguration.bukkit.builder.item.ItemConfigBuilder;
import cc.carm.lib.mineconfiguration.bukkit.builder.message.CraftMessageBuilder;
import cc.carm.lib.mineconfiguration.bukkit.builder.serializable.SerializableBuilder;
import cc.carm.lib.mineconfiguration.bukkit.builder.sound.SoundConfigBuilder;
import cc.carm.lib.mineconfiguration.bukkit.builder.title.TitleConfigBuilder;
import cc.carm.lib.mineconfiguration.bukkit.data.ItemConfig;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredItem;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -21,8 +22,12 @@ public class CraftConfigBuilder extends ConfigBuilder {
return new ItemConfigBuilder();
}
public @NotNull MessageConfigBuilder createMessage() {
return new MessageConfigBuilder();
public @NotNull CraftMessageBuilder createMessage() {
return new CraftMessageBuilder();
}
public @NotNull TitleConfigBuilder createTitle() {
return new TitleConfigBuilder();
}
public <V extends ConfigurationSerializable> @NotNull SerializableBuilder<V> ofSerializable(@NotNull Class<V> valueClass) {
@@ -1,8 +1,9 @@
package cc.carm.lib.configuration.craft.builder.item;
package cc.carm.lib.mineconfiguration.bukkit.builder.item;
import cc.carm.lib.configuration.craft.builder.AbstractCraftBuilder;
import cc.carm.lib.configuration.craft.data.ItemConfig;
import cc.carm.lib.configuration.craft.value.ConfiguredItem;
import cc.carm.lib.mineconfiguration.bukkit.builder.AbstractCraftBuilder;
import cc.carm.lib.mineconfiguration.bukkit.data.ItemConfig;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredItem;
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
import org.bukkit.Material;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -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);
}
}
@@ -0,0 +1,44 @@
package cc.carm.lib.mineconfiguration.bukkit.builder.message;
import cc.carm.lib.mineconfiguration.bukkit.data.TextConfig;
import cc.carm.lib.mineconfiguration.bukkit.utils.TextParser;
import cc.carm.lib.mineconfiguration.common.builder.message.MessageConfigBuilder;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.function.BiFunction;
public class CraftMessageBuilder extends MessageConfigBuilder<CommandSender, TextConfig> {
public CraftMessageBuilder() {
super(CommandSender.class, TextConfig.class);
}
@Override
public @NotNull <M> CraftMessageValueBuilder<M> asValue(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
return new CraftMessageValueBuilder<>(parser);
}
@Override
public @NotNull <M> CraftMessageListBuilder<M> asList(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
return new CraftMessageListBuilder<>(parser);
}
public @NotNull
CraftMessageValueBuilder<String> asStringValue() {
return asValue(defaultParser()).whenSend(CommandSender::sendMessage);
}
public @NotNull
CraftMessageListBuilder<String> asStringList() {
return asList(defaultParser()).whenSend((r, m) -> m.forEach(r::sendMessage));
}
protected static @NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable String> defaultParser() {
return (receiver, message) -> TextParser.parseText(receiver, message, new HashMap<>());
}
}
@@ -0,0 +1,36 @@
package cc.carm.lib.mineconfiguration.bukkit.builder.message;
import cc.carm.lib.mineconfiguration.bukkit.data.TextConfig;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredMessageList;
import cc.carm.lib.mineconfiguration.common.builder.message.MessageListBuilder;
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Optional;
import java.util.function.BiFunction;
public class CraftMessageListBuilder<M>
extends MessageListBuilder<M, CommandSender, TextConfig, CraftMessageListBuilder<M>> {
public CraftMessageListBuilder(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
super(CommandSender.class, TextConfig::of, parser);
}
@Override
protected @NotNull CraftMessageListBuilder<M> getThis() {
return this;
}
@Override
public @NotNull ConfiguredMessageList<M> build() {
return new ConfiguredMessageList<>(
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
);
}
}
@@ -0,0 +1,37 @@
package cc.carm.lib.mineconfiguration.bukkit.builder.message;
import cc.carm.lib.mineconfiguration.bukkit.data.TextConfig;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredMessage;
import cc.carm.lib.mineconfiguration.common.builder.message.MessageValueBuilder;
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Optional;
import java.util.function.BiFunction;
public class CraftMessageValueBuilder<M>
extends MessageValueBuilder<M, CommandSender, TextConfig, CraftMessageValueBuilder<M>> {
public CraftMessageValueBuilder(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
super(CommandSender.class, TextConfig::new, parser);
}
@Override
protected @NotNull CraftMessageValueBuilder<M> getThis() {
return this;
}
@Override
public @NotNull ConfiguredMessage<M> build() {
return new ConfiguredMessage<>(
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
);
}
}
@@ -1,7 +1,7 @@
package cc.carm.lib.configuration.craft.builder.serializable;
package cc.carm.lib.mineconfiguration.bukkit.builder.serializable;
import cc.carm.lib.configuration.craft.builder.AbstractCraftBuilder;
import cc.carm.lib.configuration.craft.value.ConfiguredSerializable;
import cc.carm.lib.mineconfiguration.bukkit.builder.AbstractCraftBuilder;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredSerializable;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.jetbrains.annotations.NotNull;
@@ -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);
}
}
@@ -0,0 +1,46 @@
package cc.carm.lib.mineconfiguration.bukkit.builder.sound;
import cc.carm.lib.mineconfiguration.bukkit.builder.AbstractCraftBuilder;
import cc.carm.lib.mineconfiguration.bukkit.data.SoundConfig;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredSound;
import org.bukkit.Sound;
import org.jetbrains.annotations.NotNull;
public class SoundConfigBuilder extends AbstractCraftBuilder<SoundConfig, SoundConfigBuilder> {
public @NotNull SoundConfigBuilder defaults(@NotNull Sound sound, float volume, float pitch) {
return defaults(new SoundConfig(sound.name(), sound, volume, pitch));
}
public @NotNull SoundConfigBuilder defaults(@NotNull Sound sound, float volume) {
return defaults(sound, volume, 1.0f);
}
public @NotNull SoundConfigBuilder defaults(@NotNull Sound sound) {
return defaults(sound, 1.0f);
}
public @NotNull SoundConfigBuilder defaults(@NotNull String soundName, float volume, float pitch) {
return defaults(new SoundConfig(soundName, volume, pitch));
}
public @NotNull SoundConfigBuilder defaults(@NotNull String soundName, float volume) {
return defaults(soundName, volume, 1.0f);
}
public @NotNull SoundConfigBuilder defaults(@NotNull String soundName) {
return defaults(soundName, 1.0f);
}
@Override
protected @NotNull SoundConfigBuilder getThis() {
return this;
}
@Override
public @NotNull ConfiguredSound build() {
return new ConfiguredSound(this.provider, this.path, this.headerComments, this.inlineComment, this.defaultValue);
}
}
@@ -0,0 +1,97 @@
package cc.carm.lib.mineconfiguration.bukkit.builder.title;
import cc.carm.lib.mineconfiguration.bukkit.builder.AbstractCraftBuilder;
import cc.carm.lib.mineconfiguration.bukkit.data.TitleConfig;
import cc.carm.lib.mineconfiguration.bukkit.function.TitleSendConsumer;
import cc.carm.lib.mineconfiguration.bukkit.utils.ProtocolLibHelper;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredTitle;
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
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) int fadeIn = 10;
protected @Range(from = 0L, to = Integer.MAX_VALUE) int stay = 60;
protected @Range(from = 0L, to = Integer.MAX_VALUE) int fadeOut = 10;
protected @NotNull 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) int fadeIn) {
this.fadeIn = fadeIn;
return this;
}
public TitleConfigBuilder stay(@Range(from = 0L, to = Integer.MAX_VALUE) int stay) {
this.stay = stay;
return this;
}
public TitleConfigBuilder fadeOut(@Range(from = 0L, to = Integer.MAX_VALUE) int fadeOut) {
this.fadeOut = fadeOut;
return this;
}
public TitleConfigBuilder formatParam(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, ParamsUtils.formatParams(this.paramFormatter, this.params),
this.sendConsumer, this.fadeIn, this.stay, this.fadeOut
);
}
}
@@ -1,17 +1,15 @@
package cc.carm.lib.configuration.craft.data;
package cc.carm.lib.mineconfiguration.bukkit.data;
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
import cc.carm.lib.mineconfiguration.bukkit.utils.TextParser;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
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 {
@@ -40,25 +38,47 @@ 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, new HashMap<>());
}
public @NotNull ItemStack getItemStack(@Nullable Player player) {
return getItemStack(player, new HashMap<>());
}
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(getName());
if (!getLore().isEmpty()) meta.setLore(getLore());
Optional.ofNullable(getName(player, placeholders)).ifPresent(meta::setDisplayName);
Optional.ofNullable(getLore(player, placeholders)).ifPresent(meta::setLore);
item.setItemMeta(meta);
return item;
}
public @NotNull Map<String, Object> serialize() {
Map<String, Object> map = new LinkedHashMap<>();
map.put("type", type.name());
@@ -78,15 +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());
}
}
@@ -1,4 +1,4 @@
package cc.carm.lib.configuration.craft.data;
package cc.carm.lib.mineconfiguration.bukkit.data;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
@@ -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
@@ -0,0 +1,35 @@
package cc.carm.lib.mineconfiguration.bukkit.data;
import cc.carm.lib.mineconfiguration.common.data.AbstractText;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class TextConfig extends AbstractText<CommandSender> {
public TextConfig(@NotNull String message) {
super(CommandSender.class, message);
}
@Contract("!null,-> !null")
public static @Nullable TextConfig of(@Nullable String message) {
if (message == null) return null;
else return new TextConfig(message);
}
public static @NotNull List<TextConfig> of(@Nullable List<String> messages) {
if (messages == null || messages.isEmpty()) return new ArrayList<>();
else return messages.stream().map(TextConfig::of).collect(Collectors.toList());
}
public static @NotNull List<TextConfig> of(@NotNull String... messages) {
return of(Arrays.asList(messages));
}
}
@@ -0,0 +1,68 @@
package cc.carm.lib.mineconfiguration.bukkit.data;
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
import cc.carm.lib.mineconfiguration.bukkit.function.TitleSendConsumer;
import cc.carm.lib.mineconfiguration.bukkit.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) int fadeIn,
@Range(from = 0L, to = Long.MAX_VALUE) int stay,
@Range(from = 0L, to = Long.MAX_VALUE) int 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"));
}
}
@@ -0,0 +1,26 @@
package cc.carm.lib.mineconfiguration.bukkit.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) int fadeIn,
@Range(from = 0L, to = Integer.MAX_VALUE) int stay,
@Range(from = 0L, to = Integer.MAX_VALUE) int fadeOut,
@NotNull String line1, @NotNull String line2);
}
@@ -1,4 +1,4 @@
package cc.carm.lib.configuration.craft.source;
package cc.carm.lib.mineconfiguration.bukkit.source;
import cc.carm.lib.configuration.core.ConfigInitializer;
import cc.carm.lib.configuration.core.source.impl.FileConfigProvider;
@@ -26,7 +26,7 @@ public abstract class CraftConfigProvider extends FileConfigProvider<CraftSectio
}
@Override
public void reload() throws Exception {
protected void onReload() throws Exception {
configuration.load(getFile());
}
@@ -1,4 +1,4 @@
package cc.carm.lib.configuration.craft.source;
package cc.carm.lib.mineconfiguration.bukkit.source;
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
import org.bukkit.configuration.ConfigurationSection;
@@ -0,0 +1,27 @@
package cc.carm.lib.mineconfiguration.bukkit.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;
}
}
}
@@ -0,0 +1,41 @@
package cc.carm.lib.mineconfiguration.bukkit.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);
}
}
@@ -0,0 +1,38 @@
package cc.carm.lib.mineconfiguration.bukkit.utils;
import cc.carm.lib.mineconfiguration.common.utils.ColorParser;
import cc.carm.lib.mineconfiguration.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;
}
}
@@ -0,0 +1,71 @@
package cc.carm.lib.mineconfiguration.bukkit.value;
import cc.carm.lib.mineconfiguration.bukkit.CraftConfigValue;
import cc.carm.lib.mineconfiguration.bukkit.data.ItemConfig;
import cc.carm.lib.mineconfiguration.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.mineconfiguration.bukkit.builder.item.ItemConfigBuilder;
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() {
return CraftConfigValue.builder().createItem();
}
public static ConfiguredItem of() {
return CraftConfigValue.builder().ofItem();
}
public static ConfiguredItem of(@Nullable ItemConfig defaultItem) {
return CraftConfigValue.builder().ofItem(defaultItem);
}
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, 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);
}
}
@@ -0,0 +1,52 @@
package cc.carm.lib.mineconfiguration.bukkit.value;
import cc.carm.lib.mineconfiguration.bukkit.CraftConfigValue;
import cc.carm.lib.mineconfiguration.bukkit.data.TextConfig;
import cc.carm.lib.mineconfiguration.common.value.ConfigMessage;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.mineconfiguration.bukkit.builder.message.CraftMessageValueBuilder;
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, TextConfig, CommandSender> {
@NotNull
public static <M> CraftMessageValueBuilder<@Nullable M> create(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser) {
return CraftConfigValue.builder().createMessage().asValue(messageParser);
}
public static CraftMessageValueBuilder<String> asString() {
return CraftConfigValue.builder().createMessage().asStringValue();
}
public static ConfiguredMessage<String> ofString() {
return asString().build();
}
public static ConfiguredMessage<String> ofString(@NotNull String defaultMessage) {
return asString().defaults(defaultMessage).build();
}
public ConfiguredMessage(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
@Nullable 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, 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);
}
}
@@ -0,0 +1,46 @@
package cc.carm.lib.mineconfiguration.bukkit.value;
import cc.carm.lib.mineconfiguration.bukkit.CraftConfigValue;
import cc.carm.lib.mineconfiguration.bukkit.data.TextConfig;
import cc.carm.lib.mineconfiguration.common.value.ConfigMessageList;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.mineconfiguration.bukkit.builder.message.CraftMessageListBuilder;
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 ConfiguredMessageList<M> extends ConfigMessageList<M, TextConfig, CommandSender> {
@NotNull
public static <M> CraftMessageListBuilder<M> create(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser) {
return CraftConfigValue.builder().createMessage().asList(messageParser);
}
public static CraftMessageListBuilder<String> asStrings() {
return CraftConfigValue.builder().createMessage().asStringList();
}
public static ConfiguredMessageList<String> ofStrings(@NotNull String... defaultMessages) {
return asStrings().defaults(defaultMessages).build();
}
public ConfiguredMessageList(@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, headerComments, inlineComments, TextConfig.class, messages, params, messageParser, sendFunction, TextConfig::of);
}
public void broadcast(@NotNull Map<String, Object> placeholders) {
Bukkit.getOnlinePlayers().forEach(pl -> send(pl, placeholders));
send(Bukkit.getConsoleSender(), placeholders);
}
}
@@ -1,12 +1,12 @@
package cc.carm.lib.configuration.craft.value;
package cc.carm.lib.mineconfiguration.bukkit.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 cc.carm.lib.mineconfiguration.bukkit.CraftConfigValue;
import cc.carm.lib.mineconfiguration.bukkit.source.CraftConfigProvider;
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;
}
@@ -1,17 +1,17 @@
package cc.carm.lib.configuration.craft.value;
package cc.carm.lib.mineconfiguration.bukkit.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.sound.SoundConfigBuilder;
import cc.carm.lib.configuration.craft.data.SoundConfig;
import cc.carm.lib.mineconfiguration.bukkit.CraftConfigValue;
import cc.carm.lib.mineconfiguration.bukkit.builder.sound.SoundConfigBuilder;
import cc.carm.lib.mineconfiguration.bukkit.data.SoundConfig;
import org.bukkit.Sound;
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) {
@@ -0,0 +1,109 @@
package cc.carm.lib.mineconfiguration.bukkit.value;
import cc.carm.lib.mineconfiguration.bukkit.CraftConfigValue;
import cc.carm.lib.mineconfiguration.bukkit.data.TitleConfig;
import cc.carm.lib.mineconfiguration.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.mineconfiguration.bukkit.builder.title.TitleConfigBuilder;
import cc.carm.lib.mineconfiguration.bukkit.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,
int fadeIn, int stay, int 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 int fadeIn;
protected final int stay;
protected final int 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,
int fadeIn, int stay, int 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 = Integer.MAX_VALUE)
public int getFadeInTicks() {
return fadeIn;
}
@Range(from = 0L, to = Integer.MAX_VALUE)
public int getStayTicks() {
return stay;
}
@Range(from = 0L, to = Integer.MAX_VALUE)
public int 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);
}
}
+51
View File
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mineconfiguration-bukkit</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
<artifactId>mineconfiguration-bukkit-general</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>mineconfiguration-bukkit-base</artifactId>
<version>${project.parent.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@@ -1,6 +1,7 @@
package cc.carm.lib.configuration;
package cc.carm.lib.mineconfiguration;
import cc.carm.lib.configuration.bukkit.source.BukkitConfigProvider;
import cc.carm.lib.mineconfiguration.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);
}
}
@@ -1,12 +1,10 @@
package cc.carm.lib.configuration.bukkit.source;
package cc.carm.lib.mineconfiguration.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 +12,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 +54,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);
}
}
@@ -0,0 +1,146 @@
package cc.carm.lib.mineconfiguration.bukkit.source;
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.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class BukkitYAMLComments {
protected final @NotNull Map<String, List<String>> headerComments = new HashMap<>();
protected final @NotNull Map<String, String> inlineComments = new HashMap<>();
protected @NotNull Map<String, List<String>> getHeaderComments() {
return headerComments;
}
protected @NotNull Map<String, String> getInlineComments() {
return inlineComments;
}
public void setHeaderComments(@Nullable String path, @Nullable List<String> comments) {
if (comments == null) {
getHeaderComments().remove(path);
} else {
getHeaderComments().put(path, comments);
}
}
public void setInlineComment(@NotNull String path, @Nullable String comment) {
if (comment == null) {
getInlineComments().remove(path);
} else {
getInlineComments().put(path, comment);
}
}
@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";
}
/**
* 从一个文件读取配置并写入注释到某个写入器中。
* 该方法的部分源代码借鉴自 tchristofferson/ConfigUpdater 项目。
*
* @param source 源配置文件
* @param writer 配置写入器
* @throws IOException 当写入发生错误时抛出
*/
public void writeComments(@NotNull YamlConfiguration source, @NotNull BufferedWriter writer) throws IOException {
FileConfiguration temp = new YamlConfiguration(); // 该对象用于临时记录配置内容
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("[" + CraftConfigProvider.SEPARATOR + "]");
String trailingKey = splitFullKey[splitFullKey.length - 1];
if (currentValue instanceof ConfigurationSection) {
ConfigurationSection section = (ConfigurationSection) currentValue;
writer.write(indents + trailingKey + ":");
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();
temp.set(trailingKey, null);
yaml = yaml.substring(0, yaml.length() - 1);
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();
}
/**
* 得到一个键的缩进。
* 该方法的源代码来自 tchristofferson/ConfigUpdater 项目。
*
* @param key 键
* @return 该键的缩进文本
*/
protected static String getIndents(String key) {
String[] splitKey = key.split("[" + BukkitConfigProvider.SEPARATOR + "]");
return IntStream.range(1, splitKey.length).mapToObj(i -> " ").collect(Collectors.joining());
}
}
@@ -3,30 +3,24 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mineconfiguration-parent</artifactId>
<artifactId>mineconfiguration-bukkit</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.1.7</version>
<version>1.4.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
<artifactId>mineconfiguration-spigot</artifactId>
<artifactId>mineconfiguration-bukkit-native</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>easyconfiguration-core</artifactId>
<version>${easyconfiguration.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>mineconfiguration-craftbukkit</artifactId>
<artifactId>mineconfiguration-bukkit-base</artifactId>
<version>${project.parent.version}</version>
<scope>compile</scope>
</dependency>
@@ -1,6 +1,7 @@
package cc.carm.lib.configuration;
package cc.carm.lib.mineconfiguration;
import cc.carm.lib.configuration.spigot.source.SpigotConfigProvider;
import cc.carm.lib.mineconfiguration.bukkit.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);
}
}
@@ -0,0 +1,53 @@
package cc.carm.lib.mineconfiguration.bukkit.source;
import cc.carm.lib.configuration.core.ConfigInitializer;
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.Collections;
import java.util.List;
public class SpigotConfigProvider extends CraftConfigProvider {
public SpigotConfigProvider(@NotNull File file) {
super(file);
}
@Override
public void initializeConfig() {
this.configuration = YamlConfiguration.loadConfiguration(file);
this.initializer = new ConfigInitializer<>(this);
}
@Override
public void setHeaderComment(@Nullable String path, @Nullable List<String> comments) {
if (path == null) {
this.configuration.options().setHeader(comments);
} else {
this.configuration.setComments(path, comments);
}
}
@Override
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));
}
}
+54
View File
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mineconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.0</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
<artifactId>mineconfiguration-bukkit</artifactId>
<packaging>pom</packaging>
<modules>
<module>base</module>
<module>general</module>
<module>native</module>
</modules>
<name>MineConfiguration-Bukkit</name>
<dependencies>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.8.8-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<!--PlaceholderAPI for general placeholder's support-->
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.11.1</version>
<scope>provided</scope>
</dependency>
<!--ProtocolLib for general packet's function support-->
<dependency>
<groupId>com.comphenix.protocol</groupId>
<artifactId>ProtocolLib</artifactId>
<version>4.8.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
+11 -1
View File
@@ -5,7 +5,8 @@
<parent>
<artifactId>mineconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.1.7</version>
<version>1.4.0</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
@@ -15,6 +16,8 @@
<artifactId>mineconfiguration-bungee</artifactId>
<packaging>jar</packaging>
<name>MineConfiguration-Bungee</name>
<dependencies>
<dependency>
@@ -40,6 +43,13 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>mineconfiguration-common</artifactId>
<version>${project.parent.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
@@ -1,6 +1,7 @@
package cc.carm.lib.configuration;
package cc.carm.lib.mineconfiguration;
import cc.carm.lib.configuration.bungee.BungeeConfigProvider;
import cc.carm.lib.mineconfiguration.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);
}
}
@@ -0,0 +1,35 @@
package cc.carm.lib.mineconfiguration.bungee;
import cc.carm.lib.mineconfiguration.bungee.builder.BungeeConfigBuilder;
import cc.carm.lib.mineconfiguration.bungee.source.BungeeConfigProvider;
import cc.carm.lib.mineconfiguration.bungee.source.BungeeSectionWrapper;
import 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 List<String> headerComments, @Nullable String inlineComments,
@Nullable T defaultValue) {
super(provider, configPath, headerComments, inlineComments, defaultValue);
}
public BungeeConfigProvider getBukkitProvider() {
ConfigurationProvider<?> provider = getProvider();
if (provider instanceof BungeeConfigProvider) return (BungeeConfigProvider) getProvider();
else throw new IllegalStateException("Provider is not a SpigotConfigProvider");
}
public BungeeSectionWrapper getBukkitConfig() {
return getBukkitProvider().getConfiguration();
}
}
@@ -0,0 +1,13 @@
package cc.carm.lib.mineconfiguration.bungee.builder;
import cc.carm.lib.mineconfiguration.bungee.source.BungeeConfigProvider;
import cc.carm.lib.configuration.core.builder.AbstractConfigBuilder;
public abstract class AbstractBungeeBuilder<T, B extends AbstractBungeeBuilder<T, B>>
extends AbstractConfigBuilder<T, B, BungeeConfigProvider> {
public AbstractBungeeBuilder() {
super(BungeeConfigProvider.class);
}
}
@@ -0,0 +1,14 @@
package cc.carm.lib.mineconfiguration.bungee.builder;
import cc.carm.lib.mineconfiguration.bungee.builder.message.BungeeMessageBuilder;
import cc.carm.lib.configuration.core.builder.ConfigBuilder;
import org.jetbrains.annotations.NotNull;
public class BungeeConfigBuilder extends ConfigBuilder {
public @NotNull BungeeMessageBuilder createMessage() {
return new BungeeMessageBuilder();
}
}
@@ -0,0 +1,43 @@
package cc.carm.lib.mineconfiguration.bungee.builder.message;
import cc.carm.lib.mineconfiguration.bungee.data.MessageText;
import cc.carm.lib.mineconfiguration.common.builder.message.MessageConfigBuilder;
import cc.carm.lib.mineconfiguration.common.utils.ColorParser;
import net.md_5.bungee.api.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.function.BiFunction;
public class BungeeMessageBuilder extends MessageConfigBuilder<CommandSender, MessageText> {
public BungeeMessageBuilder() {
super(CommandSender.class, MessageText.class);
}
@Override
public @NotNull <M> BungeeMessageValueBuilder<M> asValue(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
return new BungeeMessageValueBuilder<>(parser);
}
@Override
public @NotNull <M> BungeeMessageListBuilder<M> asList(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
return new BungeeMessageListBuilder<>(parser);
}
public @NotNull
BungeeMessageValueBuilder<String> asStringValue() {
return asValue(defaultParser()).whenSend(CommandSender::sendMessage);
}
public @NotNull
BungeeMessageListBuilder<String> asStringList() {
return asList(defaultParser()).whenSend((r, m) -> m.forEach(r::sendMessage));
}
protected static @NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable String> defaultParser() {
return (receiver, message) -> ColorParser.parse(message);
}
}
@@ -0,0 +1,36 @@
package cc.carm.lib.mineconfiguration.bungee.builder.message;
import cc.carm.lib.mineconfiguration.bungee.data.MessageText;
import cc.carm.lib.mineconfiguration.bungee.value.ConfiguredMessageList;
import cc.carm.lib.mineconfiguration.common.builder.message.MessageListBuilder;
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
import net.md_5.bungee.api.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Optional;
import java.util.function.BiFunction;
public class BungeeMessageListBuilder<M>
extends MessageListBuilder<M, CommandSender, MessageText, BungeeMessageListBuilder<M>> {
public BungeeMessageListBuilder(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
super(CommandSender.class, MessageText::of, parser);
}
@Override
protected @NotNull BungeeMessageListBuilder<M> getThis() {
return this;
}
@Override
public @NotNull ConfiguredMessageList<M> build() {
return new ConfiguredMessageList<>(
this.provider, this.path, this.headerComments, this.inlineComment,
Optional.ofNullable(this.defaultValue).orElse(MessageText.of(new ArrayList<>())),
ParamsUtils.formatParams(this.paramFormatter, this.params),
this.messageParser, this.sendFunction
);
}
}
@@ -0,0 +1,37 @@
package cc.carm.lib.mineconfiguration.bungee.builder.message;
import cc.carm.lib.mineconfiguration.bungee.data.MessageText;
import cc.carm.lib.mineconfiguration.bungee.value.ConfiguredMessage;
import cc.carm.lib.mineconfiguration.common.builder.message.MessageValueBuilder;
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
import net.md_5.bungee.api.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Optional;
import java.util.function.BiFunction;
public class BungeeMessageValueBuilder<M>
extends MessageValueBuilder<M, CommandSender, MessageText, BungeeMessageValueBuilder<M>> {
public BungeeMessageValueBuilder(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
super(CommandSender.class, MessageText::new, parser);
}
@Override
protected @NotNull BungeeMessageValueBuilder<M> getThis() {
return this;
}
@Override
public @NotNull ConfiguredMessage<M> build() {
return new ConfiguredMessage<>(
this.provider, this.path, this.headerComments, this.inlineComment,
Optional.ofNullable(this.defaultValue).orElse(MessageText.of("")),
ParamsUtils.formatParams(this.paramFormatter, this.params),
this.messageParser, this.sendHandler
);
}
}
@@ -0,0 +1,35 @@
package cc.carm.lib.mineconfiguration.bungee.data;
import cc.carm.lib.mineconfiguration.common.data.AbstractText;
import net.md_5.bungee.api.CommandSender;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class MessageText extends AbstractText<CommandSender> {
public MessageText(@NotNull String message) {
super(CommandSender.class, message);
}
@Contract("!null,-> !null")
public static @Nullable MessageText of(@Nullable String message) {
if (message == null) return null;
else return new MessageText(message);
}
public static @NotNull List<MessageText> of(@Nullable List<String> messages) {
if (messages == null || messages.isEmpty()) return new ArrayList<>();
else return messages.stream().map(MessageText::of).collect(Collectors.toList());
}
public static @NotNull List<MessageText> of(@NotNull String... messages) {
return of(Arrays.asList(messages));
}
}
@@ -1,15 +1,16 @@
package cc.carm.lib.configuration.bungee;
package cc.carm.lib.mineconfiguration.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> {
@@ -33,7 +34,7 @@ public class BungeeConfigProvider extends FileConfigProvider<BungeeSectionWrappe
}
@Override
public void reload() throws Exception {
protected void onReload() throws Exception {
this.configuration = getLoader().load(file);
}
@@ -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
@@ -1,4 +1,4 @@
package cc.carm.lib.configuration.bungee;
package cc.carm.lib.mineconfiguration.bungee.source;
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
import net.md_5.bungee.config.Configuration;
@@ -0,0 +1,52 @@
package cc.carm.lib.mineconfiguration.bungee.value;
import cc.carm.lib.mineconfiguration.bungee.BungeeConfigValue;
import cc.carm.lib.mineconfiguration.bungee.builder.message.BungeeMessageValueBuilder;
import cc.carm.lib.mineconfiguration.bungee.data.MessageText;
import cc.carm.lib.mineconfiguration.common.value.ConfigMessage;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.ProxyServer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
public class ConfiguredMessage<M> extends ConfigMessage<M, MessageText, CommandSender> {
@NotNull
public static <M> BungeeMessageValueBuilder<@Nullable M> create(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser) {
return BungeeConfigValue.builder().createMessage().asValue(messageParser);
}
public static BungeeMessageValueBuilder<String> asString() {
return BungeeConfigValue.builder().createMessage().asStringValue();
}
public static ConfiguredMessage<String> ofString() {
return asString().build();
}
public static ConfiguredMessage<String> ofString(@NotNull String defaultMessage) {
return asString().defaults(defaultMessage).build();
}
public ConfiguredMessage(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
@Nullable 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, 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);
}
}
@@ -0,0 +1,46 @@
package cc.carm.lib.mineconfiguration.bungee.value;
import cc.carm.lib.mineconfiguration.bungee.BungeeConfigValue;
import cc.carm.lib.mineconfiguration.bungee.builder.message.BungeeMessageListBuilder;
import cc.carm.lib.mineconfiguration.bungee.data.MessageText;
import cc.carm.lib.mineconfiguration.common.value.ConfigMessageList;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.ProxyServer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
public class ConfiguredMessageList<M> extends ConfigMessageList<M, MessageText, CommandSender> {
@NotNull
public static <M> BungeeMessageListBuilder<M> create(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser) {
return BungeeConfigValue.builder().createMessage().asList(messageParser);
}
public static BungeeMessageListBuilder<String> asStrings() {
return BungeeConfigValue.builder().createMessage().asStringList();
}
public static ConfiguredMessageList<String> ofStrings(@NotNull String... defaultMessages) {
return asStrings().defaults(defaultMessages).build();
}
public ConfiguredMessageList(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
@Nullable 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, headerComments, inlineComments, MessageText.class, messages, params, messageParser, sendFunction, MessageText::of);
}
public void broadcast(@NotNull Map<String, Object> placeholders) {
ProxyServer.getInstance().getPlayers().forEach(pl -> send(pl, placeholders));
send(ProxyServer.getInstance().getConsole(), placeholders);
}
}
+14 -14
View File
@@ -4,11 +4,10 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<modules>
<module>bungee</module>
<module>common</module>
<module>craftbukkit</module>
<module>bukkit</module>
<module>spigot</module>
<module>platform/bungee</module>
<module>platform/bukkit</module>
</modules>
<properties>
<java.version>1.8</java.version>
@@ -17,11 +16,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.1.0</easyconfiguration.version>
</properties>
<groupId>cc.carm.lib</groupId>
<artifactId>mineconfiguration-parent</artifactId>
<version>1.1.7</version>
<version>1.4.0</version>
<packaging>pom</packaging>
<name>MineConfiguration</name>
@@ -70,6 +69,11 @@
<url>https://repo.carm.cc/repository/maven-public/</url>
</repository>
<repository>
<id>minebench-repo</id>
<url>https://repo.minebench.de/</url>
</repository>
<repository>
<id>nexus</id>
<url>https://mvn.lumine.io/repository/maven-public/</url>
@@ -80,12 +84,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>
@@ -169,11 +167,13 @@
<charset>UTF-8</charset>
<docencoding>UTF-8</docencoding>
<locale>zh_CN</locale>
<includeDependencySources>true</includeDependencySources>
<dependencySourceIncludes>
<dependencySourceInclude>cc.carm.lib:*</dependencySourceInclude>
</dependencySourceIncludes>
<additionalOptions>
<additionalOption>-Xdoclint:none</additionalOption>
</additionalOptions>
</configuration>
<executions>
<execution>
@@ -286,7 +286,7 @@
</distributionManagement>
</profile>
</profiles>
</project>
@@ -1,57 +0,0 @@
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 java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SpigotConfigProvider extends CraftConfigProvider {
public SpigotConfigProvider(@NotNull File file) {
super(file);
}
@Override
public void initializeConfig() {
this.configuration = YamlConfiguration.loadConfiguration(file);
this.initializer = new ConfigInitializer<>(this);
}
@Override
public void setComment(@Nullable String path, @Nullable ConfigCommentInfo commentInfo) {
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()));
}
} 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);
}
}
}
@Override
public @Nullable ConfigCommentInfo getComment(@Nullable String path) {
return null;
}
}