Skip to content

Commit fce58e1

Browse files
committed
unread mail notification on join
1 parent cd09eb3 commit fce58e1

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import pro.cloudnode.smp.cloudnodemsg.command.ToggleMessageCommand;
1515
import pro.cloudnode.smp.cloudnodemsg.command.UnIgnoreCommand;
1616
import pro.cloudnode.smp.cloudnodemsg.listener.AsyncChatListener;
17+
import pro.cloudnode.smp.cloudnodemsg.listener.PlayerJoinListener;
1718

1819
import java.io.InputStream;
1920
import java.sql.PreparedStatement;
@@ -48,6 +49,7 @@ public void onEnable() {
4849
Objects.requireNonNull(getCommand("togglemsg")).setExecutor(new ToggleMessageCommand());
4950

5051
getServer().getPluginManager().registerEvents(new AsyncChatListener(), this);
52+
getServer().getPluginManager().registerEvents(new PlayerJoinListener(), this);
5153
}
5254

5355
@Override

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,9 @@ public final class Permission {
4747
* Allows to see the private messages of other players
4848
*/
4949
public final static @NotNull String SPY = "cloudnodemsg.spy";
50+
51+
/**
52+
* Allows using the /mail command
53+
*/
54+
public final static @NotNull String MAIL = "cloudnodemsg.mail";
5055
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,33 @@ public PluginConfig(final @NotNull FileConfiguration config) {
256256
);
257257
}
258258

259+
/**
260+
* Should players be notified for unread mail on login?
261+
*/
262+
public boolean mailNotifyOnLogin() {
263+
return config.getBoolean("mail.notify-on-login");
264+
}
265+
266+
/**
267+
* Interval (in seconds) to notify players of unread mail. Set to 0 to disable.
268+
*/
269+
public int mailNotifyInterval() {
270+
return config.getInt("mail.notify-interval");
271+
}
272+
273+
/**
274+
* Unread mail notification message
275+
* <p>Placeholders:</p>
276+
* <ul><li>{@code <unread>} - the number of unread mail messages</li></ul>
277+
*
278+
* @param unread the number of unread mail messages
279+
*/
280+
public @NotNull Component mailNotify(final int unread) {
281+
return MiniMessage.miniMessage().deserialize(Objects.requireNonNull(config.getString("mail.notify")),
282+
Placeholder.unparsed("unread", String.valueOf(unread))
283+
);
284+
}
285+
259286
/**
260287
* No permission
261288
*/
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package pro.cloudnode.smp.cloudnodemsg.listener;
2+
3+
import org.bukkit.entity.Player;
4+
import org.bukkit.event.EventHandler;
5+
import org.bukkit.event.Listener;
6+
import org.bukkit.event.player.PlayerJoinEvent;
7+
import org.jetbrains.annotations.NotNull;
8+
import pro.cloudnode.smp.cloudnodemsg.CloudnodeMSG;
9+
import pro.cloudnode.smp.cloudnodemsg.Mail;
10+
import pro.cloudnode.smp.cloudnodemsg.Permission;
11+
12+
public final class PlayerJoinListener implements Listener {
13+
@EventHandler
14+
public void onPlayerJoin(final @NotNull PlayerJoinEvent event) {
15+
event.getPlayer().getServer().getScheduler().runTaskAsynchronously(CloudnodeMSG.getInstance(), () -> {
16+
if (!CloudnodeMSG.getInstance().config().mailNotifyOnLogin()) return;
17+
final @NotNull Player player = event.getPlayer();
18+
if (!player.hasPermission(Permission.MAIL)) return;
19+
final int unread = Mail.unread(player);
20+
if (unread > 0) player.sendMessage(CloudnodeMSG.getInstance().config().mailNotify(unread));
21+
});
22+
}
23+
}

src/main/resources/config.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,18 @@ toggle:
9898
# <player> - the player's username
9999
other: "<green>(!) Re-enabled private messages for <gray><player></gray>.</green>"
100100

101+
mail:
102+
# Should players be notified for unread mail on login?
103+
notify-on-login: true
104+
105+
# Interval (in seconds) to notify players of unread mail. Set to 0 to disable.
106+
notify-interval: 180
107+
108+
# Unread mail notification message
109+
# Placeholders:
110+
# <unread> - the number of unread mail messages
111+
notify: "<green>(!) You have <gray><unread></gray> unread messages.</green> <hover:show_text:'Click to view mail'><click:run_command:/mail read><gray>View mail</gray></click></hover>"
112+
101113
# Error messages
102114
errors:
103115
# No permission

0 commit comments

Comments
 (0)