Added data system
This commit is contained in:
parent
68157e19de
commit
3cc819cd7d
4 changed files with 55 additions and 121 deletions
|
@ -4,8 +4,10 @@ import com.google.gson.Gson;
|
||||||
import com.google.gson.reflect.TypeToken;
|
import com.google.gson.reflect.TypeToken;
|
||||||
import nl.SBDeveloper.V10Lift.API.Objects.Lift;
|
import nl.SBDeveloper.V10Lift.API.Objects.Lift;
|
||||||
import nl.SBDeveloper.V10Lift.Utils.SBSQLiteDB;
|
import nl.SBDeveloper.V10Lift.Utils.SBSQLiteDB;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
@ -17,11 +19,19 @@ public class DBManager {
|
||||||
public DBManager(JavaPlugin plugin, String name) {
|
public DBManager(JavaPlugin plugin, String name) {
|
||||||
data = new SBSQLiteDB(plugin, 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 {
|
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()) {
|
while (liftSet.next()) {
|
||||||
//Loading a lift...
|
//Loading a lift...
|
||||||
byte[] blob = liftSet.getBytes("liftData");
|
byte[] blob = liftSet.getBytes("liftData");
|
||||||
|
@ -29,16 +39,33 @@ public class DBManager {
|
||||||
Gson gson = new Gson();
|
Gson gson = new Gson();
|
||||||
Lift lift = gson.fromJson(json, new TypeToken<Lift>(){}.getType());
|
Lift lift = gson.fromJson(json, new TypeToken<Lift>(){}.getType());
|
||||||
DataManager.addLift(liftSet.getString("liftName"), lift);
|
DataManager.addLift(liftSet.getString("liftName"), lift);
|
||||||
|
|
||||||
|
Bukkit.getLogger().info("[V10Lift] Loading lift " + liftSet.getString("liftName") + " from data...");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void save() {
|
public void save() {
|
||||||
HashMap<String, byte[]> inserts = new HashMap<>();
|
|
||||||
Gson gson = new Gson();
|
Gson gson = new Gson();
|
||||||
for (Map.Entry<String, Lift> entry : DataManager.getLifts().entrySet()) {
|
for (Map.Entry<String, Lift> 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() {
|
public void closeConnection() {
|
||||||
|
|
|
@ -27,16 +27,16 @@ public class SBSQLiteDB {
|
||||||
* @param name The database name
|
* @param name The database name
|
||||||
*/
|
*/
|
||||||
public SBSQLiteDB(@Nonnull Plugin plugin, String 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");
|
File dbFile = new File(plugin.getDataFolder(), name + ".db");
|
||||||
|
|
||||||
if (!dbFile.exists()) {
|
if (!dbFile.exists()) {
|
||||||
try {
|
try {
|
||||||
Bukkit.getLogger().info("[SBDBManager] Generating the " + name + ".db!");
|
Bukkit.getLogger().info("[V10Lift] Generating the " + name + ".db!");
|
||||||
dbFile.createNewFile();
|
dbFile.createNewFile();
|
||||||
} catch (IOException e) {
|
} 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();
|
e.printStackTrace();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -50,114 +50,16 @@ public class SBSQLiteDB {
|
||||||
this.source = new HikariDataSource(config);
|
this.source = new HikariDataSource(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
//CREATE TABLE -> execute()
|
||||||
* Execute queries like CREATE TABLE, ALTER TABLE, ....
|
//SELECT -> executeQuery()
|
||||||
*
|
//UPDATE -> executeUpdate()
|
||||||
* @param query The query you want to execute
|
|
||||||
*
|
|
||||||
* @return true/false
|
|
||||||
*/
|
|
||||||
public boolean execute(String query) {
|
|
||||||
Connection con = null;
|
|
||||||
PreparedStatement statement = null;
|
|
||||||
boolean b;
|
|
||||||
|
|
||||||
try {
|
public Connection getConnection() throws SQLException {
|
||||||
con = this.source.getConnection();
|
return 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<Integer, Object> 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<Integer, Object> 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<Object, Object> where the first object is the rowname and the second object is the value
|
|
||||||
*/
|
|
||||||
public ResultSet execute(String query, HashMap<Integer, Object> 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<Integer, Object> 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 void closeSource() {
|
public void closeSource() {
|
||||||
Bukkit.getLogger().info("[SBDBManager] Closing the database connection!");
|
Bukkit.getLogger().info("[V10Lift] Closing the database connection!");
|
||||||
this.source.close();
|
this.source.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,16 +32,16 @@ public class SBYamlFile {
|
||||||
if (!this.file.exists()) {
|
if (!this.file.exists()) {
|
||||||
try {
|
try {
|
||||||
this.file.createNewFile();
|
this.file.createNewFile();
|
||||||
Bukkit.getLogger().info("[SBYamlManager] Generating the " + name + ".yml!");
|
Bukkit.getLogger().info("[V10Lift] Generating the " + name + ".yml!");
|
||||||
} catch (IOException e) {
|
} 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);
|
this.fileConfiguration = YamlConfiguration.loadConfiguration(this.file);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadDefaults() {
|
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);
|
Reader defConfigStream1 = new InputStreamReader(this.pl.getResource(name + ".yml"), StandardCharsets.UTF_8);
|
||||||
YamlConfiguration defConfig1 = YamlConfiguration.loadConfiguration(defConfigStream1);
|
YamlConfiguration defConfig1 = YamlConfiguration.loadConfiguration(defConfigStream1);
|
||||||
getFile().setDefaults(defConfig1);
|
getFile().setDefaults(defConfig1);
|
||||||
|
@ -57,7 +57,7 @@ public class SBYamlFile {
|
||||||
try {
|
try {
|
||||||
this.fileConfiguration.save(this.file);
|
this.fileConfiguration.save(this.file);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Bukkit.getLogger().severe("[SBYamlManager] Couldn't save the " + name + ".yml!");
|
Bukkit.getLogger().severe("[V10Lift] Couldn't save the " + name + ".yml!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,8 @@ import nl.SBDeveloper.V10Lift.Utils.UpdateManager;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
public class V10LiftPlugin extends JavaPlugin {
|
public class V10LiftPlugin extends JavaPlugin {
|
||||||
|
|
||||||
private static V10LiftPlugin instance;
|
private static V10LiftPlugin instance;
|
||||||
|
@ -28,6 +30,12 @@ public class V10LiftPlugin extends JavaPlugin {
|
||||||
|
|
||||||
dbManager = new DBManager(this, "data");
|
dbManager = new DBManager(this, "data");
|
||||||
|
|
||||||
|
try {
|
||||||
|
dbManager.load();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
api = new V10LiftAPI();
|
api = new V10LiftAPI();
|
||||||
|
|
||||||
getCommand("v10lift").setExecutor(new V10LiftCommand());
|
getCommand("v10lift").setExecutor(new V10LiftCommand());
|
||||||
|
@ -47,12 +55,13 @@ public class V10LiftPlugin extends JavaPlugin {
|
||||||
}
|
}
|
||||||
}).check();
|
}).check();
|
||||||
|
|
||||||
getLogger().info("[V10Lift] Plugin loaded successfully!");
|
Bukkit.getLogger().info("[V10Lift] Plugin loaded successfully!");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
instance = null;
|
instance = null;
|
||||||
|
dbManager.save();
|
||||||
dbManager.closeConnection();
|
dbManager.closeConnection();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,10 +73,6 @@ public class V10LiftPlugin extends JavaPlugin {
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DBManager getDBManager() {
|
|
||||||
return dbManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static V10LiftAPI getAPI() {
|
public static V10LiftAPI getAPI() {
|
||||||
return api;
|
return api;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue