From 3cc819cd7dcd451d2f55f94b9321bc5dd51ed446 Mon Sep 17 00:00:00 2001 From: stijnb1234 Date: Mon, 3 Feb 2020 14:44:22 +0100 Subject: [PATCH] Added data system --- .../V10Lift/Managers/DBManager.java | 37 +++++- .../SBDeveloper/V10Lift/Utils/SBSQLiteDB.java | 116 ++---------------- .../SBDeveloper/V10Lift/Utils/SBYamlFile.java | 8 +- .../nl/SBDeveloper/V10Lift/V10LiftPlugin.java | 15 ++- 4 files changed, 55 insertions(+), 121 deletions(-) diff --git a/src/main/java/nl/SBDeveloper/V10Lift/Managers/DBManager.java b/src/main/java/nl/SBDeveloper/V10Lift/Managers/DBManager.java index aff505b..7166c8a 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/Managers/DBManager.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/Managers/DBManager.java @@ -4,8 +4,10 @@ import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import nl.SBDeveloper.V10Lift.API.Objects.Lift; import nl.SBDeveloper.V10Lift.Utils.SBSQLiteDB; +import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.*; @@ -17,11 +19,19 @@ public class DBManager { public DBManager(JavaPlugin plugin, String name) { data = new SBSQLiteDB(plugin, name); - data.execute("CREATE TABLE IF NOT EXISTS lifts (liftName varchar(255) NOT NULL, liftData blob NOT NULL, UNIQUE (liftName))"); + try { + String query = "CREATE TABLE IF NOT EXISTS lifts (liftName varchar(255) NOT NULL, liftData blob NOT NULL, UNIQUE (liftName))"; + PreparedStatement statement = data.getConnection().prepareStatement(query); + statement.execute(); + } catch (SQLException e) { + e.printStackTrace(); + } } public void load() throws SQLException { - ResultSet liftSet = data.execute("SELECT * FROM lifts", new HashMap<>()); + String query = "SELECT * FROM lifts"; + PreparedStatement statement = data.getConnection().prepareStatement(query); + ResultSet liftSet = statement.executeQuery(); while (liftSet.next()) { //Loading a lift... byte[] blob = liftSet.getBytes("liftData"); @@ -29,16 +39,33 @@ public class DBManager { Gson gson = new Gson(); Lift lift = gson.fromJson(json, new TypeToken(){}.getType()); DataManager.addLift(liftSet.getString("liftName"), lift); + + Bukkit.getLogger().info("[V10Lift] Loading lift " + liftSet.getString("liftName") + " from data..."); } } public void save() { - HashMap inserts = new HashMap<>(); Gson gson = new Gson(); for (Map.Entry entry : DataManager.getLifts().entrySet()) { - inserts.put(entry.getKey(), gson.toJson(entry.getValue()).getBytes()); + + byte[] blob = gson.toJson(entry.getValue()).getBytes(); + + try { + String query = "INSERT INTO lifts (liftName, liftData) VALUES (?, ?)"; + PreparedStatement statement = data.getConnection().prepareStatement(query); + statement.setString(1, entry.getKey()); + statement.setBytes(2, blob); + statement.executeUpdate(); + + String query2 = "UPDATE lifts SET liftData = ? WHERE liftName = ?"; + PreparedStatement statement2 = data.getConnection().prepareStatement(query2); + statement2.setBytes(1, blob); + statement2.setString(2, entry.getKey()); + statement2.executeUpdate(); + + Bukkit.getLogger().info("[V10Lift] Saving lift " + entry.getKey() + " to data..."); + } catch(SQLException ignored) {} } - //TODO Insert } public void closeConnection() { diff --git a/src/main/java/nl/SBDeveloper/V10Lift/Utils/SBSQLiteDB.java b/src/main/java/nl/SBDeveloper/V10Lift/Utils/SBSQLiteDB.java index 5eb4ce9..08de615 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/Utils/SBSQLiteDB.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/Utils/SBSQLiteDB.java @@ -27,16 +27,16 @@ public class SBSQLiteDB { * @param name The database name */ public SBSQLiteDB(@Nonnull Plugin plugin, String name) { - Bukkit.getLogger().info("[SBDBManager] Loading databases..."); + Bukkit.getLogger().info("[V10Lift] Loading databases..."); File dbFile = new File(plugin.getDataFolder(), name + ".db"); if (!dbFile.exists()) { try { - Bukkit.getLogger().info("[SBDBManager] Generating the " + name + ".db!"); + Bukkit.getLogger().info("[V10Lift] Generating the " + name + ".db!"); dbFile.createNewFile(); } catch (IOException e) { - Bukkit.getLogger().severe("[SBDBManager] Couldn't generate the " + name + ".db!"); + Bukkit.getLogger().severe("[V10Lift] Couldn't generate the " + name + ".db!"); e.printStackTrace(); return; } @@ -50,114 +50,16 @@ public class SBSQLiteDB { this.source = new HikariDataSource(config); } - /** - * Execute queries like CREATE TABLE, ALTER TABLE, .... - * - * @param query The query you want to execute - * - * @return true/false - */ - public boolean execute(String query) { - Connection con = null; - PreparedStatement statement = null; - boolean b; + //CREATE TABLE -> execute() + //SELECT -> executeQuery() + //UPDATE -> executeUpdate() - try { - con = this.source.getConnection(); - statement = con.prepareStatement(query); - b = statement.execute(); - return b; - } catch (SQLException e) { - Bukkit.getLogger().severe("[SBDBManager] SQL exception! Please check the stacktrace below."); - e.printStackTrace(); - } finally { - try { - if (statement != null) statement.close(); - if (con != null) con.close(); - } catch(SQLException e) { - e.printStackTrace(); - } - } - return false; - } - - /** - * Execute queries like INSERT, UPDATE, DELETE, ... - * - * @param query The query you want to execute - * @param objects The objects you want to insert, in the right order - * - * @return true/false - */ - public boolean execute(String query, LinkedHashMap objects) { - Connection con = null; - PreparedStatement statement = null; - int b; - try { - con = this.source.getConnection(); - statement = con.prepareStatement(query); - if (objects != null && !objects.isEmpty()) { - for (Map.Entry entry : objects.entrySet()) { - statement.setObject(entry.getKey(), entry.getValue()); - } - } - b = statement.executeUpdate(); - if (b >= 0) return true; - } catch (SQLException e) { - Bukkit.getLogger().severe("[SBDBManager] SQL exception! Please check the stacktrace below."); - e.printStackTrace(); - } finally { - try { - if (statement != null) statement.close(); - if (con != null) con.close(); - } catch(SQLException e) { - e.printStackTrace(); - } - } - return false; - } - - /** - * Execute a SELECT query - * - * @param query The query you want to execute - * @param objects The objects you want to insert, in the right order - * - * @return HashMap where the first object is the rowname and the second object is the value - */ - public ResultSet execute(String query, HashMap objects) { - Connection con = null; - PreparedStatement statement = null; - ResultSet set = null; - - try { - con = this.source.getConnection(); - statement = con.prepareStatement(query); - if (objects != null && !objects.isEmpty()) { - for (Map.Entry entry : objects.entrySet()) { - statement.setObject(entry.getKey(), entry.getValue()); - } - } - set = statement.executeQuery(); - return set; - } catch (SQLException e) { - Bukkit.getLogger().severe("[SBDBManager] SQL exception! Please check the stacktrace below."); - e.printStackTrace(); - } finally { - try { - if (set != null) set.close(); - if (statement != null) statement.close(); - if (con != null) con.close(); - } catch(SQLException e) { - e.printStackTrace(); - } - } - - return set; + public Connection getConnection() throws SQLException { + return this.source.getConnection(); } public void closeSource() { - Bukkit.getLogger().info("[SBDBManager] Closing the database connection!"); + Bukkit.getLogger().info("[V10Lift] Closing the database connection!"); this.source.close(); } } diff --git a/src/main/java/nl/SBDeveloper/V10Lift/Utils/SBYamlFile.java b/src/main/java/nl/SBDeveloper/V10Lift/Utils/SBYamlFile.java index c546775..3603bb7 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/Utils/SBYamlFile.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/Utils/SBYamlFile.java @@ -32,16 +32,16 @@ public class SBYamlFile { if (!this.file.exists()) { try { this.file.createNewFile(); - Bukkit.getLogger().info("[SBYamlManager] Generating the " + name + ".yml!"); + Bukkit.getLogger().info("[V10Lift] Generating the " + name + ".yml!"); } catch (IOException e) { - Bukkit.getLogger().severe("[SBYamlManager] Couldn't generate the " + name + ".yml!"); + Bukkit.getLogger().severe("[V10Lift] Couldn't generate the " + name + ".yml!"); } } this.fileConfiguration = YamlConfiguration.loadConfiguration(this.file); } public void loadDefaults() { - Bukkit.getLogger().info("[SBYamlManager] Copying default " + name + ".yml to the folder!"); + Bukkit.getLogger().info("[V10Lift] Copying default " + name + ".yml to the folder, if needed!"); Reader defConfigStream1 = new InputStreamReader(this.pl.getResource(name + ".yml"), StandardCharsets.UTF_8); YamlConfiguration defConfig1 = YamlConfiguration.loadConfiguration(defConfigStream1); getFile().setDefaults(defConfig1); @@ -57,7 +57,7 @@ public class SBYamlFile { try { this.fileConfiguration.save(this.file); } catch (IOException e) { - Bukkit.getLogger().severe("[SBYamlManager] Couldn't save the " + name + ".yml!"); + Bukkit.getLogger().severe("[V10Lift] Couldn't save the " + name + ".yml!"); } } diff --git a/src/main/java/nl/SBDeveloper/V10Lift/V10LiftPlugin.java b/src/main/java/nl/SBDeveloper/V10Lift/V10LiftPlugin.java index 80a394d..c29e378 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/V10LiftPlugin.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/V10LiftPlugin.java @@ -12,6 +12,8 @@ import nl.SBDeveloper.V10Lift.Utils.UpdateManager; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; +import java.sql.SQLException; + public class V10LiftPlugin extends JavaPlugin { private static V10LiftPlugin instance; @@ -28,6 +30,12 @@ public class V10LiftPlugin extends JavaPlugin { dbManager = new DBManager(this, "data"); + try { + dbManager.load(); + } catch (SQLException e) { + e.printStackTrace(); + } + api = new V10LiftAPI(); getCommand("v10lift").setExecutor(new V10LiftCommand()); @@ -47,12 +55,13 @@ public class V10LiftPlugin extends JavaPlugin { } }).check(); - getLogger().info("[V10Lift] Plugin loaded successfully!"); + Bukkit.getLogger().info("[V10Lift] Plugin loaded successfully!"); } @Override public void onDisable() { instance = null; + dbManager.save(); dbManager.closeConnection(); } @@ -64,10 +73,6 @@ public class V10LiftPlugin extends JavaPlugin { return config; } - public static DBManager getDBManager() { - return dbManager; - } - public static V10LiftAPI getAPI() { return api; }