Better Error communication with Exceptions instead of boolean. Bungee Player Listener, Database#clean made private in SQLDB

This commit is contained in:
Rsl1122 2017-08-28 13:26:56 +03:00
parent a731db656c
commit 92626b2220
33 changed files with 352 additions and 253 deletions

View File

@ -145,4 +145,26 @@ public class Log {
return PlanBungee.getInstance().getPluginLogger(); return PlanBungee.getInstance().getPluginLogger();
} }
} }
public static void logStackTrace(Throwable e) {
error(e.toString());
for (StackTraceElement stackTraceElement : e.getStackTrace()) {
error(" " + stackTraceElement);
}
Throwable cause = e.getCause();
if (cause != null) {
logCause(cause);
}
}
private static void logCause(Throwable e) {
error("caused by: " + e.toString());
for (StackTraceElement stackTraceElement : e.getStackTrace()) {
error(" " + stackTraceElement);
}
Throwable cause = e.getCause();
if (cause != null) {
logCause(cause);
}
}
} }

View File

@ -28,6 +28,7 @@ import com.djrapitops.plugin.task.RunnableFactory;
import com.djrapitops.plugin.utilities.Verify; import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.api.API; import main.java.com.djrapitops.plan.api.API;
import main.java.com.djrapitops.plan.api.IPlan; import main.java.com.djrapitops.plan.api.IPlan;
import main.java.com.djrapitops.plan.api.exceptions.DatabaseInitException;
import main.java.com.djrapitops.plan.command.PlanCommand; import main.java.com.djrapitops.plan.command.PlanCommand;
import main.java.com.djrapitops.plan.command.commands.RegisterCommandFilter; import main.java.com.djrapitops.plan.command.commands.RegisterCommandFilter;
import main.java.com.djrapitops.plan.data.additional.HookHandler; import main.java.com.djrapitops.plan.data.additional.HookHandler;
@ -47,7 +48,6 @@ import main.java.com.djrapitops.plan.systems.tasks.PeriodicDBCommitTask;
import main.java.com.djrapitops.plan.systems.tasks.TPSCountTimer; import main.java.com.djrapitops.plan.systems.tasks.TPSCountTimer;
import main.java.com.djrapitops.plan.systems.webserver.WebServer; import main.java.com.djrapitops.plan.systems.webserver.WebServer;
import main.java.com.djrapitops.plan.utilities.Benchmark; import main.java.com.djrapitops.plan.utilities.Benchmark;
import main.java.com.djrapitops.plan.utilities.Check;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
@ -147,12 +147,7 @@ public class Plan extends BukkitPlugin<Plan> implements IPlan {
Benchmark.start("Init Database"); Benchmark.start("Init Database");
Log.info(Locale.get(Msg.ENABLE_DB_INIT).toString()); Log.info(Locale.get(Msg.ENABLE_DB_INIT).toString());
if (Check.errorIfFalse(initDatabase(), Locale.get(Msg.ENABLE_DB_FAIL_DISABLE_INFO).toString())) { initDatabase();
Log.info(Locale.get(Msg.ENABLE_DB_INFO).parse(db.getConfigName()));
} else {
disablePlugin();
return;
}
Benchmark.stop("Enable", "Init Database"); Benchmark.stop("Enable", "Init Database");
Benchmark.start("WebServer Initialization"); Benchmark.start("WebServer Initialization");
@ -204,7 +199,7 @@ public class Plan extends BukkitPlugin<Plan> implements IPlan {
Log.info(Locale.get(Msg.ENABLED).toString()); Log.info(Locale.get(Msg.ENABLED).toString());
} catch (Exception e) { } catch (Exception e) {
Log.error("Plugin Failed to Initialize Correctly."); Log.error("Plugin Failed to Initialize Correctly.");
Log.toLog(this.getClass().getName(), e); Log.logStackTrace(e);
disablePlugin(); disablePlugin();
} }
} }
@ -313,7 +308,7 @@ public class Plan extends BukkitPlugin<Plan> implements IPlan {
* *
* @return true if init was successful, false if not. * @return true if init was successful, false if not.
*/ */
private boolean initDatabase() { private void initDatabase() throws DatabaseInitException {
databases = new HashSet<>(); databases = new HashSet<>();
databases.add(new MySQLDB(this)); databases.add(new MySQLDB(this));
databases.add(new SQLiteDB(this)); databases.add(new SQLiteDB(this));
@ -328,12 +323,11 @@ public class Plan extends BukkitPlugin<Plan> implements IPlan {
} }
} }
if (!Verify.notNull(db)) { if (db == null) {
Log.info(Locale.get(Msg.ENABLE_FAIL_WRONG_DB).toString() + " " + dbType); throw new DatabaseInitException(Locale.get(Msg.ENABLE_FAIL_WRONG_DB).toString() + " " + dbType);
return false;
} }
return Check.errorIfFalse(db.init(), Locale.get(Msg.ENABLE_DB_FAIL_DISABLE_INFO).toString()); db.init();
} }
/** /**

View File

@ -0,0 +1,25 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package main.java.com.djrapitops.plan.api.exceptions;
/**
* //TODO Class Javadoc Comment
*
* @author Rsl1122
*/
public class DatabaseException extends Exception {
public DatabaseException(String message, Throwable cause) {
super(message, cause);
}
public DatabaseException(Throwable cause) {
super(cause);
}
public DatabaseException(String message) {
super(message);
}
}

View File

@ -0,0 +1,24 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package main.java.com.djrapitops.plan.api.exceptions;
/**
* //TODO Class Javadoc Comment
*
* @author Rsl1122
*/
public class DatabaseInitException extends DatabaseException {
public DatabaseInitException(String message, Throwable cause) {
super(message, cause);
}
public DatabaseInitException(Throwable cause) {
super(cause);
}
public DatabaseInitException(String message) {
super(message);
}
}

View File

@ -0,0 +1,21 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package main.java.com.djrapitops.plan.api.exceptions;
/**
* //TODO Class Javadoc Comment
*
* @author Rsl1122
*/
public class DbCreateTableException extends DatabaseInitException {
public DbCreateTableException(String tableName, String message, Throwable cause) {
super(tableName + ": " + message, cause);
}
public DbCreateTableException(Throwable cause) {
super(cause);
}
}

View File

@ -0,0 +1,20 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package main.java.com.djrapitops.plan.api.exceptions;
/**
* //TODO Class Javadoc Comment
*
* @author Rsl1122
*/
public class PlanEnableException extends Exception {
public PlanEnableException(String message, Throwable cause) {
super(message, cause);
}
public PlanEnableException(String message) {
super(message);
}
}

View File

@ -10,6 +10,8 @@ import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.ServerVariableHolder; import main.java.com.djrapitops.plan.ServerVariableHolder;
import main.java.com.djrapitops.plan.Settings; import main.java.com.djrapitops.plan.Settings;
import main.java.com.djrapitops.plan.api.IPlan; import main.java.com.djrapitops.plan.api.IPlan;
import main.java.com.djrapitops.plan.api.exceptions.DatabaseInitException;
import main.java.com.djrapitops.plan.bungee.systems.BungeePlayerListener;
import main.java.com.djrapitops.plan.database.Database; import main.java.com.djrapitops.plan.database.Database;
import main.java.com.djrapitops.plan.database.databases.MySQLDB; import main.java.com.djrapitops.plan.database.databases.MySQLDB;
import main.java.com.djrapitops.plan.locale.Locale; import main.java.com.djrapitops.plan.locale.Locale;
@ -20,7 +22,6 @@ import main.java.com.djrapitops.plan.systems.processing.Processor;
import main.java.com.djrapitops.plan.systems.queue.ProcessingQueue; import main.java.com.djrapitops.plan.systems.queue.ProcessingQueue;
import main.java.com.djrapitops.plan.systems.webserver.WebServer; import main.java.com.djrapitops.plan.systems.webserver.WebServer;
import main.java.com.djrapitops.plan.utilities.Benchmark; import main.java.com.djrapitops.plan.utilities.Benchmark;
import main.java.com.djrapitops.plan.utilities.Check;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import java.io.InputStream; import java.io.InputStream;
@ -46,51 +47,58 @@ public class PlanBungee extends BungeePlugin<PlanBungee> implements IPlan {
@Override @Override
public void onEnable() { public void onEnable() {
super.setInstance(this); try {
super.setDebugMode(Settings.DEBUG.toString());
super.getPluginLogger().setFolder(getDataFolder());
super.setColorScheme(new ColorScheme(ChatColor.GREEN, ChatColor.GRAY, ChatColor.WHITE));
super.setLogPrefix("[Plan]");
super.setUpdateCheckUrl("https://raw.githubusercontent.com/Rsl1122/Plan-PlayerAnalytics/master/Plan/src/main/resources/plugin.yml");
super.setUpdateUrl("https://www.spigotmc.org/resources/plan-player-analytics.32536/");
super.copyDefaultConfig("Plan Config | More info at https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/documentation/Configuration.md");
super.onEnableDefaultTasks(); super.setInstance(this);
super.setDebugMode(Settings.DEBUG.toString());
super.getPluginLogger().setFolder(getDataFolder());
super.setColorScheme(new ColorScheme(ChatColor.GREEN, ChatColor.GRAY, ChatColor.WHITE));
super.setLogPrefix("[Plan]");
super.setUpdateCheckUrl("https://raw.githubusercontent.com/Rsl1122/Plan-PlayerAnalytics/master/Plan/src/main/resources/plugin.yml");
super.setUpdateUrl("https://www.spigotmc.org/resources/plan-player-analytics.32536/");
variableHolder = new ServerVariableHolder(getProxy()); super.copyDefaultConfig("Plan Config | More info at https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/documentation/Configuration.md");
new Locale(this).loadLocale(); super.onEnableDefaultTasks();
processingQueue = new ProcessingQueue(); variableHolder = new ServerVariableHolder(getProxy());
Log.info(Locale.get(Msg.ENABLE_DB_INIT).toString()); new Locale(this).loadLocale();
if (!initDatabase()) {
processingQueue = new ProcessingQueue();
Log.info(Locale.get(Msg.ENABLE_DB_INIT).toString());
initDatabase();
String ip = variableHolder.getIp();
if ("0.0.0.0".equals(ip)) {
Log.error("IP setting still 0.0.0.0 - Set up AlternativeIP/IP that connects to the Proxy server.");
}
Benchmark.start("WebServer Initialization");
webServer = new WebServer(this);
webServer.initServer();
if (!webServer.isEnabled()) {
Log.error("WebServer was not successfully initialized.");
disablePlugin();
return;
}
serverInfoManager = new ServerInfoManager(this);
infoManager = new InformationManager(this);
webServer.setInfoManager(infoManager);
registerListener(new BungeePlayerListener(this));
Benchmark.stop("Enable", "WebServer Initialization");
Log.info(Locale.get(Msg.ENABLED).toString());
} catch (Exception e) {
Log.error("Plugin Failed to Initialize Correctly.");
Log.logStackTrace(e);
disablePlugin(); disablePlugin();
return;
} }
String ip = variableHolder.getIp();
if ("0.0.0.0".equals(ip)) {
Log.error("IP setting still 0.0.0.0 - Set up AlternativeIP/IP that connects to the Proxy server.");
}
Benchmark.start("WebServer Initialization");
webServer = new WebServer(this);
webServer.initServer();
if (!webServer.isEnabled()) {
Log.error("WebServer was not successfully initialized.");
disablePlugin();
return;
}
serverInfoManager = new ServerInfoManager(this);
infoManager = new InformationManager(this);
webServer.setInfoManager(infoManager);
Benchmark.stop("Enable", "WebServer Initialization");
Log.info(Locale.get(Msg.ENABLED).toString());
} }
public static PlanBungee getInstance() { public static PlanBungee getInstance() {
@ -107,9 +115,9 @@ public class PlanBungee extends BungeePlugin<PlanBungee> implements IPlan {
Log.info(Locale.get(Msg.DISABLED).toString()); Log.info(Locale.get(Msg.DISABLED).toString());
} }
private boolean initDatabase() { private void initDatabase() throws DatabaseInitException {
db = new MySQLDB(this); db = new MySQLDB(this);
return Check.errorIfFalse(db.init(), Locale.get(Msg.ENABLE_DB_FAIL_DISABLE_INFO).toString()); db.init();
} }
@Override @Override

View File

@ -0,0 +1,38 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package main.java.com.djrapitops.plan.bungee.systems;
import main.java.com.djrapitops.plan.bungee.PlanBungee;
import main.java.com.djrapitops.plan.utilities.MiscUtils;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.PostLoginEvent;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.event.EventHandler;
import java.util.UUID;
/**
* Player Join listener for Bungee.
*
* @author Rsl1122
*/
public class BungeePlayerListener implements Listener {
private final PlanBungee plugin;
public BungeePlayerListener(PlanBungee plugin) {
this.plugin = plugin;
}
@EventHandler
public void onPostLogin(PostLoginEvent event) {
ProxiedPlayer player = event.getPlayer();
UUID uuid = player.getUniqueId();
String name = player.getName();
long now = MiscUtils.getTime();
plugin.getProcessingQueue().addToQueue(new BungeePlayerRegisterProcessor(uuid, name, now));
}
}

View File

@ -0,0 +1,44 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package main.java.com.djrapitops.plan.bungee.systems;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.bungee.PlanBungee;
import main.java.com.djrapitops.plan.database.tables.UsersTable;
import main.java.com.djrapitops.plan.systems.processing.player.PlayerProcessor;
import java.sql.SQLException;
import java.util.UUID;
/**
* Processor that registers a new User for all servers to use as UUID - ID reference.
*
* @author Rsl1122
*/
public class BungeePlayerRegisterProcessor extends PlayerProcessor {
private final String name;
private final long registered;
public BungeePlayerRegisterProcessor(UUID uuid, String name, long registered) {
super(uuid);
this.name = name;
this.registered = registered;
}
@Override
public void process() {
UUID uuid = getUUID();
UsersTable usersTable = PlanBungee.getInstance().getDB().getUsersTable();
try {
if (usersTable.isRegistered(uuid)) {
return;
}
usersTable.registerUser(uuid, registered, name);
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}
}
}

View File

@ -1,78 +0,0 @@
package main.java.com.djrapitops.plan.command.commands.manage;
import com.djrapitops.plugin.command.CommandType;
import com.djrapitops.plugin.command.ISender;
import com.djrapitops.plugin.command.SubCommand;
import com.djrapitops.plugin.task.AbsRunnable;
import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.Permissions;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.database.Database;
import main.java.com.djrapitops.plan.locale.Locale;
import main.java.com.djrapitops.plan.locale.Msg;
import main.java.com.djrapitops.plan.utilities.Check;
import main.java.com.djrapitops.plan.utilities.ManageUtils;
/**
* This manage subcommand is used to clear a database of all data.
*
* @author Rsl1122
* @since 2.3.0
*/
public class ManageCleanCommand extends SubCommand {
private final Plan plugin;
/**
* Class Constructor.
*
* @param plugin Current instance of Plan
*/
public ManageCleanCommand(Plan plugin) {
super("clean",
CommandType.CONSOLE_WITH_ARGUMENTS,
Permissions.MANAGE.getPermission(),
Locale.get(Msg.CMD_USG_MANAGE_CLEAN).toString(),
"<DB>");
this.plugin = plugin;
}
@Override
public boolean onCommand(ISender sender, String commandLabel, String[] args) {
if (!Check.isTrue(args.length != 0, Locale.get(Msg.CMD_FAIL_REQ_ONE_ARG).toString(), sender)) {
return true;
}
String dbName = args[0].toLowerCase();
boolean isCorrectDB = "sqlite".equals(dbName) || "mysql".equals(dbName);
if (!Check.isTrue(isCorrectDB, Locale.get(Msg.MANAGE_FAIL_INCORRECT_DB) + dbName, sender)) {
return true;
}
final Database database = ManageUtils.getDB(plugin, dbName);
// If DB is null return
if (!Check.isTrue(Verify.notNull(database), Locale.get(Msg.MANAGE_FAIL_FAULTY_DB).toString(), sender)) {
Log.error(dbName + " was null!");
return true;
}
runCleanTask(sender, database);
return true;
}
private void runCleanTask(ISender sender, final Database database) {
plugin.getRunnableFactory().createNew(new AbsRunnable("DBCleanTask") {
@Override
public void run() {
sender.sendMessage(Locale.get(Msg.MANAGE_INFO_START).parse());
database.clean();
sender.sendMessage(Locale.get(Msg.MANAGE_INFO_SUCCESS).toString());
this.cancel();
}
}).runTaskAsynchronously();
}
}

View File

@ -90,10 +90,7 @@ public class ManageRestoreCommand extends SubCommand {
} }
SQLiteDB backupDB = new SQLiteDB(plugin, backupDBName); SQLiteDB backupDB = new SQLiteDB(plugin, backupDBName);
if (!backupDB.init()) { backupDB.init();
sender.sendMessage(Locale.get(Msg.MANAGE_FAIL_FAULTY_DB).toString());
return;
}
sender.sendMessage(Locale.get(Msg.MANAGE_INFO_START).parse()); sender.sendMessage(Locale.get(Msg.MANAGE_INFO_START).parse());

View File

@ -1,6 +1,7 @@
package main.java.com.djrapitops.plan.database; package main.java.com.djrapitops.plan.database;
import main.java.com.djrapitops.plan.api.IPlan; import main.java.com.djrapitops.plan.api.IPlan;
import main.java.com.djrapitops.plan.api.exceptions.DatabaseInitException;
import main.java.com.djrapitops.plan.data.UserInfo; import main.java.com.djrapitops.plan.data.UserInfo;
import main.java.com.djrapitops.plan.database.tables.*; import main.java.com.djrapitops.plan.database.tables.*;
import org.apache.commons.dbcp2.BasicDataSource; import org.apache.commons.dbcp2.BasicDataSource;
@ -120,8 +121,7 @@ public abstract class Database {
* *
* @return Was the initiation successful? * @return Was the initiation successful?
*/ */
public boolean init() { public void init() throws DatabaseInitException {
return false;
} }
/** /**
@ -143,11 +143,6 @@ public abstract class Database {
*/ */
public abstract boolean wasSeenBefore(UUID uuid); public abstract boolean wasSeenBefore(UUID uuid);
/**
* Cleans the database of excess data.
*/
public abstract void clean();
/** /**
* Used to get the name of the database type. * Used to get the name of the database type.
* <p> * <p>

View File

@ -2,6 +2,7 @@ package main.java.com.djrapitops.plan.database.databases;
import com.djrapitops.plugin.config.fileconfig.IFileConfig; import com.djrapitops.plugin.config.fileconfig.IFileConfig;
import main.java.com.djrapitops.plan.api.IPlan; import main.java.com.djrapitops.plan.api.IPlan;
import main.java.com.djrapitops.plan.api.exceptions.DatabaseInitException;
import org.apache.commons.dbcp2.BasicDataSource; import org.apache.commons.dbcp2.BasicDataSource;
import java.io.IOException; import java.io.IOException;
@ -24,9 +25,13 @@ public class MySQLDB extends SQLDB {
* Setups the {@link BasicDataSource} * Setups the {@link BasicDataSource}
*/ */
@Override @Override
public void setupDataSource() throws IOException { public void setupDataSource() throws DatabaseInitException {
IFileConfig config = null; IFileConfig config = null;
config = plugin.getIConfig().getConfig(); try {
config = plugin.getIConfig().getConfig();
} catch (IOException e) {
throw new DatabaseInitException("Failed to read config.", e);
}
dataSource = new BasicDataSource(); dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setDriverClassName("com.mysql.jdbc.Driver");

View File

@ -2,16 +2,15 @@ package main.java.com.djrapitops.plan.database.databases;
import main.java.com.djrapitops.plan.Log; import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.api.IPlan; import main.java.com.djrapitops.plan.api.IPlan;
import main.java.com.djrapitops.plan.api.exceptions.DatabaseInitException;
import main.java.com.djrapitops.plan.data.UserInfo; import main.java.com.djrapitops.plan.data.UserInfo;
import main.java.com.djrapitops.plan.database.Database; import main.java.com.djrapitops.plan.database.Database;
import main.java.com.djrapitops.plan.database.tables.*; import main.java.com.djrapitops.plan.database.tables.*;
import main.java.com.djrapitops.plan.utilities.Benchmark; import main.java.com.djrapitops.plan.utilities.Benchmark;
import org.apache.commons.dbcp2.BasicDataSource; import org.apache.commons.dbcp2.BasicDataSource;
import java.io.IOException;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -63,22 +62,14 @@ public abstract class SQLDB extends Database {
* @return Was the Initialization successful. * @return Was the Initialization successful.
*/ */
@Override @Override
public boolean init() { public void init() throws DatabaseInitException {
setStatus("Init"); setStatus("Init");
String benchName = "Init " + getConfigName(); String benchName = "Init " + getConfigName();
Benchmark.start(benchName); Benchmark.start(benchName);
try { try {
setupDataSource(); setupDataSource();
setupDatabase();
if (!setupDatabase()) {
return false;
}
clean(); clean();
return true;
} catch (IOException | SQLException e) {
Log.toLog(this.getClass().getName(), e);
return false;
} finally { } finally {
Benchmark.stop("Database", benchName); Benchmark.stop("Database", benchName);
Log.logDebug("Database"); Log.logDebug("Database");
@ -90,40 +81,29 @@ public abstract class SQLDB extends Database {
* <p> * <p>
* Updates to latest schema. * Updates to latest schema.
* *
* @return Is the connection usable? * @throws DatabaseInitException if something goes wrong.
* @throws SQLException
*/ */
public boolean setupDatabase() throws SQLException { public void setupDatabase() throws DatabaseInitException {
boolean newDatabase = isNewDatabase(); try {
boolean newDatabase = isNewDatabase();
if (!versionTable.createTable()) { versionTable.createTable();
Log.error("Failed to create table: " + versionTable.getTableName()); createTables();
return false;
}
if (newDatabase) { if (newDatabase) {
Log.info("New Database created."); Log.info("New Database created.");
}
if (!createTables()) {
return false;
}
int version = getVersion();
boolean newVersion = version < 8;
if (newDatabase || newVersion) {
setVersion(8);
}
if (newVersion) {
try (Statement statement = getConnection().createStatement()) {
statement.execute("DROP TABLE IF EXISTS plan_locations");
endTransaction(statement.getConnection());
} }
}
return true; int version = getVersion();
boolean newVersion = version < 8;
if (newDatabase || newVersion) {
setVersion(8);
}
} catch (SQLException e) {
throw new DatabaseInitException("Failed to set-up Database", e);
}
} }
/** /**
@ -133,16 +113,12 @@ public abstract class SQLDB extends Database {
* *
* @return true if successful. * @return true if successful.
*/ */
private boolean createTables() { private void createTables() throws DatabaseInitException {
Benchmark.start("Create tables"); Benchmark.start("Create tables");
for (Table table : getAllTables()) { for (Table table : getAllTables()) {
if (!table.createTable()) { table.createTable();
Log.error("Failed to create table: " + table.getTableName());
return false;
}
} }
Benchmark.stop("Database", "Create tables"); Benchmark.stop("Database", "Create tables");
return true;
} }
/** /**
@ -175,7 +151,7 @@ public abstract class SQLDB extends Database {
/** /**
* Setups the {@link BasicDataSource} * Setups the {@link BasicDataSource}
*/ */
public abstract void setupDataSource() throws IOException; public abstract void setupDataSource() throws DatabaseInitException;
/** /**
* @throws SQLException * @throws SQLException
@ -258,17 +234,13 @@ public abstract class SQLDB extends Database {
} }
} }
/** private void clean() throws DatabaseInitException {
*
*/
@Override
public void clean() {
Log.info("Cleaning the database."); Log.info("Cleaning the database.");
try { try {
tpsTable.clean(); tpsTable.clean();
Log.info("Clean complete."); Log.info("Clean complete.");
} catch (SQLException e) { } catch (SQLException e) {
Log.toLog(this.getClass().getName(), e); throw new DatabaseInitException("Database Clean failed", e);
} }
} }

View File

@ -5,6 +5,7 @@
package main.java.com.djrapitops.plan.database.tables; package main.java.com.djrapitops.plan.database.tables;
import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.api.exceptions.DbCreateTableException;
import main.java.com.djrapitops.plan.data.Action; import main.java.com.djrapitops.plan.data.Action;
import main.java.com.djrapitops.plan.database.databases.SQLDB; import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Select; import main.java.com.djrapitops.plan.database.sql.Select;
@ -47,9 +48,9 @@ public class ActionsTable extends UserIDTable {
} }
@Override @Override
public boolean createTable() { public void createTable() throws DbCreateTableException {
ServerTable serverTable = db.getServerTable(); ServerTable serverTable = db.getServerTable();
return createTable(TableSqlParser.createTable(tableName) createTable(TableSqlParser.createTable(tableName)
.column(columnUserID, Sql.INT).notNull() .column(columnUserID, Sql.INT).notNull()
.column(columnServerID, Sql.INT).notNull() .column(columnServerID, Sql.INT).notNull()
.column(columnDate, Sql.LONG).notNull() .column(columnDate, Sql.LONG).notNull()

View File

@ -1,6 +1,7 @@
package main.java.com.djrapitops.plan.database.tables; package main.java.com.djrapitops.plan.database.tables;
import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.api.exceptions.DbCreateTableException;
import main.java.com.djrapitops.plan.database.databases.SQLDB; import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Select; import main.java.com.djrapitops.plan.database.sql.Select;
import main.java.com.djrapitops.plan.database.sql.Sql; import main.java.com.djrapitops.plan.database.sql.Sql;
@ -40,9 +41,9 @@ public class CommandUseTable extends Table {
* @return * @return
*/ */
@Override @Override
public boolean createTable() { public void createTable() throws DbCreateTableException {
ServerTable serverTable = db.getServerTable(); ServerTable serverTable = db.getServerTable();
return createTable(TableSqlParser.createTable(tableName) createTable(TableSqlParser.createTable(tableName)
.primaryKeyIDColumn(usingMySQL, columnCommandId) .primaryKeyIDColumn(usingMySQL, columnCommandId)
.column(columnCommand, Sql.varchar(20)).notNull() .column(columnCommand, Sql.varchar(20)).notNull()
.column(columnTimesUsed, Sql.INT).notNull() .column(columnTimesUsed, Sql.INT).notNull()

View File

@ -1,5 +1,6 @@
package main.java.com.djrapitops.plan.database.tables; package main.java.com.djrapitops.plan.database.tables;
import main.java.com.djrapitops.plan.api.exceptions.DbCreateTableException;
import main.java.com.djrapitops.plan.database.databases.SQLDB; import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Select; import main.java.com.djrapitops.plan.database.sql.Select;
import main.java.com.djrapitops.plan.database.sql.Sql; import main.java.com.djrapitops.plan.database.sql.Sql;
@ -32,8 +33,8 @@ public class IPsTable extends UserIDTable {
* @return if the table was created successfully * @return if the table was created successfully
*/ */
@Override @Override
public boolean createTable() { public void createTable() throws DbCreateTableException {
return createTable(TableSqlParser.createTable(tableName) createTable(TableSqlParser.createTable(tableName)
.column(columnUserID, Sql.INT).notNull() .column(columnUserID, Sql.INT).notNull()
.column(columnIP, Sql.varchar(20)).notNull() .column(columnIP, Sql.varchar(20)).notNull()
.column(columnGeolocation, Sql.varchar(50)).notNull() .column(columnGeolocation, Sql.varchar(50)).notNull()

View File

@ -2,6 +2,7 @@ package main.java.com.djrapitops.plan.database.tables;
import com.djrapitops.plugin.utilities.Verify; import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.Log; import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.api.exceptions.DbCreateTableException;
import main.java.com.djrapitops.plan.data.PlayerKill; import main.java.com.djrapitops.plan.data.PlayerKill;
import main.java.com.djrapitops.plan.data.Session; import main.java.com.djrapitops.plan.data.Session;
import main.java.com.djrapitops.plan.database.databases.SQLDB; import main.java.com.djrapitops.plan.database.databases.SQLDB;
@ -41,8 +42,8 @@ public class KillsTable extends UserIDTable {
* @return * @return
*/ */
@Override @Override
public boolean createTable() { public void createTable() throws DbCreateTableException {
return createTable(TableSqlParser.createTable(tableName) createTable(TableSqlParser.createTable(tableName)
.column(columnKillerUserID, Sql.INT).notNull() .column(columnKillerUserID, Sql.INT).notNull()
.column(columnVictimUserID, Sql.INT).notNull() .column(columnVictimUserID, Sql.INT).notNull()
.column(columnWeapon, Sql.varchar(30)).notNull() .column(columnWeapon, Sql.varchar(30)).notNull()

View File

@ -1,6 +1,7 @@
package main.java.com.djrapitops.plan.database.tables; package main.java.com.djrapitops.plan.database.tables;
import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.api.exceptions.DbCreateTableException;
import main.java.com.djrapitops.plan.database.databases.SQLDB; import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Sql; import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser; import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
@ -35,8 +36,8 @@ public class NicknamesTable extends UserIDTable {
* @return if the table was created successfully * @return if the table was created successfully
*/ */
@Override @Override
public boolean createTable() { public void createTable() throws DbCreateTableException {
return createTable(TableSqlParser.createTable(tableName) createTable(TableSqlParser.createTable(tableName)
.column(columnUserID, Sql.INT).notNull() .column(columnUserID, Sql.INT).notNull()
.column(columnNick, Sql.varchar(75)).notNull() .column(columnNick, Sql.varchar(75)).notNull()
.column(columnServerID, Sql.INT).notNull() .column(columnServerID, Sql.INT).notNull()

View File

@ -6,6 +6,7 @@
package main.java.com.djrapitops.plan.database.tables; package main.java.com.djrapitops.plan.database.tables;
import main.java.com.djrapitops.plan.Log; import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.api.exceptions.DbCreateTableException;
import main.java.com.djrapitops.plan.data.WebUser; import main.java.com.djrapitops.plan.data.WebUser;
import main.java.com.djrapitops.plan.database.databases.SQLDB; import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Insert; import main.java.com.djrapitops.plan.database.sql.Insert;
@ -33,8 +34,8 @@ public class SecurityTable extends Table {
} }
@Override @Override
public boolean createTable() { public void createTable() throws DbCreateTableException {
return createTable(TableSqlParser.createTable(tableName) createTable(TableSqlParser.createTable(tableName)
.column(columnUser, Sql.varchar(100)).notNull().unique() .column(columnUser, Sql.varchar(100)).notNull().unique()
.column(columnSaltedHash, Sql.varchar(100)).notNull().unique() .column(columnSaltedHash, Sql.varchar(100)).notNull().unique()
.column(columnPermLevel, Sql.INT).notNull() .column(columnPermLevel, Sql.INT).notNull()
@ -74,7 +75,7 @@ public class SecurityTable extends Table {
statement.setString(2, saltPassHash); statement.setString(2, saltPassHash);
statement.setInt(3, permLevel); statement.setInt(3, permLevel);
statement.execute(); statement.execute();
commit(statement.getConnection()); commit(statement.getConnection());
} finally { } finally {
close(statement); close(statement);

View File

@ -5,6 +5,7 @@
package main.java.com.djrapitops.plan.database.tables; package main.java.com.djrapitops.plan.database.tables;
import com.djrapitops.plugin.utilities.Verify; import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.api.exceptions.DbCreateTableException;
import main.java.com.djrapitops.plan.database.databases.SQLDB; import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.*; import main.java.com.djrapitops.plan.database.sql.*;
import main.java.com.djrapitops.plan.systems.info.server.ServerInfo; import main.java.com.djrapitops.plan.systems.info.server.ServerInfo;
@ -47,8 +48,8 @@ public class ServerTable extends Table {
} }
@Override @Override
public boolean createTable() { public void createTable() throws DbCreateTableException {
return createTable(TableSqlParser.createTable(tableName) createTable(TableSqlParser.createTable(tableName)
.primaryKeyIDColumn(usingMySQL, columnServerID) .primaryKeyIDColumn(usingMySQL, columnServerID)
.column(columnServerUUID, Sql.varchar(36)).notNull().unique() .column(columnServerUUID, Sql.varchar(36)).notNull().unique()
.column(columnServerName, Sql.varchar(100)) .column(columnServerName, Sql.varchar(100))

View File

@ -1,6 +1,7 @@
package main.java.com.djrapitops.plan.database.tables; package main.java.com.djrapitops.plan.database.tables;
import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.api.exceptions.DbCreateTableException;
import main.java.com.djrapitops.plan.data.Session; import main.java.com.djrapitops.plan.data.Session;
import main.java.com.djrapitops.plan.database.databases.SQLDB; import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Select; import main.java.com.djrapitops.plan.database.sql.Select;
@ -43,8 +44,8 @@ public class SessionsTable extends UserIDTable {
* @return * @return
*/ */
@Override @Override
public boolean createTable() { public void createTable() throws DbCreateTableException {
return createTable(TableSqlParser.createTable(this.tableName) createTable(TableSqlParser.createTable(this.tableName)
.primaryKeyIDColumn(usingMySQL, columnID) .primaryKeyIDColumn(usingMySQL, columnID)
.column(columnUserID, Sql.INT).notNull() .column(columnUserID, Sql.INT).notNull()
.column(columnServerID, Sql.INT).notNull() .column(columnServerID, Sql.INT).notNull()

View File

@ -2,6 +2,7 @@ package main.java.com.djrapitops.plan.database.tables;
import com.djrapitops.plugin.api.TimeAmount; import com.djrapitops.plugin.api.TimeAmount;
import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.api.exceptions.DbCreateTableException;
import main.java.com.djrapitops.plan.data.TPS; import main.java.com.djrapitops.plan.data.TPS;
import main.java.com.djrapitops.plan.database.databases.SQLDB; import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Select; import main.java.com.djrapitops.plan.database.sql.Select;
@ -45,8 +46,8 @@ public class TPSTable extends Table {
} }
@Override @Override
public boolean createTable() { public void createTable() throws DbCreateTableException {
return createTable(TableSqlParser.createTable(tableName) createTable(TableSqlParser.createTable(tableName)
.column(columnServerID, Sql.INT).notNull() .column(columnServerID, Sql.INT).notNull()
.column(columnDate, Sql.LONG).notNull() .column(columnDate, Sql.LONG).notNull()
.column(columnTPS, Sql.DOUBLE).notNull() .column(columnTPS, Sql.DOUBLE).notNull()

View File

@ -2,6 +2,7 @@ package main.java.com.djrapitops.plan.database.tables;
import com.djrapitops.plugin.utilities.Verify; import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.Log; import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.api.exceptions.DbCreateTableException;
import main.java.com.djrapitops.plan.database.Container; import main.java.com.djrapitops.plan.database.Container;
import main.java.com.djrapitops.plan.database.DBUtils; import main.java.com.djrapitops.plan.database.DBUtils;
import main.java.com.djrapitops.plan.database.databases.SQLDB; import main.java.com.djrapitops.plan.database.databases.SQLDB;
@ -48,15 +49,13 @@ public abstract class Table {
/** /**
* @return * @return
*/ */
public abstract boolean createTable(); public abstract void createTable() throws DbCreateTableException;
protected boolean createTable(String sql) { protected void createTable(String sql) throws DbCreateTableException {
try { try {
execute(sql); execute(sql);
return true; } catch (SQLException e) {
} catch (SQLException ex) { throw new DbCreateTableException(tableName, "Failed to create table", e);
Log.toLog(this.getClass().getName(), ex);
return false;
} }
} }

View File

@ -5,6 +5,7 @@
package main.java.com.djrapitops.plan.database.tables; package main.java.com.djrapitops.plan.database.tables;
import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.api.exceptions.DbCreateTableException;
import main.java.com.djrapitops.plan.data.UserInfo; import main.java.com.djrapitops.plan.data.UserInfo;
import main.java.com.djrapitops.plan.database.databases.SQLDB; import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Select; import main.java.com.djrapitops.plan.database.sql.Select;
@ -40,8 +41,8 @@ public class UserInfoTable extends UserIDTable {
} }
@Override @Override
public boolean createTable() { public void createTable() throws DbCreateTableException {
return createTable(TableSqlParser.createTable(tableName) createTable(TableSqlParser.createTable(tableName)
.column(columnUserID, Sql.INT).notNull() .column(columnUserID, Sql.INT).notNull()
.column(columnRegistered, Sql.LONG).notNull() .column(columnRegistered, Sql.LONG).notNull()
.column(columnOP, Sql.BOOL).notNull().defaultValue(false) .column(columnOP, Sql.BOOL).notNull().defaultValue(false)

View File

@ -1,6 +1,7 @@
package main.java.com.djrapitops.plan.database.tables; package main.java.com.djrapitops.plan.database.tables;
import com.djrapitops.plugin.utilities.Verify; import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.api.exceptions.DbCreateTableException;
import main.java.com.djrapitops.plan.database.databases.SQLDB; import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.*; import main.java.com.djrapitops.plan.database.sql.*;
@ -35,8 +36,8 @@ public class UsersTable extends UserIDTable {
* @return * @return
*/ */
@Override @Override
public boolean createTable() { public void createTable() throws DbCreateTableException {
return createTable(TableSqlParser.createTable(tableName) createTable(TableSqlParser.createTable(tableName)
.primaryKeyIDColumn(usingMySQL, columnID) .primaryKeyIDColumn(usingMySQL, columnID)
.column(columnUUID, Sql.varchar(36)).notNull().unique() .column(columnUUID, Sql.varchar(36)).notNull().unique()
.column(columnRegistered, Sql.LONG).notNull() .column(columnRegistered, Sql.LONG).notNull()

View File

@ -1,6 +1,7 @@
package main.java.com.djrapitops.plan.database.tables; package main.java.com.djrapitops.plan.database.tables;
import main.java.com.djrapitops.plan.Log; import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.api.exceptions.DbCreateTableException;
import main.java.com.djrapitops.plan.database.databases.SQLDB; import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Sql; import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser; import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
@ -26,8 +27,8 @@ public class VersionTable extends Table {
* @return * @return
*/ */
@Override @Override
public boolean createTable() { public void createTable() throws DbCreateTableException {
return createTable(TableSqlParser.createTable(tableName) createTable(TableSqlParser.createTable(tableName)
.column("version", Sql.INT).notNull() .column("version", Sql.INT).notNull()
.toString() .toString()
); );

View File

@ -1,6 +1,7 @@
package main.java.com.djrapitops.plan.database.tables; package main.java.com.djrapitops.plan.database.tables;
import com.djrapitops.plugin.utilities.Verify; import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.api.exceptions.DbCreateTableException;
import main.java.com.djrapitops.plan.database.databases.SQLDB; import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Sql; import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser; import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
@ -40,8 +41,8 @@ public class WorldTable extends Table {
} }
@Override @Override
public boolean createTable() { public void createTable() throws DbCreateTableException {
return createTable(TableSqlParser.createTable(tableName) createTable(TableSqlParser.createTable(tableName)
.primaryKeyIDColumn(usingMySQL, columnWorldId) .primaryKeyIDColumn(usingMySQL, columnWorldId)
.column(columnWorldName, Sql.varchar(100)).notNull() .column(columnWorldName, Sql.varchar(100)).notNull()
.primaryKey(usingMySQL, columnWorldId) .primaryKey(usingMySQL, columnWorldId)

View File

@ -2,6 +2,7 @@ package main.java.com.djrapitops.plan.database.tables;
import com.djrapitops.plugin.utilities.Verify; import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.api.exceptions.DbCreateTableException;
import main.java.com.djrapitops.plan.data.Session; import main.java.com.djrapitops.plan.data.Session;
import main.java.com.djrapitops.plan.data.time.GMTimes; import main.java.com.djrapitops.plan.data.time.GMTimes;
import main.java.com.djrapitops.plan.data.time.WorldTimes; import main.java.com.djrapitops.plan.data.time.WorldTimes;
@ -48,8 +49,8 @@ public class WorldTimesTable extends UserIDTable {
} }
@Override @Override
public boolean createTable() { public void createTable() throws DbCreateTableException {
return createTable(TableSqlParser.createTable(tableName) createTable(TableSqlParser.createTable(tableName)
.column(columnUserID, Sql.INT).notNull() .column(columnUserID, Sql.INT).notNull()
.column(columnWorldId, Sql.INT).notNull() .column(columnWorldId, Sql.INT).notNull()
.column(columnSessionID, Sql.INT).notNull() .column(columnSessionID, Sql.INT).notNull()

View File

@ -26,7 +26,7 @@ import java.util.UUID;
*/ */
public class ServerInfoFile extends BukkitConfig { public class ServerInfoFile extends BukkitConfig {
public ServerInfoFile(Plan plugin) throws IOException { public ServerInfoFile(Plan plugin) throws IOException {
super(plugin, "ServerInfoFile"); super(plugin, "ServerInfoFile.yml");
IFileConfig config = super.getConfig(); IFileConfig config = super.getConfig();
config.copyDefaults(); config.copyDefaults();
config.addDefault("Server.UUID", ""); config.addDefault("Server.UUID", "");

View File

@ -5,11 +5,11 @@
package main.java.com.djrapitops.plan.systems.info.server; package main.java.com.djrapitops.plan.systems.info.server;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.ServerVariableHolder; import main.java.com.djrapitops.plan.ServerVariableHolder;
import main.java.com.djrapitops.plan.Settings; import main.java.com.djrapitops.plan.Settings;
import main.java.com.djrapitops.plan.api.IPlan; import main.java.com.djrapitops.plan.api.IPlan;
import main.java.com.djrapitops.plan.api.exceptions.PlanEnableException;
import main.java.com.djrapitops.plan.bungee.PlanBungee; import main.java.com.djrapitops.plan.bungee.PlanBungee;
import main.java.com.djrapitops.plan.database.Database; import main.java.com.djrapitops.plan.database.Database;
import main.java.com.djrapitops.plan.database.tables.ServerTable; import main.java.com.djrapitops.plan.database.tables.ServerTable;
@ -33,7 +33,7 @@ public class ServerInfoManager {
private ServerInfoFile serverInfoFile; private ServerInfoFile serverInfoFile;
private final ServerTable serverTable; private final ServerTable serverTable;
public ServerInfoManager(Plan plugin) { public ServerInfoManager(Plan plugin) throws PlanEnableException {
this.plugin = plugin; this.plugin = plugin;
Database db = plugin.getDB(); Database db = plugin.getDB();
serverTable = db.getServerTable(); serverTable = db.getServerTable();
@ -41,9 +41,7 @@ public class ServerInfoManager {
try { try {
serverInfoFile = new ServerInfoFile(plugin); serverInfoFile = new ServerInfoFile(plugin);
} catch (IOException e) { } catch (IOException e) {
Log.toLog(this.getClass().getName(), e); throw new PlanEnableException("Failed to read ServerInfoFile.yml", e);
Log.error("Failed to read server info from local file, disabling plugin.");
plugin.disablePlugin();
} }
Optional<UUID> serverUUID = serverInfoFile.getUUID(); Optional<UUID> serverUUID = serverInfoFile.getUUID();
@ -54,10 +52,10 @@ public class ServerInfoManager {
} else { } else {
registerServer(); registerServer();
} }
} catch (Exception e) { } catch (SQLException e) {
Log.toLog(this.getClass().getName(), e); throw new PlanEnableException("Failed to update Database server info", e);
Log.error("Failed to register server info to database, disabling plugin."); } catch (IOException e) {
plugin.disablePlugin(); throw new PlanEnableException("Failed to write to ServerInfoFile.yml", e);
} }
} }

View File

@ -9,6 +9,7 @@ import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.Action; import main.java.com.djrapitops.plan.data.Action;
import main.java.com.djrapitops.plan.database.Database; import main.java.com.djrapitops.plan.database.Database;
import main.java.com.djrapitops.plan.database.tables.Actions; import main.java.com.djrapitops.plan.database.tables.Actions;
import main.java.com.djrapitops.plan.database.tables.UserInfoTable;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.UUID; import java.util.UUID;
@ -38,12 +39,13 @@ public class RegisterProcessor extends PlayerProcessor {
UUID uuid = getUUID(); UUID uuid = getUUID();
Plan plugin = Plan.getInstance(); Plan plugin = Plan.getInstance();
Database db = plugin.getDB(); Database db = plugin.getDB();
if (db.wasSeenBefore(uuid)) { UserInfoTable userInfoTable = db.getUserInfoTable();
return;
}
plugin.getDataCache().markFirstSession(uuid);
try { try {
db.getUserInfoTable().registerUserInfo(uuid, registered); if (userInfoTable.isRegistered(uuid)) {
return;
}
plugin.getDataCache().markFirstSession(uuid);
userInfoTable.registerUserInfo(uuid, registered);
db.getActionsTable().insertAction(uuid, new Action(time, Actions.FIRST_SESSION, "Online: " + playersOnline + " Players")); db.getActionsTable().insertAction(uuid, new Action(time, Actions.FIRST_SESSION, "Online: " + playersOnline + " Players"));
} catch (SQLException e) { } catch (SQLException e) {
Log.toLog(this.getClass().getName(), e); Log.toLog(this.getClass().getName(), e);

View File

@ -3,6 +3,7 @@ package main.java.com.djrapitops.plan.utilities;
import com.djrapitops.plugin.utilities.Verify; import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.Log; import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.api.exceptions.DatabaseInitException;
import main.java.com.djrapitops.plan.database.Database; import main.java.com.djrapitops.plan.database.Database;
import main.java.com.djrapitops.plan.database.databases.SQLiteDB; import main.java.com.djrapitops.plan.database.databases.SQLiteDB;
@ -28,7 +29,7 @@ public class ManageUtils {
* @param copyFromDB Database you want to backup. * @param copyFromDB Database you want to backup.
* @return success? * @return success?
*/ */
public static boolean backup(String dbName, Database copyFromDB) { public static boolean backup(String dbName, Database copyFromDB) throws DatabaseInitException {
Plan plugin = Plan.getInstance(); Plan plugin = Plan.getInstance();
String timeStamp = new Date().toString().substring(4, 10).replace(" ", "-"); String timeStamp = new Date().toString().substring(4, 10).replace(" ", "-");
String fileName = dbName + "-backup-" + timeStamp; String fileName = dbName + "-backup-" + timeStamp;
@ -79,15 +80,13 @@ public class ManageUtils {
return true; return true;
} }
public static Database getDB(Plan plugin, String dbName) { public static Database getDB(Plan plugin, String dbName) throws DatabaseInitException {
Database database = null; Database database = null;
for (Database sqldb : plugin.getDatabases()) { for (Database sqldb : plugin.getDatabases()) {
String dbConfigName = sqldb.getConfigName(); String dbConfigName = sqldb.getConfigName();
if (Verify.equalsIgnoreCase(dbName, dbConfigName)) { if (Verify.equalsIgnoreCase(dbName, dbConfigName)) {
database = sqldb; database = sqldb;
if (!database.init()) { database.init();
return null;
}
break; break;
} }
} }