Skip to content
This repository was archived by the owner on Jun 23, 2022. It is now read-only.

Added SQLite support #2

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions src/main/java/com/mnewt00/vulcandatabase/VulcanDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,19 @@
import com.mnewt00.vulcandatabase.commands.LogsCommand;
import com.mnewt00.vulcandatabase.listener.VulcanListener;
import com.mnewt00.vulcandatabase.storage.MySQLStorageProvider;
import com.mnewt00.vulcandatabase.storage.SQLiteStorageProvider;
import com.mnewt00.vulcandatabase.storage.StorageProvider;
import lombok.Getter;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.Locale;

public final class VulcanDatabase extends JavaPlugin {
@Getter private static VulcanDatabase instance;
@Getter private MySQLStorageProvider storageProvider;
@Getter private StorageProvider storageProvider;
@Getter private BukkitAudiences adventure;

@Override
Expand All @@ -45,17 +49,22 @@ public void onEnable() {

saveDefaultConfig();

ConfigurationSection data = getConfig().getConfigurationSection("connection-information");

String host = data.getString("host").split(":")[0];
String port = data.getString("host").split(":").length > 1 ? data.getString("host").split(":")[1] : "";
String username = data.getString("username");
String password = data.getString("password");
String databaseName = data.getString("database-name");
String tablePrefix = data.getString("table-prefix");
boolean useSSL = data.getBoolean("useSSL");

storageProvider = new MySQLStorageProvider(host, port, username, password, databaseName, tablePrefix, useSSL);
if (getConfig().getString("connection-type").toUpperCase(Locale.ROOT).equals("MYSQL")) {
ConfigurationSection data = getConfig().getConfigurationSection("connection-information");

String host = data.getString("host").split(":")[0];
String port = data.getString("host").split(":").length > 1 ? data.getString("host").split(":")[1] : "";
String username = data.getString("username");
String password = data.getString("password");
String databaseName = data.getString("database-name");
String tablePrefix = data.getString("table-prefix");
boolean useSSL = data.getBoolean("useSSL");
storageProvider = new MySQLStorageProvider(host, port, username, password, databaseName, tablePrefix, useSSL);
} else {
storageProvider = new SQLiteStorageProvider();
}

Bukkit.getPluginManager().registerEvents(new VulcanListener(), this);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
TextComponent.Builder builder = Component.text(Common.colorize(VulcanDatabase.getInstance().getConfig().getString("messages.log.time.message").replace("%niceformatted%", friendlyTime).replace("%longdateformat%", sdf.format(new Date(log.getTimestamp())))))
.hoverEvent(logEventHoverFirst).toBuilder();

String description = treeMap.get(log.getCheckName() + log.getCheckType()).getDescription();
String description = treeMap.get((log.getCheckName() + log.getCheckType()).replace(" ", "").toLowerCase(Locale.ROOT)).getDescription();

TextComponent mainLog = (Component.text(" " + Common.colorize(VulcanDatabase.getInstance().getConfig().getString("messages.log.main-message.message")
.replace("%player%", player.getName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,16 @@
public class VulcanListener implements Listener {
@EventHandler
public void onVulcanFlag(VulcanFlagEvent event) {
// Bukkit.getScheduler().runTaskAsynchronously(VulcanDatabase.getInstance(), () ->
VulcanDatabase.getInstance().getStorageProvider().addLog(new Log(
VulcanDatabase.getInstance().getStorageProvider().addLog(new Log(
event.getPlayer().getUniqueId(), event.getPlayer().getName(),
System.currentTimeMillis(),
VulcanDatabase.getInstance().getConfig().getString("server-name"),
event.getInfo(),
event.getCheck().getDisplayName(),
String.valueOf(event.getCheck().getType()).toUpperCase(),
event.getCheck().getVl() + 1, VulcanAPI.Factory.getApi().getClientVersion(event.getPlayer()),
event.getCheck().getVl() + 1, VulcanAPI.Factory.getApi().getClientVersion(event.getPlayer()),
VulcanAPI.Factory.getApi().getPing(event.getPlayer()),
VulcanAPI.Factory.getApi().getTps()),
event.getPlayer().getUniqueId());
// );
event.getPlayer().getUniqueId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
import java.util.List;
import java.util.UUID;

public class MySQLStorageProvider {
private final HikariDataSource dataSource;
@Getter private Connection connection;
public class MySQLStorageProvider implements StorageProvider {

@Getter private final Connection connection;

@SneakyThrows
public MySQLStorageProvider(String host, String port, String username, String password, String databaseName, String tablePrefix, boolean useSSL) {
Expand All @@ -66,12 +66,13 @@ public MySQLStorageProvider(String host, String port, String username, String pa
config.addDataSourceProperty("cacheCallableStmts", "true");
config.addDataSourceProperty("serverTimezone", "UTC");

this.dataSource = new HikariDataSource(config);
HikariDataSource dataSource = new HikariDataSource(config);
this.connection = dataSource.getConnection();

initiateTables();
createTables();
}

@Override
public int count(UUID uuid) {
int finalCount;
try (PreparedStatement preparedStatement = getConnection().prepareStatement("SELECT COUNT(*) FROM vulcandb_logs WHERE uuid = ?")) {
Expand All @@ -87,6 +88,7 @@ public int count(UUID uuid) {
return finalCount;
}

@Override
public List<Log> getLogs(int amount, int offset, UUID uuid) {
List<Log> logs = Lists.newArrayList();

Expand Down Expand Up @@ -118,29 +120,8 @@ public List<Log> getLogs(int amount, int offset, UUID uuid) {
return logs;
}

public void addLog(Log log, UUID uuid) {
try (PreparedStatement preparedStatement = getConnection().prepareStatement("INSERT INTO vulcandb_logs" +
" (uuid, name, timestamp, `server`, information, `check`, check_type, violations, version, ping, tps)" +
" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);")) {
preparedStatement.setString(1, log.getUuid().toString());
preparedStatement.setString(2, log.getPlayerName());
preparedStatement.setString(3, String.valueOf(log.getTimestamp()));
preparedStatement.setString(4, log.getServer());
preparedStatement.setString(5, log.getInfo());
preparedStatement.setString(6, log.getCheckName());
preparedStatement.setString(7, log.getCheckType());
preparedStatement.setInt(8, log.getVl());
preparedStatement.setString(9, log.getVersion());
preparedStatement.setInt(10, log.getPing());
preparedStatement.setDouble(11, log.getTps());

preparedStatement.executeUpdate();
} catch (SQLException exception) {
exception.printStackTrace();
}
}

public void initiateTables() {
@Override
public void createTables() {
try (PreparedStatement preparedStatement = getConnection().prepareStatement(
"CREATE TABLE IF NOT EXISTS vulcandb_logs (" +
"id INTEGER PRIMARY KEY AUTO_INCREMENT," +
Expand All @@ -162,4 +143,27 @@ public void initiateTables() {
exception.printStackTrace();
}
}

@Override
public void addLog(Log log, UUID uuid) {
try (PreparedStatement preparedStatement = getConnection().prepareStatement("INSERT INTO vulcandb_logs" +
" (uuid, name, timestamp, `server`, information, `check`, check_type, violations, version, ping, tps)" +
" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);")) {
preparedStatement.setString(1, log.getUuid().toString());
preparedStatement.setString(2, log.getPlayerName());
preparedStatement.setString(3, String.valueOf(log.getTimestamp()));
preparedStatement.setString(4, log.getServer());
preparedStatement.setString(5, log.getInfo());
preparedStatement.setString(6, log.getCheckName());
preparedStatement.setString(7, log.getCheckType());
preparedStatement.setInt(8, log.getVl());
preparedStatement.setString(9, log.getVersion());
preparedStatement.setInt(10, log.getPing());
preparedStatement.setDouble(11, log.getTps());

preparedStatement.executeUpdate();
} catch (SQLException exception) {
exception.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package com.mnewt00.vulcandatabase.storage;

import com.google.common.collect.Lists;
import com.mnewt00.vulcandatabase.Log;
import com.mnewt00.vulcandatabase.VulcanDatabase;
import lombok.Getter;
import lombok.SneakyThrows;

import java.io.File;
import java.sql.*;
import java.util.List;
import java.util.UUID;

public class SQLiteStorageProvider implements StorageProvider {

@Getter
private Connection connection = null;

public SQLiteStorageProvider() {
File database = new File(VulcanDatabase.getInstance().getDataFolder(), "database.db");
try {
Class.forName("org.sqlite.JDBC");
this.connection = DriverManager.getConnection("jdbc:sqlite:" + database.getAbsolutePath());
} catch (ClassNotFoundException | SQLException exception) {
exception.printStackTrace();
}
createTables();
}

@Override
public void createTables() {
try (PreparedStatement preparedStatement = getConnection().prepareStatement(
"CREATE TABLE IF NOT EXISTS vulcandb_logs (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT," +
"uuid VARCHAR(36) NOT NULL," +
"name VARCHAR(255) NOT NULL," +
"timestamp VARCHAR(255) NOT NULL," +
"server VARCHAR(255)," +
"information VARCHAR(255)," +
"`check` VARCHAR(255) NOT NULL," +
"check_type VARCHAR(255) NOT NULL," +
"violations INTEGER NOT NULL," +
"version VARCHAR(255) NOT NULL," +
"ping INTEGER," +
"tps DOUBLE" +
");"
)) {
preparedStatement.execute();
} catch (SQLException exception) {
exception.printStackTrace();
}
}

@Override
public void addLog(Log log, UUID uuid) {
try (PreparedStatement preparedStatement = getConnection().prepareStatement("INSERT INTO vulcandb_logs" +
" (uuid, name, timestamp, `server`, information, `check`, check_type, violations, version, ping, tps)" +
" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);")) {
preparedStatement.setString(1, log.getUuid().toString());
preparedStatement.setString(2, log.getPlayerName());
preparedStatement.setString(3, String.valueOf(log.getTimestamp()));
preparedStatement.setString(4, log.getServer());
preparedStatement.setString(5, log.getInfo());
preparedStatement.setString(6, log.getCheckName());
preparedStatement.setString(7, log.getCheckType());
preparedStatement.setInt(8, log.getVl());
preparedStatement.setString(9, log.getVersion());
preparedStatement.setInt(10, log.getPing());
preparedStatement.setDouble(11, log.getTps());

preparedStatement.executeUpdate();
} catch (SQLException exception) {
exception.printStackTrace();
}
}

@Override
public List<Log> getLogs(int amount, int offset, UUID uuid) {
List<Log> logs = Lists.newArrayList();

try (PreparedStatement preparedStatement = getConnection().prepareStatement("SELECT uuid,name,timestamp,`server`,information,`check`,check_type,violations,version,ping,tps FROM vulcandb_logs WHERE `uuid` = ? ORDER BY timestamp DESC LIMIT ? OFFSET ?;")) {
preparedStatement.setString(1, uuid.toString());
preparedStatement.setInt(2, amount);
preparedStatement.setInt(3, offset);

ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
logs.add(new Log(
UUID.fromString(resultSet.getString(1)),
resultSet.getString(2),
Long.parseLong(resultSet.getString(3)),
resultSet.getString(4),
resultSet.getString(5),
resultSet.getString(6),
resultSet.getString(7),
resultSet.getInt(8),
resultSet.getString(9),
resultSet.getInt(10),
resultSet.getDouble(11)
));
}
resultSet.close();
} catch (SQLException exception) {
exception.printStackTrace();
}
return logs;
}

@Override
public int count(UUID uuid) {
int finalCount;
try (PreparedStatement preparedStatement = getConnection().prepareStatement("SELECT COUNT(*) FROM vulcandb_logs WHERE uuid = ?")) {
preparedStatement.setString(1, uuid.toString());
ResultSet set = preparedStatement.executeQuery();
set.next();
finalCount = set.getInt(1);
set.close();
} catch (SQLException exception) {
exception.printStackTrace();
finalCount = 0;
}
return finalCount;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.mnewt00.vulcandatabase.storage;

import com.mnewt00.vulcandatabase.Log;

import java.util.List;
import java.util.UUID;

public interface StorageProvider {

void createTables();

void addLog(Log log, UUID uuid);

List<Log> getLogs(int amount, int offset, UUID uuid);

int count(UUID uuid);
}
4 changes: 4 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# The storage types the plugin uses

# There are two types of storage: sqlite and mysql.
connection-type: sqlite

# Only used if connection-type is MySQL
connection-information:
# By default, the ports will be 3306.
# Use ip:port if you need to.
Expand Down