-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
268 additions
and
65 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
...rc/main/java/cc/carm/lib/mineconfiguration/bukkit/builder/notify/NotifyConfigBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
|
||
} |
48 changes: 25 additions & 23 deletions
48
platform/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/data/NotifyConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
|
||
protected final @NotNull NotifyCache<?, ?> cache; | ||
|
||
|
||
public NotifyConfig(@NotNull NotifyCache<?, ?> cache) { | ||
this.cache = cache; | ||
} | ||
|
||
public NotifyConfig(@NotNull T type, @Nullable M meta) { | ||
this.type = type; | ||
this.meta = meta; | ||
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...t/src/main/java/cc/carm/lib/mineconfiguration/bukkit/value/notify/DefaultNotifyTypes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} | ||
|
||
|
62 changes: 62 additions & 0 deletions
62
...m/bukkit/src/main/java/cc/carm/lib/mineconfiguration/bukkit/value/notify/NotifyCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.