mirror of
https://github.com/YouHaveTrouble/PreventStabby.git
synced 2026-05-12 13:26:56 +00:00
use logger, use try with resources
This commit is contained in:
@@ -38,7 +38,7 @@ public final class PreventStabby extends JavaPlugin {
|
||||
Util.initData();
|
||||
reloadPluginConfig();
|
||||
File dbFile = new File("plugins/PreventStabby");
|
||||
sqLite = new DatabaseSQLite("jdbc:sqlite:plugins/PreventStabby/database.db", dbFile);
|
||||
sqLite = new DatabaseSQLite("jdbc:sqlite:plugins/PreventStabby/database.db", dbFile, getLogger());
|
||||
sqLite.createDatabaseFile();
|
||||
if (!sqLite.testConnection()) {
|
||||
getLogger().severe("Error with accessing database. Check if server has write rights.");
|
||||
|
||||
@@ -6,89 +6,80 @@ import me.youhavetrouble.preventstabby.players.PlayerData;
|
||||
import java.io.File;
|
||||
import java.sql.*;
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class DatabaseSQLite {
|
||||
|
||||
private final String url;
|
||||
private final File folder;
|
||||
private final Logger logger;
|
||||
|
||||
public DatabaseSQLite(String url, File folder) {
|
||||
public DatabaseSQLite(String url, File folder, Logger logger) {
|
||||
this.url = url;
|
||||
this.folder = folder;
|
||||
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
public boolean createDatabaseFile() {
|
||||
public void createDatabaseFile() {
|
||||
this.folder.mkdir();
|
||||
try (Connection conn = DriverManager.getConnection(url)) {
|
||||
if (conn != null) {
|
||||
DatabaseMetaData meta = conn.getMetaData();
|
||||
System.out.println("The driver name is " + meta.getDriverName());
|
||||
Statement statement = conn.createStatement();
|
||||
String sql = "CREATE TABLE IF NOT EXISTS `players` (`player_uuid` varchar(36) UNIQUE PRIMARY KEY, `pvpenabled` boolean);";
|
||||
statement.execute(sql);
|
||||
conn.close();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.out.println(e.getMessage());
|
||||
return false;
|
||||
if (conn == null) return;
|
||||
DatabaseMetaData meta = conn.getMetaData();
|
||||
logger.info("The driver name is " + meta.getDriverName());
|
||||
Statement statement = conn.createStatement();
|
||||
String sql = "CREATE TABLE IF NOT EXISTS `players` (`player_uuid` varchar(36) UNIQUE PRIMARY KEY, `pvpenabled` boolean);";
|
||||
statement.execute(sql);
|
||||
} catch (SQLException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean testConnection() {
|
||||
Connection conn = null;
|
||||
try {
|
||||
conn = DriverManager.getConnection(url);
|
||||
System.out.println("Connection to SQLite has been established.");
|
||||
if (conn != null) {
|
||||
conn.close();
|
||||
return true;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.out.println(e.getMessage());
|
||||
try (Connection conn = DriverManager.getConnection(url)) {
|
||||
logger.info("Connection to SQLite has been established.");
|
||||
if (conn != null) return true;
|
||||
} catch (SQLException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public PlayerData getPlayerInfo(UUID uuid) {
|
||||
try {
|
||||
Connection conn = DriverManager.getConnection(url);
|
||||
Statement insertnewuser = conn.createStatement();
|
||||
try (Connection conn = DriverManager.getConnection(url)) {
|
||||
try {
|
||||
String newuserdata = "INSERT OR IGNORE INTO `players` (player_uuid, pvpenabled) VALUES ('" + uuid.toString() + "', " + PreventStabby.getPlugin().getConfigCache().isPvp_enabled_by_default() + ")";
|
||||
insertnewuser.execute(newuserdata);
|
||||
PreparedStatement insertnewuser = conn.prepareStatement("INSERT OR IGNORE INTO `players` (player_uuid, pvpenabled) VALUES (?, ?)");
|
||||
insertnewuser.setString(1, uuid.toString());
|
||||
insertnewuser.setBoolean(2, PreventStabby.getPlugin().getConfigCache().isPvp_enabled_by_default());
|
||||
insertnewuser.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
if (e.getErrorCode() != 19) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
Statement statement = conn.createStatement();
|
||||
String sql = "SELECT * FROM `players` WHERE `player_uuid` = '" + uuid + "';";
|
||||
statement.execute(sql);
|
||||
PreparedStatement statement = conn.prepareStatement("SELECT * FROM `players` WHERE `player_uuid` = ?");
|
||||
statement.setString(1, uuid.toString());
|
||||
statement.executeQuery();
|
||||
ResultSet result = statement.getResultSet();
|
||||
boolean state = result.getBoolean("pvpenabled");
|
||||
conn.close();
|
||||
return new PlayerData(uuid, state);
|
||||
} catch (SQLException throwables) {
|
||||
throwables.printStackTrace();
|
||||
} catch (SQLException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void updatePlayerInfo(UUID uuid, PlayerData data) {
|
||||
try {
|
||||
Connection conn = DriverManager.getConnection(url);
|
||||
Statement insertnewuser = conn.createStatement();
|
||||
try (Connection conn = DriverManager.getConnection(url)) {
|
||||
try {
|
||||
String newuserdata = "UPDATE `players` SET pvpenabled = "+data.isPvpEnabled()+" WHERE `player_uuid` = '"+uuid.toString()+"';";
|
||||
insertnewuser.execute(newuserdata);
|
||||
} catch (SQLException e) {
|
||||
PreventStabby.getPlugin().getLogger().severe("Error while saving player data!");
|
||||
e.printStackTrace();
|
||||
PreparedStatement insertnewuser = conn.prepareStatement("UPDATE `players` SET pvpenabled = ? WHERE `player_uuid` = ?;");
|
||||
insertnewuser.setBoolean(1, data.isPvpEnabled());
|
||||
insertnewuser.setString(2, uuid.toString());
|
||||
} catch (SQLException exception) {
|
||||
logger.severe("Error while saving player data!");
|
||||
exception.printStackTrace();
|
||||
}
|
||||
} catch (SQLException throwables) {
|
||||
throwables.printStackTrace();
|
||||
} catch (SQLException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user