mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-12-23 01:27:42 +01:00
[2.6.0-DEV] Improved Error Handling and Bugfixes & Small things
- Better error handling & logging. Implemented for SQLDB.java - other catch clauses yet to be changed. This was done mainly to be able to run Debugger tests on it. Bugfixes: - DB Auto-Commit bug #12 fixed - New DB creation was no-longer execption free after adding throws clauses, fixed that. Bugs: - SQLException: The prepared statement has been finalized when closing database on disable, cause yet unknown - "___ added to cache" called 5-6 times when player joins Untested: - Events - SessionData - KillData - Does DB save everything or just plan_users UnImplemented: - Location adding in baches - Location data getting seperately for analysis, not saved to UserData to speed up get. - http://www.kryogenix.org/code/browser/sorttable/ - Player data table - Player session length - Player online activity graph, week - PlanLite features
This commit is contained in:
parent
d706d3431f
commit
ab03c43204
@ -80,6 +80,7 @@ public enum Phrase {
|
||||
ERROR_NO_DATA_VIEW(ChatColor.YELLOW + "Webserver disabled but Alternative IP/PlanLite not used, no way to view data!"),
|
||||
ERROR_WEBSERVER_OFF_ANALYSIS(ChatColor.YELLOW + "" + PREFIX + "This command can be only used if the webserver is running on this server."),
|
||||
ERROR_WEBSERVER_OFF_INSPECT(ChatColor.YELLOW + "" + PREFIX + "This command can be only used if webserver/planlite is enabled on this server."),
|
||||
ERROR_LOGGED("Ran into an error. It has been logged to the Errors.txt"),
|
||||
//
|
||||
CMD_FOOTER(COLOR_TER.color() + "" + ARROWS_RIGHT),
|
||||
MANAGE_ERROR_INCORRECT_PLUGIN(ChatColor.RED + "" + PREFIX + "Plugin not supported: "),
|
||||
@ -93,6 +94,7 @@ public enum Phrase {
|
||||
MANAGE_ERROR_BACKUP_FILE_NOT_FOUND(ChatColor.RED + "" + PREFIX + "Backup file doesn't exist!"),
|
||||
MANAGE_MOVE_SUCCESS(ChatColor.GREEN + "" + PREFIX + "All data moved successfully!"),
|
||||
MANAGE_COPY_SUCCESS(ChatColor.GREEN + "" + PREFIX + "All data copied successfully!"),
|
||||
MANAGE_PROCESS_FAIL(ChatColor.RED + "" + PREFIX + "Something went wrong while processing the data!"),
|
||||
MANAGE_CLEAR_SUCCESS(ChatColor.GREEN + "" + PREFIX + "All data cleared successfully!"),
|
||||
MANAGE_REMOVE_SUCCESS(CMD_FOOTER + " " + COLOR_MAIN.color() + "Data of " + COLOR_TER.color() + "REPLACE0" + COLOR_MAIN.color() + " was removed from Database " + COLOR_TER.color() + "REPLACE1" + COLOR_MAIN.color() + "."),
|
||||
MANAGE_IMPORTING(CMD_FOOTER + " " + COLOR_MAIN.color() + " Importing Data.."),
|
||||
|
@ -22,10 +22,13 @@ package main.java.com.djrapitops.plan;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.URL;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.concurrent.Executors;
|
||||
@ -38,6 +41,7 @@ import main.java.com.djrapitops.plan.database.Database;
|
||||
import main.java.com.djrapitops.plan.database.databases.*;
|
||||
import main.java.com.djrapitops.plan.ui.Html;
|
||||
import main.java.com.djrapitops.plan.ui.webserver.WebSocketServer;
|
||||
import main.java.com.djrapitops.plan.utilities.FormatUtils;
|
||||
import main.java.com.djrapitops.plan.utilities.MiscUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.ConsoleCommandSender;
|
||||
@ -180,6 +184,42 @@ public class Plan extends JavaPlugin {
|
||||
getLogger().severe(message);
|
||||
}
|
||||
|
||||
public void toLog(String source, Exception e) {
|
||||
logError(Phrase.ERROR_LOGGED + "");
|
||||
toLog(source + " Caught " + e);
|
||||
for (StackTraceElement x : e.getStackTrace()) {
|
||||
toLog(" " + x);
|
||||
}
|
||||
toLog("");
|
||||
}
|
||||
|
||||
public void toLog(String source, Collection<Exception> e) {
|
||||
for (Exception ex : e) {
|
||||
toLog(source, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void toLog(String message) {
|
||||
File folder = getDataFolder();
|
||||
if (!folder.exists()) {
|
||||
folder.mkdir();
|
||||
}
|
||||
File log = new File(getDataFolder(), "Errors.txt");
|
||||
try {
|
||||
if (!log.exists()) {
|
||||
log.createNewFile();
|
||||
}
|
||||
FileWriter fw = new FileWriter(log, true);
|
||||
try (PrintWriter pw = new PrintWriter(fw)) {
|
||||
String timestamp = FormatUtils.formatTimeStamp(new Date().getTime() + "");
|
||||
pw.println("[" + timestamp + "] " + message);
|
||||
pw.flush();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
getLogger().severe("Failed to create DBerrors.txt file");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Plan API
|
||||
*/
|
||||
@ -210,7 +250,6 @@ public class Plan extends JavaPlugin {
|
||||
String type = Settings.DB_TYPE + "";
|
||||
|
||||
db = null;
|
||||
|
||||
for (Database database : databases) {
|
||||
if (type.equalsIgnoreCase(database.getConfigName())) {
|
||||
this.db = database;
|
||||
@ -218,12 +257,10 @@ public class Plan extends JavaPlugin {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (db == null) {
|
||||
log(Phrase.DATABASE_TYPE_DOES_NOT_EXIST.toString());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!db.init()) {
|
||||
log(Phrase.DATABASE_FAILURE_DISABLE.toString());
|
||||
setEnabled(false);
|
||||
|
@ -11,7 +11,6 @@ import main.java.com.djrapitops.plan.utilities.HtmlUtils;
|
||||
import main.java.com.djrapitops.plan.utilities.MiscUtils;
|
||||
import main.java.com.djrapitops.plan.utilities.UUIDFetcher;
|
||||
import org.bukkit.Bukkit;
|
||||
import static org.bukkit.Bukkit.getOfflinePlayer;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@ -19,8 +18,6 @@ import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
import static org.bukkit.Bukkit.getOfflinePlayer;
|
||||
import static org.bukkit.Bukkit.getOfflinePlayer;
|
||||
import static org.bukkit.Bukkit.getOfflinePlayer;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -1,5 +1,6 @@
|
||||
package main.java.com.djrapitops.plan.command.commands.manage;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
@ -76,6 +77,7 @@ public class ManageBackupCommand extends SubCommand {
|
||||
(new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Date now = new Date();
|
||||
SQLiteDB backupDB = new SQLiteDB(plugin,
|
||||
args[0] + "-backup-" + now.toString().substring(4, 10).replaceAll(" ", "-").replaceAll(":", "-"));
|
||||
@ -103,6 +105,10 @@ public class ManageBackupCommand extends SubCommand {
|
||||
backupDB.saveMultipleUserData(allUserData);
|
||||
backupDB.saveCommandUse(copyFromDB.getCommandUse());
|
||||
sender.sendMessage(Phrase.MANAGE_COPY_SUCCESS.toString());
|
||||
} catch (SQLException e) {
|
||||
plugin.toLog(this.getClass().getName(), e);
|
||||
sender.sendMessage(Phrase.MANAGE_PROCESS_FAIL.toString());
|
||||
}
|
||||
this.cancel();
|
||||
}
|
||||
}).runTaskAsynchronously(plugin);
|
||||
|
@ -1,5 +1,6 @@
|
||||
package main.java.com.djrapitops.plan.command.commands.manage;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Arrays;
|
||||
import main.java.com.djrapitops.plan.Phrase;
|
||||
import main.java.com.djrapitops.plan.Plan;
|
||||
@ -76,8 +77,16 @@ public class ManageClearCommand extends SubCommand {
|
||||
@Override
|
||||
public void run() {
|
||||
sender.sendMessage(Phrase.MANAGE_PROCESS_START.parse());
|
||||
clearThisDB.removeAllData();
|
||||
try {
|
||||
if (clearThisDB.removeAllData()) {
|
||||
sender.sendMessage(Phrase.MANAGE_CLEAR_SUCCESS + "");
|
||||
} else {
|
||||
sender.sendMessage(Phrase.MANAGE_PROCESS_FAIL + "");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
plugin.toLog(this.getClass().getName(), e);
|
||||
sender.sendMessage(Phrase.MANAGE_PROCESS_FAIL + "");
|
||||
}
|
||||
this.cancel();
|
||||
}
|
||||
}).runTaskAsynchronously(plugin);
|
||||
|
@ -1,5 +1,6 @@
|
||||
package main.java.com.djrapitops.plan.command.commands.manage;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
@ -17,6 +18,7 @@ import main.java.com.djrapitops.plan.utilities.DataCombineUtils;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -96,10 +98,9 @@ public class ManageCombineCommand extends SubCommand {
|
||||
plugin.logError(toDB + " was null!");
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
final Set<UUID> fromUUIDS = fromDatabase.getSavedUUIDs();
|
||||
final Set<UUID> toUUIDS = toDatabase.getSavedUUIDs();
|
||||
try {
|
||||
if (fromUUIDS.isEmpty() && toUUIDS.isEmpty()) {
|
||||
sender.sendMessage(Phrase.MANAGE_ERROR_NO_PLAYERS + " (" + fromDB + ")");
|
||||
return true;
|
||||
@ -107,9 +108,10 @@ public class ManageCombineCommand extends SubCommand {
|
||||
|
||||
final Database moveFromDB = fromDatabase;
|
||||
final Database moveToDB = toDatabase;
|
||||
(new BukkitRunnable() {
|
||||
BukkitTask asyncDBCombineTask = (new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
sender.sendMessage(Phrase.MANAGE_PROCESS_START.parse());
|
||||
HashMap<UUID, UserData> allFromUserData = new HashMap<>();
|
||||
HashMap<UUID, UserData> allToUserData = new HashMap<>();
|
||||
@ -149,13 +151,17 @@ public class ManageCombineCommand extends SubCommand {
|
||||
if (!toDB.equals(plugin.getDB().getConfigName())) {
|
||||
sender.sendMessage(Phrase.MANAGE_DB_CONFIG_REMINDER + "");
|
||||
}
|
||||
} catch (SQLException | NullPointerException e) {
|
||||
plugin.toLog(this.getClass().getName(), e);
|
||||
sender.sendMessage(Phrase.MANAGE_PROCESS_FAIL + "");
|
||||
}
|
||||
this.cancel();
|
||||
}
|
||||
|
||||
}).runTaskAsynchronously(plugin);
|
||||
|
||||
} catch (NullPointerException e) {
|
||||
sender.sendMessage(Phrase.MANAGE_DATABASE_FAILURE + "");
|
||||
} catch (SQLException | NullPointerException e) {
|
||||
plugin.toLog(this.getClass().getName(), e);
|
||||
sender.sendMessage(Phrase.MANAGE_PROCESS_FAIL + "");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package main.java.com.djrapitops.plan.command.commands.manage;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import main.java.com.djrapitops.plan.Phrase;
|
||||
import main.java.com.djrapitops.plan.Plan;
|
||||
import main.java.com.djrapitops.plan.command.CommandType;
|
||||
@ -60,7 +61,8 @@ public class ManageHotswapCommand extends SubCommand {
|
||||
db.getVersion(); //Test db connection
|
||||
}
|
||||
}
|
||||
} catch (NullPointerException e) {
|
||||
} catch (NullPointerException | SQLException e) {
|
||||
plugin.toLog(this.getClass().getName(), e);
|
||||
sender.sendMessage(Phrase.MANAGE_DATABASE_FAILURE + "");
|
||||
return true;
|
||||
}
|
||||
|
@ -23,7 +23,6 @@ import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
import static org.bukkit.Bukkit.getOfflinePlayer;
|
||||
import static org.bukkit.Bukkit.getOfflinePlayer;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -1,5 +1,6 @@
|
||||
package main.java.com.djrapitops.plan.command.commands.manage;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
@ -95,7 +96,14 @@ public class ManageMoveCommand extends SubCommand {
|
||||
plugin.logError(toDB + " was null!");
|
||||
return true;
|
||||
}
|
||||
final Set<UUID> uuids = fromDatabase.getSavedUUIDs();
|
||||
final Set<UUID> uuids;
|
||||
try {
|
||||
uuids = fromDatabase.getSavedUUIDs();
|
||||
} catch (SQLException e) {
|
||||
plugin.toLog(this.getClass().getName(), e);
|
||||
sender.sendMessage(Phrase.MANAGE_PROCESS_FAIL + "");
|
||||
return true;
|
||||
}
|
||||
if (uuids.isEmpty()) {
|
||||
sender.sendMessage(Phrase.MANAGE_ERROR_NO_PLAYERS + " (" + fromDB + ")");
|
||||
return true;
|
||||
@ -127,8 +135,9 @@ public class ManageMoveCommand extends SubCommand {
|
||||
if (!toDB.equals(plugin.getDB().getConfigName())) {
|
||||
sender.sendMessage(Phrase.MANAGE_DB_CONFIG_REMINDER + "");
|
||||
}
|
||||
} catch (NullPointerException e) {
|
||||
sender.sendMessage(Phrase.MANAGE_DATABASE_FAILURE + "");
|
||||
} catch (SQLException | NullPointerException e) {
|
||||
plugin.toLog(this.getClass().getName(), e);
|
||||
sender.sendMessage(Phrase.MANAGE_PROCESS_FAIL + "");
|
||||
}
|
||||
|
||||
this.cancel();
|
||||
|
@ -1,5 +1,6 @@
|
||||
package main.java.com.djrapitops.plan.command.commands.manage;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Arrays;
|
||||
import java.util.UUID;
|
||||
import main.java.com.djrapitops.plan.Phrase;
|
||||
@ -13,7 +14,6 @@ import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
import static org.bukkit.Bukkit.getOfflinePlayer;
|
||||
import static org.bukkit.Bukkit.getOfflinePlayer;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -84,9 +84,17 @@ public class ManageRemoveCommand extends SubCommand {
|
||||
@Override
|
||||
public void run() {
|
||||
sender.sendMessage(Phrase.MANAGE_PROCESS_START.parse());
|
||||
try {
|
||||
plugin.getHandler().clearFromCache(uuid);
|
||||
plugin.getDB().removeAccount(uuid.toString());
|
||||
if (plugin.getDB().removeAccount(uuid.toString())) {
|
||||
sender.sendMessage(Phrase.MANAGE_REMOVE_SUCCESS.parse(playerName, plugin.getDB().getConfigName()));
|
||||
} else {
|
||||
sender.sendMessage(Phrase.MANAGE_PROCESS_FAIL + "");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
plugin.toLog(this.getClass().getName(), e);
|
||||
sender.sendMessage(Phrase.MANAGE_PROCESS_FAIL + "");
|
||||
}
|
||||
this.cancel();
|
||||
}
|
||||
}).runTaskAsynchronously(plugin);
|
||||
|
@ -1,6 +1,7 @@
|
||||
package main.java.com.djrapitops.plan.command.commands.manage;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
@ -82,6 +83,7 @@ public class ManageRestoreCommand extends SubCommand {
|
||||
BukkitTask asyncRestoreTask = (new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
String backupDBName = args[0];
|
||||
|
||||
File backupDBFile = new File(plugin.getDataFolder(), backupDBName + (backupDBName.contains(".db") ? "" : ".db"));
|
||||
@ -117,6 +119,10 @@ public class ManageRestoreCommand extends SubCommand {
|
||||
copyToDB.saveMultipleUserData(allUserData);
|
||||
copyToDB.saveCommandUse(backupDB.getCommandUse());
|
||||
sender.sendMessage(Phrase.MANAGE_COPY_SUCCESS.toString());
|
||||
} catch (SQLException | NullPointerException e) {
|
||||
plugin.toLog(this.getClass().getName(), e);
|
||||
sender.sendMessage(Phrase.MANAGE_PROCESS_FAIL + "");
|
||||
}
|
||||
this.cancel();
|
||||
}
|
||||
}).runTaskAsynchronously(plugin);
|
||||
|
@ -1,5 +1,6 @@
|
||||
package main.java.com.djrapitops.plan.data.cache;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
@ -32,8 +33,8 @@ public class DataCacheHandler {
|
||||
private final DemographicsHandler demographicsHandler;
|
||||
private final BasicInfoHandler basicInfoHandler;
|
||||
private final RuleBreakingHandler ruleBreakingHandler;
|
||||
private final HashMap<String, Integer> commandUse;
|
||||
private final CommandUseHandler commandUseHandler;
|
||||
private HashMap<String, Integer> commandUse;
|
||||
private CommandUseHandler commandUseHandler;
|
||||
private final KillHandler killHandler;
|
||||
private final SessionHandler sessionHandler;
|
||||
private final Database db;
|
||||
@ -60,15 +61,21 @@ public class DataCacheHandler {
|
||||
demographicsHandler = new DemographicsHandler(plugin, this);
|
||||
basicInfoHandler = new BasicInfoHandler(plugin, this);
|
||||
ruleBreakingHandler = new RuleBreakingHandler(plugin, this);
|
||||
commandUse = db.getCommandUse();
|
||||
commandUseHandler = new CommandUseHandler(commandUse);
|
||||
|
||||
newPlayerCreator = new NewPlayerCreator(plugin, this);
|
||||
killHandler = new KillHandler(plugin);
|
||||
sessionHandler = new SessionHandler(plugin);
|
||||
|
||||
timesSaved = 0;
|
||||
maxPlayers = plugin.getServer().getMaxPlayers();
|
||||
|
||||
try {
|
||||
commandUse = db.getCommandUse();
|
||||
commandUseHandler = new CommandUseHandler(commandUse);
|
||||
} catch (SQLException e) {
|
||||
plugin.toLog(this.getClass().getName(), e);
|
||||
plugin.logError(Phrase.DATABASE_FAILURE_DISABLE + "");
|
||||
plugin.getServer().getPluginManager().disablePlugin(plugin);
|
||||
}
|
||||
int minutes = Settings.SAVE_CACHE_MIN.getNumber();
|
||||
if (minutes <= 0) {
|
||||
minutes = 5;
|
||||
@ -90,7 +97,6 @@ public class DataCacheHandler {
|
||||
handler.clearCache();
|
||||
}
|
||||
saveCommandUse();
|
||||
handler.clearNulls();
|
||||
timesSaved++;
|
||||
}
|
||||
}).runTaskTimerAsynchronously(plugin, 60 * 20 * minutes, 60 * 20 * minutes);
|
||||
@ -122,7 +128,12 @@ public class DataCacheHandler {
|
||||
}
|
||||
}
|
||||
};
|
||||
try {
|
||||
db.giveUserDataToProcessors(uuid, cacher, processor);
|
||||
} catch (SQLException e) {
|
||||
plugin.toLog(this.getClass().getName(), e);
|
||||
this.cancel();
|
||||
}
|
||||
} else {
|
||||
processor.process(uData);
|
||||
}
|
||||
@ -147,12 +158,17 @@ public class DataCacheHandler {
|
||||
* Saves all data in the cache to Database with AsyncTasks
|
||||
*/
|
||||
public void saveCachedUserData() {
|
||||
clearNulls();
|
||||
BukkitTask asyncFullCacheSaveTask = (new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
List<UserData> data = new ArrayList<>();
|
||||
data.addAll(dataCache.values());
|
||||
try {
|
||||
db.saveMultipleUserData(data);
|
||||
} catch (SQLException ex) {
|
||||
plugin.toLog(this.getClass().getName(), ex);
|
||||
}
|
||||
timesSaved++;
|
||||
this.cancel();
|
||||
}
|
||||
@ -163,14 +179,19 @@ public class DataCacheHandler {
|
||||
* Saves all data in the cache to Database and closes the database down.
|
||||
*/
|
||||
public void saveCacheOnDisable() {
|
||||
clearNulls();
|
||||
List<UserData> data = new ArrayList<>();
|
||||
data.addAll(dataCache.values());
|
||||
data.parallelStream().forEach((userData) -> {
|
||||
sessionHandler.endSession(userData);
|
||||
});
|
||||
try {
|
||||
db.saveMultipleUserData(data);
|
||||
db.saveCommandUse(commandUse);
|
||||
db.close();
|
||||
} catch (SQLException e) {
|
||||
plugin.toLog(this.getClass().getName(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -179,11 +200,16 @@ public class DataCacheHandler {
|
||||
* @param uuid Player's UUID
|
||||
*/
|
||||
public void saveCachedData(UUID uuid) {
|
||||
clearNulls();
|
||||
BukkitTask asyncCachedUserSaveTask = (new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (dataCache.get(uuid) != null) {
|
||||
try {
|
||||
db.saveUserData(uuid, dataCache.get(uuid));
|
||||
} catch (SQLException e) {
|
||||
plugin.toLog(this.getClass().getName(), e);
|
||||
}
|
||||
}
|
||||
this.cancel();
|
||||
}
|
||||
@ -197,7 +223,11 @@ public class DataCacheHandler {
|
||||
* Data is saved on a new line with a long value matching current Date
|
||||
*/
|
||||
public void saveCommandUse() {
|
||||
try {
|
||||
db.saveCommandUse(commandUse);
|
||||
} catch (SQLException e) {
|
||||
plugin.toLog(this.getClass().getName(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// Should only be called from Async thread
|
||||
@ -270,7 +300,7 @@ public class DataCacheHandler {
|
||||
(new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (entry.getValue().isAccessed()) {
|
||||
if (!entry.getValue().isAccessed()) {
|
||||
entry.setValue(null);
|
||||
plugin.log("Cleared " + entry.getKey().toString() + " from Cache. (Delay task)");
|
||||
this.cancel();
|
||||
|
@ -5,9 +5,6 @@ import java.util.UUID;
|
||||
import main.java.com.djrapitops.plan.Plan;
|
||||
import main.java.com.djrapitops.plan.data.UserData;
|
||||
import main.java.com.djrapitops.plan.data.cache.DataCacheHandler;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -4,7 +4,6 @@ import java.net.InetAddress;
|
||||
import main.java.com.djrapitops.plan.Plan;
|
||||
import main.java.com.djrapitops.plan.data.UserData;
|
||||
import main.java.com.djrapitops.plan.data.cache.DataCacheHandler;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -13,8 +13,6 @@ import main.java.com.djrapitops.plan.api.Gender;
|
||||
import main.java.com.djrapitops.plan.data.DemographicsData;
|
||||
import main.java.com.djrapitops.plan.data.UserData;
|
||||
import main.java.com.djrapitops.plan.data.cache.DataCacheHandler;
|
||||
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -42,16 +40,15 @@ public class DemographicsHandler {
|
||||
* If message contains triggerwords and words that define data important,
|
||||
* informatino will be saved in the DemographicsData of UserData provided
|
||||
*
|
||||
* @param event The Chat event passed by listener
|
||||
* @param message Chat Message
|
||||
* @param data UserData corresponding to player of this event.
|
||||
*/
|
||||
public void handleChatEvent(AsyncPlayerChatEvent event, UserData data) {
|
||||
public void handleChatEvent(String message, UserData data) {
|
||||
List<String> triggers = Arrays.asList(Settings.DEM_TRIGGERS.toString().split(", "));
|
||||
List<String> female = Arrays.asList(Settings.DEM_FEMALE.toString().split(", "));
|
||||
List<String> male = Arrays.asList(Settings.DEM_MALE.toString().split(", "));
|
||||
List<String> ignore = Arrays.asList(Settings.DEM_IGNORE.toString().split(", "));
|
||||
|
||||
String message = event.getMessage();
|
||||
String[] messageA = message.toLowerCase().split("\\s+");
|
||||
|
||||
boolean trigger = false;
|
||||
|
@ -5,10 +5,6 @@ import main.java.com.djrapitops.plan.Plan;
|
||||
import main.java.com.djrapitops.plan.data.UserData;
|
||||
import main.java.com.djrapitops.plan.data.cache.DataCacheHandler;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.player.PlayerGameModeChangeEvent;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -1,5 +1,6 @@
|
||||
package main.java.com.djrapitops.plan.data.handlers;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
import main.java.com.djrapitops.plan.Plan;
|
||||
@ -19,7 +20,13 @@ public class KillHandler {
|
||||
|
||||
public void handlePlayerKill(UserData killerData, UUID victimUUID, String weapon) {
|
||||
long now = new Date().toInstant().getEpochSecond()*(long)1000;
|
||||
int victimID = plugin.getDB().getUserId(victimUUID+"");
|
||||
int victimID;
|
||||
try {
|
||||
victimID = plugin.getDB().getUserId(victimUUID+"");
|
||||
} catch (SQLException e) {
|
||||
plugin.toLog(this.getClass().getName(), e);
|
||||
return;
|
||||
}
|
||||
killerData.addPlayerKill(new KillData(victimUUID, victimID, weapon, now));
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
package main.java.com.djrapitops.plan.data.handlers;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Date;
|
||||
import main.java.com.djrapitops.plan.Plan;
|
||||
import main.java.com.djrapitops.plan.data.DemographicsData;
|
||||
@ -59,7 +60,11 @@ public class NewPlayerCreator {
|
||||
data.setLastGmSwapTime(zero);
|
||||
data.setDeaths(0);
|
||||
data.setMobKills(0);
|
||||
try {
|
||||
db.saveUserData(player.getUniqueId(), data);
|
||||
} catch (SQLException e) {
|
||||
plugin.toLog(this.getClass().getName(), e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,12 +1,8 @@
|
||||
package main.java.com.djrapitops.plan.data.handlers;
|
||||
|
||||
import java.util.Date;
|
||||
import main.java.com.djrapitops.plan.Plan;
|
||||
import main.java.com.djrapitops.plan.data.UserData;
|
||||
import main.java.com.djrapitops.plan.data.cache.DataCacheHandler;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.player.PlayerKickEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -43,7 +39,6 @@ public class RuleBreakingHandler {
|
||||
*/
|
||||
public void handleKick(UserData data) {
|
||||
data.setTimesKicked(data.getTimesKicked() + 1);
|
||||
data.setPlayTime(data.getPlayTime() + (new Date().getTime() - data.getLastPlayed()));
|
||||
data.setLastPlayed(new Date().getTime());
|
||||
handler.getActivityHandler().handleLogOut(data);
|
||||
}
|
||||
}
|
||||
|
@ -50,7 +50,7 @@ public class PlanChatListener implements Listener {
|
||||
@Override
|
||||
public void process(UserData data) {
|
||||
basicInfoH.addNickname(p.getDisplayName(), data);
|
||||
demographicsHandler.handleChatEvent(event, data);
|
||||
demographicsHandler.handleChatEvent(event.getMessage(), data);
|
||||
}
|
||||
};
|
||||
handler.getUserDataForProcessing(chatProcessor, p.getUniqueId());
|
||||
|
@ -14,7 +14,6 @@ import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerKickEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -1,5 +1,6 @@
|
||||
package main.java.com.djrapitops.plan.database;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
import main.java.com.djrapitops.plan.Plan;
|
||||
import main.java.com.djrapitops.plan.data.UserData;
|
||||
@ -14,114 +15,31 @@ public abstract class Database {
|
||||
|
||||
private final Plan plugin;
|
||||
|
||||
/**
|
||||
* Abstract class constructor.
|
||||
*
|
||||
* @param plugin Current instance of Plan
|
||||
*/
|
||||
public Database(Plan plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiates the Database.
|
||||
*
|
||||
* @return false
|
||||
*/
|
||||
public boolean init(){
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the UserData fetched from the Database.
|
||||
*
|
||||
* @param uuid UUID of Player
|
||||
* @param processors The DBCallableProcessor Objects used to process data after async
|
||||
* get task is complete.
|
||||
*/
|
||||
public abstract void giveUserDataToProcessors(UUID uuid, DBCallableProcessor... processors);
|
||||
|
||||
/**
|
||||
* Saves the UserData to the Database.
|
||||
*
|
||||
* @param uuid UUID of Player
|
||||
* @param data UserData of Player
|
||||
*/
|
||||
public abstract void saveUserData(UUID uuid, UserData data);
|
||||
|
||||
/**
|
||||
* Saves multiple UserData to the Database using batch processing.
|
||||
*
|
||||
* @param data List of Data
|
||||
*/
|
||||
public abstract void saveMultipleUserData(List<UserData> data);
|
||||
|
||||
/**
|
||||
* Check if the player is found in the database.
|
||||
*
|
||||
* @param uuid UUID of Player
|
||||
* @return true if player is found in the database
|
||||
*/
|
||||
public abstract void giveUserDataToProcessors(UUID uuid, DBCallableProcessor... processors) throws SQLException;
|
||||
public abstract void saveUserData(UUID uuid, UserData data) throws SQLException;
|
||||
public abstract void saveMultipleUserData(List<UserData> data) throws SQLException;
|
||||
public abstract boolean wasSeenBefore(UUID uuid);
|
||||
|
||||
/**
|
||||
* Cleans the database.
|
||||
*/
|
||||
public abstract void clean();
|
||||
|
||||
/**
|
||||
* Used by the Config section.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public abstract String getName();
|
||||
|
||||
/**
|
||||
* Used by the Config section.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getConfigName() {
|
||||
return getName().toLowerCase().replace(" ", "");
|
||||
}
|
||||
|
||||
/**
|
||||
* Used by the Config section.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public ConfigurationSection getConfigSection() {
|
||||
return plugin.getConfig().getConfigurationSection(getConfigName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the version of the database in case of updates.
|
||||
*
|
||||
* @return Current version of the database
|
||||
*/
|
||||
public abstract int getVersion();
|
||||
|
||||
/**
|
||||
* Set the version of the database.
|
||||
*
|
||||
* @param version Version number
|
||||
*/
|
||||
public abstract void setVersion(int version);
|
||||
|
||||
/**
|
||||
* Closes the database.
|
||||
*/
|
||||
public abstract void close();
|
||||
|
||||
public abstract void removeAccount(String uuid);
|
||||
|
||||
public abstract void removeAllData();
|
||||
|
||||
public abstract void saveCommandUse(HashMap<String, Integer> data);
|
||||
|
||||
public abstract Set<UUID> getSavedUUIDs();
|
||||
|
||||
public abstract HashMap<String, Integer> getCommandUse();
|
||||
|
||||
public abstract int getUserId(String uuid);
|
||||
public abstract int getVersion() throws SQLException;
|
||||
public abstract void setVersion(int version) throws SQLException;
|
||||
public abstract void close() throws SQLException;
|
||||
public abstract boolean removeAccount(String uuid) throws SQLException;
|
||||
public abstract boolean removeAllData() throws SQLException;
|
||||
public abstract void saveCommandUse(HashMap<String, Integer> data) throws SQLException;
|
||||
public abstract Set<UUID> getSavedUUIDs() throws SQLException;
|
||||
public abstract HashMap<String, Integer> getCommandUse() throws SQLException;
|
||||
public abstract int getUserId(String uuid) throws SQLException;
|
||||
}
|
||||
|
@ -36,8 +36,9 @@ public class MySQLDB extends SQLDB {
|
||||
Class.forName("com.mysql.jdbc.Driver");
|
||||
|
||||
String url = "jdbc:mysql://" + config.getString("mysql.host") + ":" + config.getString("mysql.port") + "/" + config.getString("mysql.database");
|
||||
Connection connection = DriverManager.getConnection(url, config.getString("mysql.user"), config.getString("mysql.password"));
|
||||
|
||||
return DriverManager.getConnection(url, config.getString("mysql.user"), config.getString("mysql.password"));
|
||||
return connection;
|
||||
} catch (ClassNotFoundException | SQLException e) {
|
||||
getPlugin(Plan.class).logError(Phrase.DB_CONNECTION_FAIL.parse(getConfigName(), e.getMessage()));
|
||||
return null;
|
||||
|
@ -7,6 +7,7 @@ import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
@ -31,7 +32,6 @@ public abstract class SQLDB extends Database {
|
||||
private final boolean supportsModification;
|
||||
|
||||
private Connection connection;
|
||||
private int transactionsActive;
|
||||
|
||||
private final String userName;
|
||||
private final String locationName;
|
||||
@ -93,7 +93,6 @@ public abstract class SQLDB extends Database {
|
||||
super(plugin);
|
||||
this.plugin = plugin;
|
||||
this.supportsModification = supportsModification;
|
||||
transactionsActive = 0;
|
||||
|
||||
userName = "plan_users";
|
||||
locationName = "plan_locations";
|
||||
@ -171,11 +170,15 @@ public abstract class SQLDB extends Database {
|
||||
@Override
|
||||
public boolean init() {
|
||||
super.init();
|
||||
try {
|
||||
return checkConnection();
|
||||
} catch (SQLException e) {
|
||||
plugin.toLog(this.getClass().getName(), e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean checkConnection() {
|
||||
try {
|
||||
public boolean checkConnection() throws SQLException {
|
||||
if (connection == null || connection.isClosed()) {
|
||||
connection = getNewConnection();
|
||||
|
||||
@ -187,9 +190,6 @@ public abstract class SQLDB extends Database {
|
||||
ResultSet set = connection.prepareStatement(supportsModification ? ("SHOW TABLES LIKE '" + userName + "'") : "SELECT name FROM sqlite_master WHERE type='table' AND name='" + userName + "'").executeQuery();
|
||||
boolean newDatabase = set.next();
|
||||
set.close();
|
||||
if (newDatabase) {
|
||||
setVersion(1);
|
||||
}
|
||||
query("CREATE TABLE IF NOT EXISTS " + userName + " ("
|
||||
+ userColumnID + " integer " + ((usingMySQL) ? "NOT NULL AUTO_INCREMENT" : "PRIMARY KEY") + ", "
|
||||
+ userColumnUUID + " varchar(36) NOT NULL, "
|
||||
@ -202,8 +202,7 @@ public abstract class SQLDB extends Database {
|
||||
+ userColumnLoginTimes + " integer NOT NULL, "
|
||||
+ userColumnLastPlayed + " bigint NOT NULL, "
|
||||
+ userColumnDeaths + " int NOT NULL, "
|
||||
+ userColumnMobKills + " int NOT NULL, "
|
||||
+ userColumnPlayerKills + " int NOT NULL"
|
||||
+ userColumnMobKills + " int NOT NULL"
|
||||
+ (usingMySQL ? ", PRIMARY KEY (" + userColumnID + ")" : "")
|
||||
+ ")"
|
||||
);
|
||||
@ -270,8 +269,10 @@ public abstract class SQLDB extends Database {
|
||||
+ "version integer NOT NULL"
|
||||
+ ")"
|
||||
);
|
||||
if (newDatabase) {
|
||||
setVersion(2);
|
||||
}
|
||||
int version = getVersion();
|
||||
version = 1;
|
||||
if (version < 1) {
|
||||
String[] queries = new String[]{
|
||||
"ALTER TABLE " + userName + " ADD " + userColumnDeaths + " integer NOT NULL DEFAULT 0",
|
||||
@ -288,7 +289,6 @@ public abstract class SQLDB extends Database {
|
||||
setVersion(1);
|
||||
} else if (version < 2) {
|
||||
String[] queries = new String[]{
|
||||
"ALTER TABLE " + userName + " DROP INDEX " + userColumnPlayerKills,
|
||||
"ALTER TABLE " + nicknamesName + " ADD " + nicknamesColumnCurrent + " boolean NOT NULL DEFAULT 0",
|
||||
"DROP TABLE IF EXISTS " + serverdataName
|
||||
};
|
||||
@ -298,42 +298,36 @@ public abstract class SQLDB extends Database {
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
if (usingMySQL) {
|
||||
query("ALTER TABLE " + userName + " DROP INDEX " + userColumnPlayerKills);
|
||||
}
|
||||
setVersion(2);
|
||||
}
|
||||
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
protected abstract Connection getNewConnection();
|
||||
|
||||
public boolean query(String sql) throws SQLException {
|
||||
return connection.createStatement().execute(sql);
|
||||
boolean success = connection.createStatement().execute(sql);
|
||||
return success;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
try {
|
||||
public void close() throws SQLException {
|
||||
if (connection != null) {
|
||||
connection.close();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getVersion() {
|
||||
checkConnection();
|
||||
|
||||
int version = 0;
|
||||
|
||||
try {
|
||||
ResultSet set = connection.prepareStatement("SELECT * from " + versionName).executeQuery();
|
||||
checkConnection();
|
||||
ResultSet set = connection.prepareStatement("SELECT * FROM " + versionName).executeQuery();
|
||||
|
||||
if (set.next()) {
|
||||
version = set.getInt("version");
|
||||
@ -343,34 +337,37 @@ public abstract class SQLDB extends Database {
|
||||
|
||||
return version;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
||||
plugin.toLog(this.getClass().getName(), e);
|
||||
return version;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setVersion(int version) {
|
||||
public void setVersion(int version) throws SQLException {
|
||||
checkConnection();
|
||||
|
||||
try {
|
||||
connection.prepareStatement("DELETE FROM " + versionName).executeUpdate();
|
||||
|
||||
connection.prepareStatement("INSERT INTO " + versionName + " (version) VALUES (" + version + ")").executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean wasSeenBefore(UUID uuid) {
|
||||
try {
|
||||
return getUserId(uuid.toString()) != -1;
|
||||
} catch (SQLException e) {
|
||||
plugin.toLog(this.getClass().getName(), e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getUserId(String uuid) {
|
||||
public int getUserId(String uuid) throws SQLException {
|
||||
int userId = -1;
|
||||
try {
|
||||
checkConnection();
|
||||
} catch (Exception e) {
|
||||
plugin.toLog(this.getClass().getName(), e);
|
||||
}
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT " + userColumnID + " FROM " + userName + " WHERE UPPER(" + userColumnUUID + ") LIKE UPPER(?)");
|
||||
statement.setString(1, uuid);
|
||||
ResultSet set = statement.executeQuery();
|
||||
@ -378,15 +375,16 @@ public abstract class SQLDB extends Database {
|
||||
userId = set.getInt(userColumnID);
|
||||
}
|
||||
set.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return userId;
|
||||
}
|
||||
|
||||
private UUID getUserUUID(String userID) {
|
||||
UUID uuid = null;
|
||||
private UUID getUserUUID(String userID) throws SQLException {
|
||||
try {
|
||||
checkConnection();
|
||||
} catch (Exception e) {
|
||||
plugin.toLog(this.getClass().getName(), e);
|
||||
}
|
||||
UUID uuid = null;
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT " + userColumnUUID + " FROM " + userName + " WHERE UPPER(" + userColumnID + ") LIKE UPPER(?)");
|
||||
statement.setString(1, userID);
|
||||
ResultSet set = statement.executeQuery();
|
||||
@ -394,40 +392,32 @@ public abstract class SQLDB extends Database {
|
||||
uuid = UUID.fromString(set.getString(userColumnUUID));
|
||||
}
|
||||
set.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return uuid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<UUID> getSavedUUIDs() {
|
||||
public Set<UUID> getSavedUUIDs() throws SQLException {
|
||||
Set<UUID> uuids = new HashSet<>();
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT " + userColumnUUID + " FROM " + userName);
|
||||
ResultSet set = statement.executeQuery();
|
||||
while (set.next()) {
|
||||
uuids.add(UUID.fromString(set.getString(userColumnUUID)));
|
||||
}
|
||||
set.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return uuids;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveCommandUse(HashMap<String, Integer> data) {
|
||||
public void saveCommandUse(HashMap<String, Integer> data) throws SQLException {
|
||||
if (data.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement(
|
||||
"DELETE FROM " + commanduseName);
|
||||
statement.execute();
|
||||
statement.close();
|
||||
|
||||
connection.setAutoCommit(false);
|
||||
|
||||
statement = connection.prepareStatement("INSERT INTO " + commanduseName + " ("
|
||||
+ commanduseColumnCommand + ", "
|
||||
+ commanduseColumnTimesUsed
|
||||
@ -440,23 +430,19 @@ public abstract class SQLDB extends Database {
|
||||
statement.addBatch();
|
||||
commitRequired = true;
|
||||
}
|
||||
statement.executeBatch();
|
||||
statement.close();
|
||||
if (commitRequired) {
|
||||
connection.commit();
|
||||
}
|
||||
}
|
||||
connection.setAutoCommit(true);
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
if (commitRequired) {
|
||||
statement.executeBatch();
|
||||
|
||||
}
|
||||
statement.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public HashMap<String, Integer> getCommandUse() {
|
||||
public HashMap<String, Integer> getCommandUse() throws SQLException {
|
||||
HashMap<String, Integer> commandUse = new HashMap<>();
|
||||
try {
|
||||
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + commanduseName);
|
||||
|
||||
ResultSet set = statement.executeQuery();
|
||||
@ -466,23 +452,23 @@ public abstract class SQLDB extends Database {
|
||||
}
|
||||
set.close();
|
||||
statement.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return commandUse;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeAccount(String uuid) {
|
||||
|
||||
public boolean removeAccount(String uuid) throws SQLException {
|
||||
try {
|
||||
checkConnection();
|
||||
} catch (Exception e) {
|
||||
plugin.toLog(this.getClass().getName(), e);
|
||||
return false;
|
||||
}
|
||||
|
||||
int userId = getUserId(uuid);
|
||||
if (userId == -1) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
PreparedStatement statement;
|
||||
try {
|
||||
statement = connection.prepareStatement("DELETE FROM " + locationName + " WHERE UPPER(" + locationColumnUserID + ") LIKE UPPER(?)");
|
||||
statement.setString(1, "" + userId);
|
||||
statement.execute();
|
||||
@ -512,14 +498,19 @@ public abstract class SQLDB extends Database {
|
||||
statement.setString(1, uuid);
|
||||
statement.execute();
|
||||
statement.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void giveUserDataToProcessors(UUID uuid, DBCallableProcessor... processors) {
|
||||
public void giveUserDataToProcessors(UUID uuid, DBCallableProcessor... processors) throws SQLException {
|
||||
try {
|
||||
checkConnection();
|
||||
} catch (Exception e) {
|
||||
plugin.toLog("Preparing for Exception report - Processors: " + Arrays.toString(processors));
|
||||
plugin.toLog(this.getClass().getName(), e);
|
||||
return;
|
||||
}
|
||||
// Check if user is in the database
|
||||
if (!wasSeenBefore(uuid)) {
|
||||
plugin.logError(uuid + " was not found from the database!");
|
||||
@ -533,7 +524,7 @@ public abstract class SQLDB extends Database {
|
||||
}
|
||||
// Get the data
|
||||
UserData data = new UserData(getOfflinePlayer(uuid), new DemographicsData(), this);
|
||||
try {
|
||||
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + userName + " WHERE UPPER(" + userColumnUUID + ") LIKE UPPER(?)");
|
||||
statement.setString(1, uuid.toString());
|
||||
ResultSet set = statement.executeQuery();
|
||||
@ -579,9 +570,6 @@ public abstract class SQLDB extends Database {
|
||||
for (DBCallableProcessor processor : processors) {
|
||||
processor.process(data);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private HashMap<GameMode, Long> getGMTimes(String userId) throws SQLException {
|
||||
@ -615,7 +603,7 @@ public abstract class SQLDB extends Database {
|
||||
while (set.next()) {
|
||||
try {
|
||||
ips.add(InetAddress.getByName(set.getString(ipsColumnIP)));
|
||||
} catch (SQLException | UnknownHostException e) {
|
||||
} catch (UnknownHostException e) {
|
||||
}
|
||||
}
|
||||
set.close();
|
||||
@ -631,10 +619,7 @@ public abstract class SQLDB extends Database {
|
||||
set = statement.executeQuery();
|
||||
List<SessionData> sessions = new ArrayList<>();
|
||||
while (set.next()) {
|
||||
try {
|
||||
sessions.add(new SessionData(set.getLong(sessionColumnSessionStart), set.getLong(sessionColumnSessionEnd)));
|
||||
} catch (SQLException e) {
|
||||
}
|
||||
}
|
||||
set.close();
|
||||
statement.close();
|
||||
@ -705,6 +690,7 @@ public abstract class SQLDB extends Database {
|
||||
if (data.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
Set<Exception> exceptions = new HashSet<>();
|
||||
List<UserData> saveLast = new ArrayList<>();
|
||||
String uSQL = "UPDATE " + userName + " SET "
|
||||
+ userColumnDemAge + "=?, "
|
||||
@ -718,19 +704,22 @@ public abstract class SQLDB extends Database {
|
||||
+ userColumnDeaths + "=?, "
|
||||
+ userColumnMobKills + "=? "
|
||||
+ "WHERE UPPER(" + userColumnUUID + ") LIKE UPPER(?)";
|
||||
|
||||
try {
|
||||
connection.setAutoCommit(false);
|
||||
PreparedStatement uStatement = connection.prepareStatement(uSQL);
|
||||
boolean commitRequired = false;
|
||||
transactionsActive++;
|
||||
for (UserData uData : data) {
|
||||
try {
|
||||
if (uData == null) {
|
||||
throw new IllegalStateException("UserData is null somehow!");
|
||||
}
|
||||
uData.access();
|
||||
int userId = getUserId(uData.getUuid().toString());
|
||||
if (userId == -1) {
|
||||
saveLast.add(uData);
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
|
||||
uStatement.setInt(1, uData.getDemData().getAge());
|
||||
uStatement.setString(2, uData.getDemData().getGender().toString().toLowerCase());
|
||||
uStatement.setString(3, uData.getDemData().getGeoLocation());
|
||||
@ -751,49 +740,58 @@ public abstract class SQLDB extends Database {
|
||||
} catch (SQLException | NullPointerException e) {
|
||||
saveLast.add(uData);
|
||||
uData.stopAccessing();
|
||||
exceptions.add(e);
|
||||
continue;
|
||||
}
|
||||
uData.stopAccessing();
|
||||
commitRequired = true;
|
||||
}
|
||||
uStatement.executeBatch();
|
||||
uStatement.close();
|
||||
transactionsActive--;
|
||||
if (commitRequired) {
|
||||
connection.commit();
|
||||
}
|
||||
if (noActiveTransaction()) {
|
||||
connection.setAutoCommit(true);
|
||||
uStatement.executeBatch();
|
||||
|
||||
}
|
||||
uStatement.close();
|
||||
data.removeAll(saveLast);
|
||||
} catch (Exception ex) {
|
||||
exceptions.add(ex);
|
||||
}
|
||||
for (UserData uData : data) {
|
||||
if (uData == null) {
|
||||
throw new IllegalStateException("UserData is null somehow!");
|
||||
}
|
||||
uData.access();
|
||||
try {
|
||||
int userId = getUserId(uData.getUuid().toString());
|
||||
saveLocationList(userId, uData.getLocations());
|
||||
saveNickList(userId, uData.getNicknames(), uData.getLastNick());
|
||||
saveIPList(userId, uData.getIps());
|
||||
saveSessionList(userId, uData.getSessions());
|
||||
savePlayerKills(userId, uData.getPlayerKills());
|
||||
if (noActiveTransaction()) {
|
||||
connection.setAutoCommit(true);
|
||||
}
|
||||
|
||||
saveGMTimes(userId, uData.getGmTimes());
|
||||
} catch (Exception e) {
|
||||
exceptions.add(e);
|
||||
}
|
||||
uData.stopAccessing();
|
||||
}
|
||||
for (UserData userData : saveLast) {
|
||||
try {
|
||||
saveUserData(userData.getUuid(), userData);
|
||||
} catch (SQLException e) {
|
||||
exceptions.add(e);
|
||||
}
|
||||
} catch (SQLException | NullPointerException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (!exceptions.isEmpty()) {
|
||||
plugin.logError("SEVERE: MULTIPLE ERRORS OCCURRED: " + exceptions.size());
|
||||
plugin.toLog(this.getClass().getName(), exceptions);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveUserData(UUID uuid, UserData data) {
|
||||
public void saveUserData(UUID uuid, UserData data) throws SQLException {
|
||||
checkConnection();
|
||||
data.access();
|
||||
int userId = getUserId(uuid.toString());
|
||||
try {
|
||||
int update = 0;
|
||||
if (userId != -1) {
|
||||
String sql = "UPDATE " + userName + " SET "
|
||||
@ -864,33 +862,26 @@ public abstract class SQLDB extends Database {
|
||||
statement.close();
|
||||
userId = getUserId(uuid.toString());
|
||||
}
|
||||
connection.setAutoCommit(false);
|
||||
|
||||
saveLocationList(userId, data.getLocations());
|
||||
saveNickList(userId, data.getNicknames(), data.getLastNick());
|
||||
saveIPList(userId, data.getIps());
|
||||
saveSessionList(userId, data.getSessions());
|
||||
savePlayerKills(userId, data.getPlayerKills());
|
||||
if (noActiveTransaction()) {
|
||||
connection.setAutoCommit(true);
|
||||
}
|
||||
saveGMTimes(userId, data.getGmTimes());
|
||||
} catch (SQLException | NullPointerException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
data.stopAccessing();
|
||||
}
|
||||
|
||||
public void saveLocationList(int userId, List<Location> locations) {
|
||||
public void saveLocationList(int userId, List<Location> locations) throws SQLException {
|
||||
if (locations.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
transactionsActive++;
|
||||
try {
|
||||
PreparedStatement deleteStatement = connection.prepareStatement(
|
||||
"DELETE FROM " + locationName + " WHERE UPPER(" + locationColumnUserID + ") LIKE UPPER(?)");
|
||||
deleteStatement.setString(1, "" + userId);
|
||||
deleteStatement.execute();
|
||||
deleteStatement.close();
|
||||
|
||||
PreparedStatement saveStatement = connection.prepareStatement("INSERT INTO " + locationName + " ("
|
||||
+ locationColumnUserID + ", "
|
||||
+ locationColumnCoordinatesX + ", "
|
||||
@ -911,29 +902,23 @@ public abstract class SQLDB extends Database {
|
||||
saveStatement.addBatch();
|
||||
commitRequired = true;
|
||||
}
|
||||
saveStatement.executeBatch();
|
||||
if (commitRequired) {
|
||||
connection.commit();
|
||||
saveStatement.executeBatch();
|
||||
|
||||
}
|
||||
}
|
||||
saveStatement.close();
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
transactionsActive--;
|
||||
}
|
||||
|
||||
public void saveNickList(int userId, HashSet<String> names, String lastNick) {
|
||||
public void saveNickList(int userId, HashSet<String> names, String lastNick) throws SQLException {
|
||||
if (names.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
transactionsActive++;
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement(
|
||||
"DELETE FROM " + nicknamesName + " WHERE UPPER(" + nicknamesColumnUserID + ") LIKE UPPER(?)");
|
||||
statement.setString(1, "" + userId);
|
||||
statement.execute();
|
||||
|
||||
statement = connection.prepareStatement("INSERT INTO " + nicknamesName + " ("
|
||||
+ nicknamesColumnUserID + ", "
|
||||
+ nicknamesColumnCurrent + ", "
|
||||
@ -947,28 +932,22 @@ public abstract class SQLDB extends Database {
|
||||
statement.addBatch();
|
||||
commitRequired = true;
|
||||
}
|
||||
statement.executeBatch();
|
||||
if (commitRequired) {
|
||||
connection.commit();
|
||||
statement.executeBatch();
|
||||
|
||||
}
|
||||
statement.close();
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
transactionsActive--;
|
||||
}
|
||||
|
||||
public void saveSessionList(int userId, List<SessionData> sessions) {
|
||||
public void saveSessionList(int userId, List<SessionData> sessions) throws SQLException {
|
||||
if (sessions.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
transactionsActive++;
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement(
|
||||
"DELETE FROM " + sessionName + " WHERE UPPER(" + sessionColumnUserID + ") LIKE UPPER(?)");
|
||||
statement.setString(1, "" + userId);
|
||||
statement.execute();
|
||||
|
||||
statement = connection.prepareStatement("INSERT INTO " + sessionName + " ("
|
||||
+ sessionColumnUserID + ", "
|
||||
+ sessionColumnSessionStart + ", "
|
||||
@ -982,28 +961,22 @@ public abstract class SQLDB extends Database {
|
||||
statement.addBatch();
|
||||
commitRequired = true;
|
||||
}
|
||||
statement.executeBatch();
|
||||
if (commitRequired) {
|
||||
connection.commit();
|
||||
statement.executeBatch();
|
||||
|
||||
}
|
||||
statement.close();
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
transactionsActive--;
|
||||
}
|
||||
|
||||
public void savePlayerKills(int userId, List<KillData> kills) {
|
||||
public void savePlayerKills(int userId, List<KillData> kills) throws SQLException {
|
||||
if (kills.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
transactionsActive++;
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement(
|
||||
"DELETE FROM " + killsName + " WHERE UPPER(" + killsColumnKillerUserID + ") LIKE UPPER(?)");
|
||||
statement.setString(1, "" + userId);
|
||||
statement.execute();
|
||||
|
||||
statement = connection.prepareStatement("INSERT INTO " + killsName + " ("
|
||||
+ killsColumnKillerUserID + ", "
|
||||
+ killsColumnVictimUserID + ", "
|
||||
@ -1019,24 +992,17 @@ public abstract class SQLDB extends Database {
|
||||
statement.addBatch();
|
||||
commitRequired = true;
|
||||
}
|
||||
statement.executeBatch();
|
||||
if (commitRequired) {
|
||||
connection.commit();
|
||||
statement.executeBatch();
|
||||
|
||||
}
|
||||
statement.close();
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
transactionsActive--;
|
||||
}
|
||||
|
||||
public void saveIPList(int userId, HashSet<InetAddress> ips) {
|
||||
public void saveIPList(int userId, HashSet<InetAddress> ips) throws SQLException {
|
||||
if (ips.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
transactionsActive++;
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement(
|
||||
"DELETE FROM " + ipsName + " WHERE UPPER(" + ipsColumnUserID + ") LIKE UPPER(?)");
|
||||
statement.setString(1, "" + userId);
|
||||
@ -1054,23 +1020,17 @@ public abstract class SQLDB extends Database {
|
||||
statement.addBatch();
|
||||
commitRequired = true;
|
||||
}
|
||||
statement.executeBatch();
|
||||
if (commitRequired) {
|
||||
connection.commit();
|
||||
statement.executeBatch();
|
||||
|
||||
}
|
||||
statement.close();
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
transactionsActive--;
|
||||
}
|
||||
|
||||
public void saveGMTimes(int userId, HashMap<GameMode, Long> gamemodeTimes) {
|
||||
public void saveGMTimes(int userId, HashMap<GameMode, Long> gamemodeTimes) throws SQLException {
|
||||
if (gamemodeTimes.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement(
|
||||
"DELETE FROM " + gamemodetimesName + " WHERE UPPER(" + gamemodetimesColumnUserID + ") LIKE UPPER(?)");
|
||||
statement.setString(1, "" + userId);
|
||||
@ -1102,37 +1062,39 @@ public abstract class SQLDB extends Database {
|
||||
statement.execute();
|
||||
statement.close();
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clean() {
|
||||
checkConnection();
|
||||
try {
|
||||
checkConnection();
|
||||
query("DROP TABLE " + serverdataName);
|
||||
} catch (Exception e) {
|
||||
|
||||
} catch (SQLException e) {
|
||||
plugin.toLog(this.getClass().getName(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeAllData() {
|
||||
checkConnection();
|
||||
|
||||
public boolean removeAllData() {
|
||||
try {
|
||||
query("DROP TABLE " + locationName);
|
||||
query("DROP TABLE " + ipsName);
|
||||
query("DROP TABLE " + gamemodetimesName);
|
||||
query("DROP TABLE " + nicknamesName);
|
||||
query("DROP TABLE " + killsName);
|
||||
query("DROP TABLE " + sessionName);
|
||||
query("DROP TABLE " + commanduseName);
|
||||
query("DROP TABLE " + userName);
|
||||
checkConnection();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
plugin.toLog(this.getClass().getName(), e);
|
||||
return false;
|
||||
}
|
||||
String[] queries = new String[]{
|
||||
locationName, ipsName, gamemodetimesName, nicknamesName, killsName, sessionName, commanduseName, userName
|
||||
};
|
||||
boolean success = true;
|
||||
for (String tableName : queries) {
|
||||
try {
|
||||
query("DROP TABLE " + tableName);
|
||||
} catch (SQLException e) {
|
||||
plugin.toLog(this.getClass().getName(), e);
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
public boolean supportsModification() {
|
||||
@ -1142,9 +1104,4 @@ public abstract class SQLDB extends Database {
|
||||
public Connection getConnection() {
|
||||
return connection;
|
||||
}
|
||||
|
||||
private boolean noActiveTransaction() {
|
||||
return transactionsActive == 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -44,8 +44,9 @@ public class SQLiteDB extends SQLDB {
|
||||
public Connection getNewConnection(String dbName) {
|
||||
try {
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
Connection connection = DriverManager.getConnection("jdbc:sqlite:" + new File(plugin.getDataFolder(), dbName+".db").getAbsolutePath());
|
||||
|
||||
return DriverManager.getConnection("jdbc:sqlite:" + new File(plugin.getDataFolder(), dbName+".db").getAbsolutePath());
|
||||
return connection;
|
||||
} catch (ClassNotFoundException | SQLException e) {
|
||||
return null;
|
||||
}
|
||||
|
@ -12,7 +12,6 @@ import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.stream.Collectors;
|
||||
import main.java.com.djrapitops.plan.Phrase;
|
||||
import main.java.com.djrapitops.plan.Plan;
|
||||
import main.java.com.djrapitops.plan.data.SessionData;
|
||||
|
@ -84,7 +84,7 @@ public class Analysis {
|
||||
}
|
||||
// Create empty Dataset
|
||||
final RawAnalysisData sorted = new RawAnalysisData();
|
||||
sorted.setCommandUse(plugin.getDB().getCommandUse());
|
||||
sorted.setCommandUse(plugin.getHandler().getCommandUse());
|
||||
log(Phrase.ANALYSIS_BEGIN_ANALYSIS + "");
|
||||
AnalysisData analysisData = new AnalysisData();
|
||||
|
||||
@ -134,6 +134,7 @@ public class Analysis {
|
||||
sorted.getRegistered().add(uData.getRegistered());
|
||||
} catch (NullPointerException e) {
|
||||
plugin.logError(Phrase.DATA_CORRUPTION_WARN.parse(uData.getUuid() + ""));
|
||||
plugin.toLog(this.getClass().getName(), e);
|
||||
}
|
||||
});
|
||||
|
||||
@ -237,12 +238,16 @@ public class Analysis {
|
||||
private List<UUID> fetchPlayersInDB() {
|
||||
final List<UUID> uuids = new ArrayList<>();
|
||||
log(Phrase.ANALYSIS_FETCH_PLAYERS + "");
|
||||
try {
|
||||
Set<UUID> savedUUIDs = plugin.getDB().getSavedUUIDs();
|
||||
savedUUIDs.parallelStream()
|
||||
.filter((uuid) -> (getOfflinePlayer(uuid).hasPlayedBefore()))
|
||||
.forEach((uuid) -> {
|
||||
uuids.add(uuid);
|
||||
});
|
||||
} catch (Exception e) {
|
||||
plugin.toLog(this.getClass().getName(), e);
|
||||
}
|
||||
return uuids;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user