1
mirror of https://github.com/CarmJos/MineConfiguration.git synced 2026-06-04 13:55:03 +08:00

feat(send): 提供新消息发送方法,分离玩家参数和消息参数

This commit is contained in:
2023-09-06 06:40:52 +08:00
parent 92ecb55a95
commit 0f7fc39f87
7 changed files with 173 additions and 45 deletions
@@ -38,6 +38,16 @@ public interface BaseMessage<R, M> {
@ApiStatus.OverrideOnly
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.Nullable;
import java.util.Collection;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
@@ -71,4 +72,28 @@ public abstract class ConfigMessage<M, T extends AbstractText<R>, R>
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());
}
}