Skip to content

Commit cd09eb3

Browse files
committed
mail class
1 parent 543b527 commit cd09eb3

File tree

1 file changed

+105
-0
lines changed
  • src/main/java/pro/cloudnode/smp/cloudnodemsg

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package pro.cloudnode.smp.cloudnodemsg;
2+
3+
import org.bukkit.OfflinePlayer;
4+
import org.bukkit.Server;
5+
import org.jetbrains.annotations.NotNull;
6+
7+
import java.sql.PreparedStatement;
8+
import java.sql.ResultSet;
9+
import java.sql.SQLException;
10+
import java.util.Optional;
11+
import java.util.UUID;
12+
import java.util.logging.Level;
13+
14+
public final class Mail {
15+
public final @NotNull UUID id;
16+
public final @NotNull OfflinePlayer sender;
17+
public final @NotNull OfflinePlayer recipient;
18+
public final @NotNull String message;
19+
public boolean seen;
20+
21+
public boolean starred;
22+
23+
public Mail(final @NotNull OfflinePlayer sender, final @NotNull OfflinePlayer recipient, final @NotNull String message) {
24+
this.id = UUID.randomUUID();
25+
this.sender = sender;
26+
this.recipient = recipient;
27+
this.message = message;
28+
this.seen = false;
29+
this.starred = false;
30+
}
31+
32+
public Mail(final @NotNull ResultSet rs) throws SQLException {
33+
final @NotNull Server server = CloudnodeMSG.getInstance().getServer();
34+
35+
this.id = UUID.fromString(rs.getString("id"));
36+
this.sender = server.getOfflinePlayer(UUID.fromString(rs.getString("sender")));
37+
this.recipient = server.getOfflinePlayer(UUID.fromString(rs.getString("recipient")));
38+
this.message = rs.getString("message");
39+
this.seen = rs.getBoolean("seen");
40+
this.starred = rs.getBoolean("starred");
41+
}
42+
43+
public void update() {
44+
try (final @NotNull PreparedStatement stmt = CloudnodeMSG.getInstance().db().getConnection().prepareStatement("UPDATE `cloudnodemsg_mail` SET `seen` = ?, `starred` = ? WHERE `id` = ?")) {
45+
stmt.setBoolean(1, seen);
46+
stmt.setBoolean(2, starred);
47+
stmt.setString(3, id.toString());
48+
stmt.executeUpdate();
49+
}
50+
catch (final @NotNull SQLException exception) {
51+
CloudnodeMSG.getInstance().getLogger().log(Level.SEVERE, "Could not update mail: " + id, exception);
52+
}
53+
}
54+
55+
public void delete() {
56+
try (final @NotNull PreparedStatement stmt = CloudnodeMSG.getInstance().db().getConnection().prepareStatement("DELETE FROM `cloudnodemsg_mail` WHERE `id` = ?")) {
57+
stmt.setString(1, id.toString());
58+
stmt.executeUpdate();
59+
}
60+
catch (final @NotNull SQLException exception) {
61+
CloudnodeMSG.getInstance().getLogger().log(Level.SEVERE, "Could not delete mail: " + id, exception);
62+
}
63+
}
64+
65+
public void insert() {
66+
try (final @NotNull PreparedStatement stmt = CloudnodeMSG.getInstance().db().getConnection().prepareStatement("INSERT INTO `cloudnodemsg_mail` (`id`, `sender`, `recipient`, `message`, `seen`, `starred`) VALUES (?, ?, ?, ?, ?, ?)")) {
67+
stmt.setString(1, id.toString());
68+
stmt.setString(2, sender.getUniqueId().toString());
69+
stmt.setString(3, recipient.getUniqueId().toString());
70+
stmt.setString(4, message);
71+
stmt.setBoolean(5, seen);
72+
stmt.setBoolean(6, starred);
73+
stmt.executeUpdate();
74+
}
75+
catch (final @NotNull SQLException exception) {
76+
CloudnodeMSG.getInstance().getLogger().log(Level.SEVERE, "Could not insert mail: " + id, exception);
77+
}
78+
}
79+
80+
public static @NotNull Optional<@NotNull Mail> get(final @NotNull UUID id) {
81+
try (final @NotNull PreparedStatement stmt = CloudnodeMSG.getInstance().db().getConnection().prepareStatement("SELECT * FROM `cloudnodemsg_mail` WHERE `id` = ? LIMIT 1")) {
82+
stmt.setString(1, id.toString());
83+
final @NotNull ResultSet rs = stmt.executeQuery();
84+
if (!rs.next()) return Optional.empty();
85+
return Optional.of(new Mail(rs));
86+
}
87+
catch (final @NotNull SQLException exception) {
88+
CloudnodeMSG.getInstance().getLogger().log(Level.SEVERE, "Could not get mail: " + id, exception);
89+
return Optional.empty();
90+
}
91+
}
92+
93+
public static int unread(final @NotNull OfflinePlayer player) {
94+
try (final @NotNull PreparedStatement stmt = CloudnodeMSG.getInstance().db().getConnection().prepareStatement("SELECT COUNT(`id`) as `n` FROM `cloudnodemsg_mail` WHERE `recipient` = ? AND `seen` = 0")) {
95+
stmt.setString(1, player.getUniqueId().toString());
96+
final @NotNull ResultSet rs = stmt.executeQuery();
97+
if (!rs.next()) return 0;
98+
return rs.getInt("n");
99+
}
100+
catch (final @NotNull SQLException exception) {
101+
CloudnodeMSG.getInstance().getLogger().log(Level.SEVERE, "Could not get unread mails: " + player.getName(), exception);
102+
return 0;
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)