diff --git a/platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/builder/notify/NotifyConfigBuilder.java b/platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/builder/notify/NotifyConfigBuilder.java new file mode 100644 index 0000000..b08e35d --- /dev/null +++ b/platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/builder/notify/NotifyConfigBuilder.java @@ -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, NotifyConfigBuilder> { + + protected final @NotNull List 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 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 params) { + return params(params.toArray(new String[0])); + } + + + public NotifyConfigBuilder formatParam(UnaryOperator 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)); + } + +} diff --git a/platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/data/NotifyConfig.java b/platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/data/NotifyConfig.java index 976f420..6b2e7b8 100644 --- a/platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/data/NotifyConfig.java +++ b/platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/data/NotifyConfig.java @@ -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, 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 , M> NotifyConfig 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 , M> NotifyConfig of(@NotNull T type, @Nullable M meta) { - return new NotifyConfig<>(type, meta); + public static , 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 , 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 placeholders) { - type.execute(player, meta, placeholders); + getCache().execute(player, placeholders); } public String serialize() { - return type.serializeConfig(meta); + return getCache().serialize(); } } diff --git a/platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/value/notify/ConfiguredNotify.java b/platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/value/notify/ConfiguredNotify.java index c63ce22..f27c44b 100644 --- a/platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/value/notify/ConfiguredNotify.java +++ b/platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/value/notify/ConfiguredNotify.java @@ -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 { - public static final Set> 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> 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 { 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 players, @NotNull String... values) { + public void send(@NotNull Iterable players, @NotNull Object... values) { prepare(values).to(players); } diff --git a/platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/value/notify/DefaultNotifyTypes.java b/platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/value/notify/DefaultNotifyTypes.java new file mode 100644 index 0000000..c2ab77d --- /dev/null +++ b/platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/value/notify/DefaultNotifyTypes.java @@ -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); + } + +} + + diff --git a/platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/value/notify/NotifyCache.java b/platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/value/notify/NotifyCache.java new file mode 100644 index 0000000..ff9ce7a --- /dev/null +++ b/platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/value/notify/NotifyCache.java @@ -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, M> { + + // Notify config format: [TYPE(@PARAM)] CONTENTS... + public static final @NotNull Pattern CONFIG_FORMAT = Pattern.compile("\\[(?[^@\\]]+)(@(?[^]]+))?] (?.*)"); + + 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 , M> NotifyCache of(@NotNull T type, @Nullable String param, @Nullable String content) { + return new NotifyCache<>(type, type.parseMeta(param, content)); + } + + public static , M> NotifyCache 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 placeholders) { + type.execute(player, meta, placeholders); + } + + public String serialize() { + return type.serializeConfig(meta); + } + + +} diff --git a/platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/value/notify/NotifyType.java b/platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/value/notify/NotifyType.java index 95b332a..88567a5 100644 --- a/platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/value/notify/NotifyType.java +++ b/platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/value/notify/NotifyType.java @@ -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 { - // Notify config format: [TYPE(@PARAM)] CONTENTS... - public static final @NotNull Pattern CONFIG_FORMAT = Pattern.compile("^\\[(?[^@\\]]+)(@(?[^]]+))?] (?.*)$"); - protected final @NotNull String key; protected final @NotNull Class metaClass; diff --git a/platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/value/notify/PreparedNotify.java b/platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/value/notify/PreparedNotify.java index be8ea1c..ddd4ba3 100644 --- a/platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/value/notify/PreparedNotify.java +++ b/platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/value/notify/PreparedNotify.java @@ -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 notifications; diff --git a/platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/value/notify/type/SoundNotify.java b/platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/value/notify/type/SoundNotify.java index 8b5dc93..adff57c 100644 --- a/platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/value/notify/type/SoundNotify.java +++ b/platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/value/notify/type/SoundNotify.java @@ -10,6 +10,7 @@ import java.util.Map; public class SoundNotify extends NotifyType { + public SoundNotify(@NotNull String key) { super(key, SoundConfig.class); } @@ -18,13 +19,14 @@ public class SoundNotify extends NotifyType { 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 { @Override public void execute(@NotNull Player player, @Nullable SoundConfig meta, @NotNull Map placeholders) { - if (meta != null) meta.playTo(player); + System.out.println("SoundNotify.execute"); + if (meta != null) { + System.out.println("SoundNotify.play"); + meta.playTo(player); + } } } diff --git a/platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/value/notify/type/TitleNotify.java b/platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/value/notify/type/TitleNotify.java index b8f1428..31c9909 100644 --- a/platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/value/notify/type/TitleNotify.java +++ b/platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/value/notify/type/TitleNotify.java @@ -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 { - //Title param format fadeIn,Stay,FadeOut - public static final String PARAM_FORMAT = "(?\\d+),(?\\d+),(?\\d+)"; + //Content format line1{N}line2 + public static final Pattern CONTENT_FORMAT = Pattern.compile("(?.+?)(?:\\{n}(?.+))?"); + //Param format fadeIn,stay,fadeOut + public static final Pattern PARAM_FORMAT = Pattern.compile("(?\\d+),(?\\d+),(?\\d+)"); public TitleNotify(String key) { super(key, TitleConfig.class); @@ -21,23 +25,25 @@ public class TitleNotify extends NotifyType { @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")); } - return null; + + Matcher paramMatcher = PARAM_FORMAT.matcher(param); + if (!paramMatcher.matches()) { + return TitleConfig.of(contentMatcher.group("line1"), contentMatcher.group("line2")); + } + + 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