Skip to content

Commit 8dc0307

Browse files
authored
GH-60 Improve auto messages features.
- Added ignoreAdmins feature - Added DispatchCommand, EnableCommand, DisableCommand - Improved message configuration - Code cleanup
2 parents 63a34f5 + 52a28ca commit 8dc0307

15 files changed

+454
-57
lines changed

src/main/java/com/github/imdmk/automessage/AutoMessage.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@
1010
import com.github.imdmk.automessage.feature.command.builder.player.PlayerArgument;
1111
import com.github.imdmk.automessage.feature.command.builder.player.PlayerContextual;
1212
import com.github.imdmk.automessage.feature.command.implementation.DelayCommand;
13+
import com.github.imdmk.automessage.feature.command.implementation.DisableCommand;
14+
import com.github.imdmk.automessage.feature.command.implementation.DispatchCommand;
15+
import com.github.imdmk.automessage.feature.command.implementation.EnableCommand;
1316
import com.github.imdmk.automessage.feature.command.implementation.ReloadCommand;
1417
import com.github.imdmk.automessage.feature.message.MessageConfig;
1518
import com.github.imdmk.automessage.feature.message.MessageResultHandler;
1619
import com.github.imdmk.automessage.feature.message.MessageService;
17-
import com.github.imdmk.automessage.feature.message.auto.AutoMessageConfig;
20+
import com.github.imdmk.automessage.feature.message.auto.AutoMessageNotice;
21+
import com.github.imdmk.automessage.feature.message.auto.AutoMessageNoticeArgument;
22+
import com.github.imdmk.automessage.feature.message.auto.AutoMessageNoticeConfig;
1823
import com.github.imdmk.automessage.feature.message.auto.dispatcher.AutoMessageDispatcher;
1924
import com.github.imdmk.automessage.feature.message.auto.eligibility.AutoMessageEligibilityEvaluator;
2025
import com.github.imdmk.automessage.feature.message.auto.eligibility.DefaultEligibilityEvaluator;
@@ -67,7 +72,7 @@ class AutoMessage {
6772

6873
PluginConfig pluginConfig = this.configurationManager.create(PluginConfig.class);
6974
MessageConfig messageConfig = this.configurationManager.create(MessageConfig.class);
70-
AutoMessageConfig autoMessageConfig = this.configurationManager.create(AutoMessageConfig.class);
75+
AutoMessageNoticeConfig autoMessageNoticeConfig = this.configurationManager.create(AutoMessageNoticeConfig.class);
7176
CommandConfig commandConfig = this.configurationManager.create(CommandConfig.class);
7277

7378
/* Services */
@@ -80,7 +85,7 @@ class AutoMessage {
8085
/* Dispatcher */
8186
AutoMessageEligibilityEvaluator eligibilityEvaluator = new DefaultEligibilityEvaluator();
8287

83-
AutoMessageDispatcher autoMessageDispatcher = new AutoMessageDispatcher(this.server, this.configurationManager, autoMessageConfig, this.messageService, this.taskScheduler, eligibilityEvaluator);
88+
AutoMessageDispatcher autoMessageDispatcher = new AutoMessageDispatcher(this.server, this.configurationManager, autoMessageNoticeConfig, this.messageService, this.taskScheduler, eligibilityEvaluator);
8489
autoMessageDispatcher.schedule();
8590

8691
/* Controllers */
@@ -91,6 +96,7 @@ class AutoMessage {
9196
/* LiteCommands */
9297
this.liteCommands = LiteBukkitFactory.builder("AutoMessage", plugin, this.server)
9398
.argument(Player.class, new PlayerArgument(this.server, messageConfig))
99+
.argument(AutoMessageNotice.class, new AutoMessageNoticeArgument(messageConfig, autoMessageNoticeConfig))
94100

95101
.context(Player.class, new PlayerContextual())
96102
.result(Notice.class, new MessageResultHandler(this.messageService))
@@ -100,6 +106,9 @@ class AutoMessage {
100106

101107
.commands(
102108
new DelayCommand(this.messageService, autoMessageDispatcher),
109+
new DisableCommand(this.messageService, autoMessageDispatcher),
110+
new DispatchCommand(this.server, this.messageService, autoMessageDispatcher, eligibilityEvaluator),
111+
new EnableCommand(this.messageService, autoMessageDispatcher),
103112
new ReloadCommand(this.logger, this.configurationManager, this.messageService)
104113
)
105114

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.github.imdmk.automessage.feature.command.implementation;
2+
3+
import com.github.imdmk.automessage.feature.message.MessageService;
4+
import com.github.imdmk.automessage.feature.message.auto.dispatcher.AutoMessageDispatcher;
5+
import dev.rollczi.litecommands.annotations.command.Command;
6+
import dev.rollczi.litecommands.annotations.context.Context;
7+
import dev.rollczi.litecommands.annotations.execute.Execute;
8+
import dev.rollczi.litecommands.annotations.permission.Permission;
9+
import org.bukkit.command.CommandSender;
10+
import org.jetbrains.annotations.NotNull;
11+
12+
import java.util.Objects;
13+
14+
@Command(name = "automessage disable")
15+
@Permission("command.automessage.disable")
16+
public class DisableCommand {
17+
18+
private final MessageService messageService;
19+
private final AutoMessageDispatcher dispatcher;
20+
21+
public DisableCommand(@NotNull MessageService messageService, @NotNull AutoMessageDispatcher dispatcher) {
22+
this.messageService = Objects.requireNonNull(messageService, "messageService cannot be null");
23+
this.dispatcher = Objects.requireNonNull(dispatcher, "dispatcher cannot be null");
24+
}
25+
26+
@Execute
27+
void enable(@Context CommandSender sender) {
28+
if (!this.dispatcher.isEnabled()) {
29+
this.messageService.send(sender, notice -> notice.autoMessageAlreadyDisabled);
30+
return;
31+
}
32+
33+
this.dispatcher.setEnabled(false);
34+
this.dispatcher.cancel(); // Cancel task
35+
36+
this.messageService.send(sender, notice -> notice.autoMessageDisable);
37+
}
38+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.github.imdmk.automessage.feature.command.implementation;
2+
3+
import com.github.imdmk.automessage.feature.message.MessageService;
4+
import com.github.imdmk.automessage.feature.message.auto.AutoMessageNotice;
5+
import com.github.imdmk.automessage.feature.message.auto.dispatcher.AutoMessageDispatcher;
6+
import com.github.imdmk.automessage.feature.message.auto.eligibility.AutoMessageEligibilityEvaluator;
7+
import com.github.imdmk.automessage.feature.message.auto.selector.AutoMessageSelector;
8+
import com.github.imdmk.automessage.feature.message.auto.selector.AutoMessageSelectorFactory;
9+
import com.github.imdmk.automessage.feature.message.auto.selector.AutoMessageSelectorMode;
10+
import dev.rollczi.litecommands.annotations.argument.Arg;
11+
import dev.rollczi.litecommands.annotations.command.Command;
12+
import dev.rollczi.litecommands.annotations.context.Context;
13+
import dev.rollczi.litecommands.annotations.execute.Execute;
14+
import dev.rollczi.litecommands.annotations.permission.Permission;
15+
import org.bukkit.Server;
16+
import org.bukkit.command.CommandSender;
17+
import org.bukkit.entity.Player;
18+
import org.jetbrains.annotations.NotNull;
19+
20+
import java.util.Objects;
21+
22+
@Command(name = "automessage dispatch")
23+
@Permission("command.automessage.dispatch")
24+
public class DispatchCommand {
25+
26+
private final Server server;
27+
private final MessageService messageService;
28+
private final AutoMessageDispatcher dispatcher;
29+
private final AutoMessageEligibilityEvaluator evaluator;
30+
private final AutoMessageSelector randomSelector;
31+
32+
public DispatchCommand(
33+
@NotNull Server server,
34+
@NotNull MessageService messageService,
35+
@NotNull AutoMessageDispatcher dispatcher,
36+
@NotNull AutoMessageEligibilityEvaluator evaluator
37+
) {
38+
this.server = Objects.requireNonNull(server, "server cannot be null");
39+
this.messageService = Objects.requireNonNull(messageService, "messageService cannot be null");
40+
this.dispatcher = Objects.requireNonNull(dispatcher, "dispatcher cannot be null");
41+
this.evaluator = Objects.requireNonNull(evaluator, "evaluator cannot be null");
42+
this.randomSelector = AutoMessageSelectorFactory.create(AutoMessageSelectorMode.RANDOM, evaluator);
43+
}
44+
45+
@Execute(name = "random")
46+
void random(@Context CommandSender sender) {
47+
this.server.getOnlinePlayers().forEach(player -> this.dispatcher.dispatch(player, this.randomSelector));
48+
this.messageService.send(sender, notice -> notice.autoMessageRandomDispatched);
49+
}
50+
51+
@Execute(name = "random")
52+
void random(@Context CommandSender sender, @Arg Player player) {
53+
this.dispatcher.dispatch(player, this.randomSelector);
54+
this.messageService.send(sender, notice -> notice.autoMessageRandomDispatched);
55+
}
56+
57+
@Execute(name = "select")
58+
void select(@Context CommandSender sender, @Arg AutoMessageNotice autoMessage) {
59+
this.server.getOnlinePlayers()
60+
.stream()
61+
.filter(player -> this.evaluator.canReceive(player, autoMessage))
62+
.forEach(player -> this.dispatcher.dispatch(player, autoMessage));
63+
64+
this.messageService.send(sender, notice -> notice.autoMessageSelectedDispatched);
65+
}
66+
67+
@Execute(name = "select")
68+
void select(@Context CommandSender sender, @Arg AutoMessageNotice autoMessage, @Arg Player player) {
69+
if (!this.evaluator.canReceive(player, autoMessage)) {
70+
this.messageService.send(sender, notice -> notice.autoMessageSelectedCannotReceive);
71+
return;
72+
}
73+
74+
this.dispatcher.dispatch(player, autoMessage);
75+
this.messageService.send(sender, notice -> notice.autoMessageSelectedDispatched);
76+
}
77+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.github.imdmk.automessage.feature.command.implementation;
2+
3+
import com.github.imdmk.automessage.feature.message.MessageService;
4+
import com.github.imdmk.automessage.feature.message.auto.dispatcher.AutoMessageDispatcher;
5+
import dev.rollczi.litecommands.annotations.command.Command;
6+
import dev.rollczi.litecommands.annotations.context.Context;
7+
import dev.rollczi.litecommands.annotations.execute.Execute;
8+
import dev.rollczi.litecommands.annotations.permission.Permission;
9+
import org.bukkit.command.CommandSender;
10+
import org.jetbrains.annotations.NotNull;
11+
12+
import java.util.Objects;
13+
14+
@Command(name = "automessage enable")
15+
@Permission("command.automessage.enable")
16+
public class EnableCommand {
17+
18+
private final MessageService messageService;
19+
private final AutoMessageDispatcher dispatcher;
20+
21+
public EnableCommand(@NotNull MessageService messageService, @NotNull AutoMessageDispatcher dispatcher) {
22+
this.messageService = Objects.requireNonNull(messageService, "messageService cannot be null");
23+
this.dispatcher = Objects.requireNonNull(dispatcher, "dispatcher cannot be null");
24+
}
25+
26+
@Execute
27+
void enable(@Context CommandSender sender) {
28+
if (this.dispatcher.isEnabled()) {
29+
this.messageService.send(sender, notice -> notice.autoMessageAlreadyEnabled);
30+
return;
31+
}
32+
33+
this.dispatcher.setEnabled(true);
34+
this.dispatcher.schedule(); // Reset task
35+
36+
this.messageService.send(sender, notice -> notice.autoMessageEnable);
37+
}
38+
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,33 @@ public class MessageConfig extends ConfigSection {
2222
})
2323
public Notice autoMessageDelay = Notice.chat("<green>Current auto message delay is {DELAY}.");
2424

25+
@Comment("# Shown when auto messages are successfully enabled.")
26+
public Notice autoMessageEnable = Notice.chat("<green>Enabled auto messages!");
27+
28+
@Comment("# Shown when auto messages are already enabled.")
29+
public Notice autoMessageAlreadyEnabled = Notice.chat("<red>Auto messages are currently enabled!");
30+
31+
@Comment("# Shown when auto messages are successfully disabled.")
32+
public Notice autoMessageDisable = Notice.chat("<green>Disabled auto messages!");
33+
34+
@Comment("# Sent when automatic messages are already disabled")
35+
public Notice autoMessageAlreadyDisabled = Notice.chat("<red>Auto messages are already disabled!");
36+
37+
@Comment("# Sent when a random automatic message is dispatched")
38+
public Notice autoMessageRandomDispatched = Notice.chat("<green>Dispatched a random auto message.");
39+
40+
@Comment("# Sent when a selected automatic message is dispatched")
41+
public Notice autoMessageSelectedDispatched = Notice.chat("<green>Dispatched a selected auto message.");
42+
43+
@Comment({
44+
"# Sent when the selected target player cannot receive the selected message",
45+
"# Typically due to insufficient group or permission as defined in the configuration"
46+
})
47+
public Notice autoMessageSelectedCannotReceive = Notice.chat("<red>Selected player cannot receive the selected auto message (e.g. missing required group or permission).");
48+
49+
@Comment("# Sent when the provided auto message name does not exist")
50+
public Notice autoMessageNotFound = Notice.chat("<red>Auto message with the given name not found.");
51+
2552
@Comment({
2653
"# Sent when a command is used incorrectly",
2754
"# {USAGE} - Correct command usage"

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

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,36 @@
1919
*/
2020
public class AutoMessageNotice {
2121

22+
private final @NotNull String name;
2223
private final @NotNull List<Notice> notices;
2324

2425
private final @Nullable AutoMessageSound sound;
2526
private final @Nullable String requiredPermission;
2627
private final @Nullable String requiredGroup;
2728

29+
private final boolean ignoreAdmins;
30+
2831
private AutoMessageNotice(
32+
@NotNull String name,
2933
@NotNull List<Notice> notices,
3034
@Nullable AutoMessageSound sound,
3135
@Nullable String requiredPermission,
32-
@Nullable String requiredGroup
36+
@Nullable String requiredGroup,
37+
boolean ignoreAdmins
3338
) {
39+
this.name = Objects.requireNonNull(name, "name cannot be null");
3440
this.notices = Objects.requireNonNull(notices, "notice cannot be null");
3541
this.sound = sound;
3642
this.requiredPermission = requiredPermission;
3743
this.requiredGroup = requiredGroup;
44+
this.ignoreAdmins = ignoreAdmins;
45+
}
46+
47+
/**
48+
* @return the associated AutoMessageNotice name
49+
*/
50+
public @NotNull String getName() {
51+
return this.name;
3852
}
3953

4054
/**
@@ -65,6 +79,13 @@ public Optional<String> getRequiredGroup() {
6579
return Optional.ofNullable(this.requiredGroup);
6680
}
6781

82+
/**
83+
* @return ignoreAdmins boolean
84+
*/
85+
public boolean isIgnoreAdmins() {
86+
return this.ignoreAdmins;
87+
}
88+
6889
/**
6990
* Creates a new builder instance.
7091
*
@@ -79,11 +100,21 @@ public Optional<String> getRequiredGroup() {
79100
*/
80101
public static class Builder {
81102

82-
private @NotNull List<Notice> notices = new ArrayList<>();
103+
private String name;
104+
private List<Notice> notices = new ArrayList<>();
83105

84-
private @Nullable AutoMessageSound sound;
85-
private @Nullable String requiredPermission;
86-
private @Nullable String requiredGroup;
106+
private AutoMessageSound sound;
107+
private String requiredPermission;
108+
private String requiredGroup;
109+
private boolean ignoreAdmins;
110+
111+
@Contract("_ -> this")
112+
@CheckReturnValue
113+
public @NotNull Builder name(@NotNull String name) {
114+
Objects.requireNonNull(name, "name cannot be null");
115+
this.name = name;
116+
return this;
117+
}
87118

88119
@Contract("_ -> this")
89120
@CheckReturnValue
@@ -103,8 +134,7 @@ public static class Builder {
103134

104135
@Contract("_ -> this")
105136
@CheckReturnValue
106-
public @NotNull Builder sound(@NotNull AutoMessageSound sound) {
107-
Objects.requireNonNull(sound, "notice cannot be null");
137+
public @NotNull Builder sound(@Nullable AutoMessageSound sound) {
108138
this.sound = sound;
109139
return this;
110140
}
@@ -123,6 +153,13 @@ public static class Builder {
123153
return this;
124154
}
125155

156+
@Contract("_ -> this")
157+
@CheckReturnValue
158+
public @NotNull Builder ignoreAdmins(boolean ignore) {
159+
this.ignoreAdmins = ignore;
160+
return this;
161+
}
162+
126163
/**
127164
* Builds the {@link AutoMessageNotice} instance.
128165
*
@@ -131,7 +168,7 @@ public static class Builder {
131168
*/
132169
@CheckReturnValue
133170
public @NotNull AutoMessageNotice build() {
134-
return new AutoMessageNotice(this.notices, this.sound, this.requiredPermission, this.requiredGroup);
171+
return new AutoMessageNotice(this.name, this.notices, this.sound, this.requiredPermission, this.requiredGroup, this.ignoreAdmins);
135172
}
136173
}
137174
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.github.imdmk.automessage.feature.message.auto;
2+
3+
import com.github.imdmk.automessage.feature.message.MessageConfig;
4+
import dev.rollczi.litecommands.argument.Argument;
5+
import dev.rollczi.litecommands.argument.parser.ParseResult;
6+
import dev.rollczi.litecommands.argument.resolver.ArgumentResolver;
7+
import dev.rollczi.litecommands.invocation.Invocation;
8+
import dev.rollczi.litecommands.suggestion.SuggestionContext;
9+
import dev.rollczi.litecommands.suggestion.SuggestionResult;
10+
import org.bukkit.command.CommandSender;
11+
import org.jetbrains.annotations.NotNull;
12+
13+
import java.util.Objects;
14+
15+
public class AutoMessageNoticeArgument extends ArgumentResolver<CommandSender, AutoMessageNotice> {
16+
17+
private final MessageConfig messageConfig;
18+
private final AutoMessageNoticeConfig noticeConfig;
19+
20+
public AutoMessageNoticeArgument(@NotNull MessageConfig messageConfig, @NotNull AutoMessageNoticeConfig noticeConfig) {
21+
this.messageConfig = Objects.requireNonNull(messageConfig, "messageConfig cannot be null");
22+
this.noticeConfig = Objects.requireNonNull(noticeConfig, "noticeConfig cannot be null");
23+
}
24+
25+
@Override
26+
protected ParseResult<AutoMessageNotice> parse(Invocation<CommandSender> invocation, Argument<AutoMessageNotice> context, String argument) {
27+
return this.noticeConfig.getMessage(argument)
28+
.map(ParseResult::success)
29+
.orElseGet(() -> ParseResult.failure(this.messageConfig.autoMessageNotFound));
30+
}
31+
32+
@Override
33+
public SuggestionResult suggest(Invocation<CommandSender> invocation, Argument<AutoMessageNotice> argument, SuggestionContext context) {
34+
return this.noticeConfig.messages.stream()
35+
.map(AutoMessageNotice::getName)
36+
.collect(SuggestionResult.collector());
37+
}
38+
}

0 commit comments

Comments
 (0)