From d790d5b7c8152c54bd2a8e2c119821b98c972a92 Mon Sep 17 00:00:00 2001 From: boy0001 Date: Wed, 29 Apr 2015 23:07:12 +1000 Subject: [PATCH] Fixed basic UUID conversion --- .../plot/PlotSquared.java | 21 +++++++++--------- .../plot/commands/DebugUUID.java | 22 ++++++++++++++++++- .../plot/database/Database.java | 2 ++ .../plot/database/SQLManager.java | 12 +++++----- .../plot/database/SQLite.java | 7 ++++++ 5 files changed, 47 insertions(+), 17 deletions(-) diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSquared.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSquared.java index 2bd1aad21..8b0bba94d 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSquared.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSquared.java @@ -36,6 +36,7 @@ import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.Configuration; import com.intellectualcrafters.plot.config.Settings; import com.intellectualcrafters.plot.database.DBFunc; +import com.intellectualcrafters.plot.database.Database; import com.intellectualcrafters.plot.database.MySQL; import com.intellectualcrafters.plot.database.SQLManager; import com.intellectualcrafters.plot.database.SQLite; @@ -94,11 +95,11 @@ public class PlotSquared { private final static HashMap plotworlds = new HashMap<>(); private final static HashMap plotmanagers = new HashMap<>(); private static LinkedHashMap> plots; - private static MySQL mySQL; + private static Database database; public static Connection connection; - public static MySQL getMySQL() { - return mySQL; + public static Database getDatabase() { + return database; } public static void updatePlot(final Plot plot) { @@ -631,12 +632,9 @@ public class PlotSquared { public void disable() { try { - connection.close(); - mySQL.closeConnection(); + database.closeConnection(); } catch (NullPointerException | SQLException e) { - if (mySQL != null) { - log("&cCould not close mysql connection!"); - } + log("&cCould not close database connection!"); } } @@ -654,8 +652,8 @@ public class PlotSquared { } if (Settings.DB.USE_MYSQL) { try { - mySQL = new MySQL(THIS, Settings.DB.HOST_NAME, Settings.DB.PORT, Settings.DB.DATABASE, Settings.DB.USER, Settings.DB.PASSWORD); - connection = mySQL.openConnection(); + database = new MySQL(THIS, Settings.DB.HOST_NAME, Settings.DB.PORT, Settings.DB.DATABASE, Settings.DB.USER, Settings.DB.PASSWORD); + connection = database.openConnection(); { if (DBFunc.dbManager == null) { DBFunc.dbManager = new SQLManager(connection, Settings.DB.PREFIX); @@ -693,7 +691,8 @@ public class PlotSquared { log(C.PREFIX.s() + "MongoDB is not yet implemented"); } else if (Settings.DB.USE_SQLITE) { try { - connection = new SQLite(THIS, IMP.getDirectory() + File.separator + Settings.DB.SQLITE_DB + ".db").openConnection(); + this.database = new SQLite(THIS, IMP.getDirectory() + File.separator + Settings.DB.SQLITE_DB + ".db"); + connection = this.database.openConnection(); { DBFunc.dbManager = new SQLManager(connection, Settings.DB.PREFIX); final DatabaseMetaData meta = connection.getMetaData(); diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/DebugUUID.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/DebugUUID.java index 6000e56b5..0280dd40a 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/DebugUUID.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/DebugUUID.java @@ -32,6 +32,7 @@ import org.bukkit.Bukkit; import com.intellectualcrafters.plot.PlotSquared; import com.intellectualcrafters.plot.config.C; +import com.intellectualcrafters.plot.config.Settings; import com.intellectualcrafters.plot.database.AbstractDB; import com.intellectualcrafters.plot.database.DBFunc; import com.intellectualcrafters.plot.database.SQLManager; @@ -233,7 +234,7 @@ public class DebugUUID extends SubCommand { MainUtil.sendConsoleMessage("&7 - Creating tables"); try { - database.createTables(PlotSquared.getMySQL() != null ? "mysql" : "sqlite", true); + database.createTables(Settings.DB.USE_MYSQL ? "mysql" : "sqlite", true); if (!result) { MainUtil.sendConsoleMessage("&cConversion failed! Attempting recovery"); for (Plot plot : PlotSquared.getPlots()) { @@ -256,6 +257,25 @@ public class DebugUUID extends SubCommand { return false; } + if (newWrapper instanceof OfflineUUIDWrapper) { + PlotSquared.config.set("UUID.force-lowercase", false); + PlotSquared.config.set("UUID.offline", true); + } + else if (newWrapper instanceof LowerOfflineUUIDWrapper) { + PlotSquared.config.set("UUID.force-lowercase", true); + PlotSquared.config.set("UUID.offline", true); + } + else if (newWrapper instanceof DefaultUUIDWrapper) { + PlotSquared.config.set("UUID.force-lowercase", false); + PlotSquared.config.set("UUID.offline", false); + } + try { + PlotSquared.config.save(PlotSquared.configFile); + } + catch (Exception e) { + MainUtil.sendConsoleMessage("Could not save configuration. It will need to be manuall set!"); + } + MainUtil.sendConsoleMessage("&7 - Populating tables"); TaskManager.runTaskAsync(new Runnable() { diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/Database.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/Database.java index f473aa5d0..e785db7e7 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/Database.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/Database.java @@ -46,6 +46,8 @@ public abstract class Database { protected Database(final PlotSquared plotsquared) { this.plotsquared = plotsquared; } + + public abstract Connection forceConnection() throws SQLException, ClassNotFoundException ; /** * Opens a connection with the database diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java index fab845c31..db19b460a 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java @@ -40,6 +40,7 @@ import org.apache.commons.lang.StringUtils; import org.bukkit.block.Biome; import com.intellectualcrafters.plot.PlotSquared; +import com.intellectualcrafters.plot.config.Settings; import com.intellectualcrafters.plot.flag.Flag; import com.intellectualcrafters.plot.flag.FlagManager; import com.intellectualcrafters.plot.object.BlockLoc; @@ -94,12 +95,12 @@ public class SQLManager implements AbstractDB { this.CREATE_PLOT = "INSERT INTO `" + this.prefix + "plot`(`plot_id_x`, `plot_id_z`, `owner`, `world`) VALUES(?, ?, ?, ?)"; this.CREATE_CLUSTER = "INSERT INTO `" + this.prefix + "cluster`(`pos1_x`, `pos1_z`, `pos2_x`, `pos2_z`, `owner`, `world`) VALUES(?, ?, ?, ?, ?, ?)"; // schedule reconnect - if (PlotSquared.getMySQL() != null) { + if (Settings.DB.USE_MYSQL) { TaskManager.runTaskRepeat(new Runnable() { @Override public void run() { try { - SQLManager.this.connection = PlotSquared.getMySQL().forceConnection(); + SQLManager.this.connection = PlotSquared.getDatabase().forceConnection(); } catch (Exception e) { e.printStackTrace(); @@ -325,7 +326,7 @@ public class SQLManager implements AbstractDB { return; } int packet; - if (PlotSquared.getMySQL() != null) { + if (Settings.DB.USE_MYSQL) { packet = Math.min(size, 50000); } else { packet = Math.min(size, 50); @@ -673,7 +674,7 @@ public class SQLManager implements AbstractDB { try { final Statement statement = this.connection.createStatement(); statement.addBatch("DROP TABLE `" + this.prefix + "plot_comments`"); - if (PlotSquared.getMySQL() != null) { + if (Settings.DB.USE_MYSQL) { statement.addBatch("CREATE TABLE IF NOT EXISTS `" + this.prefix + "plot_comments` (" + "`world` VARCHAR(40) NOT NULL, `hashcode` INT(11) NOT NULL," + "`comment` VARCHAR(40) NOT NULL," + "`inbox` VARCHAR(40) NOT NULL," + "`timestamp` INT(11) NOT NULL," + "`sender` VARCHAR(40) NOT NULL" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8"); } else { @@ -1971,7 +1972,8 @@ public class SQLManager implements AbstractDB { @Override public boolean deleteTables() { try { - SQLManager.this.connection = PlotSquared.getMySQL().forceConnection(); + SQLManager.this.connection.close(); + SQLManager.this.connection = PlotSquared.getDatabase().forceConnection(); final Statement stmt = this.connection.createStatement(); stmt.addBatch("DROP TABLE `" + prefix + "cluster_invited`"); stmt.addBatch("DROP TABLE `" + prefix + "cluster_helpers`"); diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLite.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLite.java index 3810ab34c..5e16ad5f6 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLite.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLite.java @@ -107,4 +107,11 @@ public class SQLite extends Database { final Statement statement = this.connection.createStatement(); return statement.executeUpdate(query); } + + @Override + public Connection forceConnection() throws SQLException, ClassNotFoundException { + Class.forName("org.sqlite.JDBC"); + this.connection = DriverManager.getConnection("jdbc:sqlite:" + this.dbLocation); + return this.connection; + } }