Skip to content

Commit 6d5c714

Browse files
committed
wip
1 parent 4390b56 commit 6d5c714

6 files changed

Lines changed: 173 additions & 1 deletion

File tree

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,11 @@
8181
<version>1.20.2-R0.1-SNAPSHOT</version>
8282
<scope>provided</scope>
8383
</dependency>
84+
<dependency>
85+
<groupId>com.zaxxer</groupId>
86+
<artifactId>HikariCP</artifactId>
87+
<version>5.1.0</version>
88+
<scope>compile</scope>
89+
</dependency>
8490
</dependencies>
8591
</project>

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

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package pro.cloudnode.smp.cloudnodemsg;
22

3+
import com.zaxxer.hikari.HikariConfig;
4+
import com.zaxxer.hikari.HikariDataSource;
35
import org.bukkit.entity.Player;
46
import org.bukkit.metadata.MetadataValue;
57
import org.bukkit.plugin.java.JavaPlugin;
68
import org.jetbrains.annotations.NotNull;
9+
import org.jetbrains.annotations.Nullable;
710
import pro.cloudnode.smp.cloudnodemsg.command.IgnoreCommand;
811
import pro.cloudnode.smp.cloudnodemsg.command.MainCommand;
912
import pro.cloudnode.smp.cloudnodemsg.command.MessageCommand;
@@ -12,7 +15,14 @@
1215
import pro.cloudnode.smp.cloudnodemsg.listener.AsyncChatListener;
1316
import pro.cloudnode.smp.cloudnodemsg.command.ToggleMessageCommand;
1417

18+
import java.io.InputStream;
19+
import java.sql.Connection;
20+
import java.sql.PreparedStatement;
21+
import java.sql.SQLException;
22+
import java.util.Arrays;
23+
import java.util.Map;
1524
import java.util.Objects;
25+
import java.util.logging.Level;
1626

1727
public final class CloudnodeMSG extends JavaPlugin {
1828
public static @NotNull CloudnodeMSG getInstance() {
@@ -41,7 +51,7 @@ public void onEnable() {
4151

4252
@Override
4353
public void onDisable() {
44-
// Plugin shutdown logic
54+
dbSource.close();
4555
}
4656

4757
public static boolean isVanished(final @NotNull Player player) {
@@ -55,4 +65,52 @@ public static boolean isVanished(final @NotNull Player player) {
5565
public @NotNull PluginConfig config() {
5666
return config;
5767
}
68+
69+
public final @NotNull HikariConfig hikariConfig = new HikariConfig();
70+
private HikariDataSource dbSource;
71+
72+
public @NotNull HikariDataSource db() {
73+
return dbSource;
74+
}
75+
76+
private void setupDbSource() {
77+
if (dbSource != null) dbSource.close();
78+
hikariConfig.setDriverClassName("org.sqlite.JDBC");
79+
hikariConfig.setJdbcUrl("jdbc:sqlite:" + getDataFolder().getAbsolutePath() + "/" + config().dbSqliteFile());
80+
81+
for (final @NotNull Map.Entry<@NotNull String, @NotNull String> entry : config().dbHikariProperties().entrySet())
82+
hikariConfig.addDataSourceProperty(entry.getKey(), entry.getValue());
83+
84+
dbSource = new HikariDataSource(hikariConfig);
85+
}
86+
87+
/**
88+
* Run DDL script
89+
*/
90+
public void runDDL() {
91+
final @NotNull String file = "ddl/sqlite.sql";
92+
final @NotNull String @NotNull [] queries;
93+
try (final @Nullable InputStream inputStream = getClassLoader().getResourceAsStream(file)) {
94+
queries = Arrays.stream(
95+
new String(Objects.requireNonNull(inputStream).readAllBytes()).split(";")
96+
).map(q -> q.stripTrailing().stripIndent().replaceAll("^\\s+(?:--.+)*", "")).toArray(String[]::new);
97+
}
98+
catch (final @NotNull Exception exception) {
99+
getLogger().log(Level.SEVERE, "Could not read DDL script: " + file, exception);
100+
getServer().getPluginManager().disablePlugin(this);
101+
return;
102+
}
103+
for (final @NotNull String query : queries) {
104+
if (query.isBlank()) continue;
105+
try (final @NotNull PreparedStatement stmt = db().getConnection().prepareStatement(query)) {
106+
stmt.execute();
107+
}
108+
catch (final @NotNull SQLException exception) {
109+
getLogger().log(Level.SEVERE, "Could not execute DDL query: " + query, exception);
110+
getServer().getPluginManager().disablePlugin(this);
111+
return;
112+
}
113+
}
114+
getLogger().info("Database successfully initialised with DDL");
115+
}
58116
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.ResultSet;
8+
import java.sql.SQLException;
9+
import java.util.UUID;
10+
11+
public final class Mail {
12+
public final @NotNull UUID id;
13+
public final @NotNull OfflinePlayer sender;
14+
public final @NotNull OfflinePlayer recipient;
15+
public final @NotNull String message;
16+
public final boolean seen;
17+
public final boolean starred;
18+
19+
public Mail(final @NotNull OfflinePlayer sender, final @NotNull OfflinePlayer recipient, final @NotNull String message) {
20+
this.id = UUID.randomUUID();
21+
this.sender = sender;
22+
this.recipient = recipient;
23+
this.message = message;
24+
this.seen = false;
25+
this.starred = false;
26+
}
27+
28+
public Mail(final @NotNull ResultSet rs) throws SQLException {
29+
final @NotNull Server server = CloudnodeMSG.getInstance().getServer();
30+
31+
this.id = UUID.fromString(rs.getString("id"));
32+
this.sender = server.getOfflinePlayer(UUID.fromString(rs.getString("sender")));
33+
this.recipient = server.getOfflinePlayer(UUID.fromString(rs.getString("recipient")));
34+
this.message = rs.getString("message");
35+
this.seen = rs.getBoolean("seen");
36+
this.starred = rs.getBoolean("starred");
37+
}
38+
}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
import org.bukkit.configuration.file.FileConfiguration;
77
import org.jetbrains.annotations.NotNull;
88

9+
import java.util.ArrayList;
10+
import java.util.HashMap;
11+
import java.util.List;
12+
import java.util.Map;
913
import java.util.Objects;
1014

1115
public final class PluginConfig {
@@ -15,6 +19,31 @@ public PluginConfig(final @NotNull FileConfiguration config) {
1519
this.config = config;
1620
}
1721

22+
/**
23+
* Database file name (in the plugin's folder)
24+
* <p>If the file does not exist, it will be created</p>
25+
*/
26+
public @NotNull String dbSqliteFile() {
27+
return Objects.requireNonNull(config.getString("db.sqlite.file"));
28+
}
29+
30+
/**
31+
* Advanced DB configuration / HikariCP properties
32+
* <p>Only change if you know what you're doing; you can add or remove any property you want</p>
33+
*/
34+
public @NotNull HashMap<@NotNull String, @NotNull String> dbHikariProperties() {
35+
final @NotNull List<@NotNull Map<?, ?>> mapList = config.getMapList("db.hikaricp");
36+
final @NotNull HashMap<@NotNull String, @NotNull String> properties = new HashMap<>(mapList.size());
37+
38+
for (final @NotNull Map<?, ?> map : mapList)
39+
if (
40+
map.get("name") instanceof final @NotNull String name
41+
&& map.get("value") instanceof final @NotNull String value
42+
) properties.put(name, value);
43+
44+
return properties;
45+
}
46+
1847
/**
1948
* Incoming message format (recipient's point of view)
2049
* <p>Placeholders:</p>

src/main/resources/config.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
1+
# Database configuration
2+
db:
3+
sqlite:
4+
# Database file name (in the plugin's folder)
5+
# If the file does not exist, it will be created
6+
file: 'cloudnodemsg.db'
7+
8+
# Advanced DB configuration / HikariCP properties
9+
# Only change if you know what you're doing; you can add or remove any property you want
10+
hikaricp:
11+
- name: "cachePrepStmts"
12+
value: "true"
13+
- name: "prepStmtCacheSize"
14+
value: "250"
15+
- name: "prepStmtCacheSqlLimit"
16+
value: "2048"
17+
- name: "useServerPrepStmts"
18+
value: "true"
19+
- name: "useLocalSessionState"
20+
value: "true"
21+
- name: "rewriteBatchedStatements"
22+
value: "true"
23+
- name: "cacheResultSetMetadata"
24+
value: "true"
25+
- name: "cacheServerConfiguration"
26+
value: "true"
27+
- name: "elideSetAutoCommits"
28+
value: "true"
29+
- name: "maintainTimeStats"
30+
value: "false"
31+
32+
133
# Incoming message format (recipient's point of view)
234
# Placeholders:
335
# <sender> - the username of the message sender

src/main/resources/ddl/sqlite.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CREATE TABLE IF NOT EXISTS `cloudnodemsg_mail`
2+
(
3+
`id` CHAR(32) COLLATE NOCASE PRIMARY KEY,
4+
`sender` CHAR(32) COLLATE NOCASE NOT NULL,
5+
`recipient` CHAR(32) COLLATE NOCASE NOT NULL,
6+
`message` TEXT NOT NULL,
7+
`seen` BOOLEAN DEFAULT 0,
8+
`starred` BOOLEAN DEFAULT 0
9+
);

0 commit comments

Comments
 (0)