1
mirror of https://github.com/CarmJos/MineConfiguration.git synced 2026-06-04 21:58:16 +08:00

[1.3.0] 版本更新

1. 更新 EasyConfiguration 到 3.0.0 (破坏性更新)。
2. 添加 ConfiguredTitle 用于快捷向玩家发送title消息。
3. 为 ConfiguredItem 添加name和lore的原生params变量支持。
4. 添加 ProtocolLibHelper 用于支持多版本情况下的部分功能。(如需使用请安装 ProtocolLib 插件)
This commit is contained in:
2022-05-19 01:45:16 +08:00
parent af614deae6
commit 98ee47f676
51 changed files with 895 additions and 370 deletions
+8 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>mineconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.2.2</version>
<version>1.3.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
@@ -45,6 +45,13 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.comphenix.protocol</groupId>
<artifactId>ProtocolLib</artifactId>
<version>4.8.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
@@ -1,23 +1,25 @@
package cc.carm.lib.configuration.craft;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
import cc.carm.lib.configuration.craft.builder.CraftConfigBuilder;
import cc.carm.lib.configuration.craft.source.CraftConfigProvider;
import cc.carm.lib.configuration.craft.source.CraftSectionWrapper;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
public abstract class CraftConfigValue<T> extends CachedConfigValue<T> {
public static @NotNull CraftConfigBuilder builder() {
return new CraftConfigBuilder();
}
public CraftConfigValue(@Nullable CraftConfigProvider provider,
@Nullable String configPath, @Nullable ConfigCommentInfo comments, @Nullable T defaultValue) {
super(provider, configPath, comments, defaultValue);
public CraftConfigValue(@Nullable CraftConfigProvider provider, @Nullable String sectionPath,
@Nullable List<String> headerComments, @Nullable String inlineComments,
@Nullable T defaultValue) {
super(provider, sectionPath, headerComments, inlineComments, defaultValue);
}
public CraftConfigProvider getBukkitProvider() {
@@ -5,6 +5,7 @@ import cc.carm.lib.configuration.craft.builder.item.ItemConfigBuilder;
import cc.carm.lib.configuration.craft.builder.message.CraftMessageBuilder;
import cc.carm.lib.configuration.craft.builder.serializable.SerializableBuilder;
import cc.carm.lib.configuration.craft.builder.sound.SoundConfigBuilder;
import cc.carm.lib.configuration.craft.builder.title.TitleConfigBuilder;
import cc.carm.lib.configuration.craft.data.ItemConfig;
import cc.carm.lib.configuration.craft.value.ConfiguredItem;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
@@ -25,6 +26,10 @@ public class CraftConfigBuilder extends ConfigBuilder {
return new CraftMessageBuilder();
}
public @NotNull TitleConfigBuilder createTitle() {
return new TitleConfigBuilder();
}
public <V extends ConfigurationSerializable> @NotNull SerializableBuilder<V> ofSerializable(@NotNull Class<V> valueClass) {
return new SerializableBuilder<>(valueClass);
}
@@ -1,5 +1,6 @@
package cc.carm.lib.configuration.craft.builder.item;
import cc.carm.lib.configuration.common.utils.ParamsUtils;
import cc.carm.lib.configuration.craft.builder.AbstractCraftBuilder;
import cc.carm.lib.configuration.craft.data.ItemConfig;
import cc.carm.lib.configuration.craft.value.ConfiguredItem;
@@ -11,6 +12,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
public class ItemConfigBuilder extends AbstractCraftBuilder<ItemConfig, ItemConfigBuilder> {
@@ -19,6 +21,9 @@ public class ItemConfigBuilder extends AbstractCraftBuilder<ItemConfig, ItemConf
protected String name;
protected List<String> lore = new ArrayList<>();
protected @NotNull String[] params;
protected @NotNull Function<@NotNull String, @NotNull String> paramFormatter = ParamsUtils.DEFAULT_PARAM_FORMATTER;
public ItemConfigBuilder defaults(@NotNull Material type,
@Nullable String name, @NotNull String... lore) {
return defaults(type, (short) 0, name, Arrays.asList(lore));
@@ -58,6 +63,21 @@ public class ItemConfigBuilder extends AbstractCraftBuilder<ItemConfig, ItemConf
return this;
}
public ItemConfigBuilder formatParam(@NotNull Function<@NotNull String, @NotNull String> paramFormatter) {
this.paramFormatter = paramFormatter;
return getThis();
}
public ItemConfigBuilder params(@NotNull String... params) {
this.params = params;
return getThis();
}
public ItemConfigBuilder params(@NotNull List<String> params) {
this.params = params.toArray(new String[0]);
return getThis();
}
@Override
protected @NotNull ItemConfigBuilder getThis() {
return this;
@@ -71,7 +91,11 @@ public class ItemConfigBuilder extends AbstractCraftBuilder<ItemConfig, ItemConf
@Override
public @NotNull ConfiguredItem build() {
ItemConfig defaultItem = Optional.ofNullable(this.defaultValue).orElse(buildDefault());
return new ConfiguredItem(this.provider, this.path, buildComments(), defaultItem);
return new ConfiguredItem(this.provider, this.path, this.headerComments, this.inlineComment, defaultItem, buildParams());
}
protected final String[] buildParams() {
return Arrays.stream(params).map(param -> paramFormatter.apply(param)).toArray(String[]::new);
}
}
@@ -1,22 +1,20 @@
package cc.carm.lib.configuration.craft.builder.message;
import cc.carm.lib.configuration.common.builder.message.MessageConfigBuilder;
import cc.carm.lib.configuration.common.utils.ColorParser;
import cc.carm.lib.configuration.craft.data.MessageText;
import cc.carm.lib.configuration.craft.utils.PAPIHelper;
import org.bukkit.Bukkit;
import cc.carm.lib.configuration.craft.data.TextConfig;
import cc.carm.lib.configuration.craft.utils.TextParser;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.function.BiFunction;
public class CraftMessageBuilder extends MessageConfigBuilder<CommandSender, MessageText> {
public class CraftMessageBuilder extends MessageConfigBuilder<CommandSender, TextConfig> {
public CraftMessageBuilder() {
super(CommandSender.class, MessageText.class);
super(CommandSender.class, TextConfig.class);
}
@Override
@@ -40,16 +38,7 @@ public class CraftMessageBuilder extends MessageConfigBuilder<CommandSender, Mes
}
protected static @NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable String> defaultParser() {
return (receiver, message) -> {
if (receiver instanceof Player && hasPlaceholderAPI()) {
message = PAPIHelper.parseMessages((Player) receiver, message);
}
return ColorParser.parse(message);
};
}
public static boolean hasPlaceholderAPI() {
return Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null;
return (receiver, message) -> TextParser.parseText(receiver, message, new HashMap<>());
}
}
@@ -1,7 +1,8 @@
package cc.carm.lib.configuration.craft.builder.message;
import cc.carm.lib.configuration.common.builder.message.MessageListBuilder;
import cc.carm.lib.configuration.craft.data.MessageText;
import cc.carm.lib.configuration.common.utils.ParamsUtils;
import cc.carm.lib.configuration.craft.data.TextConfig;
import cc.carm.lib.configuration.craft.value.ConfiguredMessageList;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
@@ -12,10 +13,10 @@ import java.util.Optional;
import java.util.function.BiFunction;
public class CraftMessageListBuilder<M>
extends MessageListBuilder<M, CommandSender, MessageText, CraftMessageListBuilder<M>> {
extends MessageListBuilder<M, CommandSender, TextConfig, CraftMessageListBuilder<M>> {
public CraftMessageListBuilder(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
super(CommandSender.class, MessageText::of, parser);
super(CommandSender.class, TextConfig::of, parser);
}
@Override
@@ -26,9 +27,10 @@ public class CraftMessageListBuilder<M>
@Override
public @NotNull ConfiguredMessageList<M> build() {
return new ConfiguredMessageList<>(
this.provider, this.path, buildComments(),
Optional.ofNullable(this.defaultValue).orElse(MessageText.of(new ArrayList<>())),
buildParams(), this.messageParser, this.sendFunction
this.provider, this.path, this.headerComments, this.inlineComment,
Optional.ofNullable(this.defaultValue).orElse(TextConfig.of(new ArrayList<>())),
ParamsUtils.formatParams(this.paramFormatter, this.params),
this.messageParser, this.sendFunction
);
}
}
@@ -1,7 +1,8 @@
package cc.carm.lib.configuration.craft.builder.message;
import cc.carm.lib.configuration.common.builder.message.MessageValueBuilder;
import cc.carm.lib.configuration.craft.data.MessageText;
import cc.carm.lib.configuration.common.utils.ParamsUtils;
import cc.carm.lib.configuration.craft.data.TextConfig;
import cc.carm.lib.configuration.craft.value.ConfiguredMessage;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
@@ -11,10 +12,10 @@ import java.util.Optional;
import java.util.function.BiFunction;
public class CraftMessageValueBuilder<M>
extends MessageValueBuilder<M, CommandSender, MessageText, CraftMessageValueBuilder<M>> {
extends MessageValueBuilder<M, CommandSender, TextConfig, CraftMessageValueBuilder<M>> {
public CraftMessageValueBuilder(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
super(CommandSender.class, MessageText::new, parser);
super(CommandSender.class, TextConfig::new, parser);
}
@Override
@@ -25,9 +26,10 @@ public class CraftMessageValueBuilder<M>
@Override
public @NotNull ConfiguredMessage<M> build() {
return new ConfiguredMessage<>(
this.provider, this.path, buildComments(),
Optional.ofNullable(this.defaultValue).orElse(MessageText.of("")),
buildParams(), this.messageParser, this.sendHandler
this.provider, this.path, this.headerComments, this.inlineComment,
Optional.ofNullable(this.defaultValue).orElse(TextConfig.of("")),
ParamsUtils.formatParams(this.paramFormatter, this.params),
this.messageParser, this.sendHandler
);
}
@@ -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);
}
}
@@ -10,7 +10,7 @@ public class SoundConfigBuilder extends AbstractCraftBuilder<SoundConfig, SoundC
public @NotNull SoundConfigBuilder defaults(@NotNull Sound sound, float volume, float pitch) {
return defaults(new SoundConfig(sound, volume, pitch));
return defaults(new SoundConfig(sound.name(), sound, volume, pitch));
}
public @NotNull SoundConfigBuilder defaults(@NotNull Sound sound, float volume) {
@@ -21,6 +21,18 @@ public class SoundConfigBuilder extends AbstractCraftBuilder<SoundConfig, SoundC
return defaults(sound, 1.0f);
}
public @NotNull SoundConfigBuilder defaults(@NotNull String soundName, float volume, float pitch) {
return defaults(new SoundConfig(soundName, volume, pitch));
}
public @NotNull SoundConfigBuilder defaults(@NotNull String soundName, float volume) {
return defaults(soundName, volume, 1.0f);
}
public @NotNull SoundConfigBuilder defaults(@NotNull String soundName) {
return defaults(soundName, 1.0f);
}
@Override
protected @NotNull SoundConfigBuilder getThis() {
return this;
@@ -28,7 +40,7 @@ public class SoundConfigBuilder extends AbstractCraftBuilder<SoundConfig, SoundC
@Override
public @NotNull ConfiguredSound build() {
return new ConfiguredSound(this.provider, this.path, buildComments(), this.defaultValue);
return new ConfiguredSound(this.provider, this.path, this.headerComments, this.inlineComment, this.defaultValue);
}
}
@@ -0,0 +1,97 @@
package cc.carm.lib.configuration.craft.builder.title;
import cc.carm.lib.configuration.common.utils.ParamsUtils;
import cc.carm.lib.configuration.craft.builder.AbstractCraftBuilder;
import cc.carm.lib.configuration.craft.data.TitleConfig;
import cc.carm.lib.configuration.craft.function.TitleSendConsumer;
import cc.carm.lib.configuration.craft.utils.ProtocolLibHelper;
import cc.carm.lib.configuration.craft.value.ConfiguredTitle;
import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Range;
import java.util.List;
import java.util.function.Function;
public class TitleConfigBuilder extends AbstractCraftBuilder<TitleConfig, TitleConfigBuilder> {
@SuppressWarnings("deprecation")
protected static @NotNull TitleSendConsumer DEFAULT_TITLE_CONSUMER = (player, fadeIn, stay, fadeOut, line1, line2) -> {
if (Bukkit.getPluginManager().isPluginEnabled("ProtocolLib")) {
try {
ProtocolLibHelper.sendTitle(player, fadeIn, stay, fadeOut, line1, line2);
} catch (Exception ignored) {
}
} else {
player.sendTitle(line1, line2);
}
};
protected @NotNull String[] params;
protected @Range(from = 0L, to = Integer.MAX_VALUE) long fadeIn = 10;
protected @Range(from = 0L, to = Integer.MAX_VALUE) long stay = 60;
protected @Range(from = 0L, to = Integer.MAX_VALUE) long fadeOut = 10;
protected @NotNull TitleSendConsumer sendConsumer;
protected @NotNull Function<@NotNull String, @NotNull String> paramFormatter;
public TitleConfigBuilder() {
this.sendConsumer = TitleConfigBuilder.DEFAULT_TITLE_CONSUMER;
this.paramFormatter = ParamsUtils.DEFAULT_PARAM_FORMATTER;
}
public @NotNull TitleConfigBuilder defaults(@Nullable String line1,
@Nullable String line2) {
return defaults(TitleConfig.of(line1, line2));
}
public @NotNull TitleConfigBuilder whenSend(@NotNull TitleSendConsumer consumer) {
this.sendConsumer = consumer;
return this;
}
public TitleConfigBuilder params(String... params) {
this.params = params;
return this;
}
public TitleConfigBuilder params(@NotNull List<String> params) {
return params(params.toArray(new String[0]));
}
public TitleConfigBuilder fadeIn(@Range(from = 0L, to = Integer.MAX_VALUE) long fadeIn) {
this.fadeIn = fadeIn;
return this;
}
public TitleConfigBuilder stay(@Range(from = 0L, to = Integer.MAX_VALUE) long stay) {
this.stay = stay;
return this;
}
public TitleConfigBuilder fadeOut(@Range(from = 0L, to = Integer.MAX_VALUE) long fadeOut) {
this.fadeOut = fadeOut;
return this;
}
public TitleConfigBuilder formatParam(Function<String, String> paramFormatter) {
this.paramFormatter = paramFormatter;
return this;
}
@Override
protected @NotNull TitleConfigBuilder getThis() {
return this;
}
@Override
public @NotNull ConfiguredTitle build() {
return new ConfiguredTitle(
this.provider, this.path, this.headerComments, this.inlineComment,
this.defaultValue, this.params, this.sendConsumer,
this.fadeIn, this.stay, this.fadeOut
);
}
}
@@ -1,9 +1,7 @@
package cc.carm.lib.configuration.craft.data;
import cc.carm.lib.configuration.common.utils.ColorParser;
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
import cc.carm.lib.configuration.craft.utils.PAPIHelper;
import org.bukkit.Bukkit;
import cc.carm.lib.configuration.craft.utils.TextParser;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
@@ -11,11 +9,7 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.*;
public class ItemConfig {
@@ -44,28 +38,43 @@ public class ItemConfig {
return name;
}
public @Nullable String getName(@Nullable Player player, @NotNull Map<String, Object> placeholders) {
return Optional.ofNullable(getName())
.map(name -> TextParser.parseText(player, name, placeholders))
.orElse(null);
}
public @NotNull List<String> getLore() {
return lore;
}
public @Nullable List<String> getLore(@Nullable Player player, @NotNull Map<String, Object> placeholders) {
if (getLore().isEmpty()) return null;
else return TextParser.parseList(player, getLore(), placeholders);
}
public final @NotNull ItemStack getItemStack() {
return getItemStack(1);
}
public @NotNull ItemStack getItemStack(int amount) {
return getItemStack(null, amount);
return getItemStack(null, amount, new HashMap<>());
}
public @NotNull ItemStack getItemStack(@Nullable Player player) {
return getItemStack(player, 1);
return getItemStack(player, new HashMap<>());
}
public @NotNull ItemStack getItemStack(@Nullable Player player, int amount) {
public @NotNull ItemStack getItemStack(@Nullable Player player, @NotNull Map<String, Object> placeholders) {
return getItemStack(player, 1, placeholders);
}
public @NotNull ItemStack getItemStack(@Nullable Player player, int amount, @NotNull Map<String, Object> placeholders) {
ItemStack item = new ItemStack(type, amount, data);
ItemMeta meta = item.getItemMeta();
if (meta == null) return item;
if (getName() != null) meta.setDisplayName(parseName(player, getName()));
if (!getLore().isEmpty()) meta.setLore(parseLore(player, getLore()));
Optional.ofNullable(getName(player, placeholders)).ifPresent(meta::setDisplayName);
Optional.ofNullable(getLore(player, placeholders)).ifPresent(meta::setLore);
item.setItemMeta(meta);
return item;
}
@@ -89,31 +98,8 @@ public class ItemConfig {
else return new ItemConfig(
type, section.getShort("data", (short) 0),
section.getString("name"),
parseStringList(section.getList("lore"))
section.getStringList("lore")
);
}
private static List<String> parseStringList(@Nullable List<?> data) {
if (data == null) return new ArrayList<>();
else return data.stream()
.map(o -> o instanceof String ? (String) o : o.toString())
.collect(Collectors.toList());
}
protected static @NotNull String parseName(@Nullable Player player, String message) {
if (player != null && hasPlaceholderAPI()) message = PAPIHelper.parseMessages(player, message);
return ColorParser.parse(message);
}
protected static @NotNull List<String> parseLore(@Nullable Player player, List<String> messages) {
if (player != null && hasPlaceholderAPI()) messages = PAPIHelper.parseMessages(player, messages);
return ColorParser.parse(messages);
}
public static boolean hasPlaceholderAPI() {
return Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null;
}
}
@@ -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
@@ -11,24 +11,24 @@ import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class MessageText extends AbstractText<CommandSender> {
public class TextConfig extends AbstractText<CommandSender> {
public MessageText(@NotNull String message) {
public TextConfig(@NotNull String message) {
super(CommandSender.class, message);
}
@Contract("!null,-> !null")
public static @Nullable MessageText of(@Nullable String message) {
public static @Nullable TextConfig of(@Nullable String message) {
if (message == null) return null;
else return new MessageText(message);
else return new TextConfig(message);
}
public static @NotNull List<MessageText> of(@Nullable List<String> messages) {
public static @NotNull List<TextConfig> of(@Nullable List<String> messages) {
if (messages == null || messages.isEmpty()) return new ArrayList<>();
else return messages.stream().map(MessageText::of).collect(Collectors.toList());
else return messages.stream().map(TextConfig::of).collect(Collectors.toList());
}
public static @NotNull List<MessageText> of(@NotNull String... messages) {
public static @NotNull List<TextConfig> of(@NotNull String... messages) {
return of(Arrays.asList(messages));
}
@@ -0,0 +1,68 @@
package cc.carm.lib.configuration.craft.data;
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
import cc.carm.lib.configuration.craft.function.TitleSendConsumer;
import cc.carm.lib.configuration.craft.utils.TextParser;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Range;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
public class TitleConfig {
public static @NotNull TitleConfig of(@Nullable String line1, @Nullable String line2) {
return of(
Optional.ofNullable(line1).map(TextConfig::of).orElse(null),
Optional.ofNullable(line2).map(TextConfig::of).orElse(null)
);
}
public static @NotNull TitleConfig of(@Nullable TextConfig line1, @Nullable TextConfig line2) {
return new TitleConfig(line1, line2);
}
protected @Nullable TextConfig line1;
protected @Nullable TextConfig line2;
protected TitleConfig(@Nullable TextConfig line1, @Nullable TextConfig line2) {
this.line1 = line1;
this.line2 = line2;
}
public void send(@NotNull Player player,
@Range(from = 0L, to = Long.MAX_VALUE) long fadeIn,
@Range(from = 0L, to = Long.MAX_VALUE) long stay,
@Range(from = 0L, to = Long.MAX_VALUE) long fadeOut,
@NotNull Map<String, Object> placeholders,
@Nullable TitleSendConsumer sendConsumer) {
if (this.line1 == null && this.line2 == null) return;
if (sendConsumer == null) return;
sendConsumer.send(
player, fadeIn, stay, fadeOut,
parseLine(this.line1, player, placeholders),
parseLine(this.line2, player, placeholders)
);
}
protected @NotNull String parseLine(@Nullable TextConfig text,
@NotNull Player player, @NotNull Map<String, Object> placeholders) {
if (text == null) return "";
else return TextParser.parseText(player, text.getMessage(), placeholders);
}
public @NotNull Map<String, Object> serialize() {
Map<String, Object> map = new LinkedHashMap<>();
if (this.line1 != null) map.put("line1", this.line1.getMessage());
if (this.line2 != null) map.put("line2", this.line2.getMessage());
return map;
}
public static @NotNull TitleConfig deserialize(@NotNull ConfigurationWrapper section) {
return of(section.getString("line1"), section.getString("line2"));
}
}
@@ -0,0 +1,26 @@
package cc.carm.lib.configuration.craft.function;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Range;
@FunctionalInterface
public interface TitleSendConsumer {
/**
* 向目标玩家发送标题文字
*
* @param player 目标玩家
* @param fadeIn 淡入时间 (ticks)
* @param stay 保留时间 (ticks)
* @param fadeOut 淡出时间 (ticks)
* @param line1 第一行文字
* @param line2 第二行文字
*/
void send(@NotNull Player player,
@Range(from = 0L, to = Integer.MAX_VALUE) long fadeIn,
@Range(from = 0L, to = Integer.MAX_VALUE) long stay,
@Range(from = 0L, to = Integer.MAX_VALUE) long fadeOut,
@NotNull String line1, @NotNull String line2);
}
@@ -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,19 +0,0 @@
package cc.carm.lib.configuration.craft.utils;
import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.entity.Player;
import java.util.List;
public class PAPIHelper {
public static String parseMessages(Player player, String message) {
return PlaceholderAPI.setPlaceholders(player, message);
}
public static List<String> parseMessages(Player player, List<String> messages) {
return PlaceholderAPI.setPlaceholders(player, messages);
}
}
@@ -0,0 +1,27 @@
package cc.carm.lib.configuration.craft.utils;
import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.entity.Player;
import java.util.List;
public class PlaceholderAPIHelper {
public static String parseMessages(Player player, String message) {
try {
return PlaceholderAPI.setPlaceholders(player, message);
} catch (Exception ignored) {
return message;
}
}
public static List<String> parseMessages(Player player, List<String> messages) {
try {
return PlaceholderAPI.setPlaceholders(player, messages);
} catch (Exception ignored) {
return messages;
}
}
}
@@ -0,0 +1,41 @@
package cc.carm.lib.configuration.craft.utils;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.EnumWrappers;
import com.comphenix.protocol.wrappers.WrappedChatComponent;
import org.bukkit.entity.Player;
public class ProtocolLibHelper {
@SuppressWarnings("deprecation")
public static void sendTitle(Player player, long fadeIn, long stay, long fadeOut, String line1, String line2) throws Exception {
ProtocolManager pm = ProtocolLibrary.getProtocolManager();
if (line1 != null) {
PacketContainer packet = pm.createPacket(PacketType.Play.Server.TITLE);
packet.getTitleActions().write(0, EnumWrappers.TitleAction.TITLE);
packet.getChatComponents().write(0, WrappedChatComponent.fromText(line1));
pm.sendServerPacket(player, packet, false);
}
if (line2 != null) {
PacketContainer packet = pm.createPacket(PacketType.Play.Server.TITLE);
packet.getTitleActions().write(0, EnumWrappers.TitleAction.SUBTITLE);
packet.getChatComponents().write(0, WrappedChatComponent.fromText(line2));
pm.sendServerPacket(player, packet, false);
}
PacketContainer timePacket = pm.createPacket(PacketType.Play.Server.TITLE);
timePacket.getTitleActions().write(0, EnumWrappers.TitleAction.TIMES);
timePacket.getIntegers()
.write(0, Math.toIntExact(fadeIn))
.write(1, Math.toIntExact(stay))
.write(2, Math.toIntExact(fadeOut));
pm.sendServerPacket(player, timePacket, false);
}
}
@@ -0,0 +1,38 @@
package cc.carm.lib.configuration.craft.utils;
import cc.carm.lib.configuration.common.utils.ColorParser;
import cc.carm.lib.configuration.common.utils.ParamsUtils;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class TextParser {
@Contract("_,!null,_->!null")
public static @Nullable String parseText(@Nullable CommandSender sender, @Nullable String message, @NotNull Map<String, Object> placeholders) {
if (message == null) return null;
if (sender instanceof Player && hasPlaceholderAPI()) {
message = PlaceholderAPIHelper.parseMessages((Player) sender, message);
}
return ColorParser.parse(ParamsUtils.setPlaceholders(message, placeholders));
}
public static @NotNull List<String> parseList(@Nullable CommandSender sender, List<String> messages, @NotNull Map<String, Object> placeholders) {
if (sender instanceof Player && hasPlaceholderAPI()) {
messages = PlaceholderAPIHelper.parseMessages((Player) sender, messages);
}
return ColorParser.parse(messages.stream().map(s -> ParamsUtils.setPlaceholders(s, placeholders)).collect(Collectors.toList()));
}
public static boolean hasPlaceholderAPI() {
return Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null;
}
}
@@ -1,15 +1,22 @@
package cc.carm.lib.configuration.craft.value;
import cc.carm.lib.configuration.common.utils.ParamsUtils;
import cc.carm.lib.configuration.core.function.ConfigValueParser;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
import cc.carm.lib.configuration.core.value.type.ConfiguredSection;
import cc.carm.lib.configuration.craft.CraftConfigValue;
import cc.carm.lib.configuration.craft.builder.item.ItemConfigBuilder;
import cc.carm.lib.configuration.craft.data.ItemConfig;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ConfiguredItem extends ConfiguredSection<ItemConfig> {
public static ItemConfigBuilder create() {
@@ -24,14 +31,45 @@ public class ConfiguredItem extends ConfiguredSection<ItemConfig> {
return CraftConfigValue.builder().ofItem(defaultItem);
}
public ConfiguredItem(@Nullable ConfigurationProvider<?> provider,
@Nullable String sectionPath, @Nullable ConfigCommentInfo comments,
@Nullable ItemConfig defaultValue) {
super(provider, sectionPath, comments, ItemConfig.class, defaultValue, getItemParser(), ItemConfig::serialize);
protected final @NotNull String[] params;
public ConfiguredItem(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
@Nullable List<String> headerComments, @Nullable String inlineComments,
@Nullable ItemConfig defaultValue, @NotNull String[] params) {
super(provider, sectionPath, headerComments, inlineComments, ItemConfig.class, defaultValue, getItemParser(), ItemConfig::serialize);
this.params = params;
}
public static ConfigValueParser<ConfigurationWrapper, ItemConfig> getItemParser() {
return (s, d) -> ItemConfig.deserialize(s);
}
public @NotNull String[] getParams() {
return params;
}
public @Nullable ItemStack getItem(@Nullable Player player) {
return getItem(player, 1);
}
public @Nullable ItemStack getItem(@Nullable Player player, int amount) {
return getItem(player, amount, new HashMap<>());
}
public @Nullable ItemStack getItem(@Nullable Player player, @NotNull Object... values) {
return getItem(player, 1, values);
}
public @Nullable ItemStack getItem(@Nullable Player player, int amount, @NotNull Object... values) {
return getItem(player, amount, ParamsUtils.buildParams(params, values));
}
public @Nullable ItemStack getItem(@Nullable Player player, int amount, @NotNull String[] params, @NotNull Object[] values) {
return getItem(player, amount, ParamsUtils.buildParams(params, values));
}
public @Nullable ItemStack getItem(@Nullable Player player, int amount, @NotNull Map<String, Object> placeholders) {
return getOptional().map(item -> item.getItemStack(player, amount, placeholders)).orElse(null);
}
}
@@ -1,21 +1,21 @@
package cc.carm.lib.configuration.craft.value;
import cc.carm.lib.configuration.common.value.ConfigMessage;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.craft.CraftConfigValue;
import cc.carm.lib.configuration.craft.builder.message.CraftMessageValueBuilder;
import cc.carm.lib.configuration.craft.data.MessageText;
import cc.carm.lib.configuration.craft.data.TextConfig;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
public class ConfiguredMessage<M> extends ConfigMessage<M, MessageText, CommandSender> {
public class ConfiguredMessage<M> extends ConfigMessage<M, TextConfig, CommandSender> {
@NotNull
public static <M> CraftMessageValueBuilder<@Nullable M> create(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser) {
@@ -34,15 +34,15 @@ public class ConfiguredMessage<M> extends ConfigMessage<M, MessageText, CommandS
return asString().defaults(defaultMessage).build();
}
public ConfiguredMessage(@Nullable ConfigurationProvider<?> provider,
@Nullable String sectionPath, @Nullable ConfigCommentInfo comments,
@NotNull MessageText defaultMessage, @NotNull String[] params,
public ConfiguredMessage(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
@Nullable List<String> headerComments, @Nullable String inlineComments,
@NotNull TextConfig defaultMessage, @NotNull String[] params,
@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser,
@NotNull BiConsumer<@NotNull CommandSender, @NotNull M> sendFunction) {
super(provider, sectionPath, comments, MessageText.class, defaultMessage, params, messageParser, sendFunction, MessageText::of);
super(provider, sectionPath, headerComments, inlineComments, TextConfig.class, defaultMessage, params, messageParser, sendFunction, TextConfig::of);
}
@Override
public void broadcast(@NotNull Map<String, Object> placeholders) {
Bukkit.getOnlinePlayers().forEach(pl -> send(pl, placeholders));
send(Bukkit.getConsoleSender(), placeholders);
@@ -1,11 +1,10 @@
package cc.carm.lib.configuration.craft.value;
import cc.carm.lib.configuration.common.value.ConfigMessageList;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.craft.CraftConfigValue;
import cc.carm.lib.configuration.craft.builder.message.CraftMessageListBuilder;
import cc.carm.lib.configuration.craft.data.MessageText;
import cc.carm.lib.configuration.craft.data.TextConfig;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
@@ -16,7 +15,7 @@ import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
public class ConfiguredMessageList<M> extends ConfigMessageList<M, MessageText, CommandSender> {
public class ConfiguredMessageList<M> extends ConfigMessageList<M, TextConfig, CommandSender> {
@NotNull
public static <M> CraftMessageListBuilder<M> create(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser) {
@@ -31,12 +30,12 @@ public class ConfiguredMessageList<M> extends ConfigMessageList<M, MessageText,
return asStrings().defaults(defaultMessages).build();
}
public ConfiguredMessageList(@Nullable ConfigurationProvider<?> provider,
@Nullable String sectionPath, @Nullable ConfigCommentInfo comments,
@NotNull List<MessageText> messages, @NotNull String[] params,
public ConfiguredMessageList(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
@Nullable List<String> headerComments, @Nullable String inlineComments,
@NotNull List<TextConfig> messages, @NotNull String[] params,
@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> messageParser,
@NotNull BiConsumer<@NotNull CommandSender, @NotNull List<M>> sendFunction) {
super(provider, sectionPath, comments, MessageText.class, messages, params, messageParser, sendFunction, MessageText::of);
super(provider, sectionPath, headerComments, inlineComments, TextConfig.class, messages, params, messageParser, sendFunction, TextConfig::of);
}
public void broadcast(@NotNull Map<String, Object> placeholders) {
@@ -2,11 +2,11 @@ package cc.carm.lib.configuration.craft.value;
import cc.carm.lib.configuration.craft.CraftConfigValue;
import cc.carm.lib.configuration.craft.source.CraftConfigProvider;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Optional;
public class ConfiguredSerializable<T extends ConfigurationSerializable> extends CraftConfigValue<T> {
@@ -22,10 +22,10 @@ public class ConfiguredSerializable<T extends ConfigurationSerializable> extends
protected final @NotNull Class<T> valueClass;
public ConfiguredSerializable(@Nullable CraftConfigProvider provider,
@Nullable String configPath, @Nullable ConfigCommentInfo comments,
public ConfiguredSerializable(@Nullable CraftConfigProvider provider, @Nullable String sectionPath,
@Nullable List<String> headerComments, @Nullable String inlineComments,
@NotNull Class<T> valueClass, @Nullable T defaultValue) {
super(provider, configPath, comments, defaultValue);
super(provider, sectionPath, headerComments, inlineComments, defaultValue);
this.valueClass = valueClass;
}
@@ -1,7 +1,6 @@
package cc.carm.lib.configuration.craft.value;
import cc.carm.lib.configuration.core.function.ConfigValueParser;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
import cc.carm.lib.configuration.craft.CraftConfigValue;
@@ -12,6 +11,7 @@ import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Optional;
public class ConfiguredSound extends ConfiguredValue<SoundConfig> {
@@ -32,10 +32,22 @@ public class ConfiguredSound extends ConfiguredValue<SoundConfig> {
return CraftConfigValue.builder().createSound().defaults(sound, volume, pitch).build();
}
public ConfiguredSound(@Nullable ConfigurationProvider<?> provider,
@Nullable String sectionPath, @Nullable ConfigCommentInfo comments,
public static @NotNull ConfiguredSound of(String soundName) {
return CraftConfigValue.builder().createSound().defaults(soundName).build();
}
public static @NotNull ConfiguredSound of(String soundName, float volume) {
return CraftConfigValue.builder().createSound().defaults(soundName, volume).build();
}
public static @NotNull ConfiguredSound of(String soundName, float volume, float pitch) {
return CraftConfigValue.builder().createSound().defaults(soundName, volume, pitch).build();
}
public ConfiguredSound(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
@Nullable List<String> headerComments, @Nullable String inlineComments,
@Nullable SoundConfig defaultValue) {
super(provider, sectionPath, comments, SoundConfig.class, defaultValue, getSoundParser(), SoundConfig::serialize);
super(provider, sectionPath, headerComments, inlineComments, SoundConfig.class, defaultValue, getSoundParser(), SoundConfig::serialize);
}
public void setSound(@NotNull Sound sound) {
@@ -47,7 +59,7 @@ public class ConfiguredSound extends ConfiguredValue<SoundConfig> {
}
public void setSound(@NotNull Sound sound, float volume, float pitch) {
set(new SoundConfig(sound, volume, pitch));
set(new SoundConfig(sound.name(), sound, volume, pitch));
}
public void playTo(@NotNull Player player) {
@@ -0,0 +1,109 @@
package cc.carm.lib.configuration.craft.value;
import cc.carm.lib.configuration.common.utils.ParamsUtils;
import cc.carm.lib.configuration.core.function.ConfigValueParser;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
import cc.carm.lib.configuration.core.value.type.ConfiguredSection;
import cc.carm.lib.configuration.craft.CraftConfigValue;
import cc.carm.lib.configuration.craft.builder.title.TitleConfigBuilder;
import cc.carm.lib.configuration.craft.data.TitleConfig;
import cc.carm.lib.configuration.craft.function.TitleSendConsumer;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Range;
import java.util.List;
import java.util.Map;
public class ConfiguredTitle extends ConfiguredSection<TitleConfig> {
public static TitleConfigBuilder create() {
return CraftConfigValue.builder().createTitle();
}
public static ConfiguredTitle of(@Nullable String line1, @Nullable String line2) {
return create().defaults(line1, line2).build();
}
public static ConfiguredTitle of(@Nullable String line1, @Nullable String line2,
long fadeIn, long stay, long fadeOut) {
return create().defaults(line1, line2).fadeIn(fadeIn).stay(stay).fadeOut(fadeOut).build();
}
protected final @NotNull TitleSendConsumer sendConsumer;
protected final @NotNull String[] params;
protected final long fadeIn;
protected final long stay;
protected final long fadeOut;
public ConfiguredTitle(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
@Nullable List<String> headerComments, @Nullable String inlineComments,
@Nullable TitleConfig defaultValue, @NotNull String[] params,
@NotNull TitleSendConsumer sendConsumer,
long fadeIn, long stay, long fadeOut) {
super(provider, sectionPath, headerComments, inlineComments, TitleConfig.class, defaultValue, getTitleParser(), TitleConfig::serialize);
this.sendConsumer = sendConsumer;
this.params = params;
this.fadeIn = fadeIn;
this.stay = stay;
this.fadeOut = fadeOut;
}
@Range(from = 0L, to = Long.MAX_VALUE)
public long getFadeInTicks() {
return fadeIn;
}
@Range(from = 0L, to = Long.MAX_VALUE)
public long getStayTicks() {
return stay;
}
@Range(from = 0L, to = Long.MAX_VALUE)
public long getFadeOutTicks() {
return fadeOut;
}
public @NotNull TitleSendConsumer getSendConsumer() {
return sendConsumer;
}
public void send(@NotNull Player player, Object... values) {
send(player, this.params, values);
}
public void send(@NotNull Player player, @NotNull String[] params, @NotNull Object[] values) {
send(player, ParamsUtils.buildParams(params, values));
}
public void send(@NotNull Player player, @NotNull Map<String, Object> placeholders) {
TitleConfig config = get();
if (config != null) {
config.send(player, fadeIn, stay, fadeOut, placeholders, sendConsumer);
}
}
public void sendAll(Object... values) {
sendAll(this.params, values);
}
public void sendAll(@NotNull String[] params, @NotNull Object[] values) {
sendAll(ParamsUtils.buildParams(params, values));
}
public void sendAll(@NotNull Map<String, Object> placeholders) {
TitleConfig config = get();
if (config == null) return;
Bukkit.getOnlinePlayers().forEach(onlinePlayer -> send(onlinePlayer, placeholders));
}
public static ConfigValueParser<ConfigurationWrapper, TitleConfig> getTitleParser() {
return (s, d) -> TitleConfig.deserialize(s);
}
}