mirror of
https://github.com/CarmJos/MineConfiguration.git
synced 2026-06-04 13:55:03 +08:00
feat(send): 提供新消息发送方法,分离玩家参数和消息参数
This commit is contained in:
@@ -38,6 +38,16 @@ public interface BaseMessage<R, M> {
|
|||||||
@ApiStatus.OverrideOnly
|
@ApiStatus.OverrideOnly
|
||||||
void apply(@NotNull R receiver, @NotNull M message);
|
void apply(@NotNull R receiver, @NotNull M message);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 填入变量值,返回一个准备好待发送的消息。
|
||||||
|
*
|
||||||
|
* @param values 变量值
|
||||||
|
* @return 准备好待发送的消息
|
||||||
|
*/
|
||||||
|
default @NotNull PreparedMessage<R, M> prepare(@NotNull Object... values) {
|
||||||
|
return new PreparedMessage<>(this, values);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 为某位接收者解析此消息。
|
* 为某位接收者解析此消息。
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import cc.carm.lib.mineconfiguration.common.data.AbstractText;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
import java.util.function.BiFunction;
|
import java.util.function.BiFunction;
|
||||||
@@ -71,4 +72,28 @@ public abstract class ConfigMessage<M, T extends AbstractText<R>, R>
|
|||||||
return textBuilder.apply(value);
|
return textBuilder.apply(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public abstract class PreparedMessage<P, N> {
|
||||||
|
|
||||||
|
protected final @NotNull Object[] values;
|
||||||
|
|
||||||
|
protected PreparedMessage(@NotNull Object[] values) {
|
||||||
|
this.values = values;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object[] getValues() {
|
||||||
|
return values;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract void to(P receiver);
|
||||||
|
|
||||||
|
public void to(Collection<P> receivers) {
|
||||||
|
receivers.forEach(this::to);
|
||||||
|
}
|
||||||
|
|
||||||
|
public N get(P receiver) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package cc.carm.lib.mineconfiguration.common.value;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
public class PreparedMessage<R, M> {
|
||||||
|
|
||||||
|
protected final @NotNull BaseMessage<R, M> message;
|
||||||
|
protected final @NotNull Object[] values;
|
||||||
|
|
||||||
|
protected PreparedMessage(@NotNull BaseMessage<R, M> message, @NotNull Object[] values) {
|
||||||
|
this.message = message;
|
||||||
|
this.values = values;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为某位接收者解析此消息。
|
||||||
|
*
|
||||||
|
* @param receiver 接收者
|
||||||
|
* @return 解析变量后的消息内容
|
||||||
|
*/
|
||||||
|
public @Nullable M parse(@Nullable R receiver) {
|
||||||
|
return message.parse(receiver, values);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 向某位接收者发送消息
|
||||||
|
*
|
||||||
|
* @param receiver 消息的接收者
|
||||||
|
*/
|
||||||
|
public void to(@Nullable R receiver) {
|
||||||
|
message.send(receiver, values);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 向某位接收者发送消息
|
||||||
|
*
|
||||||
|
* @param receivers 消息的接收者们
|
||||||
|
*/
|
||||||
|
public void to(@NotNull Iterable<R> receivers) {
|
||||||
|
receivers.forEach(this::to);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void toAll() {
|
||||||
|
to(message.getAllReceivers());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+5
-8
@@ -2,32 +2,29 @@ package cc.carm.lib.mineconfiguration.bukkit.builder.title;
|
|||||||
|
|
||||||
import cc.carm.lib.mineconfiguration.bukkit.builder.AbstractCraftBuilder;
|
import cc.carm.lib.mineconfiguration.bukkit.builder.AbstractCraftBuilder;
|
||||||
import cc.carm.lib.mineconfiguration.bukkit.data.TitleConfig;
|
import cc.carm.lib.mineconfiguration.bukkit.data.TitleConfig;
|
||||||
import cc.carm.lib.mineconfiguration.bukkit.function.TitleSendConsumer;
|
|
||||||
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredTitle;
|
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredTitle;
|
||||||
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
|
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
|
||||||
import com.cryptomorin.xseries.messages.Titles;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.annotations.Range;
|
import org.jetbrains.annotations.Range;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
import java.util.function.UnaryOperator;
|
||||||
|
|
||||||
public class TitleConfigBuilder extends AbstractCraftBuilder<TitleConfig, TitleConfigBuilder> {
|
public class TitleConfigBuilder extends AbstractCraftBuilder<TitleConfig, TitleConfigBuilder> {
|
||||||
|
|
||||||
protected static @NotNull TitleSendConsumer DEFAULT_TITLE_CONSUMER = Titles::sendTitle;
|
|
||||||
|
|
||||||
protected @NotNull String[] params = new String[0];
|
protected @NotNull String[] params = new String[0];
|
||||||
|
|
||||||
protected @Range(from = 0L, to = Integer.MAX_VALUE) int fadeIn = 10;
|
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 stay = 60;
|
||||||
protected @Range(from = 0L, to = Integer.MAX_VALUE) int fadeOut = 10;
|
protected @Range(from = 0L, to = Integer.MAX_VALUE) int fadeOut = 10;
|
||||||
|
|
||||||
protected @NotNull TitleSendConsumer sendConsumer;
|
protected @NotNull ConfiguredTitle.TitleConsumer sendConsumer;
|
||||||
protected @NotNull Function<@NotNull String, @NotNull String> paramFormatter;
|
protected @NotNull Function<@NotNull String, @NotNull String> paramFormatter;
|
||||||
|
|
||||||
public TitleConfigBuilder() {
|
public TitleConfigBuilder() {
|
||||||
this.sendConsumer = TitleConfigBuilder.DEFAULT_TITLE_CONSUMER;
|
this.sendConsumer = ConfiguredTitle.DEFAULT_TITLE_CONSUMER;
|
||||||
this.paramFormatter = ParamsUtils.DEFAULT_PARAM_FORMATTER;
|
this.paramFormatter = ParamsUtils.DEFAULT_PARAM_FORMATTER;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,7 +33,7 @@ public class TitleConfigBuilder extends AbstractCraftBuilder<TitleConfig, TitleC
|
|||||||
return defaults(TitleConfig.of(line1, line2));
|
return defaults(TitleConfig.of(line1, line2));
|
||||||
}
|
}
|
||||||
|
|
||||||
public @NotNull TitleConfigBuilder whenSend(@NotNull TitleSendConsumer consumer) {
|
public @NotNull TitleConfigBuilder whenSend(@NotNull ConfiguredTitle.TitleConsumer consumer) {
|
||||||
this.sendConsumer = consumer;
|
this.sendConsumer = consumer;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -65,7 +62,7 @@ public class TitleConfigBuilder extends AbstractCraftBuilder<TitleConfig, TitleC
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TitleConfigBuilder formatParam(Function<String, String> paramFormatter) {
|
public TitleConfigBuilder formatParam(UnaryOperator<String> paramFormatter) {
|
||||||
this.paramFormatter = paramFormatter;
|
this.paramFormatter = paramFormatter;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|||||||
+57
-7
@@ -1,8 +1,8 @@
|
|||||||
package cc.carm.lib.mineconfiguration.bukkit.data;
|
package cc.carm.lib.mineconfiguration.bukkit.data;
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
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 cc.carm.lib.mineconfiguration.bukkit.utils.TextParser;
|
||||||
|
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredTitle;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
@@ -25,20 +25,58 @@ public class TitleConfig {
|
|||||||
return new TitleConfig(line1, line2);
|
return new TitleConfig(line1, line2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static @NotNull TitleConfig of(@Nullable String line1, @Nullable String line2,
|
||||||
|
int fadeIn, int stay, int fadeOut) {
|
||||||
|
return of(
|
||||||
|
Optional.ofNullable(line1).map(TextConfig::of).orElse(null),
|
||||||
|
Optional.ofNullable(line2).map(TextConfig::of).orElse(null),
|
||||||
|
fadeIn, stay, fadeOut
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull TitleConfig of(@Nullable TextConfig line1, @Nullable TextConfig line2,
|
||||||
|
int fadeIn, int stay, int fadeOut) {
|
||||||
|
return new TitleConfig(line1, line2, fadeIn, stay, fadeOut);
|
||||||
|
}
|
||||||
|
|
||||||
protected @Nullable TextConfig line1;
|
protected @Nullable TextConfig line1;
|
||||||
protected @Nullable TextConfig line2;
|
protected @Nullable TextConfig line2;
|
||||||
|
|
||||||
|
protected final int fadeIn;
|
||||||
|
protected final int stay;
|
||||||
|
protected final int fadeOut;
|
||||||
|
|
||||||
protected TitleConfig(@Nullable TextConfig line1, @Nullable TextConfig line2) {
|
protected TitleConfig(@Nullable TextConfig line1, @Nullable TextConfig line2) {
|
||||||
|
this(line1, line2, -1, -1, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected TitleConfig(@Nullable TextConfig line1, @Nullable TextConfig line2, int fadeIn, int stay, int fadeOut) {
|
||||||
this.line1 = line1;
|
this.line1 = line1;
|
||||||
this.line2 = line2;
|
this.line2 = line2;
|
||||||
|
this.fadeIn = fadeIn;
|
||||||
|
this.stay = stay;
|
||||||
|
this.fadeOut = fadeOut;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void send(@NotNull Player player,
|
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,
|
@NotNull Map<String, Object> placeholders,
|
||||||
@Nullable TitleSendConsumer sendConsumer) {
|
@Nullable ConfiguredTitle.TitleConsumer sendConsumer) {
|
||||||
|
send(
|
||||||
|
player,
|
||||||
|
this.fadeIn < 0 ? 10 : this.fadeIn,
|
||||||
|
this.stay < 0 ? 60 : this.stay,
|
||||||
|
this.fadeOut < 0 ? 10 : this.fadeOut,
|
||||||
|
placeholders,
|
||||||
|
sendConsumer
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public 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 Map<String, Object> placeholders,
|
||||||
|
@Nullable ConfiguredTitle.TitleConsumer sendConsumer) {
|
||||||
if (this.line1 == null && this.line2 == null) return;
|
if (this.line1 == null && this.line2 == null) return;
|
||||||
if (sendConsumer == null) return;
|
if (sendConsumer == null) return;
|
||||||
sendConsumer.send(
|
sendConsumer.send(
|
||||||
@@ -58,11 +96,23 @@ public class TitleConfig {
|
|||||||
Map<String, Object> map = new LinkedHashMap<>();
|
Map<String, Object> map = new LinkedHashMap<>();
|
||||||
if (this.line1 != null) map.put("line1", this.line1.getMessage());
|
if (this.line1 != null) map.put("line1", this.line1.getMessage());
|
||||||
if (this.line2 != null) map.put("line2", this.line2.getMessage());
|
if (this.line2 != null) map.put("line2", this.line2.getMessage());
|
||||||
|
if (this.fadeIn > 0) map.put("fadeIn", this.fadeIn);
|
||||||
|
if (this.stay > 0) map.put("stay", this.stay);
|
||||||
|
if (this.fadeOut > 0) map.put("fadeOut", this.fadeOut);
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static @NotNull TitleConfig deserialize(@NotNull ConfigurationWrapper<?> section) {
|
public static @Nullable TitleConfig deserialize(@NotNull ConfigurationWrapper<?> section) {
|
||||||
return of(section.getString("line1"), section.getString("line2"));
|
String line1 = section.getString("line1");
|
||||||
|
String line2 = section.getString("line2");
|
||||||
|
if (line1 == null && line2 == null) return null;
|
||||||
|
|
||||||
|
return of(
|
||||||
|
line1, line2,
|
||||||
|
section.getInt("fadeIn", -1),
|
||||||
|
section.getInt("stay", -1),
|
||||||
|
section.getInt("fadeOut", -1)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
-26
@@ -1,26 +0,0 @@
|
|||||||
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);
|
|
||||||
|
|
||||||
}
|
|
||||||
+28
-4
@@ -7,8 +7,8 @@ import cc.carm.lib.configuration.core.value.type.ConfiguredSection;
|
|||||||
import cc.carm.lib.mineconfiguration.bukkit.CraftConfigValue;
|
import cc.carm.lib.mineconfiguration.bukkit.CraftConfigValue;
|
||||||
import cc.carm.lib.mineconfiguration.bukkit.builder.title.TitleConfigBuilder;
|
import cc.carm.lib.mineconfiguration.bukkit.builder.title.TitleConfigBuilder;
|
||||||
import cc.carm.lib.mineconfiguration.bukkit.data.TitleConfig;
|
import cc.carm.lib.mineconfiguration.bukkit.data.TitleConfig;
|
||||||
import cc.carm.lib.mineconfiguration.bukkit.function.TitleSendConsumer;
|
|
||||||
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
|
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
|
||||||
|
import com.cryptomorin.xseries.messages.Titles;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@@ -22,6 +22,8 @@ import java.util.function.Predicate;
|
|||||||
|
|
||||||
public class ConfiguredTitle extends ConfiguredSection<TitleConfig> {
|
public class ConfiguredTitle extends ConfiguredSection<TitleConfig> {
|
||||||
|
|
||||||
|
public static final @NotNull ConfiguredTitle.TitleConsumer DEFAULT_TITLE_CONSUMER = Titles::sendTitle;
|
||||||
|
|
||||||
public static TitleConfigBuilder create() {
|
public static TitleConfigBuilder create() {
|
||||||
return CraftConfigValue.builder().createTitle();
|
return CraftConfigValue.builder().createTitle();
|
||||||
}
|
}
|
||||||
@@ -35,7 +37,7 @@ public class ConfiguredTitle extends ConfiguredSection<TitleConfig> {
|
|||||||
return create().defaults(line1, line2).fadeIn(fadeIn).stay(stay).fadeOut(fadeOut).build();
|
return create().defaults(line1, line2).fadeIn(fadeIn).stay(stay).fadeOut(fadeOut).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected final @NotNull TitleSendConsumer sendConsumer;
|
protected final @NotNull ConfiguredTitle.TitleConsumer sendConsumer;
|
||||||
protected final @NotNull String[] params;
|
protected final @NotNull String[] params;
|
||||||
|
|
||||||
protected final int fadeIn;
|
protected final int fadeIn;
|
||||||
@@ -43,7 +45,7 @@ public class ConfiguredTitle extends ConfiguredSection<TitleConfig> {
|
|||||||
protected final int fadeOut;
|
protected final int fadeOut;
|
||||||
|
|
||||||
public ConfiguredTitle(@NotNull ValueManifest<TitleConfig> manifest, @NotNull String[] params,
|
public ConfiguredTitle(@NotNull ValueManifest<TitleConfig> manifest, @NotNull String[] params,
|
||||||
@NotNull TitleSendConsumer sendConsumer,
|
@NotNull ConfiguredTitle.TitleConsumer sendConsumer,
|
||||||
int fadeIn, int stay, int fadeOut) {
|
int fadeIn, int stay, int fadeOut) {
|
||||||
super(manifest, TitleConfig.class, getTitleParser(), TitleConfig::serialize);
|
super(manifest, TitleConfig.class, getTitleParser(), TitleConfig::serialize);
|
||||||
this.sendConsumer = sendConsumer;
|
this.sendConsumer = sendConsumer;
|
||||||
@@ -68,7 +70,7 @@ public class ConfiguredTitle extends ConfiguredSection<TitleConfig> {
|
|||||||
return fadeOut;
|
return fadeOut;
|
||||||
}
|
}
|
||||||
|
|
||||||
public @NotNull TitleSendConsumer getSendConsumer() {
|
public @NotNull ConfiguredTitle.TitleConsumer getSendConsumer() {
|
||||||
return sendConsumer;
|
return sendConsumer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,4 +118,26 @@ public class ConfiguredTitle extends ConfiguredSection<TitleConfig> {
|
|||||||
public static ConfigValueParser<ConfigurationWrapper<?>, TitleConfig> getTitleParser() {
|
public static ConfigValueParser<ConfigurationWrapper<?>, TitleConfig> getTitleParser() {
|
||||||
return (s, d) -> TitleConfig.deserialize(s);
|
return (s, d) -> TitleConfig.deserialize(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface TitleConsumer {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 向目标玩家发送标题文字
|
||||||
|
*
|
||||||
|
* @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);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user