-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMail.java
More file actions
119 lines (106 loc) · 5.13 KB
/
Mail.java
File metadata and controls
119 lines (106 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package pro.cloudnode.smp.cloudnodemsg;
import org.bukkit.OfflinePlayer;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Optional;
import java.util.UUID;
import java.util.logging.Level;
public final class Mail {
public final @NotNull UUID id;
public final @NotNull OfflinePlayer sender;
public final @NotNull OfflinePlayer recipient;
public final @NotNull String message;
public boolean seen;
public boolean starred;
public Mail(final @NotNull OfflinePlayer sender, final @NotNull OfflinePlayer recipient, final @NotNull String message) {
this.id = UUID.randomUUID();
this.sender = sender;
this.recipient = recipient;
this.message = message;
this.seen = false;
this.starred = false;
}
public Mail(final @NotNull ResultSet rs) throws SQLException {
final @NotNull Server server = CloudnodeMSG.getInstance().getServer();
this.id = UUID.fromString(rs.getString("id"));
this.sender = server.getOfflinePlayer(UUID.fromString(rs.getString("sender")));
this.recipient = server.getOfflinePlayer(UUID.fromString(rs.getString("recipient")));
this.message = rs.getString("message");
this.seen = rs.getBoolean("seen");
this.starred = rs.getBoolean("starred");
}
public void update() {
try (final @NotNull PreparedStatement stmt = CloudnodeMSG.getInstance().db().getConnection().prepareStatement("UPDATE `cloudnodemsg_mail` SET `seen` = ?, `starred` = ? WHERE `id` = ?")) {
stmt.setBoolean(1, seen);
stmt.setBoolean(2, starred);
stmt.setString(3, id.toString());
stmt.executeUpdate();
}
catch (final @NotNull SQLException exception) {
CloudnodeMSG.getInstance().getLogger().log(Level.SEVERE, "Could not update mail: " + id, exception);
}
}
public void delete() {
try (final @NotNull PreparedStatement stmt = CloudnodeMSG.getInstance().db().getConnection().prepareStatement("DELETE FROM `cloudnodemsg_mail` WHERE `id` = ?")) {
stmt.setString(1, id.toString());
stmt.executeUpdate();
}
catch (final @NotNull SQLException exception) {
CloudnodeMSG.getInstance().getLogger().log(Level.SEVERE, "Could not delete mail: " + id, exception);
}
}
public void insert() {
try (final @NotNull PreparedStatement stmt = CloudnodeMSG.getInstance().db().getConnection().prepareStatement("INSERT INTO `cloudnodemsg_mail` (`id`, `sender`, `recipient`, `message`, `seen`, `starred`) VALUES (?, ?, ?, ?, ?, ?)")) {
stmt.setString(1, id.toString());
stmt.setString(2, sender.getUniqueId().toString());
stmt.setString(3, recipient.getUniqueId().toString());
stmt.setString(4, message);
stmt.setBoolean(5, seen);
stmt.setBoolean(6, starred);
stmt.executeUpdate();
}
catch (final @NotNull SQLException exception) {
CloudnodeMSG.getInstance().getLogger().log(Level.SEVERE, "Could not insert mail: " + id, exception);
}
}
public static @NotNull Optional<@NotNull Mail> get(final @NotNull UUID id) {
try (final @NotNull PreparedStatement stmt = CloudnodeMSG.getInstance().db().getConnection().prepareStatement("SELECT * FROM `cloudnodemsg_mail` WHERE `id` = ? LIMIT 1")) {
stmt.setString(1, id.toString());
final @NotNull ResultSet rs = stmt.executeQuery();
if (!rs.next()) return Optional.empty();
return Optional.of(new Mail(rs));
}
catch (final @NotNull SQLException exception) {
CloudnodeMSG.getInstance().getLogger().log(Level.SEVERE, "Could not get mail: " + id, exception);
return Optional.empty();
}
}
public static int unread(final @NotNull OfflinePlayer player) {
try (final @NotNull PreparedStatement stmt = CloudnodeMSG.getInstance().db().getConnection().prepareStatement("SELECT COUNT(`id`) as `n` FROM `cloudnodemsg_mail` WHERE `recipient` = ? AND `seen` = 0")) {
stmt.setString(1, player.getUniqueId().toString());
final @NotNull ResultSet rs = stmt.executeQuery();
if (!rs.next()) return 0;
return rs.getInt("n");
}
catch (final @NotNull SQLException exception) {
CloudnodeMSG.getInstance().getLogger().log(Level.SEVERE, "Could not get unread mails: " + player.getName(), exception);
return 0;
}
}
/**
* Notify online players for their unread mail
*/
public static void notifyUnread() {
CloudnodeMSG.runAsync(() -> {
for (final @NotNull Player player : CloudnodeMSG.getInstance().getServer().getOnlinePlayers()) {
if (!player.hasPermission(Permission.MAIL)) continue;
final int unread = Mail.unread(player);
if (unread > 0) player.sendMessage(CloudnodeMSG.getInstance().config().mailNotify(unread));
}
});
}
}