Skip to content

Commit

Permalink
notify for received mail
Browse files Browse the repository at this point in the history
  • Loading branch information
zefir-git committed May 19, 2024
1 parent 994a2d7 commit 4ed6241
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/main/java/pro/cloudnode/smp/cloudnodemsg/PluginConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,38 @@ public int mailNotifyInterval() {
* <li>{@code <message>} - the message</li>
* <li>{@code <seen>} - whether the mail was read/opened by the recipient (see: <a href="https://docs.advntr.dev/minimessage/dynamic-replacements.html#insert-a-choice">Insert a choice</a>)</li>
* <li>{@code <starred>} - whether the recipient has starred the mail (see: <a href="https://docs.advntr.dev/minimessage/dynamic-replacements.html#insert-a-choice">Insert a choice</a>)</li>
* <li>{@code <id>} - the ID of the mail</li>
* </ul>
*
* @param mail the mail
*/
public @NotNull Component mailSent(final @NotNull Mail mail) {
return MiniMessage.miniMessage().deserialize(Objects.requireNonNull(config.getString("mail.sent"))
.replace("<id>", mail.id.toString())
.replace("<sender>", Message.name(mail.sender))
.replace("<recipient>", Message.name(mail.recipient)),
Formatter.booleanChoice("seen", mail.seen),
Formatter.booleanChoice("starred", mail.starred)
).replaceText(configurer -> configurer.matchLiteral("<message>").replacement(Component.text(mail.message)));
}

/**
* Received mail while online
* <p>Placeholders:</p>
* <ul>
* <li>{@code <sender>} - the username of the sender</li>
* <li>{@code <recipient>} - the username of the recipient</li>
* <li>{@code <message>} - the message</li>
* <li>{@code <seen>} - whether the mail was read/opened by the recipient (see: <a href="https://docs.advntr.dev/minimessage/dynamic-replacements.html#insert-a-choice">Insert a choice</a>)</li>
* <li>{@code <starred>} - whether the recipient has starred the mail (see: <a href="https://docs.advntr.dev/minimessage/dynamic-replacements.html#insert-a-choice">Insert a choice</a>)</li>
* <li>{@code <id>} - the ID of the mail</li>
* </ul>
*
* @param mail the mail
*/
public @NotNull Component mailReceived(final @NotNull Mail mail) {
return MiniMessage.miniMessage().deserialize(Objects.requireNonNull(config.getString("mail.received"))
.replace("<id>", mail.id.toString())
.replace("<sender>", Message.name(mail.sender))
.replace("<recipient>", Message.name(mail.recipient)),
Formatter.booleanChoice("seen", mail.seen),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import pro.cloudnode.smp.cloudnodemsg.CloudnodeMSG;
Expand All @@ -11,6 +12,7 @@

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public final class MailCommand extends Command {

Expand Down Expand Up @@ -39,7 +41,11 @@ public static boolean send(final @NotNull CommandSender sender, final @NotNull S
if (!recipient.hasPlayedBefore()) return sendMessage(sender, CloudnodeMSG.getInstance().config().playerNotFound(Message.name(recipient)));
final @NotNull Mail mail = new Mail(Message.offlinePlayer(sender), recipient, message);
mail.insert();
// TODO: notify recipient (if online)
final @NotNull OfflinePlayer senderOfflinePlayer = Message.offlinePlayer(sender);
final @NotNull Optional<@NotNull Player> senderPlayer = Optional.ofNullable(senderOfflinePlayer.getPlayer());
final @NotNull Optional<@NotNull Player> recipientPlayer = Optional.ofNullable(recipient.getPlayer());
if (recipientPlayer.isPresent() && (!Message.isIgnored(recipientPlayer.get(), senderOfflinePlayer) || senderPlayer.isEmpty() || senderPlayer.get().hasPermission(Permission.IGNORE_BYPASS)))
sendMessage(recipientPlayer.get(), CloudnodeMSG.getInstance().config().mailReceived(mail));
return sendMessage(sender, CloudnodeMSG.getInstance().config().mailSent(mail));
}

Expand Down
11 changes: 11 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,19 @@ mail:
# <message> - the message
# <seen> - whether the mail was read/opened by the recipient (see: https://docs.advntr.dev/minimessage/dynamic-replacements.html#insert-a-choice)
# <starred> - whether the recipient has starred the mail (see: https://docs.advntr.dev/minimessage/dynamic-replacements.html#insert-a-choice)
# <id> - the ID of the mail
sent: "<green>(!) Successfully sent mail to <gray><recipient></gray>.</green>"

# Received mail while online
# Placeholders:
# <sender> - the username of the sender
# <recipient> - the username of the recipient
# <message> - the message
# <seen> - whether the mail was read/opened by the recipient (see: https://docs.advntr.dev/minimessage/dynamic-replacements.html#insert-a-choice)
# <starred> - whether the recipient has starred the mail (see: https://docs.advntr.dev/minimessage/dynamic-replacements.html#insert-a-choice)
# <id> - the ID of the mail
received: "<green>(!) You have new mail from <gray><sender></gray>. <hover:show_text:Click to see the message><click:run_command:/mail view <id>><white>Click to view</white></click></hover>"

# Error messages
errors:
# No permission
Expand Down

0 comments on commit 4ed6241

Please sign in to comment.