From 03bfe80ee2704ca68e659553c3f654b9cd192900 Mon Sep 17 00:00:00 2001 From: Xephi59 Date: Tue, 21 Jul 2015 01:13:27 +0200 Subject: [PATCH] Set SQLite as default database and convert actual flat to sqlite --- src/main/java/fr/xephi/authme/AuthMe.java | 11 ++++-- .../authme/commands/ConverterCommand.java | 5 --- .../xephi/authme/converter/FlatToSqlite.java | 23 ++++++------ .../authme/converter/ForceFlatToSqlite.java | 35 +++++++++++++++++++ .../authme/listener/AuthMePlayerListener.java | 5 +-- .../fr/xephi/authme/settings/Settings.java | 5 +++ 6 files changed, 63 insertions(+), 21 deletions(-) create mode 100644 src/main/java/fr/xephi/authme/converter/ForceFlatToSqlite.java diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index 5bbcac601..f9434a76f 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -51,6 +51,8 @@ import fr.xephi.authme.commands.LogoutCommand; import fr.xephi.authme.commands.PasspartuCommand; import fr.xephi.authme.commands.RegisterCommand; import fr.xephi.authme.commands.UnregisterCommand; +import fr.xephi.authme.converter.Converter; +import fr.xephi.authme.converter.ForceFlatToSqlite; import fr.xephi.authme.datasource.CacheDataSource; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.datasource.DatabaseCalls; @@ -766,8 +768,6 @@ public class AuthMe extends JavaPlugin { switch (Settings.getDataSource) { case FILE: database = new FlatFile(); - final int a = database.getAccountsRegistered(); - ConsoleLogger.showError("YOU'RE USING THE FILE BACKEND WITH " + a + "+ ACCOUNTS, YOU SHOULD NEVER USE THIS, PLEASE UPGRADE TO SQLITE!!!"); break; case MYSQL: database = new MySQL(); @@ -785,5 +785,12 @@ public class AuthMe extends JavaPlugin { } database = new DatabaseCalls(database); + + if (Settings.getDataSource == DataSource.DataSourceType.FILE) { + Converter converter = new ForceFlatToSqlite(database, this); + Bukkit.getScheduler().runTaskAsynchronously(this, converter); + ConsoleLogger.showError("FlatFile backend as been detected and is now deprecated, next time server startup, it will be changed to SQLite... Conversion will be started Asynchronously, it will not drop down your performances !"); + ConsoleLogger.showError("If you want to keep FlatFile, set file again into config at backend, but this message and this change will appear again at the next restart"); + } } } diff --git a/src/main/java/fr/xephi/authme/commands/ConverterCommand.java b/src/main/java/fr/xephi/authme/commands/ConverterCommand.java index 3da052a04..56deecdf3 100644 --- a/src/main/java/fr/xephi/authme/commands/ConverterCommand.java +++ b/src/main/java/fr/xephi/authme/commands/ConverterCommand.java @@ -4,7 +4,6 @@ import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; import fr.xephi.authme.AuthMe; import fr.xephi.authme.converter.Converter; @@ -30,10 +29,6 @@ public class ConverterCommand implements CommandExecutor { @Override public boolean onCommand(CommandSender sender, Command cmnd, String label, final String[] args) { - if (!(sender instanceof Player)) { - return true; - } - if (!plugin.authmePermissible(sender, "authme.admin.converter")) { m.send(sender, "no_perm"); return true; diff --git a/src/main/java/fr/xephi/authme/converter/FlatToSqlite.java b/src/main/java/fr/xephi/authme/converter/FlatToSqlite.java index 1a662bec7..cbcb283a1 100644 --- a/src/main/java/fr/xephi/authme/converter/FlatToSqlite.java +++ b/src/main/java/fr/xephi/authme/converter/FlatToSqlite.java @@ -39,6 +39,8 @@ public class FlatToSqlite implements Converter { private static File source; private static String database; private static String columnID; + private static String columnSalt; + private static String columnGroup; private static Connection con; @Override @@ -49,23 +51,20 @@ public class FlatToSqlite implements Converter { columnPassword = Settings.getMySQLColumnPassword; columnIp = Settings.getMySQLColumnIp; columnLastLogin = Settings.getMySQLColumnLastLogin; + columnSalt = Settings.getMySQLColumnSalt; + columnGroup = Settings.getMySQLColumnGroup; lastlocX = Settings.getMySQLlastlocX; lastlocY = Settings.getMySQLlastlocY; lastlocZ = Settings.getMySQLlastlocZ; lastlocWorld = Settings.getMySQLlastlocWorld; columnEmail = Settings.getMySQLColumnEmail; columnID = Settings.getMySQLColumnId; - ConsoleLogger.info("Converting FlatFile to SQLite ..."); - if (new File(AuthMe.getInstance().getDataFolder() + File.separator + database + ".db").exists()) { - sender.sendMessage("The Database " + database + ".db can't be created cause the file already exist"); - return; - } + try { connect(); setup(); } catch (Exception e) { - ConsoleLogger.showError("Problem while trying to convert to sqlite !"); - sender.sendMessage("Problem while trying to convert to sqlite !"); + sender.sendMessage("Some error appears while trying to setup and connect to sqlite database... Aborting"); return; } try { @@ -107,7 +106,6 @@ public class FlatToSqlite implements Converter { private synchronized static void connect() throws ClassNotFoundException, SQLException { Class.forName("org.sqlite.JDBC"); - ConsoleLogger.info("SQLite driver loaded"); con = DriverManager.getConnection("jdbc:sqlite:plugins/AuthMe/" + database + ".db"); } @@ -116,7 +114,7 @@ public class FlatToSqlite implements Converter { ResultSet rs = null; try { st = con.createStatement(); - st.executeUpdate("CREATE TABLE IF NOT EXISTS " + tableName + " (" + columnID + " INTEGER AUTO_INCREMENT," + columnName + " VARCHAR(255) NOT NULL UNIQUE," + columnPassword + " VARCHAR(255) NOT NULL," + columnIp + " VARCHAR(40) NOT NULL," + columnLastLogin + " BIGINT," + lastlocX + " smallint(6) DEFAULT '0'," + lastlocY + " smallint(6) DEFAULT '0'," + lastlocZ + " smallint(6) DEFAULT '0'," + lastlocWorld + " VARCHAR(255) DEFAULT 'world'," + columnEmail + " VARCHAR(255) DEFAULT 'your@email.com'," + "CONSTRAINT table_const_prim PRIMARY KEY (" + columnID + "));"); + st.executeUpdate("CREATE TABLE IF NOT EXISTS " + tableName + " (" + columnID + " INTEGER AUTO_INCREMENT," + columnName + " VARCHAR(255) NOT NULL UNIQUE," + columnPassword + " VARCHAR(255) NOT NULL," + columnIp + " VARCHAR(40) NOT NULL," + columnLastLogin + " BIGINT," + lastlocX + " DOUBLE NOT NULL DEFAULT '0.0'," + lastlocY + " DOUBLE NOT NULL DEFAULT '0.0'," + lastlocZ + " DOUBLE NOT NULL DEFAULT '0.0'," + lastlocWorld + " VARCHAR(255) NOT NULL DEFAULT '" + Settings.defaultWorld + "'," + columnEmail + " VARCHAR(255) DEFAULT 'your@email.com'," + "CONSTRAINT table_const_prim PRIMARY KEY (" + columnID + "));"); rs = con.getMetaData().getColumns(null, null, tableName, columnPassword); if (!rs.next()) { st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + columnPassword + " VARCHAR(255) NOT NULL;"); @@ -134,12 +132,14 @@ public class FlatToSqlite implements Converter { rs.close(); rs = con.getMetaData().getColumns(null, null, tableName, lastlocX); if (!rs.next()) { - st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + lastlocX + " smallint(6) NOT NULL DEFAULT '0'; " + "ALTER TABLE " + tableName + " ADD COLUMN " + lastlocY + " smallint(6) NOT NULL DEFAULT '0'; " + "ALTER TABLE " + tableName + " ADD COLUMN " + lastlocZ + " smallint(6) NOT NULL DEFAULT '0';"); + st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + lastlocX + " DOUBLE NOT NULL DEFAULT '0.0';"); + st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + lastlocY + " DOUBLE NOT NULL DEFAULT '0.0';"); + st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + lastlocZ + " DOUBLE NOT NULL DEFAULT '0.0';"); } rs.close(); rs = con.getMetaData().getColumns(null, null, tableName, lastlocWorld); if (!rs.next()) { - st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + lastlocWorld + " VARCHAR(255) NOT NULL DEFAULT 'world' AFTER " + lastlocZ + ";"); + st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + lastlocWorld + " VARCHAR(255) NOT NULL DEFAULT 'world';"); } rs.close(); rs = con.getMetaData().getColumns(null, null, tableName, columnEmail); @@ -150,7 +150,6 @@ public class FlatToSqlite implements Converter { close(rs); close(st); } - ConsoleLogger.info("SQLite Setup finished"); } private static synchronized boolean saveAuth(String s) { diff --git a/src/main/java/fr/xephi/authme/converter/ForceFlatToSqlite.java b/src/main/java/fr/xephi/authme/converter/ForceFlatToSqlite.java new file mode 100644 index 000000000..007f95415 --- /dev/null +++ b/src/main/java/fr/xephi/authme/converter/ForceFlatToSqlite.java @@ -0,0 +1,35 @@ +package fr.xephi.authme.converter; + +import fr.xephi.authme.AuthMe; +import fr.xephi.authme.ConsoleLogger; +import fr.xephi.authme.cache.auth.PlayerAuth; +import fr.xephi.authme.datasource.DataSource; +import fr.xephi.authme.datasource.SQLite; + +public class ForceFlatToSqlite implements Converter { + + private DataSource data; + private AuthMe plugin; + + public ForceFlatToSqlite(DataSource data, AuthMe plugin) { + this.data = data; + this.plugin = plugin; + } + + @Override + public void run() { + DataSource sqlite = null; + try { + sqlite = new SQLite(); + for (PlayerAuth auth : data.getAllAuths()) + sqlite.saveAuth(auth); + plugin.getSettings().setValue("DataSource.backend", "sqlite"); + ConsoleLogger.info("Database successfully converted to sqlite !"); + } catch (Exception e) { + ConsoleLogger.showError("An error appears while trying to convert flatfile ..."); + } finally { + if (sqlite != null) + sqlite.close(); + } + } +} diff --git a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java index b0aaf5ed1..6b42f2e29 100644 --- a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java +++ b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java @@ -5,6 +5,7 @@ import java.io.DataOutputStream; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.concurrent.ConcurrentHashMap; @@ -407,7 +408,7 @@ public class AuthMePlayerListener implements Listener { return; } - if (player.isBanned()) + if (event.getResult() != PlayerLoginEvent.Result.ALLOWED) return; if (Settings.enablePasspartu && !Settings.countriesBlacklist.isEmpty()) { @@ -460,7 +461,7 @@ public class AuthMePlayerListener implements Listener { String regex = Settings.getNickRegex; if (name.length() > max || name.length() < min) { - event.setKickMessage(m.send("name_len")[0]); + event.setKickMessage(Arrays.toString(m.send("name_len"))); event.setResult(PlayerLoginEvent.Result.KICK_OTHER); return; } diff --git a/src/main/java/fr/xephi/authme/settings/Settings.java b/src/main/java/fr/xephi/authme/settings/Settings.java index abd5e45f0..fe1f34eae 100644 --- a/src/main/java/fr/xephi/authme/settings/Settings.java +++ b/src/main/java/fr/xephi/authme/settings/Settings.java @@ -602,6 +602,11 @@ public final class Settings extends YamlConfiguration { return; } + public void setValue(String key, Object value) { + this.set(key, value); + this.save(); + } + private static HashAlgorithm getPasswordHash() { String key = "settings.security.passwordHash"; try {