Skip to content

Commit

Permalink
Merge pull request #89 from webhead1104/master
Browse files Browse the repository at this point in the history
Group the mysql database table creation together
  • Loading branch information
webhead1104 authored Jun 15, 2024
2 parents 3b51c8a + ee18e38 commit 58edcef
Showing 1 changed file with 52 additions and 53 deletions.
105 changes: 52 additions & 53 deletions src/main/java/net/cytonic/cytosis/data/MysqlDatabase.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.cytonic.cytosis.data;

import net.cytonic.cytosis.Cytosis;
import net.cytonic.cytosis.auditlog.Category;
import net.cytonic.cytosis.auditlog.Entry;
import net.cytonic.cytosis.config.CytosisSettings;
Expand Down Expand Up @@ -79,21 +78,21 @@ public void disconnect() {
connection.close();
Logger.info("Database connection closed!");
} catch (SQLException e) {
Logger.error("An error occurred whilst disconnecting from the database. Please report the following stacktrace to Foxikle: ", e);
Logger.error("An error occurred whilst disconnecting from the database. Please report the following stacktrace to CytonicMC: ", e);
}
}
});
}

public void createTables() {
createRanksTable();
createChatTable();
createRanksTable();
createBansTable();
createAuditLogTable();
createPlayersTable();
createWorldTable();
createPlayerJoinsTable();
createChatChannelsTable();
createPlayerJoinsTable();
createAuditLogTable();
}

private Connection getConnection() {
Expand All @@ -108,7 +107,7 @@ private void createChatTable() {
ps = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS cytonicchat (id INT NOT NULL AUTO_INCREMENT, timestamp TIMESTAMP, uuid VARCHAR(36), message TEXT, PRIMARY KEY(id))");
ps.executeUpdate();
} catch (SQLException e) {
Logger.error("An error occoured whilst fetching data from the database. Please report the following stacktrace to Foxikle:", e);
Logger.error("An error occoured whilst fetching data from the database. Please report the following stacktrace to Cy:", e);
}
}
});
Expand Down Expand Up @@ -156,6 +155,52 @@ private void createPlayersTable() {
});
}

public void createWorldTable() {
worker.submit(() -> {
if (isConnected()) {
PreparedStatement ps;
try {
ps = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS cytonic_worlds (world_name TEXT, world_type TEXT, last_modified TIMESTAMP, world_data MEDIUMBLOB, spawn_point TEXT, extra_data varchar(100))");
ps.executeUpdate();
} catch (SQLException e) {
Logger.error("An error occurred whilst creating the `cytonic_worlds` table.", e);
}
}
});
}

/**
* Creates the 'cytonic_chat_channels' table in the database if it doesn't exist.
* The table contains information about player's chat channels.
*
* @throws IllegalStateException if the database connection is not open.
*/
private void createChatChannelsTable() {
worker.submit(() -> {
if (isConnected()) {
PreparedStatement ps;
try {
ps = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS cytonic_chat_channels (uuid VARCHAR(36), chat_channel VARCHAR(16), PRIMARY KEY(uuid))");
ps.executeUpdate();
} catch (SQLException e) {
Logger.error("An error occurred whilst creating the `cytonic_chat_channels` table.", e);
}
}
});
}

private void createPlayerJoinsTable() {
worker.submit(() -> {
if (isConnected()) {
try (PreparedStatement ps = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS cytonic_player_joins (joined TIMESTAMP, uuid VARCHAR(36), ip TEXT)")) {
ps.executeUpdate();
} catch (SQLException e) {
Logger.error("An error occurred whilst creating the `cytonic_player_joins` table.", e);
}
}
});
}

/**
* actor is staff<p>
* category would be BAN, see {@link Category}<p>
Expand All @@ -170,7 +215,7 @@ private void createAuditLogTable() {
ps = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS cytonic_audit_log (id INT NOT NULL AUTO_INCREMENT, timestamp TIMESTAMP, uuid VARCHAR(36), reason TINYTEXT, category VARCHAR(50), actor VARCHAR(36), PRIMARY KEY(id))");
ps.executeUpdate();
} catch (SQLException e) {
Logger.error("An error occoured whilst fetching data from the database. Please report the following stacktrace to Foxikle:", e);
Logger.error("An error occoured whilst fetching data from the database. Please report the following stacktrace to CytonicMC:", e);
}
}
});
Expand Down Expand Up @@ -383,52 +428,6 @@ public CompletableFuture<Void> unbanPlayer(UUID uuid) {
return future;
}

public void createWorldTable() {
worker.submit(() -> {
if (isConnected()) {
PreparedStatement ps;
try {
ps = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS cytonic_worlds (world_name TEXT, world_type TEXT, last_modified TIMESTAMP, world_data MEDIUMBLOB, spawn_point TEXT, extra_data varchar(100))");
ps.executeUpdate();
} catch (SQLException e) {
Logger.error("An error occurred whilst creating the `cytonic_worlds` table.", e);
}
}
});
}

/**
* Creates the 'cytonic_chat_channels' table in the database if it doesn't exist.
* The table contains information about player's chat channels.
*
* @throws IllegalStateException if the database connection is not open.
*/
private void createChatChannelsTable() {
worker.submit(() -> {
if (isConnected()) {
PreparedStatement ps;
try {
ps = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS cytonic_chat_channels (uuid VARCHAR(36), chat_channel VARCHAR(16), PRIMARY KEY(uuid))");
ps.executeUpdate();
} catch (SQLException e) {
Logger.error("An error occurred whilst creating the `cytonic_chat_channels` table.", e);
}
}
});
}

private void createPlayerJoinsTable() {
worker.submit(() -> {
if (isConnected()) {
try (PreparedStatement ps = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS cytonic_player_joins (joined TIMESTAMP, uuid VARCHAR(36), ip TEXT)")) {
ps.executeUpdate();
} catch (SQLException e) {
Logger.error("An error occurred whilst creating the `cytonic_player_joins` table.", e);
}
}
});
}

public void setChatChannel(UUID uuid, ChatChannel chatChannel) {
worker.submit(() -> {
if (!isConnected())
Expand Down

0 comments on commit 58edcef

Please sign in to comment.