Skip to content

Commit 0e046a6

Browse files
authored
GH-57 Add feature to multiple notices.
- Add sound feature.
2 parents 3b514f5 + 6cc081e commit 0e046a6

File tree

9 files changed

+185
-38
lines changed

9 files changed

+185
-38
lines changed

src/main/java/com/github/imdmk/automessage/feature/message/MessageService.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.eternalcode.multification.adventure.AudienceConverter;
44
import com.eternalcode.multification.bukkit.BukkitMultification;
5+
import com.eternalcode.multification.notice.Notice;
56
import com.eternalcode.multification.notice.provider.NoticeProvider;
67
import com.eternalcode.multification.translation.TranslationProvider;
78
import net.kyori.adventure.platform.AudienceProvider;
@@ -55,6 +56,14 @@ public void send(@NotNull CommandSender sender, @NotNull NoticeProvider<MessageC
5556
this.create().viewer(sender).notice(notice).send();
5657
}
5758

59+
public void send(@NotNull CommandSender sender, @NotNull Notice notice) {
60+
this.create().viewer(sender).notice(notice).send();
61+
}
62+
63+
public void sendAsync(@NotNull CommandSender sender, @NotNull Notice notice) {
64+
this.create().viewer(sender).notice(notice).sendAsync();
65+
}
66+
5867
public void close() {
5968
this.audienceProvider.close();
6069
}

src/main/java/com/github/imdmk/automessage/feature/message/auto/AutoMessageConfig.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,20 @@
55
import com.eternalcode.multification.okaeri.MultificationSerdesPack;
66
import com.github.imdmk.automessage.configuration.ConfigSection;
77
import com.github.imdmk.automessage.feature.message.auto.selector.AutoMessageSelectorMode;
8+
import com.github.imdmk.automessage.feature.message.auto.sound.AutoMessageSerializer;
9+
import com.github.imdmk.automessage.feature.message.auto.sound.AutoMessageSound;
10+
import com.github.imdmk.automessage.feature.message.auto.sound.SoundSerializer;
811
import eu.okaeri.configs.annotation.Comment;
912
import eu.okaeri.configs.serdes.OkaeriSerdesPack;
1013
import eu.okaeri.configs.serdes.commons.SerdesCommons;
1114
import net.kyori.adventure.bossbar.BossBar;
15+
import org.bukkit.Sound;
1216
import org.jetbrains.annotations.NotNull;
1317

1418
import java.time.Duration;
1519
import java.util.Arrays;
1620
import java.util.List;
21+
import java.util.Objects;
1722

1823
public class AutoMessageConfig extends ConfigSection {
1924

@@ -29,11 +34,13 @@ public class AutoMessageConfig extends ConfigSection {
2934

3035
@Comment({
3136
"# List of automatic messages to be dispatched.",
32-
"# Supports different Notice types like chat, actionbar, title, boss bar."
37+
"# Supports different Notice types like chat, actionbar, title, boss bar.",
38+
"# To make a new line in chat message, use '\n'"
3339
})
3440
public List<AutoMessageNotice> messages = Arrays.asList(
3541
AutoMessageNotice.builder()
3642
.notice(Notice.chat("<dark_gray>[<red>!<dark_gray>] <gray>This is first announcement of <rainbow>automessage <gray>plugin!"))
43+
.sound(new AutoMessageSound(Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1, 1))
3744
.build(),
3845

3946
AutoMessageNotice.builder()
@@ -46,6 +53,14 @@ public class AutoMessageConfig extends ConfigSection {
4653

4754
AutoMessageNotice.builder()
4855
.notice(Notice.bossBar(BossBar.Color.RED, BossBar.Overlay.PROGRESS, Duration.ofSeconds(5L), "<dark_gray>[<red><bold>!<dark_gray>] <rainbow>This is fourth announcement!"))
56+
.sound(new AutoMessageSound(Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1, 1))
57+
.build(),
58+
59+
AutoMessageNotice.builder()
60+
.notices(List.of(
61+
Notice.chat("<dark_gray>[<red>!<dark_gray>] <gray>This is multiple announcements!"),
62+
Notice.actionbar("<dark_gray>[<red>!<dark_gray>] <gray>This is multiple announcements!")
63+
))
4964
.build(),
5065

5166
AutoMessageNotice.builder()
@@ -65,6 +80,9 @@ public class AutoMessageConfig extends ConfigSection {
6580
registry.register(new SerdesCommons());
6681
registry.register(new AutoMessageNoticeSerializer());
6782
registry.register(new MultificationSerdesPack(NoticeResolverDefaults.createRegistry()));
83+
84+
registry.register(new AutoMessageSerializer());
85+
registry.register(new SoundSerializer());
6886
};
6987
}
7088

@@ -74,6 +92,7 @@ public class AutoMessageConfig extends ConfigSection {
7492
}
7593

7694
public void setDelay(@NotNull Duration delay) {
95+
Objects.requireNonNull(delay, "delay cannot be null");
7796
this.delay = delay;
7897
}
7998
}

src/main/java/com/github/imdmk/automessage/feature/message/auto/AutoMessageNotice.java

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,67 @@
11
package com.github.imdmk.automessage.feature.message.auto;
22

33
import com.eternalcode.multification.notice.Notice;
4+
import com.github.imdmk.automessage.feature.message.auto.sound.AutoMessageSound;
45
import org.jetbrains.annotations.CheckReturnValue;
56
import org.jetbrains.annotations.Contract;
67
import org.jetbrains.annotations.NotNull;
78
import org.jetbrains.annotations.Nullable;
9+
import org.jetbrains.annotations.Unmodifiable;
810

11+
import java.util.ArrayList;
12+
import java.util.Collections;
13+
import java.util.List;
14+
import java.util.Objects;
915
import java.util.Optional;
1016

1117
/**
1218
* Represents an auto-message notice with optional permission and group requirements.
1319
*/
1420
public class AutoMessageNotice {
1521

16-
private final @NotNull Notice notice;
22+
private final @NotNull List<Notice> notices;
23+
24+
private final @Nullable AutoMessageSound sound;
1725
private final @Nullable String requiredPermission;
1826
private final @Nullable String requiredGroup;
1927

20-
private AutoMessageNotice(@NotNull Notice notice, @Nullable String requiredPermission, @Nullable String requiredGroup) {
21-
this.notice = notice;
28+
private AutoMessageNotice(
29+
@NotNull List<Notice> notices,
30+
@Nullable AutoMessageSound sound,
31+
@Nullable String requiredPermission,
32+
@Nullable String requiredGroup
33+
) {
34+
this.notices = Objects.requireNonNull(notices, "notice cannot be null");
35+
this.sound = sound;
2236
this.requiredPermission = requiredPermission;
2337
this.requiredGroup = requiredGroup;
2438
}
2539

2640
/**
2741
* @return the associated {@link Notice}
2842
*/
29-
public @NotNull Notice getNotice() {
30-
return this.notice;
43+
public @Unmodifiable List<Notice> getNotices() {
44+
return Collections.unmodifiableList(this.notices);
45+
}
46+
47+
/**
48+
* @return the associated {@link AutoMessageSound}
49+
*/
50+
public Optional<AutoMessageSound> getSound() {
51+
return Optional.ofNullable(this.sound);
3152
}
3253

3354
/**
3455
* @return optional required permission
3556
*/
36-
public @NotNull Optional<String> getRequiredPermission() {
57+
public Optional<String> getRequiredPermission() {
3758
return Optional.ofNullable(this.requiredPermission);
3859
}
3960

4061
/**
4162
* @return optional required group
4263
*/
43-
public @NotNull Optional<String> getRequiredGroup() {
64+
public Optional<String> getRequiredGroup() {
4465
return Optional.ofNullable(this.requiredGroup);
4566
}
4667

@@ -58,14 +79,33 @@ private AutoMessageNotice(@NotNull Notice notice, @Nullable String requiredPermi
5879
*/
5980
public static class Builder {
6081

61-
private @Nullable Notice notice;
82+
private @NotNull List<Notice> notices = new ArrayList<>();
83+
84+
private @Nullable AutoMessageSound sound;
6285
private @Nullable String requiredPermission;
6386
private @Nullable String requiredGroup;
6487

6588
@Contract("_ -> this")
6689
@CheckReturnValue
6790
public @NotNull Builder notice(@NotNull Notice notice) {
68-
this.notice = notice;
91+
Objects.requireNonNull(notice, "notice cannot be null");
92+
this.notices.add(notice);
93+
return this;
94+
}
95+
96+
@Contract("_ -> this")
97+
@CheckReturnValue
98+
public @NotNull Builder notices(@NotNull List<Notice> notices) {
99+
Objects.requireNonNull(notices, "notices cannot be null");
100+
this.notices = notices;
101+
return this;
102+
}
103+
104+
@Contract("_ -> this")
105+
@CheckReturnValue
106+
public @NotNull Builder sound(@NotNull AutoMessageSound sound) {
107+
Objects.requireNonNull(sound, "notice cannot be null");
108+
this.sound = sound;
69109
return this;
70110
}
71111

@@ -91,11 +131,7 @@ public static class Builder {
91131
*/
92132
@CheckReturnValue
93133
public @NotNull AutoMessageNotice build() {
94-
if (this.notice == null) {
95-
throw new IllegalStateException("Notice must not be null");
96-
}
97-
98-
return new AutoMessageNotice(this.notice, this.requiredPermission, this.requiredGroup);
134+
return new AutoMessageNotice(this.notices, this.sound, this.requiredPermission, this.requiredGroup);
99135
}
100136
}
101137
}

src/main/java/com/github/imdmk/automessage/feature/message/auto/AutoMessageNoticeSerializer.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package com.github.imdmk.automessage.feature.message.auto;
22

33
import com.eternalcode.multification.notice.Notice;
4+
import com.github.imdmk.automessage.feature.message.auto.sound.AutoMessageSound;
45
import eu.okaeri.configs.schema.GenericsDeclaration;
56
import eu.okaeri.configs.serdes.DeserializationData;
67
import eu.okaeri.configs.serdes.ObjectSerializer;
78
import eu.okaeri.configs.serdes.SerializationData;
89
import org.jetbrains.annotations.NotNull;
910

11+
import java.util.List;
12+
1013
public class AutoMessageNoticeSerializer implements ObjectSerializer<AutoMessageNotice> {
1114

1215
@Override
@@ -16,26 +19,24 @@ public boolean supports(@NotNull Class<? super AutoMessageNotice> type) {
1619

1720
@Override
1821
public void serialize(@NotNull AutoMessageNotice message, @NotNull SerializationData data, @NotNull GenericsDeclaration generics) {
19-
Notice notice = message.getNotice();
20-
21-
data.add("notice", notice, Notice.class);
22-
23-
message.getRequiredPermission()
24-
.ifPresent(permission -> data.add("requiredPermission", permission, String.class));
22+
data.addCollection("notices", message.getNotices(), Notice.class);
2523

26-
message.getRequiredGroup()
27-
.ifPresent(group -> data.add("requiredGroup", group, String.class));
24+
message.getSound().ifPresent(sound -> data.add("sound", sound, AutoMessageSound.class));
25+
message.getRequiredPermission().ifPresent(permission -> data.add("requiredPermission", permission, String.class));
26+
message.getRequiredGroup().ifPresent(group -> data.add("requiredGroup", group, String.class));
2827
}
2928

3029
@Override
3130
public AutoMessageNotice deserialize(@NotNull DeserializationData data, @NotNull GenericsDeclaration generics) {
32-
Notice notice = data.get("notice", Notice.class);
31+
List<Notice> notices = data.getAsList("notices", Notice.class);
3332

33+
AutoMessageSound sound = data.get("sound", AutoMessageSound.class);
3434
String requiredPermission = data.get("requiredPermission", String.class);
3535
String requiredGroup = data.get("requiredGroup", String.class);
3636

3737
return AutoMessageNotice.builder()
38-
.notice(notice)
38+
.notices(notices)
39+
.sound(sound)
3940
.requiredPermission(requiredPermission)
4041
.requiredGroup(requiredGroup)
4142
.build();

src/main/java/com/github/imdmk/automessage/feature/message/auto/dispatcher/AutoMessageDispatcher.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.github.imdmk.automessage.configuration.ConfigurationManager;
44
import com.github.imdmk.automessage.feature.message.MessageService;
55
import com.github.imdmk.automessage.feature.message.auto.AutoMessageConfig;
6-
import com.github.imdmk.automessage.feature.message.auto.AutoMessageNotice;
76
import com.github.imdmk.automessage.feature.message.auto.eligibility.AutoMessageEligibilityEvaluator;
87
import com.github.imdmk.automessage.feature.message.auto.selector.AutoMessageSelector;
98
import com.github.imdmk.automessage.feature.message.auto.selector.AutoMessageSelectorFactory;
@@ -68,11 +67,10 @@ public AutoMessageDispatcher(
6867
*/
6968
public void dispatch(@NotNull Player player) {
7069
this.selector.selectFor(player, this.configuration.messages)
71-
.map(AutoMessageNotice::getNotice)
72-
.ifPresent(notice -> this.messageService.create()
73-
.viewer(player)
74-
.notice(notice)
75-
.sendAsync());
70+
.ifPresent(message -> {
71+
message.getSound().ifPresent(sound -> sound.play(player));
72+
message.getNotices().forEach(notice -> this.messageService.sendAsync(player, notice));
73+
});
7674
}
7775

7876
/**
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.github.imdmk.automessage.feature.message.auto.sound;
2+
3+
import eu.okaeri.configs.schema.GenericsDeclaration;
4+
import eu.okaeri.configs.serdes.DeserializationData;
5+
import eu.okaeri.configs.serdes.ObjectSerializer;
6+
import eu.okaeri.configs.serdes.SerializationData;
7+
import org.bukkit.Sound;
8+
import org.jetbrains.annotations.NotNull;
9+
10+
public class AutoMessageSerializer implements ObjectSerializer<AutoMessageSound> {
11+
12+
@Override
13+
public boolean supports(@NotNull Class<? super AutoMessageSound> type) {
14+
return AutoMessageSound.class.isAssignableFrom(type);
15+
}
16+
17+
@Override
18+
public void serialize(@NotNull AutoMessageSound autoSound, @NotNull SerializationData data, @NotNull GenericsDeclaration generics) {
19+
data.add("sound", autoSound.sound(), Sound.class);
20+
data.add("volume", autoSound.volume(), float.class);
21+
data.add("pitch", autoSound.pitch(), float.class);
22+
}
23+
24+
@Override
25+
public AutoMessageSound deserialize(@NotNull DeserializationData data, @NotNull GenericsDeclaration generics) {
26+
Sound sound = data.get("sound", Sound.class);
27+
float volume = data.get("volume", float.class);
28+
float pitch = data.get("pitch", float.class);
29+
30+
return new AutoMessageSound(sound, volume, pitch);
31+
}
32+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.github.imdmk.automessage.feature.message.auto.sound;
2+
3+
import org.bukkit.Sound;
4+
import org.bukkit.entity.Player;
5+
import org.jetbrains.annotations.NotNull;
6+
7+
/**
8+
* Represents a sound effect configuration to be played during a jump.
9+
* <p>
10+
* Wraps a {@link Sound} along with volume and pitch parameters
11+
*
12+
* @param sound the {@link Sound} to be played (must not be null)
13+
* @param volume the volume of the sound (typically between 0.0 and 1.0+)
14+
* @param pitch the pitch of the sound (typically between 0.5 and 2.0)
15+
*/
16+
public record AutoMessageSound(@NotNull Sound sound, float volume, float pitch) {
17+
18+
/**
19+
* Plays the configured sound effect for the specified player at their current location.
20+
*
21+
* @param player the {@link Player} to play the sound for (must not be null)
22+
*/
23+
public void play(@NotNull Player player) {
24+
player.playSound(player.getLocation(), this.sound, this.volume, this.pitch);
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.github.imdmk.automessage.feature.message.auto.sound;
2+
3+
import eu.okaeri.configs.schema.GenericsDeclaration;
4+
import eu.okaeri.configs.serdes.DeserializationData;
5+
import eu.okaeri.configs.serdes.ObjectSerializer;
6+
import eu.okaeri.configs.serdes.SerializationData;
7+
import org.bukkit.Sound;
8+
import org.jetbrains.annotations.NotNull;
9+
10+
public class SoundSerializer implements ObjectSerializer<Sound> {
11+
12+
@Override
13+
public boolean supports(@NotNull Class<? super Sound> type) {
14+
return Sound.class.isAssignableFrom(type);
15+
}
16+
17+
@Override
18+
public void serialize(@NotNull Sound sound, @NotNull SerializationData data, @NotNull GenericsDeclaration generics) {
19+
data.setValue(sound.name(), String.class);
20+
}
21+
22+
@Override
23+
public Sound deserialize(@NotNull DeserializationData data, @NotNull GenericsDeclaration generics) {
24+
return Sound.valueOf(data.getValue(String.class));
25+
}
26+
}

0 commit comments

Comments
 (0)