Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/main/java/com/github/imdmk/automessage/AutoMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@
import com.github.imdmk.automessage.feature.command.builder.player.PlayerArgument;
import com.github.imdmk.automessage.feature.command.builder.player.PlayerContextual;
import com.github.imdmk.automessage.feature.command.implementation.DelayCommand;
import com.github.imdmk.automessage.feature.command.implementation.DisableCommand;
import com.github.imdmk.automessage.feature.command.implementation.DispatchCommand;
import com.github.imdmk.automessage.feature.command.implementation.EnableCommand;
import com.github.imdmk.automessage.feature.command.implementation.ReloadCommand;
import com.github.imdmk.automessage.feature.message.MessageConfig;
import com.github.imdmk.automessage.feature.message.MessageResultHandler;
import com.github.imdmk.automessage.feature.message.MessageService;
import com.github.imdmk.automessage.feature.message.auto.AutoMessageConfig;
import com.github.imdmk.automessage.feature.message.auto.AutoMessageNotice;
import com.github.imdmk.automessage.feature.message.auto.AutoMessageNoticeArgument;
import com.github.imdmk.automessage.feature.message.auto.AutoMessageNoticeConfig;
import com.github.imdmk.automessage.feature.message.auto.dispatcher.AutoMessageDispatcher;
import com.github.imdmk.automessage.feature.message.auto.eligibility.AutoMessageEligibilityEvaluator;
import com.github.imdmk.automessage.feature.message.auto.eligibility.DefaultEligibilityEvaluator;
Expand Down Expand Up @@ -67,7 +72,7 @@ class AutoMessage {

PluginConfig pluginConfig = this.configurationManager.create(PluginConfig.class);
MessageConfig messageConfig = this.configurationManager.create(MessageConfig.class);
AutoMessageConfig autoMessageConfig = this.configurationManager.create(AutoMessageConfig.class);
AutoMessageNoticeConfig autoMessageNoticeConfig = this.configurationManager.create(AutoMessageNoticeConfig.class);
CommandConfig commandConfig = this.configurationManager.create(CommandConfig.class);

/* Services */
Expand All @@ -80,7 +85,7 @@ class AutoMessage {
/* Dispatcher */
AutoMessageEligibilityEvaluator eligibilityEvaluator = new DefaultEligibilityEvaluator();

AutoMessageDispatcher autoMessageDispatcher = new AutoMessageDispatcher(this.server, this.configurationManager, autoMessageConfig, this.messageService, this.taskScheduler, eligibilityEvaluator);
AutoMessageDispatcher autoMessageDispatcher = new AutoMessageDispatcher(this.server, this.configurationManager, autoMessageNoticeConfig, this.messageService, this.taskScheduler, eligibilityEvaluator);
autoMessageDispatcher.schedule();

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

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

.commands(
new DelayCommand(this.messageService, autoMessageDispatcher),
new DisableCommand(this.messageService, autoMessageDispatcher),
new DispatchCommand(this.server, this.messageService, autoMessageDispatcher, eligibilityEvaluator),
new EnableCommand(this.messageService, autoMessageDispatcher),
new ReloadCommand(this.logger, this.configurationManager, this.messageService)
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.github.imdmk.automessage.feature.command.implementation;

import com.github.imdmk.automessage.feature.message.MessageService;
import com.github.imdmk.automessage.feature.message.auto.dispatcher.AutoMessageDispatcher;
import dev.rollczi.litecommands.annotations.command.Command;
import dev.rollczi.litecommands.annotations.context.Context;
import dev.rollczi.litecommands.annotations.execute.Execute;
import dev.rollczi.litecommands.annotations.permission.Permission;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

@Command(name = "automessage disable")
@Permission("command.automessage.disable")
public class DisableCommand {

private final MessageService messageService;
private final AutoMessageDispatcher dispatcher;

public DisableCommand(@NotNull MessageService messageService, @NotNull AutoMessageDispatcher dispatcher) {
this.messageService = Objects.requireNonNull(messageService, "messageService cannot be null");
this.dispatcher = Objects.requireNonNull(dispatcher, "dispatcher cannot be null");
}

@Execute
void enable(@Context CommandSender sender) {
if (!this.dispatcher.isEnabled()) {
this.messageService.send(sender, notice -> notice.autoMessageAlreadyDisabled);
return;
}

this.dispatcher.setEnabled(false);
this.dispatcher.cancel(); // Cancel task

this.messageService.send(sender, notice -> notice.autoMessageDisable);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.github.imdmk.automessage.feature.command.implementation;

import com.github.imdmk.automessage.feature.message.MessageService;
import com.github.imdmk.automessage.feature.message.auto.AutoMessageNotice;
import com.github.imdmk.automessage.feature.message.auto.dispatcher.AutoMessageDispatcher;
import com.github.imdmk.automessage.feature.message.auto.eligibility.AutoMessageEligibilityEvaluator;
import com.github.imdmk.automessage.feature.message.auto.selector.AutoMessageSelector;
import com.github.imdmk.automessage.feature.message.auto.selector.AutoMessageSelectorFactory;
import com.github.imdmk.automessage.feature.message.auto.selector.AutoMessageSelectorMode;
import dev.rollczi.litecommands.annotations.argument.Arg;
import dev.rollczi.litecommands.annotations.command.Command;
import dev.rollczi.litecommands.annotations.context.Context;
import dev.rollczi.litecommands.annotations.execute.Execute;
import dev.rollczi.litecommands.annotations.permission.Permission;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

@Command(name = "automessage dispatch")
@Permission("command.automessage.dispatch")
public class DispatchCommand {

private final Server server;
private final MessageService messageService;
private final AutoMessageDispatcher dispatcher;
private final AutoMessageEligibilityEvaluator evaluator;
private final AutoMessageSelector randomSelector;

public DispatchCommand(
@NotNull Server server,
@NotNull MessageService messageService,
@NotNull AutoMessageDispatcher dispatcher,
@NotNull AutoMessageEligibilityEvaluator evaluator
) {
this.server = Objects.requireNonNull(server, "server cannot be null");
this.messageService = Objects.requireNonNull(messageService, "messageService cannot be null");
this.dispatcher = Objects.requireNonNull(dispatcher, "dispatcher cannot be null");
this.evaluator = Objects.requireNonNull(evaluator, "evaluator cannot be null");
this.randomSelector = AutoMessageSelectorFactory.create(AutoMessageSelectorMode.RANDOM, evaluator);
}

@Execute(name = "random")
void random(@Context CommandSender sender) {
this.server.getOnlinePlayers().forEach(player -> this.dispatcher.dispatch(player, this.randomSelector));
this.messageService.send(sender, notice -> notice.autoMessageRandomDispatched);
}

@Execute(name = "random")
void random(@Context CommandSender sender, @Arg Player player) {
this.dispatcher.dispatch(player, this.randomSelector);
this.messageService.send(sender, notice -> notice.autoMessageRandomDispatched);
}

@Execute(name = "select")
void select(@Context CommandSender sender, @Arg AutoMessageNotice autoMessage) {
this.server.getOnlinePlayers()
.stream()
.filter(player -> this.evaluator.canReceive(player, autoMessage))
.forEach(player -> this.dispatcher.dispatch(player, autoMessage));

this.messageService.send(sender, notice -> notice.autoMessageSelectedDispatched);
}

@Execute(name = "select")
void select(@Context CommandSender sender, @Arg AutoMessageNotice autoMessage, @Arg Player player) {
if (!this.evaluator.canReceive(player, autoMessage)) {
this.messageService.send(sender, notice -> notice.autoMessageSelectedCannotReceive);
return;
}

this.dispatcher.dispatch(player, autoMessage);
this.messageService.send(sender, notice -> notice.autoMessageSelectedDispatched);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.github.imdmk.automessage.feature.command.implementation;

import com.github.imdmk.automessage.feature.message.MessageService;
import com.github.imdmk.automessage.feature.message.auto.dispatcher.AutoMessageDispatcher;
import dev.rollczi.litecommands.annotations.command.Command;
import dev.rollczi.litecommands.annotations.context.Context;
import dev.rollczi.litecommands.annotations.execute.Execute;
import dev.rollczi.litecommands.annotations.permission.Permission;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

@Command(name = "automessage enable")
@Permission("command.automessage.enable")
public class EnableCommand {

private final MessageService messageService;
private final AutoMessageDispatcher dispatcher;

public EnableCommand(@NotNull MessageService messageService, @NotNull AutoMessageDispatcher dispatcher) {
this.messageService = Objects.requireNonNull(messageService, "messageService cannot be null");
this.dispatcher = Objects.requireNonNull(dispatcher, "dispatcher cannot be null");
}

@Execute
void enable(@Context CommandSender sender) {
if (this.dispatcher.isEnabled()) {
this.messageService.send(sender, notice -> notice.autoMessageAlreadyEnabled);
return;
}

this.dispatcher.setEnabled(true);
this.dispatcher.schedule(); // Reset task

this.messageService.send(sender, notice -> notice.autoMessageEnable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,33 @@ public class MessageConfig extends ConfigSection {
})
public Notice autoMessageDelay = Notice.chat("<green>Current auto message delay is {DELAY}.");

@Comment("# Shown when auto messages are successfully enabled.")
public Notice autoMessageEnable = Notice.chat("<green>Enabled auto messages!");

@Comment("# Shown when auto messages are already enabled.")
public Notice autoMessageAlreadyEnabled = Notice.chat("<red>Auto messages are currently enabled!");

@Comment("# Shown when auto messages are successfully disabled.")
public Notice autoMessageDisable = Notice.chat("<green>Disabled auto messages!");

@Comment("# Sent when automatic messages are already disabled")
public Notice autoMessageAlreadyDisabled = Notice.chat("<red>Auto messages are already disabled!");

@Comment("# Sent when a random automatic message is dispatched")
public Notice autoMessageRandomDispatched = Notice.chat("<green>Dispatched a random auto message.");

@Comment("# Sent when a selected automatic message is dispatched")
public Notice autoMessageSelectedDispatched = Notice.chat("<green>Dispatched a selected auto message.");

@Comment({
"# Sent when the selected target player cannot receive the selected message",
"# Typically due to insufficient group or permission as defined in the configuration"
})
public Notice autoMessageSelectedCannotReceive = Notice.chat("<red>Selected player cannot receive the selected auto message (e.g. missing required group or permission).");

@Comment("# Sent when the provided auto message name does not exist")
public Notice autoMessageNotFound = Notice.chat("<red>Auto message with the given name not found.");

@Comment({
"# Sent when a command is used incorrectly",
"# {USAGE} - Correct command usage"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,36 @@
*/
public class AutoMessageNotice {

private final @NotNull String name;
private final @NotNull List<Notice> notices;

private final @Nullable AutoMessageSound sound;
private final @Nullable String requiredPermission;
private final @Nullable String requiredGroup;

private final boolean ignoreAdmins;

private AutoMessageNotice(
@NotNull String name,
@NotNull List<Notice> notices,
@Nullable AutoMessageSound sound,
@Nullable String requiredPermission,
@Nullable String requiredGroup
@Nullable String requiredGroup,
boolean ignoreAdmins
) {
this.name = Objects.requireNonNull(name, "name cannot be null");
this.notices = Objects.requireNonNull(notices, "notice cannot be null");
this.sound = sound;
this.requiredPermission = requiredPermission;
this.requiredGroup = requiredGroup;
this.ignoreAdmins = ignoreAdmins;
}

/**
* @return the associated AutoMessageNotice name
*/
public @NotNull String getName() {
return this.name;
}

/**
Expand Down Expand Up @@ -65,6 +79,13 @@ public Optional<String> getRequiredGroup() {
return Optional.ofNullable(this.requiredGroup);
}

/**
* @return ignoreAdmins boolean
*/
public boolean isIgnoreAdmins() {
return this.ignoreAdmins;
}

/**
* Creates a new builder instance.
*
Expand All @@ -79,11 +100,21 @@ public Optional<String> getRequiredGroup() {
*/
public static class Builder {

private @NotNull List<Notice> notices = new ArrayList<>();
private String name;
private List<Notice> notices = new ArrayList<>();

private @Nullable AutoMessageSound sound;
private @Nullable String requiredPermission;
private @Nullable String requiredGroup;
private AutoMessageSound sound;
private String requiredPermission;
private String requiredGroup;
private boolean ignoreAdmins;

@Contract("_ -> this")
@CheckReturnValue
public @NotNull Builder name(@NotNull String name) {
Objects.requireNonNull(name, "name cannot be null");
this.name = name;
return this;
}

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

@Contract("_ -> this")
@CheckReturnValue
public @NotNull Builder sound(@NotNull AutoMessageSound sound) {
Objects.requireNonNull(sound, "notice cannot be null");
public @NotNull Builder sound(@Nullable AutoMessageSound sound) {
this.sound = sound;
return this;
}
Expand All @@ -123,6 +153,13 @@ public static class Builder {
return this;
}

@Contract("_ -> this")
@CheckReturnValue
public @NotNull Builder ignoreAdmins(boolean ignore) {
this.ignoreAdmins = ignore;
return this;
}

/**
* Builds the {@link AutoMessageNotice} instance.
*
Expand All @@ -131,7 +168,7 @@ public static class Builder {
*/
@CheckReturnValue
public @NotNull AutoMessageNotice build() {
return new AutoMessageNotice(this.notices, this.sound, this.requiredPermission, this.requiredGroup);
return new AutoMessageNotice(this.name, this.notices, this.sound, this.requiredPermission, this.requiredGroup, this.ignoreAdmins);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.github.imdmk.automessage.feature.message.auto;

import com.github.imdmk.automessage.feature.message.MessageConfig;
import dev.rollczi.litecommands.argument.Argument;
import dev.rollczi.litecommands.argument.parser.ParseResult;
import dev.rollczi.litecommands.argument.resolver.ArgumentResolver;
import dev.rollczi.litecommands.invocation.Invocation;
import dev.rollczi.litecommands.suggestion.SuggestionContext;
import dev.rollczi.litecommands.suggestion.SuggestionResult;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

public class AutoMessageNoticeArgument extends ArgumentResolver<CommandSender, AutoMessageNotice> {

private final MessageConfig messageConfig;
private final AutoMessageNoticeConfig noticeConfig;

public AutoMessageNoticeArgument(@NotNull MessageConfig messageConfig, @NotNull AutoMessageNoticeConfig noticeConfig) {
this.messageConfig = Objects.requireNonNull(messageConfig, "messageConfig cannot be null");
this.noticeConfig = Objects.requireNonNull(noticeConfig, "noticeConfig cannot be null");
}

@Override
protected ParseResult<AutoMessageNotice> parse(Invocation<CommandSender> invocation, Argument<AutoMessageNotice> context, String argument) {
return this.noticeConfig.getMessage(argument)
.map(ParseResult::success)
.orElseGet(() -> ParseResult.failure(this.messageConfig.autoMessageNotFound));
}

@Override
public SuggestionResult suggest(Invocation<CommandSender> invocation, Argument<AutoMessageNotice> argument, SuggestionContext context) {
return this.noticeConfig.messages.stream()
.map(AutoMessageNotice::getName)
.collect(SuggestionResult.collector());
}
}
Loading
Loading