1
mirror of https://github.com/CarmJos/MineConfiguration.git synced 2024-09-19 20:05:49 +00:00

feat(notify): 尝试实现整合的消息配置 #39

This commit is contained in:
Carm Jos 2023-12-25 03:40:33 +08:00
parent f86229037d
commit bf5c6a2fce
9 changed files with 268 additions and 65 deletions

View File

@ -0,0 +1,110 @@
package cc.carm.lib.mineconfiguration.bukkit.builder.notify;
import cc.carm.lib.configuration.core.builder.CommonConfigBuilder;
import cc.carm.lib.mineconfiguration.bukkit.data.NotifyConfig;
import cc.carm.lib.mineconfiguration.bukkit.data.SoundConfig;
import cc.carm.lib.mineconfiguration.bukkit.data.TitleConfig;
import cc.carm.lib.mineconfiguration.bukkit.value.notify.ConfiguredNotify;
import cc.carm.lib.mineconfiguration.bukkit.value.notify.DefaultNotifyTypes;
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
import org.bukkit.Sound;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Range;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.UnaryOperator;
public class NotifyConfigBuilder extends CommonConfigBuilder<List<NotifyConfig>, NotifyConfigBuilder> {
protected final @NotNull List<NotifyConfig> notifications = new ArrayList<>();
protected @NotNull String[] params = new String[0];
protected @NotNull Function<@NotNull String, @NotNull String> paramFormatter;
public NotifyConfigBuilder() {
this.paramFormatter = ParamsUtils.DEFAULT_PARAM_FORMATTER;
}
public NotifyConfigBuilder defaultMessages(@NotNull String... messages) {
return defaultMessages(Arrays.asList(messages));
}
public NotifyConfigBuilder defaultMessages(@NotNull List<String> messages) {
for (String message : messages) {
notifications.add(NotifyConfig.of(DefaultNotifyTypes.MESSAGE, message));
}
return defaults(this.notifications);
}
public NotifyConfigBuilder defaultActionBar(@NotNull String message) {
notifications.add(NotifyConfig.of(DefaultNotifyTypes.ACTIONBAR, message));
return defaults(this.notifications);
}
public NotifyConfigBuilder defaultSound(@NotNull Sound sound, float volume, float pitch) {
return defaultSound(sound.name(), volume, pitch);
}
public NotifyConfigBuilder defaultSound(@NotNull Sound sound, float volume) {
return defaultSound(sound, volume, 1.0f);
}
public NotifyConfigBuilder defaultSound(@NotNull Sound sound) {
return defaultSound(sound, 1.0f);
}
public NotifyConfigBuilder defaultSound(@NotNull String soundName, float volume, float pitch) {
notifications.add(NotifyConfig.of(DefaultNotifyTypes.SOUND, new SoundConfig(soundName, volume, pitch)));
return defaults(this.notifications);
}
public NotifyConfigBuilder defaultSound(@NotNull String soundName, float volume) {
return defaultSound(soundName, volume, 1.0f);
}
public NotifyConfigBuilder defaultSound(@NotNull String soundName) {
return defaultSound(soundName, 1.0f);
}
public NotifyConfigBuilder defaultTitle(@Nullable String line1, @Nullable String line2,
@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) {
notifications.add(NotifyConfig.of(DefaultNotifyTypes.TITLE, TitleConfig.of(line1, line2, fadeIn, stay, fadeOut)));
return defaults(this.notifications);
}
public NotifyConfigBuilder defaultTitle(@Nullable String line1, @Nullable String line2) {
return defaultTitle(line1, line2, 10, 60, 10);
}
public NotifyConfigBuilder params(String... params) {
this.params = params;
return this;
}
public NotifyConfigBuilder params(@NotNull List<String> params) {
return params(params.toArray(new String[0]));
}
public NotifyConfigBuilder formatParam(UnaryOperator<String> paramFormatter) {
this.paramFormatter = paramFormatter;
return this;
}
@Override
protected @NotNull NotifyConfigBuilder getThis() {
return this;
}
@Override
public @NotNull ConfiguredNotify build() {
return new ConfiguredNotify(buildManifest(), ParamsUtils.formatParams(this.paramFormatter, this.params));
}
}

View File

@ -1,51 +1,53 @@
package cc.carm.lib.mineconfiguration.bukkit.data;
import cc.carm.lib.mineconfiguration.bukkit.value.notify.ConfiguredNotify;
import cc.carm.lib.mineconfiguration.bukkit.value.notify.NotifyCache;
import cc.carm.lib.mineconfiguration.bukkit.value.notify.NotifyType;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.Optional;
public class NotifyConfig<T extends NotifyType<M>, M> {
public class NotifyConfig {
public static @Nullable NotifyConfig<?, ?> deserialize(@NotNull String config) {
// parse config with config_format
Matcher matcher = NotifyType.CONFIG_FORMAT.matcher(config.trim());
if (!matcher.matches()) return of("MESSAGE", null, config);
else return of(matcher.group("type"), matcher.group("param"), matcher.group("content"));
public static @Nullable NotifyConfig deserialize(@NotNull String config) {
return Optional.ofNullable(NotifyCache.deserialize(config)).map(NotifyConfig::of).orElse(null);
}
public static @Nullable NotifyConfig<?, ?> of(@NotNull String key, @Nullable String param, @Nullable String content) {
NotifyType<?> type = ConfiguredNotify.getType(key);
if (type == null) return null;
return NotifyConfig.of(type, param, content);
public static @NotNull NotifyConfig of(@NotNull NotifyCache<?, ?> cache) {
return new NotifyConfig(cache);
}
public static <T extends NotifyType<M>, M> NotifyConfig<T, M> of(@NotNull T type, @Nullable String param, @Nullable String content) {
return new NotifyConfig<>(type, type.parseMeta(param, content));
public static @Nullable NotifyConfig of(@NotNull String key, @Nullable String param, @Nullable String content) {
return Optional.ofNullable(NotifyCache.of(key, param, content)).map(NotifyConfig::of).orElse(null);
}
public static <T extends NotifyType<M>, M> NotifyConfig<T, M> of(@NotNull T type, @Nullable M meta) {
return new NotifyConfig<>(type, meta);
public static <T extends NotifyType<M>, M> NotifyConfig of(@NotNull T type, @Nullable String param, @Nullable String content) {
return of(type, type.parseMeta(param, content));
}
protected final @NotNull T type;
protected final @Nullable M meta;
public static <T extends NotifyType<M>, M> NotifyConfig of(@NotNull T type, @Nullable M meta) {
return of(NotifyCache.of(type, meta));
}
public NotifyConfig(@NotNull T type, @Nullable M meta) {
this.type = type;
this.meta = meta;
protected final @NotNull NotifyCache<?, ?> cache;
public NotifyConfig(@NotNull NotifyCache<?, ?> cache) {
this.cache = cache;
}
public @NotNull NotifyCache<?, ?> getCache() {
return cache;
}
public void execute(@NotNull Player player, @NotNull Map<String, Object> placeholders) {
type.execute(player, meta, placeholders);
getCache().execute(player, placeholders);
}
public String serialize() {
return type.serializeConfig(meta);
return getCache().serialize();
}
}

View File

@ -2,32 +2,24 @@ package cc.carm.lib.mineconfiguration.bukkit.value.notify;
import cc.carm.lib.configuration.core.value.ValueManifest;
import cc.carm.lib.configuration.core.value.type.ConfiguredList;
import cc.carm.lib.mineconfiguration.bukkit.builder.notify.NotifyConfigBuilder;
import cc.carm.lib.mineconfiguration.bukkit.data.NotifyConfig;
import cc.carm.lib.mineconfiguration.bukkit.value.notify.type.SoundNotify;
import cc.carm.lib.mineconfiguration.bukkit.value.notify.type.StringNotify;
import cc.carm.lib.mineconfiguration.bukkit.value.notify.type.TitleNotify;
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
import com.cryptomorin.xseries.messages.ActionBar;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.*;
@SuppressWarnings("rawtypes")
public class ConfiguredNotify extends ConfiguredList<NotifyConfig> {
public static final Set<NotifyType<?>> TYPES = new HashSet<>();
static {
TYPES.add(StringNotify.of("MESSAGE", Player::sendMessage, content -> Optional.ofNullable(content).orElse(" ")));
TYPES.add(StringNotify.of("MSG", Player::sendMessage));
TYPES.add(StringNotify.of("ACTIONBAR", ActionBar::sendActionBar));
TYPES.add(new TitleNotify("TITLE"));
TYPES.add(new SoundNotify("SOUND"));
public static NotifyConfigBuilder create() {
return new NotifyConfigBuilder();
}
public static final Set<NotifyType<?>> TYPES = new HashSet<>(Arrays.asList(DefaultNotifyTypes.values()));
public static NotifyType<?> getType(@NotNull String key) {
return TYPES.stream().filter(type -> type.key.equals(key)).findFirst().orElse(null);
return TYPES.stream().filter(type -> type.key.equalsIgnoreCase(key)).findFirst().orElse(null);
}
protected final @NotNull String[] params;
@ -41,15 +33,15 @@ public class ConfiguredNotify extends ConfiguredList<NotifyConfig> {
return params;
}
public @NotNull PreparedNotify prepare(@NotNull String... values) {
public @NotNull PreparedNotify prepare(@NotNull Object... values) {
return new PreparedNotify(getNotNull(), ParamsUtils.buildParams(getParams(), values));
}
public void send(@NotNull Player player, @NotNull String... values) {
public void send(@NotNull Player player, @NotNull Object... values) {
prepare(values).to(player);
}
public void send(@NotNull Iterable<? extends Player> players, @NotNull String... values) {
public void send(@NotNull Iterable<? extends Player> players, @NotNull Object... values) {
prepare(values).to(players);
}

View File

@ -0,0 +1,30 @@
package cc.carm.lib.mineconfiguration.bukkit.value.notify;
import cc.carm.lib.mineconfiguration.bukkit.value.notify.type.SoundNotify;
import cc.carm.lib.mineconfiguration.bukkit.value.notify.type.StringNotify;
import cc.carm.lib.mineconfiguration.bukkit.value.notify.type.TitleNotify;
import com.cryptomorin.xseries.messages.ActionBar;
import org.bukkit.entity.Player;
import java.util.Arrays;
import java.util.Optional;
public interface DefaultNotifyTypes {
StringNotify MESSAGE = StringNotify.of("MESSAGE", Player::sendMessage, content -> Optional.ofNullable(content).orElse(" "));
StringNotify MSG = StringNotify.of("MSG", Player::sendMessage);
StringNotify ACTIONBAR = StringNotify.of("ACTIONBAR", ActionBar::sendActionBar);
TitleNotify TITLE = new TitleNotify("TITLE");
SoundNotify SOUND = new SoundNotify("SOUND");
static NotifyType<?>[] values() {
return new NotifyType<?>[]{MESSAGE, MSG, ACTIONBAR, TITLE, SOUND};
}
static NotifyType<?> valueOf(String name) {
return Arrays.stream(values()).filter(type -> type.key.equalsIgnoreCase(name)).findFirst().orElse(null);
}
}

View File

@ -0,0 +1,62 @@
package cc.carm.lib.mineconfiguration.bukkit.value.notify;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class NotifyCache<T extends NotifyType<M>, M> {
// Notify config format: [TYPE(@PARAM)] CONTENTS...
public static final @NotNull Pattern CONFIG_FORMAT = Pattern.compile("\\[(?<type>[^@\\]]+)(@(?<param>[^]]+))?] (?<content>.*)");
public static @Nullable NotifyCache<?, ?> deserialize(@NotNull String config) {
// parse config with config_format
Matcher matcher = CONFIG_FORMAT.matcher(config.trim());
if (!matcher.matches()) return of("MESSAGE", null, config);
return of(matcher.group("type"), matcher.group("param"), matcher.group("content"));
}
public static @Nullable NotifyCache<?, ?> of(@NotNull String key, @Nullable String param, @Nullable String content) {
NotifyType<?> type = ConfiguredNotify.getType(key);
if (type == null) return null;
return NotifyCache.of(type, param, content);
}
public static <T extends NotifyType<M>, M> NotifyCache<T, M> of(@NotNull T type, @Nullable String param, @Nullable String content) {
return new NotifyCache<>(type, type.parseMeta(param, content));
}
public static <T extends NotifyType<M>, M> NotifyCache<T, M> of(@NotNull T type, @Nullable M meta) {
return new NotifyCache<>(type, meta);
}
protected final @NotNull T type;
protected final @Nullable M meta;
public NotifyCache(@NotNull T type, @Nullable M meta) {
this.type = type;
this.meta = meta;
}
public @NotNull T getType() {
return type;
}
public @Nullable M getMeta() {
return meta;
}
public void execute(@NotNull Player player, @NotNull Map<String, Object> placeholders) {
type.execute(player, meta, placeholders);
}
public String serialize() {
return type.serializeConfig(meta);
}
}

View File

@ -7,13 +7,9 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
import java.util.regex.Pattern;
public abstract class NotifyType<M> {
// Notify config format: [TYPE(@PARAM)] CONTENTS...
public static final @NotNull Pattern CONFIG_FORMAT = Pattern.compile("^\\[(?<type>[^@\\]]+)(@(?<param>[^]]+))?] (?<content>.*)$");
protected final @NotNull String key;
protected final @NotNull Class<M> metaClass;

View File

@ -8,7 +8,6 @@ import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Map;
@SuppressWarnings({"rawtypes", "unchecked"})
public class PreparedNotify {
protected final @NotNull List<NotifyConfig> notifications;

View File

@ -10,6 +10,7 @@ import java.util.Map;
public class SoundNotify extends NotifyType<SoundConfig> {
public SoundNotify(@NotNull String key) {
super(key, SoundConfig.class);
}
@ -18,13 +19,14 @@ public class SoundNotify extends NotifyType<SoundConfig> {
public @Nullable SoundConfig parseMeta(@Nullable String param, @Nullable String content) {
if (content == null) return null;
String[] args = content.split(",");
String[] args = param == null ? new String[0] : param.split(",");
try {
return new SoundConfig(content,
return new SoundConfig(content.trim(),
(args.length >= 1) ? Float.parseFloat(args[0]) : 1,
(args.length >= 2) ? Float.parseFloat(args[1]) : 1
);
} catch (Exception exception) {
exception.printStackTrace();
return null;
}
}
@ -42,7 +44,11 @@ public class SoundNotify extends NotifyType<SoundConfig> {
@Override
public void execute(@NotNull Player player, @Nullable SoundConfig meta, @NotNull Map<String, Object> placeholders) {
if (meta != null) meta.playTo(player);
System.out.println("SoundNotify.execute");
if (meta != null) {
System.out.println("SoundNotify.play");
meta.playTo(player);
}
}
}

View File

@ -8,11 +8,15 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TitleNotify extends NotifyType<TitleConfig> {
//Title param format fadeIn,Stay,FadeOut
public static final String PARAM_FORMAT = "(?<fadeIn>\\d+),(?<stay>\\d+),(?<fadeOut>\\d+)";
//Content format line1{N}line2
public static final Pattern CONTENT_FORMAT = Pattern.compile("(?<line1>.+?)(?:\\{n}(?<line2>.+))?");
//Param format fadeIn,stay,fadeOut
public static final Pattern PARAM_FORMAT = Pattern.compile("(?<fadeIn>\\d+),(?<stay>\\d+),(?<fadeOut>\\d+)");
public TitleNotify(String key) {
super(key, TitleConfig.class);
@ -21,23 +25,25 @@ public class TitleNotify extends NotifyType<TitleConfig> {
@Override
public @Nullable TitleConfig parseMeta(@Nullable String param, @Nullable String content) {
if (content == null) return null;
String[] lines = content.split("\\{n}");
Matcher contentMatcher = CONTENT_FORMAT.matcher(content);
if (!contentMatcher.matches()) return null;
if (param == null) {
return TitleConfig.of(content, null);
} else {
String[] params = param.split(",");
if (params.length == 3) {
try {
return TitleConfig.of(content, null,
Integer.parseInt(params[0]),
Integer.parseInt(params[1]),
Integer.parseInt(params[2]));
} catch (Exception ex) {
return TitleConfig.of(content, null);
return TitleConfig.of(contentMatcher.group("line1"), contentMatcher.group("line2"));
}
Matcher paramMatcher = PARAM_FORMAT.matcher(param);
if (!paramMatcher.matches()) {
return TitleConfig.of(contentMatcher.group("line1"), contentMatcher.group("line2"));
}
}
return null;
return TitleConfig.of(
contentMatcher.group("line1"), contentMatcher.group("line2"),
Integer.parseInt(paramMatcher.group("fadeIn")),
Integer.parseInt(paramMatcher.group("stay")),
Integer.parseInt(paramMatcher.group("fadeOut"))
);
}
@Override