Skip to content

Commit 4ed6241

Browse files
committed
notify for received mail
1 parent 994a2d7 commit 4ed6241

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

src/main/java/pro/cloudnode/smp/cloudnodemsg/PluginConfig.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,12 +378,38 @@ public int mailNotifyInterval() {
378378
* <li>{@code <message>} - the message</li>
379379
* <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>
380380
* <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>
381+
* <li>{@code <id>} - the ID of the mail</li>
381382
* </ul>
382383
*
383384
* @param mail the mail
384385
*/
385386
public @NotNull Component mailSent(final @NotNull Mail mail) {
386387
return MiniMessage.miniMessage().deserialize(Objects.requireNonNull(config.getString("mail.sent"))
388+
.replace("<id>", mail.id.toString())
389+
.replace("<sender>", Message.name(mail.sender))
390+
.replace("<recipient>", Message.name(mail.recipient)),
391+
Formatter.booleanChoice("seen", mail.seen),
392+
Formatter.booleanChoice("starred", mail.starred)
393+
).replaceText(configurer -> configurer.matchLiteral("<message>").replacement(Component.text(mail.message)));
394+
}
395+
396+
/**
397+
* Received mail while online
398+
* <p>Placeholders:</p>
399+
* <ul>
400+
* <li>{@code <sender>} - the username of the sender</li>
401+
* <li>{@code <recipient>} - the username of the recipient</li>
402+
* <li>{@code <message>} - the message</li>
403+
* <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>
404+
* <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>
405+
* <li>{@code <id>} - the ID of the mail</li>
406+
* </ul>
407+
*
408+
* @param mail the mail
409+
*/
410+
public @NotNull Component mailReceived(final @NotNull Mail mail) {
411+
return MiniMessage.miniMessage().deserialize(Objects.requireNonNull(config.getString("mail.received"))
412+
.replace("<id>", mail.id.toString())
387413
.replace("<sender>", Message.name(mail.sender))
388414
.replace("<recipient>", Message.name(mail.recipient)),
389415
Formatter.booleanChoice("seen", mail.seen),

src/main/java/pro/cloudnode/smp/cloudnodemsg/command/MailCommand.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.bukkit.OfflinePlayer;
44
import org.bukkit.command.CommandSender;
5+
import org.bukkit.entity.Player;
56
import org.jetbrains.annotations.NotNull;
67
import org.jetbrains.annotations.Nullable;
78
import pro.cloudnode.smp.cloudnodemsg.CloudnodeMSG;
@@ -11,6 +12,7 @@
1112

1213
import java.util.Arrays;
1314
import java.util.List;
15+
import java.util.Optional;
1416

1517
public final class MailCommand extends Command {
1618

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

src/main/resources/config.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,19 @@ mail:
142142
# <message> - the message
143143
# <seen> - whether the mail was read/opened by the recipient (see: https://docs.advntr.dev/minimessage/dynamic-replacements.html#insert-a-choice)
144144
# <starred> - whether the recipient has starred the mail (see: https://docs.advntr.dev/minimessage/dynamic-replacements.html#insert-a-choice)
145+
# <id> - the ID of the mail
145146
sent: "<green>(!) Successfully sent mail to <gray><recipient></gray>.</green>"
146147

148+
# Received mail while online
149+
# Placeholders:
150+
# <sender> - the username of the sender
151+
# <recipient> - the username of the recipient
152+
# <message> - the message
153+
# <seen> - whether the mail was read/opened by the recipient (see: https://docs.advntr.dev/minimessage/dynamic-replacements.html#insert-a-choice)
154+
# <starred> - whether the recipient has starred the mail (see: https://docs.advntr.dev/minimessage/dynamic-replacements.html#insert-a-choice)
155+
# <id> - the ID of the mail
156+
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>"
157+
147158
# Error messages
148159
errors:
149160
# No permission

0 commit comments

Comments
 (0)