diff --git a/src/main/java/com/songoda/epicfurnaces/EpicFurnaces.java b/src/main/java/com/songoda/epicfurnaces/EpicFurnaces.java index 70f115e..4b04bf0 100644 --- a/src/main/java/com/songoda/epicfurnaces/EpicFurnaces.java +++ b/src/main/java/com/songoda/epicfurnaces/EpicFurnaces.java @@ -23,7 +23,6 @@ import com.songoda.epicfurnaces.listeners.InventoryListeners; import com.songoda.epicfurnaces.settings.Settings; import com.songoda.epicfurnaces.storage.Storage; import com.songoda.epicfurnaces.storage.StorageRow; -import com.songoda.epicfurnaces.storage.types.StorageMysql; import com.songoda.epicfurnaces.storage.types.StorageYaml; import com.songoda.epicfurnaces.tasks.FurnaceTask; import com.songoda.epicfurnaces.tasks.HologramTask; @@ -301,11 +300,7 @@ public class EpicFurnaces extends SongodaPlugin { } private void checkStorage() { - if (getConfig().getBoolean("Database.Activate Mysql Support")) { - this.storage = new StorageMysql(this); - } else { - this.storage = new StorageYaml(this); - } + this.storage = new StorageYaml(this); } /* diff --git a/src/main/java/com/songoda/epicfurnaces/storage/types/MySQLDatabase.java b/src/main/java/com/songoda/epicfurnaces/storage/types/MySQLDatabase.java deleted file mode 100644 index e72b139..0000000 --- a/src/main/java/com/songoda/epicfurnaces/storage/types/MySQLDatabase.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.songoda.epicfurnaces.storage.types; - -import com.songoda.epicfurnaces.EpicFurnaces; - -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; - -public class MySQLDatabase { - - private final EpicFurnaces plugin; - - private Connection connection; - - public MySQLDatabase(EpicFurnaces plugin) { - this.plugin = plugin; - try { - Class.forName("com.mysql.jdbc.Driver"); - - String url = "jdbc:mysql://" + plugin.getConfig().getString("Database.IP") + ":" + plugin.getConfig().getString("Database.Port") + "/" + plugin.getConfig().getString("Database.Database Name") + "?autoReconnect=true&useSSL=false"; - this.connection = DriverManager.getConnection(url, plugin.getConfig().getString("Database.Username"), plugin.getConfig().getString("Database.Password")); - - //ToDo: This is sloppy - connection.createStatement().execute( - "CREATE TABLE IF NOT EXISTS `" + plugin.getConfig().getString("Database.Prefix") + "charged` (\n" + - "\t`location` TEXT NULL,\n" + - "\t`level` INT NULL,\n" + - "\t`uses` INT NULL,\n" + - "\t`tolevel` INT NULL,\n" + - "\t`nickname` TEXT NULL,\n" + - "\t`accesslist` TEXT NULL,\n" + - "\t`placedby` TEXT NULL\n" + - ")"); - - connection.createStatement().execute("CREATE TABLE IF NOT EXISTS `" + plugin.getConfig().getString("Database.Prefix") + "boosts` (\n" + - "\t`endtime` TEXT NULL,\n" + - "\t`amount` INT NULL,\n" + - "\t`uuid` TEXT NULL\n" + - ")"); - - } catch (ClassNotFoundException | SQLException e) { - System.out.println("Database connection failed."); - } - } - - public Connection getConnection() { - return connection; - } -} \ No newline at end of file diff --git a/src/main/java/com/songoda/epicfurnaces/storage/types/StorageMysql.java b/src/main/java/com/songoda/epicfurnaces/storage/types/StorageMysql.java deleted file mode 100644 index db3328b..0000000 --- a/src/main/java/com/songoda/epicfurnaces/storage/types/StorageMysql.java +++ /dev/null @@ -1,196 +0,0 @@ -package com.songoda.epicfurnaces.storage.types; - -import com.songoda.epicfurnaces.EpicFurnaces; -import com.songoda.epicfurnaces.storage.Storage; -import com.songoda.epicfurnaces.storage.StorageItem; -import com.songoda.epicfurnaces.storage.StorageRow; - -import java.sql.DatabaseMetaData; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class StorageMysql extends Storage { - - private Map toSave = new HashMap<>(); - private Map lastSave = null; - private final MySQLDatabase database; - - public StorageMysql(EpicFurnaces plugin) { - super(plugin); - this.database = new MySQLDatabase(plugin); - } - - @Override - public boolean containsGroup(String group) { - try { - DatabaseMetaData dbm = database.getConnection().getMetaData(); - ResultSet rs = dbm.getTables(null, null, plugin.getConfig().getString("Database.Prefix") + group, null); - if (rs.next()) { - return true; - } - } catch (SQLException e) { - e.printStackTrace(); - } - return false; - - } - - @Override - public List getRowsByGroup(String group) { - List rows = new ArrayList<>(); - try { - ResultSet set = database.getConnection().createStatement().executeQuery(String.format("SELECT * FROM `" + plugin.getConfig().getString("Database.Prefix") + "%s`", group)); - while (set.next()) { - Map items = new HashMap<>(); - - String key = set.getString(1); - for (int i = 2; i <= set.getMetaData().getColumnCount(); i++) { - if (set.getObject(i) == null || set.getObject(i) == "") continue; - StorageItem item = new StorageItem(set.getObject(i)); - items.put(set.getMetaData().getColumnName(i), item); - } - StorageRow row = new StorageRow(key, items); - rows.add(row); - } - } catch (SQLException e) { - e.printStackTrace(); - } - return rows; - } - - @Override - public void prepareSaveItem(String group, StorageItem... items) { - toSave.put(group + "]" + items[0].asObject().toString(), items); - } - - @Override - public void doSave() { - this.updateData(plugin); - - if (lastSave == null) - lastSave = new HashMap<>(toSave); - - if (toSave.isEmpty()) return; - Map nextSave = new HashMap<>(toSave); - - this.makeBackup(); - this.save(); - - toSave.clear(); - lastSave.clear(); - lastSave.putAll(nextSave); - } - - @Override - public void save() { - try { - Statement stmt = database.getConnection().createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); - - last: - for (Map.Entry last : lastSave.entrySet()) { - String lastKey = last.getKey().split("]")[0]; - String lastValue = last.getValue()[0].asObject().toString(); - - for (Map.Entry to : toSave.entrySet()) { - String toKey = to.getKey().split("]")[0]; - if (!toKey.equals(lastKey) - || !to.getValue()[0].asObject().equals(lastValue) - || to.getValue().length != last.getValue().length) - continue; - toSave.remove(to.getKey()); - for (int i = 0; i < to.getValue().length; i ++) { - if ((to.getValue()[i].asObject() != null && last.getValue()[i].asObject() == null) - || (last.getValue()[i].asObject() == null && to.getValue()[i].asObject() != null) - || (last.getValue()[i].asObject() != null && to.getValue()[i].asObject() != null - && !to.getValue()[i].asObject().toString().equals(last.getValue()[i].asObject().toString()))) { - //Update - StorageItem[] items = to.getValue(); - StringBuilder sql = new StringBuilder(String.format("UPDATE `" + plugin.getConfig().getString("Database.Prefix") + "%s`", toKey)); - - sql.append(" SET"); - - for (StorageItem item : items) { - if (item == null || item.asObject() == null) continue; - String key = item.getKey().split("]")[0]; - sql.append(String.format("`%s` = '%s', ", key, item.asObject().toString())); - } - - sql = new StringBuilder(sql.substring(0, sql.length() - 2)); - - sql.append(String.format(" WHERE `%s`='%s'", last.getValue()[0].getKey(), last.getValue()[0].asObject().toString())); - - stmt.addBatch(sql.toString()); - - continue last; - } - } - // Already up to date. - - continue last; - } - //Was not found delete. - StringBuilder sql = new StringBuilder(String.format("DELETE FROM `" + plugin.getConfig().getString("Database.Prefix") + "%s`", lastKey)); - sql.append(String.format(" WHERE `%s`='%s'", last.getValue()[0].getKey(), last.getValue()[0].asObject().toString())); - stmt.addBatch(sql.toString()); - - } - - for (Map.Entry to : toSave.entrySet()) { - String toKey = to.getKey().split("]")[0]; - //Add - StorageItem[] items = to.getValue(); - StringBuilder sql = new StringBuilder(String.format("INSERT INTO `" + plugin.getConfig().getString("Database.Prefix") + "%s`", toKey)); - - sql.append(" ("); - - for (StorageItem item : items) { - if (item == null || item.asObject() == null) continue; - String key = item.getKey().split("]")[0]; - sql.append(String.format("`%s`, ", key)); - } - - sql = new StringBuilder(sql.substring(0, sql.length() - 2)); - - sql.append(") VALUES ("); - - for (StorageItem item : items) { - if (item == null || item.asObject() == null) continue; - sql.append(String.format("'%s', ", item.asObject().toString())); - } - - sql = new StringBuilder(sql.substring(0, sql.length() - 2)); - - sql.append(");"); - - stmt.addBatch(sql.toString()); - } - - stmt.executeBatch(); - - toSave.clear(); - - } catch (SQLException e) { - e.printStackTrace(); - } - } - - @Override - public void makeBackup() { - - } - - @Override - public void closeConnection() { - try { - database.getConnection().close(); - } catch (SQLException e) { - e.printStackTrace(); - } - } -} -