Deleted KillHandling, Cleaned Database RemoveUser methods

This commit is contained in:
Rsl1122 2017-08-23 13:56:44 +03:00
parent f635ccbf0a
commit c205a85a6a
14 changed files with 46 additions and 221 deletions

View File

@ -54,8 +54,6 @@ import org.bukkit.ChatColor;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
/**
* Main class for Bukkit that manages the plugin.
@ -263,18 +261,11 @@ public class Plan extends BukkitPlugin<Plan> {
getServer().getScheduler().cancelTasks(this);
if (Verify.notNull(dataCache, db)) {
Benchmark.start("Disable: DataCache Save");
// Saves the DataCache to the database without Bukkit's Schedulers.
Log.info(Locale.get(Msg.DISABLE_CACHE_SAVE).toString());
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.execute(() -> {
dataCache.saveCacheOnDisable();
taskStatus().cancelAllKnownTasks();
Benchmark.stop("Disable: DataCache Save");
});
scheduler.shutdown(); // Schedules the save to shutdown after it has ran the execute method.
// TODO Process all leftover Processors.
taskStatus().cancelAllKnownTasks();
}
getPluginLogger().endAllDebugs();

View File

@ -1,21 +1,19 @@
package main.java.com.djrapitops.plan.data.cache;
import com.djrapitops.plugin.task.AbsRunnable;
import com.djrapitops.plugin.utilities.player.IPlayer;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.TPS;
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.queue.processing.Processor;
import main.java.com.djrapitops.plan.utilities.Benchmark;
import main.java.com.djrapitops.plan.utilities.MiscUtils;
import main.java.com.djrapitops.plan.utilities.analysis.MathUtils;
import org.bukkit.entity.Player;
import java.sql.SQLException;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* This Class contains the Cache.
@ -30,8 +28,6 @@ import java.util.*;
*/
public class DataCache extends SessionCache {
// Plan
private final Plan plugin;
private final Database db;
//Cache
@ -53,9 +49,7 @@ public class DataCache extends SessionCache {
* @param plugin Current instance of Plan
*/
public DataCache(Plan plugin) {
super(); // Initializes Session & Location cache.
this.plugin = plugin;
super(plugin); // Initializes Session & Location cache.
db = plugin.getDB();
commandUse = new HashMap<>();
@ -115,47 +109,6 @@ public class DataCache extends SessionCache {
}).runTaskTimerAsynchronously(60L * 20L * 5, 60L * 20L * 5);
}
/**
* Saves all data in the cache to Database and closes the database down.
* <p>
* Stops all tasks.
* <p>
* If processingQueue has unprocessed information, it will be processed.
*/
@Deprecated
public void saveCacheOnDisable() { // TODO Rewrite
long time = MiscUtils.getTime();
Benchmark.start("Cache: SaveOnDisable");
Benchmark.start("Cache: ProcessOnlineHandlingInfo");
List<IPlayer> onlinePlayers = plugin.fetch().getOnlinePlayers();
Log.debug("Online: " + onlinePlayers.size());
for (IPlayer p : onlinePlayers) {
UUID uuid = p.getUuid();
endSession(uuid);
String worldName = ((Player) p.getWrappedPlayerClass()).getWorld().getName();
}
Benchmark.stop("Cache: ProcessOnlineHandlingInfo");
try {
db.saveCommandUse(commandUse);
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}
saveUnsavedTPSHistory();
try {
db.close();
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}
Benchmark.stop("Cache: SaveOnDisable");
}
private void processUnprocessedHandlingInfo(List<Processor> toProcess) {
Log.debug("PROCESS: " + toProcess.size());
for (Processor i : toProcess) {
i.process();
}
}
/**
* Saves the cached CommandUse.
* <p>

View File

@ -1,57 +0,0 @@
package main.java.com.djrapitops.plan.data.handling;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.UserData;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import java.sql.SQLException;
import java.util.UUID;
/**
* Class containing static methods for processing information contained in a
* DeathEvent when the killer is a player.
*
* @author Rsl1122
* @since 3.0.0
*/
public class KillHandling {
/**
* Utility Class, hides constructor.
*/
private KillHandling() {
throw new IllegalStateException("Utility Class.");
}
/**
* Processes the information of the Event and changes UserData object
* accordingly.
*
* @param data UserData of the player.
* @param time Epoch ms the event occurred.
* @param dead Mob or a Player the player killed.
* @param weaponName The name of the Weapon used.
*/
public static void processKillInfo(UserData data, long time, LivingEntity dead, String weaponName) {
Plan plugin = Plan.getInstance();
if (dead instanceof Player) {
Player deadPlayer = (Player) dead;
int victimID;
try {
UUID victimUUID = deadPlayer.getUniqueId();
victimID = plugin.getDB().getUsersTable().getUserId(victimUUID);
if (victimID == -1) {
return;
}
//TODO Move to Session data.addPlayerKill(new KillData(victimUUID, victimID, weaponName, time));
} catch (SQLException e) {
Log.toLog("main.java.com.djrapitops.plan.KillHandling", e);
}
} else {
//TODO Move to Session data.setMobKills(data.getMobKills() + 1);
}
}
}

View File

@ -191,7 +191,7 @@ public abstract class Database {
* @return Success of the removal.
* @throws SQLException If a database error occurs.
*/
public abstract boolean removeAccount(String uuid) throws SQLException;
public abstract boolean removeAccount(UUID uuid) throws SQLException;
/**
* Used to clear all data from the database.

View File

@ -21,7 +21,7 @@ public class MySQLDB extends SQLDB {
* @param plugin Current instance of Plan
*/
public MySQLDB(Plan plugin) {
super(plugin, true);
super(plugin);
}
/**

View File

@ -12,8 +12,10 @@ import main.java.com.djrapitops.plan.utilities.MiscUtils;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import java.util.stream.Collectors;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
/**
* Class containing main logic for different data related save & load functionality.
@ -23,18 +25,15 @@ import java.util.stream.Collectors;
*/
public abstract class SQLDB extends Database {
private final boolean supportsModification;
private final boolean usingMySQL;
private Connection connection;
/**
* @param plugin
* @param supportsModification
*/
public SQLDB(Plan plugin, boolean supportsModification) {
public SQLDB(Plan plugin) {
super(plugin);
this.supportsModification = supportsModification;
usingMySQL = getName().equals("MySQL");
versionTable = new VersionTable(this, usingMySQL);
@ -252,7 +251,7 @@ public abstract class SQLDB extends Database {
return false;
}
try {
return usersTable.getUserId(uuid.toString()) != -1;
return usersTable.isRegistered(uuid);
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
return false;
@ -261,39 +260,34 @@ public abstract class SQLDB extends Database {
}
}
/**
* @param uuid
* @return
* @throws SQLException
*/
@Override
public boolean removeAccount(String uuid) throws SQLException {
if (uuid == null || uuid.isEmpty()) {
public boolean removeAccount(UUID uuid) throws SQLException {
if (uuid == null) {
return false;
}
try {
Benchmark.start("Remove Account");
Log.debug("Database", "Removing Account: " + uuid);
try {
checkConnection();
} catch (Exception e) {
Log.toLog(this.getClass().getName(), e);
return false;
checkConnection();
boolean success = true;
for (Table t : getAllTablesInRemoveOrder()) {
if (!success) {
continue;
}
if (t instanceof UserIDTable) {
UserIDTable table = (UserIDTable) t;
success = table.removeUser(uuid);
}
}
int userId = usersTable.getUserId(uuid);
boolean success = userId != -1
&& ipsTable.removeUserIPs(userId)
&& nicknamesTable.removeUserNicknames(userId)
&& killsTable.removeUserKillsAndVictims(userId)
&& worldTimesTable.removeUserWorldTimes(userId)
&& sessionsTable.removeUserSessions(userId)
&& usersTable.removeUser(uuid);
if (success) {
commit();
} else {
rollback();
return true;
}
return success;
throw new IllegalStateException("Removal Failed");
} catch (Exception e) {
Log.toLog(this.getClass().getName(), e);
rollback(); // TODO Test case
return false;
} finally {
Benchmark.stop("Database", "Remove Account");
setAvailable();
@ -346,33 +340,8 @@ public abstract class SQLDB extends Database {
if (uuidsCol == null || uuidsCol.isEmpty()) {
return new ArrayList<>();
}
setStatus("Get userdata (multiple) for: " + uuidsCol.size());
Benchmark.start("Get UserData for " + uuidsCol.size());
Map<UUID, Integer> userIds = usersTable.getAllUserIds();
Set<UUID> remove = uuidsCol.stream()
.filter(uuid -> !userIds.containsKey(uuid))
.collect(Collectors.toSet());
List<UUID> uuids = new ArrayList<>(uuidsCol);
Log.debug("Database", "Data not found for: " + remove.size());
uuids.removeAll(remove);
Benchmark.start("Create UserData objects for " + userIds.size());
List<UserData> data = usersTable.getUserData(uuids);
Benchmark.stop("Database", "Create UserData objects for " + userIds.size());
if (data.isEmpty()) {
return data;
}
// TODO REWRITE
Benchmark.stop("Database", "Get UserData for " + uuidsCol.size());
setAvailable();
return data;
}
/**
* @return
*/
public boolean supportsModification() {
return supportsModification;
return new ArrayList<>();
}
/**

View File

@ -28,7 +28,7 @@ public class SQLiteDB extends SQLDB {
* @param dbName
*/
public SQLiteDB(Plan plugin, String dbName) {
super(plugin, false);
super(plugin);
this.dbName = dbName;
}

View File

@ -53,14 +53,6 @@ public class IPsTable extends UserIDTable {
}
}
/**
* @param userId The User ID from which the IPs should be removed from
* @return if the IPs were removed successfully
*/
public boolean removeUserIPs(int userId) {
return super.removeDataOf(userId);
}
/**
* @param uuid UUID of the user.
* @return Users's Login Geolocations.

View File

@ -61,16 +61,15 @@ public class KillsTable extends UserIDTable {
}
}
/**
* @param userId
* @return
*/
public boolean removeUserKillsAndVictims(int userId) {
@Override
public boolean removeUser(UUID uuid) {
PreparedStatement statement = null;
try {
statement = prepareStatement("DELETE FROM " + tableName + " WHERE " + columnKillerUserID + " = ? OR " + columnVictimUserID + " = ?");
statement.setInt(1, userId);
statement.setInt(2, userId);
statement = prepareStatement("DELETE FROM " + tableName +
" WHERE " + columnKillerUserID + " = " + usersTable.statementSelectID +
" OR " + columnVictimUserID + " = " + usersTable.statementSelectID);
statement.setString(1, uuid.toString());
statement.setString(2, uuid.toString());
statement.execute();
return true;
} catch (SQLException ex) {

View File

@ -54,14 +54,6 @@ public class NicknamesTable extends UserIDTable {
}
}
/**
* @param userId The User ID whose the nicknames should be removed
* @return if the removal was successful
*/
public boolean removeUserNicknames(int userId) {
return super.removeDataOf(userId);
}
/**
* Get ALL nicknames of the user.
* <p>

View File

@ -69,17 +69,6 @@ public class SessionsTable extends UserIDTable {
}
}
/**
* Removes User's Sessions from the Database.
* <p>
*
* @param userId
* @return
*/
public boolean removeUserSessions(int userId) {
return super.removeDataOf(userId);
}
/**
* Used to save a session, with all it's information into the database.
* <p>

View File

@ -39,7 +39,7 @@ public abstract class UserIDTable extends Table {
}
}
protected boolean removeDataOf(UUID uuid) {
public boolean removeUser(UUID uuid) {
PreparedStatement statement = null;
try {
statement = prepareStatement("DELETE FROM " + tableName +

View File

@ -13,7 +13,7 @@ import java.util.*;
/**
* @author Rsl1122
*/
public class UsersTable extends Table {
public class UsersTable extends UserIDTable {
private final String columnID = "id";
private final String columnUUID = "uuid";
@ -78,6 +78,7 @@ public class UsersTable extends Table {
* @param uuid
* @return
*/
@Override
public boolean removeUser(UUID uuid) {
PreparedStatement statement = null;
try {

View File

@ -69,10 +69,6 @@ public class WorldTimesTable extends UserIDTable {
}
}
public boolean removeUserWorldTimes(int userId) {
return super.removeDataOf(userId);
}
public void saveWorldTimes(UUID uuid, long sessionID, WorldTimes worldTimes) throws SQLException {
if (Verify.isEmpty(worldTimes.getWorldTimes())) {
return;