11package pro .cloudnode .smp .cloudnodemsg ;
22
3+ import com .zaxxer .hikari .HikariConfig ;
4+ import com .zaxxer .hikari .HikariDataSource ;
35import org .bukkit .entity .Player ;
46import org .bukkit .metadata .MetadataValue ;
57import org .bukkit .plugin .java .JavaPlugin ;
68import org .jetbrains .annotations .NotNull ;
9+ import org .jetbrains .annotations .Nullable ;
710import pro .cloudnode .smp .cloudnodemsg .command .IgnoreCommand ;
811import pro .cloudnode .smp .cloudnodemsg .command .MainCommand ;
912import pro .cloudnode .smp .cloudnodemsg .command .MessageCommand ;
1215import pro .cloudnode .smp .cloudnodemsg .listener .AsyncChatListener ;
1316import 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 ;
1524import java .util .Objects ;
25+ import java .util .logging .Level ;
1626
1727public 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}
0 commit comments