Skip to content

Commit 6b98924

Browse files
committed
add method to easily run async code
1 parent fce58e1 commit 6b98924

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,11 @@ public void runDDL() {
117117
}
118118
getLogger().info("Database successfully initialised with DDL");
119119
}
120+
121+
/**
122+
* Run code asynchronously
123+
*/
124+
public static void runAsync(final @NotNull Runnable runnable) {
125+
getInstance().getServer().getScheduler().runTaskAsynchronously(getInstance(), runnable);
126+
}
120127
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.bukkit.OfflinePlayer;
44
import org.bukkit.Server;
5+
import org.bukkit.entity.Player;
56
import org.jetbrains.annotations.NotNull;
67

78
import java.sql.PreparedStatement;
@@ -102,4 +103,17 @@ public static int unread(final @NotNull OfflinePlayer player) {
102103
return 0;
103104
}
104105
}
106+
107+
/**
108+
* Notify online players for their unread mail
109+
*/
110+
public static void notifyUnread() {
111+
CloudnodeMSG.runAsync(() -> {
112+
for (final @NotNull Player player : CloudnodeMSG.getInstance().getServer().getOnlinePlayers()) {
113+
if (!player.hasPermission(Permission.MAIL)) continue;
114+
final int unread = Mail.unread(player);
115+
if (unread > 0) player.sendMessage(CloudnodeMSG.getInstance().config().mailNotify(unread));
116+
}
117+
});
118+
}
105119
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static boolean sendMessage(final @NotNull Audience recipient, final @NotN
1818

1919
@Override
2020
public final boolean onCommand(final @NotNull CommandSender sender, final @NotNull org.bukkit.command.Command command, final @NotNull String label, @NotNull String @NotNull [] args) {
21-
CloudnodeMSG.getInstance().getServer().getScheduler().runTaskAsynchronously(CloudnodeMSG.getInstance(), () -> {
21+
CloudnodeMSG.runAsync(() -> {
2222
final boolean ignored = run(sender, label, args);
2323
});
2424
return true;

src/main/java/pro/cloudnode/smp/cloudnodemsg/listener/PlayerJoinListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
public final class PlayerJoinListener implements Listener {
1313
@EventHandler
1414
public void onPlayerJoin(final @NotNull PlayerJoinEvent event) {
15-
event.getPlayer().getServer().getScheduler().runTaskAsynchronously(CloudnodeMSG.getInstance(), () -> {
15+
CloudnodeMSG.runAsync(() -> {
1616
if (!CloudnodeMSG.getInstance().config().mailNotifyOnLogin()) return;
1717
final @NotNull Player player = event.getPlayer();
1818
if (!player.hasPermission(Permission.MAIL)) return;

0 commit comments

Comments
 (0)