Construct PlayerProfiles in the database manager

- New method, DatabaseManager.loadPlayerProfile(String playerName)
 - PlayerProfile no longer has magic numbers that were mapped from two different sets of magic numbers, they have migrated to the DatabaseManagers where they belong
 - Will now be possible to convert SQL -> flatfile, once command is added
This commit is contained in:
riking 2013-06-08 18:56:56 -07:00
parent 98b98256b7
commit 58e8fd4691
14 changed files with 415 additions and 314 deletions

View File

@ -4,6 +4,7 @@ import java.util.Set;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.api.exceptions.InvalidPlayerException; import com.gmail.nossr50.api.exceptions.InvalidPlayerException;
import com.gmail.nossr50.api.exceptions.InvalidSkillException; import com.gmail.nossr50.api.exceptions.InvalidSkillException;
import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.Config;
@ -552,7 +553,7 @@ public final class ExperienceAPI {
} }
private static PlayerProfile getOfflineProfile(String playerName) { private static PlayerProfile getOfflineProfile(String playerName) {
PlayerProfile profile = new PlayerProfile(playerName, false); PlayerProfile profile = mcMMO.getDatabaseManager().loadPlayerProfile(playerName, false);
if (!profile.isLoaded()) { if (!profile.isLoaded()) {
throw new InvalidPlayerException(); throw new InvalidPlayerException();

View File

@ -22,7 +22,7 @@ public class McremoveCommand implements TabExecutor {
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
switch (args.length) { switch (args.length) {
case 1: case 1:
if (UserManager.getPlayer(args[0]) == null && CommandUtils.unloadedProfile(sender, new PlayerProfile(args[0], false))) { if (UserManager.getPlayer(args[0]) == null && CommandUtils.unloadedProfile(sender, mcMMO.getDatabaseManager().loadPlayerProfile(args[0], false))) {
return true; return true;
} }

View File

@ -10,7 +10,7 @@ import org.bukkit.entity.Player;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.runnables.database.SQLConversionTask; import com.gmail.nossr50.runnables.database.FromFlatfileConversionTask;
import com.gmail.nossr50.util.player.UserManager; import com.gmail.nossr50.util.player.UserManager;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
@ -28,7 +28,7 @@ public class MmoupdateCommand implements TabExecutor {
sender.sendMessage(LocaleLoader.getString("Commands.mmoupdate.Start")); sender.sendMessage(LocaleLoader.getString("Commands.mmoupdate.Start"));
UserManager.saveAll(); UserManager.saveAll();
UserManager.clearAll(); UserManager.clearAll();
new SQLConversionTask().runTaskLaterAsynchronously(mcMMO.p, 1); new FromFlatfileConversionTask().runTaskAsynchronously(mcMMO.p);
for (Player player : mcMMO.p.getServer().getOnlinePlayers()) { for (Player player : mcMMO.p.getServer().getOnlinePlayers()) {
UserManager.addUser(player); UserManager.addUser(player);

View File

@ -10,6 +10,7 @@ import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.util.StringUtil; import org.bukkit.util.StringUtil;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.player.PlayerProfile; import com.gmail.nossr50.datatypes.player.PlayerProfile;
import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.datatypes.skills.SkillType;
@ -68,7 +69,7 @@ public abstract class ExperienceCommand implements TabExecutor {
// If the mcMMOPlayer doesn't exist, create a temporary profile and check if it's present in the database. If it's not, abort the process. // If the mcMMOPlayer doesn't exist, create a temporary profile and check if it's present in the database. If it's not, abort the process.
if (mcMMOPlayer == null) { if (mcMMOPlayer == null) {
profile = new PlayerProfile(args[0], false); profile = mcMMO.getDatabaseManager().loadPlayerProfile(args[0], false);
if (CommandUtils.unloadedProfile(sender, profile)) { if (CommandUtils.unloadedProfile(sender, profile)) {
return true; return true;

View File

@ -61,7 +61,7 @@ public class SkillresetCommand extends ExperienceCommand {
// If the mcMMOPlayer doesn't exist, create a temporary profile and check if it's present in the database. If it's not, abort the process. // If the mcMMOPlayer doesn't exist, create a temporary profile and check if it's present in the database. If it's not, abort the process.
if (mcMMOPlayer == null) { if (mcMMOPlayer == null) {
profile = new PlayerProfile(args[0], false); profile = mcMMO.getDatabaseManager().loadPlayerProfile(args[0], false);
if (CommandUtils.unloadedProfile(sender, profile)) { if (CommandUtils.unloadedProfile(sender, profile)) {
return true; return true;

View File

@ -10,6 +10,7 @@ import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.util.StringUtil; import org.bukkit.util.StringUtil;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.player.PlayerProfile; import com.gmail.nossr50.datatypes.player.PlayerProfile;
@ -35,7 +36,7 @@ public class InspectCommand implements TabExecutor {
// If the mcMMOPlayer doesn't exist, create a temporary profile and check if it's present in the database. If it's not, abort the process. // If the mcMMOPlayer doesn't exist, create a temporary profile and check if it's present in the database. If it's not, abort the process.
if (mcMMOPlayer == null) { if (mcMMOPlayer == null) {
PlayerProfile profile = new PlayerProfile(args[0], false); // Temporary Profile PlayerProfile profile = mcMMO.getDatabaseManager().loadPlayerProfile(args[0], false); // Temporary Profile
if (CommandUtils.inspectOffline(sender, profile, Permissions.inspectOffline(sender))) { if (CommandUtils.inspectOffline(sender, profile, Permissions.inspectOffline(sender))) {
return true; return true;

View File

@ -61,7 +61,7 @@ public class McrankCommand implements TabExecutor {
return true; return true;
} }
} }
else if (CommandUtils.inspectOffline(sender, new PlayerProfile(playerName, false), Permissions.mcrankOffline(sender))) { else if (CommandUtils.inspectOffline(sender, mcMMO.getDatabaseManager().loadPlayerProfile(playerName, false), Permissions.mcrankOffline(sender))) {
return true; return true;
} }

View File

@ -64,16 +64,25 @@ public interface DatabaseManager {
* Load a player from the database. * Load a player from the database.
* *
* @param playerName The name of the player to load from the database * @param playerName The name of the player to load from the database
* @return The player's data * @param createNew Whether to create a new record if the player is not
* found
* @return The player's data, or an unloaded PlayerProfile if not found
* and createNew is false
*/ */
public List<String> loadPlayerData(String playerName); public PlayerProfile loadPlayerProfile(String playerName, boolean createNew);
/** /**
* Convert player data to a different storage format. * Get all users currently stored in the database.
* *
* @param data The player's data * @return list of playernames
* @return true if the conversion was successful, false otherwise
* @throws Exception
*/ */
public boolean convert(String[] data) throws Exception; public List<String> getStoredUsers();
/**
* Convert all users from this database to the provided database using
* {@link #saveUser(PlayerProfile)}.
*
* @param the DatabaseManager to save to
*/
public void convertUsers(DatabaseManager destination);
} }

View File

@ -6,4 +6,14 @@ public class DatabaseManagerFactory {
public static DatabaseManager getDatabaseManager() { public static DatabaseManager getDatabaseManager() {
return Config.getInstance().getUseMySQL() ? new SQLDatabaseManager() : new FlatfileDatabaseManager(); return Config.getInstance().getUseMySQL() ? new SQLDatabaseManager() : new FlatfileDatabaseManager();
} }
// For data conversion purposes
public static FlatfileDatabaseManager createFlatfileDatabaseManager() {
return new FlatfileDatabaseManager();
}
public static SQLDatabaseManager createSQLDatabaseManager() {
return new SQLDatabaseManager();
}
} }

View File

@ -294,12 +294,13 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
} }
} }
public List<String> loadPlayerData(String playerName) { public PlayerProfile loadPlayerProfile(String playerName, boolean create) {
List<String> playerData = new ArrayList<String>(); FileReader file = null;
BufferedReader in = null;
try { try {
// Open the user file // Open the user file
FileReader file = new FileReader(mcMMO.getUsersFilePath()); file = new FileReader(mcMMO.getUsersFilePath());
BufferedReader in = new BufferedReader(file); in = new BufferedReader(file);
String line; String line;
while ((line = in.readLine()) != null) { while ((line = in.readLine()) != null) {
@ -310,74 +311,105 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
continue; continue;
} }
// Skill levels return loadFromLine(character);
playerData.add(character[24]); // Taming
playerData.add(character[1]); // Mining
playerData.add(character[7]); // Repair
playerData.add(character[5]); // Woodcutting
playerData.add(character[8]); // Unarmed
playerData.add(character[9]); // Herbalism
playerData.add(character[10]); // Excavation
playerData.add(character[11]); // Archery
playerData.add(character[12]); // Swords
playerData.add(character[13]); // Axes
playerData.add(character[14]); // Acrobatics
playerData.add(character[34]); // Fishing
// Experience
playerData.add(character[25]); // Taming
playerData.add(character[4]); // Mining
playerData.add(character[15]); // Repair
playerData.add(character[6]); // Woodcutting
playerData.add(character[16]); // Unarmed
playerData.add(character[17]); // Herbalism
playerData.add(character[18]); // Excavation
playerData.add(character[19]); // Archery
playerData.add(character[20]); // Swords
playerData.add(character[21]); // Axes
playerData.add(character[22]); // Acrobatics
playerData.add(character[35]); // Fishing
// Cooldowns
playerData.add(null); // Taming
playerData.add(character[32]); // SuperBreaker
playerData.add(null); // Repair
playerData.add(character[28]); // Tree Feller
playerData.add(character[26]); // Beserk
playerData.add(character[29]); // Green Terra
playerData.add(character[27]); // Giga Drill Breaker
playerData.add(null); // Archery
playerData.add(character[30]); // Serrated Strikes
playerData.add(character[31]); // Skull Splitter
playerData.add(null); // Acrobatics
playerData.add(character[36]); // Blast Mining
playerData.add(character.length > 33 ? character[33] : null); // HudType
playerData.add(character.length > 38 ? character[38] : null); // MobHealthBar
} }
in.close();
} }
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
finally {
try {
if (in != null) {
in.close();
}
if (file != null) {
file.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return playerData; if (create) {
newUser(playerName);
return new PlayerProfile(playerName, true);
}
return new PlayerProfile(playerName);
} }
public boolean convert(String[] character) throws Exception { public void convertUsers(DatabaseManager destination) {
// Not implemented FileReader file = null;
return false; BufferedReader in = null;
try {
// Open the user file
file = new FileReader(mcMMO.getUsersFilePath());
in = new BufferedReader(file);
String line;
while ((line = in.readLine()) != null) {
String[] character = line.split(":");
try {
destination.saveUser(loadFromLine(character));
}
catch (Exception e) {
e.printStackTrace();
}
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
if (in != null) {
in.close();
}
if (file != null) {
file.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} }
public boolean checkConnected() { public boolean checkConnected() {
// Not implemented // Not implemented
return false; return true;
}
public List<String> getStoredUsers() {
ArrayList<String> users = new ArrayList<String>();
BufferedReader in = null;
try {
// Open the user file
FileReader file = new FileReader(mcMMO.getUsersFilePath());
in = new BufferedReader(file);
String line;
while ((line = in.readLine()) != null) {
String[] character = line.split(":");
users.add(character[0]);
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return users;
} }
/** /**
* Update the leader boards. * Update the leader boards.
*/ */
private void updateLeaderboards() { private void updateLeaderboards() {
// Only update FFS leaderboards every 10 minutes.. this puts a lot of strain on the server (depending on the size of the database) and should not be done frequently // Only update FFS leaderboards every 10 minutes.. this puts a lot of strain on the server (depending on the size of the database) and should not be done frequently
if (System.currentTimeMillis() < lastUpdate + UPDATE_WAIT_TIME) { if (System.currentTimeMillis() < lastUpdate + UPDATE_WAIT_TIME) {
@ -522,4 +554,67 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
return (o2.statVal - o1.statVal); return (o2.statVal - o1.statVal);
} }
} }
private PlayerProfile loadFromLine(String[] character) throws Exception {
Map<SkillType, Integer> skills = new HashMap<SkillType, Integer>(); // Skill & Level
Map<SkillType, Float> skillsXp = new HashMap<SkillType, Float>(); // Skill & XP
Map<AbilityType, Integer> skillsDATS = new HashMap<AbilityType, Integer>(); // Ability & Cooldown
HudType hudType;
MobHealthbarType mobHealthbarType;
skills.put(SkillType.TAMING, Integer.valueOf(character[24]));
skills.put(SkillType.MINING, Integer.valueOf(character[1]));
skills.put(SkillType.REPAIR, Integer.valueOf(character[7]));
skills.put(SkillType.WOODCUTTING, Integer.valueOf(character[5]));
skills.put(SkillType.UNARMED, Integer.valueOf(character[8]));
skills.put(SkillType.HERBALISM, Integer.valueOf(character[9]));
skills.put(SkillType.EXCAVATION, Integer.valueOf(character[10]));
skills.put(SkillType.ARCHERY, Integer.valueOf(character[11]));
skills.put(SkillType.SWORDS, Integer.valueOf(character[12]));
skills.put(SkillType.AXES, Integer.valueOf(character[13]));
skills.put(SkillType.ACROBATICS, Integer.valueOf(character[14]));
skills.put(SkillType.FISHING, Integer.valueOf(character[34]));
skillsXp.put(SkillType.TAMING, (float) Integer.valueOf(character[25]));
skillsXp.put(SkillType.MINING, (float) Integer.valueOf(character[4]));
skillsXp.put(SkillType.REPAIR, (float) Integer.valueOf(character[15]));
skillsXp.put(SkillType.WOODCUTTING, (float) Integer.valueOf(character[6]));
skillsXp.put(SkillType.UNARMED, (float) Integer.valueOf(character[16]));
skillsXp.put(SkillType.HERBALISM, (float) Integer.valueOf(character[17]));
skillsXp.put(SkillType.EXCAVATION, (float) Integer.valueOf(character[18]));
skillsXp.put(SkillType.ARCHERY, (float) Integer.valueOf(character[19]));
skillsXp.put(SkillType.SWORDS, (float) Integer.valueOf(character[20]));
skillsXp.put(SkillType.AXES, (float) Integer.valueOf(character[21]));
skillsXp.put(SkillType.ACROBATICS, (float) Integer.valueOf(character[22]));
skillsXp.put(SkillType.FISHING, (float) Integer.valueOf(character[35]));
// Taming - Unused
skillsDATS.put(AbilityType.SUPER_BREAKER, Integer.valueOf(character[32]));
// Repair - Unused
skillsDATS.put(AbilityType.TREE_FELLER, Integer.valueOf(character[28]));
skillsDATS.put(AbilityType.BERSERK, Integer.valueOf(character[26]));
skillsDATS.put(AbilityType.GREEN_TERRA, Integer.valueOf(character[29]));
skillsDATS.put(AbilityType.GIGA_DRILL_BREAKER, Integer.valueOf(character[27]));
// Archery - Unused
skillsDATS.put(AbilityType.SERRATED_STRIKES, Integer.valueOf(character[30]));
skillsDATS.put(AbilityType.SKULL_SPLITTER, Integer.valueOf(character[31]));
// Acrobatics - Unused
skillsDATS.put(AbilityType.BLAST_MINING, Integer.valueOf(character[36]));
try {
hudType = HudType.valueOf(character[33]);
}
catch (Exception e) {
hudType = HudType.STANDARD; // Shouldn't happen unless database is being tampered with
}
try {
mobHealthbarType = MobHealthbarType.valueOf(character[38]);
}
catch (Exception e) {
mobHealthbarType = Config.getInstance().getMobHealthbarDefault();
}
return new PlayerProfile(character[0], skills, skillsXp, skillsDATS, hudType, mobHealthbarType);
}
} }

View File

@ -5,6 +5,7 @@ import java.sql.DriverManager;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
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.HashMap; import java.util.HashMap;
@ -23,7 +24,6 @@ import com.gmail.nossr50.datatypes.skills.SkillType;
import com.gmail.nossr50.datatypes.spout.huds.HudType; import com.gmail.nossr50.datatypes.spout.huds.HudType;
import com.gmail.nossr50.runnables.database.SQLReconnectTask; import com.gmail.nossr50.runnables.database.SQLReconnectTask;
import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.StringUtils;
public final class SQLDatabaseManager implements DatabaseManager { public final class SQLDatabaseManager implements DatabaseManager {
private String connectionString; private String connectionString;
@ -314,8 +314,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
} }
} }
public List<String> loadPlayerData(String playerName) { public PlayerProfile loadPlayerProfile(String playerName, boolean create) {
List<String> playerData = null;
PreparedStatement statement = null; PreparedStatement statement = null;
try { try {
@ -333,21 +332,38 @@ public final class SQLDatabaseManager implements DatabaseManager {
+ "WHERE u.user = ?"); + "WHERE u.user = ?");
statement.setString(1, playerName); statement.setString(1, playerName);
playerData = readRow(statement); ResultSet result = statement.executeQuery();
if (playerData == null || playerData.size() == 0) { if (result.next()) {
PlayerProfile ret = loadFromResult(playerName, result);
result.close();
return ret;
}
else {
// Problem, no rows returned
int userId = readId(playerName); int userId = readId(playerName);
// Check if user doesn't exist
if (userId == 0) { if (userId == 0) {
return playerData; if (!create) {
// Give up
return new PlayerProfile(playerName, false);
}
else {
newUser(playerName);
userId = readId(playerName);
}
} }
// Write missing table rows
writeMissingRows(userId); writeMissingRows(userId);
// Re-read data if (!create) {
playerData = loadPlayerData(playerName); // Give up
return new PlayerProfile(playerName, false);
}
else {
// Re-read data
return loadPlayerProfile(playerName, false);
}
} }
} }
catch (SQLException ex) { catch (SQLException ex) {
@ -363,108 +379,54 @@ public final class SQLDatabaseManager implements DatabaseManager {
} }
} }
} }
return playerData; return new PlayerProfile(playerName, false);
} }
public boolean convert(String[] data) throws Exception { public void convertUsers(DatabaseManager destination) {
String playerName = data[0]; PreparedStatement statement = null;
// Check for things we don't want put in the DB try {
if (playerName == null || playerName.equalsIgnoreCase("null") || playerName.length() > 16) { statement = connection.prepareStatement(
return false; "SELECT "
+ "s.taming, s.mining, s.repair, s.woodcutting, s.unarmed, s.herbalism, s.excavation, s.archery, s.swords, s.axes, s.acrobatics, s.fishing, "
+ "e.taming, e.mining, e.repair, e.woodcutting, e.unarmed, e.herbalism, e.excavation, e.archery, e.swords, e.axes, e.acrobatics, e.fishing, "
+ "c.taming, c.mining, c.repair, c.woodcutting, c.unarmed, c.herbalism, c.excavation, c.archery, c.swords, c.axes, c.acrobatics, c.blast_mining, "
+ "h.hudtype, h.mobhealthbar "
+ "FROM " + tablePrefix + "users u "
+ "JOIN " + tablePrefix + "skills s ON (u.id = s.user_id) "
+ "JOIN " + tablePrefix + "experience e ON (u.id = e.user_id) "
+ "JOIN " + tablePrefix + "cooldowns c ON (u.id = c.user_id) "
+ "JOIN " + tablePrefix + "huds h ON (u.id = h.user_id) "
+ "WHERE u.user = ?");
List<String> usernames = getStoredUsers();
ResultSet result = null;
for (String playerName : usernames) {
statement.setString(1, playerName);
try {
result = statement.executeQuery();
result.next();
destination.saveUser(loadFromResult(playerName, result));
result.close();
}
catch (SQLException e) {
// Ignore
}
}
}
catch (SQLException e) {
printErrors(e);
}
finally {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
// Ignore
}
}
} }
String mining = (data.length > 1) ? data[1] : null;
String woodcutting = (data.length > 5) ? data[5] : null;
String repair = (data.length > 7) ? data[7] : null;
String unarmed = (data.length > 8) ? data[8] : null;
String herbalism = (data.length > 9) ? data[9] : null;
String excavation = (data.length > 10) ? data[10] : null;
String archery = (data.length > 11) ? data[11] : null;
String swords = (data.length > 12) ? data[12] : null;
String axes = (data.length > 13) ? data[13] : null;
String acrobatics = (data.length > 14) ? data[14] : null;
String taming = (data.length > 24) ? data[24] : null;
String fishing = (data.length > 34) ? data[34] : null;
String miningXP = (data.length > 4) ? data[4] : null;
String woodCuttingXP = (data.length > 6) ? data[6] : null;;
String repairXP = (data.length > 15) ? data[15] : null;
String unarmedXP = (data.length > 16) ? data[16] : null;
String herbalismXP = (data.length > 17) ? data[17] : null;
String excavationXP = (data.length > 18) ? data[18] : null;
String archeryXP = (data.length > 19) ? data[19] : null;
String swordsXP = (data.length > 20) ? data[20] : null;
String axesXP = (data.length > 21) ? data[21] : null;
String acrobaticsXP = (data.length > 22) ? data[22] : null;
String tamingXP = (data.length > 25) ? data[25] : null;
String fishingXP = (data.length > 35) ? data[35] : null;
String superBreakerCooldown = (data.length > 32) ? data[32] : null;
String treeFellerCooldown = (data.length > 28) ? data[28] : null;
String berserkCooldown = (data.length > 26) ? data[26] : null;
String greenTerraCooldown = (data.length > 29) ? data[29] : null;
String gigaDrillBreakerCooldown = (data.length > 27) ? data[27] : null;
String serratedStrikesCooldown = (data.length > 30) ? data[30] : null;
String skullSplitterCooldown = (data.length > 31) ? data[31] : null;
String blastMiningCooldown = (data.length > 36) ? data[36] : null;
String hudType = (data.length > 33) ? data[33] : null;
String mobHealthbarType = (data.length > 38 ? data[38] : null);
long lastLogin = mcMMO.p.getServer().getOfflinePlayer(playerName).getLastPlayed();
int id = readId(playerName); // Check to see if the user is in the DB
// Create the user if they don't exist
if (id == 0) {
newUser(playerName);
id = readId(playerName);
}
saveLogin(id, lastLogin);
saveIntegers(
"UPDATE " + tablePrefix + "skills SET "
+ " taming = ?, mining = ?, repair = ?, woodcutting = ?"
+ ", unarmed = ?, herbalism = ?, excavation = ?"
+ ", archery = ?, swords = ?, axes = ?, acrobatics = ?"
+ ", fishing = ? WHERE user_id = ?",
StringUtils.getInt(taming), StringUtils.getInt(mining),
StringUtils.getInt(repair), StringUtils.getInt(woodcutting),
StringUtils.getInt(unarmed), StringUtils.getInt(herbalism),
StringUtils.getInt(excavation), StringUtils.getInt(archery),
StringUtils.getInt(swords), StringUtils.getInt(axes),
StringUtils.getInt(acrobatics), StringUtils.getInt(fishing),
id);
saveIntegers(
"UPDATE " + tablePrefix + "experience SET "
+ " taming = ?, mining = ?, repair = ?, woodcutting = ?"
+ ", unarmed = ?, herbalism = ?, excavation = ?"
+ ", archery = ?, swords = ?, axes = ?, acrobatics = ?"
+ ", fishing = ? WHERE user_id = ?",
StringUtils.getInt(tamingXP), StringUtils.getInt(miningXP),
StringUtils.getInt(repairXP), StringUtils.getInt(woodCuttingXP),
StringUtils.getInt(unarmedXP), StringUtils.getInt(herbalismXP),
StringUtils.getInt(excavationXP), StringUtils.getInt(archeryXP),
StringUtils.getInt(swordsXP), StringUtils.getInt(axesXP),
StringUtils.getInt(acrobaticsXP), StringUtils.getInt(fishingXP),
id);
saveLongs(
"UPDATE " + tablePrefix + "cooldowns SET "
+ " taming = ?, mining = ?, repair = ?, woodcutting = ?"
+ ", unarmed = ?, herbalism = ?, excavation = ?"
+ ", archery = ?, swords = ?, axes = ?, acrobatics = ?"
+ ", blast_mining = ? WHERE user_id = ?",
id,
StringUtils.getLong(null), StringUtils.getLong(superBreakerCooldown),
StringUtils.getLong(null), StringUtils.getInt(treeFellerCooldown),
StringUtils.getLong(berserkCooldown), StringUtils.getLong(greenTerraCooldown),
StringUtils.getLong(gigaDrillBreakerCooldown), StringUtils.getLong(null),
StringUtils.getLong(serratedStrikesCooldown), StringUtils.getLong(skullSplitterCooldown),
StringUtils.getLong(null), StringUtils.getLong(blastMiningCooldown));
saveHuds(id, hudType, mobHealthbarType);
return true;
} }
/** /**
* Check connection status and re-establish if dead or stale. * Check connection status and re-establish if dead or stale.
* *
@ -555,9 +517,35 @@ public final class SQLDatabaseManager implements DatabaseManager {
return false; return false;
} }
public List<String> getStoredUsers() {
ArrayList<String> users = new ArrayList<String>();
Statement stmt = null;
try {
stmt = connection.createStatement();
ResultSet result = stmt.executeQuery("SELECT user FROM " + tablePrefix + "users");
while (result.next()) {
users.add(result.getString("user"));
}
result.close();
}
catch (SQLException e) {
printErrors(e);
}
finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
// Ignore
}
}
}
return users;
}
/** /**
* Attempt to connect to the mySQL database. * Attempt to connect to the mySQL database.
*/ */
private void connect() { private void connect() {
connectionString = "jdbc:mysql://" + Config.getInstance().getMySQLServerName() + ":" + Config.getInstance().getMySQLServerPort() + "/" + Config.getInstance().getMySQLDatabaseName(); connectionString = "jdbc:mysql://" + Config.getInstance().getMySQLServerName() + ":" + Config.getInstance().getMySQLServerPort() + "/" + Config.getInstance().getMySQLDatabaseName();
@ -592,8 +580,8 @@ public final class SQLDatabaseManager implements DatabaseManager {
} }
/** /**
* Attempt to create the database structure. * Attempt to create the database structure.
*/ */
private void createStructure() { private void createStructure() {
write("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "users` (" write("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "users` ("
+ "`id` int(10) unsigned NOT NULL AUTO_INCREMENT," + "`id` int(10) unsigned NOT NULL AUTO_INCREMENT,"
@ -662,10 +650,10 @@ public final class SQLDatabaseManager implements DatabaseManager {
} }
/** /**
* Check database structure for missing values. * Check database structure for missing values.
* *
* @param update Type of data to check updates for * @param update Type of data to check updates for
*/ */
private void checkDatabaseStructure(DatabaseUpdateType update) { private void checkDatabaseStructure(DatabaseUpdateType update) {
String sql = ""; String sql = "";
@ -793,11 +781,11 @@ public final class SQLDatabaseManager implements DatabaseManager {
} }
/** /**
* Attempt to write the SQL query. * Attempt to write the SQL query.
* *
* @param sql Query to write. * @param sql Query to write.
* @return true if the query was successfully written, false otherwise. * @return true if the query was successfully written, false otherwise.
*/ */
private boolean write(String sql) { private boolean write(String sql) {
if (!checkConnected()) { if (!checkConnected()) {
return false; return false;
@ -828,11 +816,11 @@ public final class SQLDatabaseManager implements DatabaseManager {
} }
/** /**
* Returns the number of rows affected by either a DELETE or UPDATE query * Returns the number of rows affected by either a DELETE or UPDATE query
* *
* @param sql SQL query to execute * @param sql SQL query to execute
* @return the number of rows affected * @return the number of rows affected
*/ */
private int update(String sql) { private int update(String sql) {
int rows = 0; int rows = 0;
@ -862,11 +850,11 @@ public final class SQLDatabaseManager implements DatabaseManager {
} }
/** /**
* Read SQL query. * Read SQL query.
* *
* @param sql SQL query to read * @param sql SQL query to read
* @return the rows in this SQL query * @return the rows in this SQL query
*/ */
private HashMap<Integer, ArrayList<String>> read(String sql) { private HashMap<Integer, ArrayList<String>> read(String sql) {
HashMap<Integer, ArrayList<String>> rows = new HashMap<Integer, ArrayList<String>>(); HashMap<Integer, ArrayList<String>> rows = new HashMap<Integer, ArrayList<String>>();
@ -906,45 +894,12 @@ public final class SQLDatabaseManager implements DatabaseManager {
return rows; return rows;
} }
private ArrayList<String> readRow(PreparedStatement statement) {
ArrayList<String> playerData = new ArrayList<String>();
if (checkConnected()) {
ResultSet resultSet = null;
try {
resultSet = statement.executeQuery();
if (resultSet.next()) {
for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) {
playerData.add(resultSet.getString(i));
}
}
}
catch (SQLException ex) {
printErrors(ex);
}
finally {
if (statement != null) {
try {
statement.close();
}
catch (SQLException e) {
// Ignore
}
}
}
}
return playerData;
}
/** /**
* Get the Integer. Only return first row / first field. * Get the Integer. Only return first row / first field.
* *
* @param sql SQL query to execute * @param sql SQL query to execute
* @return the value in the first row / first field * @return the value in the first row / first field
*/ */
private int readInt(PreparedStatement statement) { private int readInt(PreparedStatement statement) {
int result = 0; int result = 0;
@ -1142,6 +1097,74 @@ public final class SQLDatabaseManager implements DatabaseManager {
} }
} }
private PlayerProfile loadFromResult(String playerName, ResultSet result) throws SQLException {
Map<SkillType, Integer> skills = new HashMap<SkillType, Integer>(); // Skill & Level
Map<SkillType, Float> skillsXp = new HashMap<SkillType, Float>(); // Skill & XP
Map<AbilityType, Integer> skillsDATS = new HashMap<AbilityType, Integer>(); // Ability & Cooldown
HudType hudType;
MobHealthbarType mobHealthbarType;
final int OFFSET_SKILLS = 0; // TODO update these numbers when the query changes (a new skill is added)
final int OFFSET_XP = 12;
final int OFFSET_DATS = 24;
final int OFFSET_OTHER = 36;
skills.put(SkillType.TAMING, result.getInt(OFFSET_SKILLS + 1));
skills.put(SkillType.MINING, result.getInt(OFFSET_SKILLS + 2));
skills.put(SkillType.REPAIR, result.getInt(OFFSET_SKILLS + 3));
skills.put(SkillType.WOODCUTTING, result.getInt(OFFSET_SKILLS + 4));
skills.put(SkillType.UNARMED, result.getInt(OFFSET_SKILLS + 5));
skills.put(SkillType.HERBALISM, result.getInt(OFFSET_SKILLS + 6));
skills.put(SkillType.EXCAVATION, result.getInt(OFFSET_SKILLS + 7));
skills.put(SkillType.ARCHERY, result.getInt(OFFSET_SKILLS + 8));
skills.put(SkillType.SWORDS, result.getInt(OFFSET_SKILLS + 9));
skills.put(SkillType.AXES, result.getInt(OFFSET_SKILLS + 10));
skills.put(SkillType.ACROBATICS, result.getInt(OFFSET_SKILLS + 11));
skills.put(SkillType.FISHING, result.getInt(OFFSET_SKILLS + 12));
skillsXp.put(SkillType.TAMING, result.getFloat(OFFSET_XP + 1));
skillsXp.put(SkillType.MINING, result.getFloat(OFFSET_XP + 2));
skillsXp.put(SkillType.REPAIR, result.getFloat(OFFSET_XP + 3));
skillsXp.put(SkillType.WOODCUTTING, result.getFloat(OFFSET_XP + 4));
skillsXp.put(SkillType.UNARMED, result.getFloat(OFFSET_XP + 5));
skillsXp.put(SkillType.HERBALISM, result.getFloat(OFFSET_XP + 6));
skillsXp.put(SkillType.EXCAVATION, result.getFloat(OFFSET_XP + 7));
skillsXp.put(SkillType.ARCHERY, result.getFloat(OFFSET_XP + 8));
skillsXp.put(SkillType.SWORDS, result.getFloat(OFFSET_XP + 9));
skillsXp.put(SkillType.AXES, result.getFloat(OFFSET_XP + 10));
skillsXp.put(SkillType.ACROBATICS, result.getFloat(OFFSET_XP + 11));
skillsXp.put(SkillType.FISHING, result.getFloat(OFFSET_XP + 12));
// Taming - Unused - result.getInt(OFFSET_DATS + 1)
skillsDATS.put(AbilityType.SUPER_BREAKER, result.getInt(OFFSET_DATS + 2));
// Repair - Unused - result.getInt(OFFSET_DATS + 3)
skillsDATS.put(AbilityType.TREE_FELLER, result.getInt(OFFSET_DATS + 4));
skillsDATS.put(AbilityType.BERSERK, result.getInt(OFFSET_DATS + 5));
skillsDATS.put(AbilityType.GREEN_TERRA, result.getInt(OFFSET_DATS + 6));
skillsDATS.put(AbilityType.GIGA_DRILL_BREAKER, result.getInt(OFFSET_DATS + 7));
// Archery - Unused - result.getInt(OFFSET_DATS + 8)
skillsDATS.put(AbilityType.SERRATED_STRIKES, result.getInt(OFFSET_DATS + 9));
skillsDATS.put(AbilityType.SKULL_SPLITTER, result.getInt(OFFSET_DATS + 10));
// Acrobatics - Unused - result.getInt(OFFSET_DATS + 11)
skillsDATS.put(AbilityType.BLAST_MINING, result.getInt(OFFSET_DATS + 12));
try {
hudType = HudType.valueOf(result.getString(OFFSET_OTHER + 1));
}
catch (Exception e) {
hudType = HudType.STANDARD; // Shouldn't happen unless database is being tampered with
}
try {
mobHealthbarType = MobHealthbarType.valueOf(result.getString(OFFSET_OTHER + 2));
}
catch (Exception e) {
mobHealthbarType = Config.getInstance().getMobHealthbarDefault();
}
return new PlayerProfile(playerName, skills, skillsXp, skillsDATS, hudType, mobHealthbarType);
}
private void printErrors(SQLException ex) { private void printErrors(SQLException ex) {
mcMMO.p.getLogger().severe("SQLException: " + ex.getMessage()); mcMMO.p.getLogger().severe("SQLException: " + ex.getMessage());
mcMMO.p.getLogger().severe("SQLState: " + ex.getSQLState()); mcMMO.p.getLogger().severe("SQLState: " + ex.getSQLState());

View File

@ -92,10 +92,10 @@ public class McMMOPlayer {
String playerName = player.getName(); String playerName = player.getName();
this.player = player; this.player = player;
profile = new PlayerProfile(playerName, true); profile = mcMMO.getDatabaseManager().loadPlayerProfile(playerName, true);
party = PartyManager.getPlayerParty(playerName); party = PartyManager.getPlayerParty(playerName);
/* /*
* I'm using this method because it makes code shorter and safer (we don't have to add all SkillTypes manually), * I'm using this method because it makes code shorter and safer (we don't have to add all SkillTypes manually),
* but I actually have no idea about the performance impact, if there is any. * but I actually have no idea about the performance impact, if there is any.
* If in the future someone wants to remove this, don't forget to also remove what is in the SkillType enum. - bm01 * If in the future someone wants to remove this, don't forget to also remove what is in the SkillType enum. - bm01

View File

@ -32,7 +32,7 @@ public class PlayerProfile {
private final Map<SkillType, Float> skillsXp = new HashMap<SkillType, Float>(); // Skill & XP private final Map<SkillType, Float> skillsXp = new HashMap<SkillType, Float>(); // Skill & XP
private final Map<AbilityType, Integer> skillsDATS = new HashMap<AbilityType, Integer>(); // Ability & Cooldown private final Map<AbilityType, Integer> skillsDATS = new HashMap<AbilityType, Integer>(); // Ability & Cooldown
public PlayerProfile(String playerName, boolean addNew) { public PlayerProfile(String playerName) {
this.playerName = playerName; this.playerName = playerName;
hudType = mcMMO.isSpoutEnabled() ? SpoutConfig.getInstance().getDefaultHudType() : HudType.DISABLED; hudType = mcMMO.isSpoutEnabled() ? SpoutConfig.getInstance().getDefaultHudType() : HudType.DISABLED;
@ -46,11 +46,27 @@ public class PlayerProfile {
skills.put(skillType, 0); skills.put(skillType, 0);
skillsXp.put(skillType, 0F); skillsXp.put(skillType, 0F);
} }
}
if (!loadPlayer() && addNew) { public PlayerProfile(String playerName, boolean isLoaded) {
mcMMO.getDatabaseManager().newUser(playerName); this(playerName);
loaded = true; this.loaded = isLoaded;
} }
/**
* Calling this constructor is considered loading the profile.
*/
public PlayerProfile(String playerName, Map<SkillType, Integer> argSkills, Map<SkillType, Float> argSkillsXp, Map<AbilityType, Integer> argSkillsDats, HudType hudType, MobHealthbarType mobHealthbarType) {
this(playerName, true);
this.hudType = hudType;
this.mobHealthbarType = mobHealthbarType;
this.skills.putAll(argSkills);
this.skillsXp.putAll(argSkillsXp);
this.skillsDATS.putAll(argSkillsDats);
loaded = true;
} }
public void save() { public void save() {
@ -254,68 +270,4 @@ public class PlayerProfile {
return sum / parents.size(); return sum / parents.size();
} }
private boolean loadPlayer() {
List<String> playerData = mcMMO.getDatabaseManager().loadPlayerData(playerName);
if (playerData == null || playerData.isEmpty()) {
return false;
}
skills.put(SkillType.TAMING, Integer.valueOf(playerData.get(0)));
skills.put(SkillType.MINING, Integer.valueOf(playerData.get(1)));
skills.put(SkillType.REPAIR, Integer.valueOf(playerData.get(2)));
skills.put(SkillType.WOODCUTTING, Integer.valueOf(playerData.get(3)));
skills.put(SkillType.UNARMED, Integer.valueOf(playerData.get(4)));
skills.put(SkillType.HERBALISM, Integer.valueOf(playerData.get(5)));
skills.put(SkillType.EXCAVATION, Integer.valueOf(playerData.get(6)));
skills.put(SkillType.ARCHERY, Integer.valueOf(playerData.get(7)));
skills.put(SkillType.SWORDS, Integer.valueOf(playerData.get(8)));
skills.put(SkillType.AXES, Integer.valueOf(playerData.get(9)));
skills.put(SkillType.ACROBATICS, Integer.valueOf(playerData.get(10)));
skills.put(SkillType.FISHING, Integer.valueOf(playerData.get(11)));
skillsXp.put(SkillType.TAMING, (float) Integer.valueOf(playerData.get(12)));
skillsXp.put(SkillType.MINING, (float) Integer.valueOf(playerData.get(13)));
skillsXp.put(SkillType.REPAIR, (float) Integer.valueOf(playerData.get(14)));
skillsXp.put(SkillType.WOODCUTTING, (float) Integer.valueOf(playerData.get(15)));
skillsXp.put(SkillType.UNARMED, (float) Integer.valueOf(playerData.get(16)));
skillsXp.put(SkillType.HERBALISM, (float) Integer.valueOf(playerData.get(17)));
skillsXp.put(SkillType.EXCAVATION, (float) Integer.valueOf(playerData.get(18)));
skillsXp.put(SkillType.ARCHERY, (float) Integer.valueOf(playerData.get(19)));
skillsXp.put(SkillType.SWORDS, (float) Integer.valueOf(playerData.get(20)));
skillsXp.put(SkillType.AXES, (float) Integer.valueOf(playerData.get(21)));
skillsXp.put(SkillType.ACROBATICS, (float) Integer.valueOf(playerData.get(22)));
skillsXp.put(SkillType.FISHING, (float) Integer.valueOf(playerData.get(23)));
// Taming 24 - Unused
skillsDATS.put(AbilityType.SUPER_BREAKER, Integer.valueOf(playerData.get(25)));
// Repair 26 - Unused
skillsDATS.put(AbilityType.TREE_FELLER, Integer.valueOf(playerData.get(27)));
skillsDATS.put(AbilityType.BERSERK, Integer.valueOf(playerData.get(28)));
skillsDATS.put(AbilityType.GREEN_TERRA, Integer.valueOf(playerData.get(29)));
skillsDATS.put(AbilityType.GIGA_DRILL_BREAKER, Integer.valueOf(playerData.get(30)));
// Archery 31 - Unused
skillsDATS.put(AbilityType.SERRATED_STRIKES, Integer.valueOf(playerData.get(32)));
skillsDATS.put(AbilityType.SKULL_SPLITTER, Integer.valueOf(playerData.get(33)));
// Acrobatics 34 - Unused
skillsDATS.put(AbilityType.BLAST_MINING, Integer.valueOf(playerData.get(35)));
try {
hudType = HudType.valueOf(playerData.get(36));
}
catch (Exception e) {
hudType = HudType.STANDARD; // Shouldn't happen unless database is being tampered with
}
try {
mobHealthbarType = MobHealthbarType.valueOf(playerData.get(37));
}
catch (Exception e) {
mobHealthbarType = Config.getInstance().getMobHealthbarDefault();
}
loaded = true;
return true;
}
} }

View File

@ -6,14 +6,20 @@ import java.io.FileReader;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.database.DatabaseManager;
import com.gmail.nossr50.database.DatabaseManagerFactory;
import com.gmail.nossr50.datatypes.player.PlayerProfile;
public class SQLConversionTask extends BukkitRunnable { public class FromFlatfileConversionTask extends BukkitRunnable {
@Override @Override
public void run() { public void run() {
String location = mcMMO.getUsersFilePath(); String location = mcMMO.getUsersFilePath();
try { try {
DatabaseManager from = DatabaseManagerFactory.createFlatfileDatabaseManager();
DatabaseManager to = mcMMO.getDatabaseManager();
BufferedReader in = new BufferedReader(new FileReader(location)); BufferedReader in = new BufferedReader(new FileReader(location));
String line = ""; String line = "";
int converted = 0; int converted = 0;
@ -22,16 +28,19 @@ public class SQLConversionTask extends BukkitRunnable {
// Find if the line contains the player we want. // Find if the line contains the player we want.
String[] playerData = line.split(":"); String[] playerData = line.split(":");
if (mcMMO.getDatabaseManager().convert(playerData)) { String playerName = playerData[0];
PlayerProfile profile = from.loadPlayerProfile(playerName, false);
if (profile.isLoaded()) {
to.saveUser(profile);
converted++; converted++;
} }
} }
mcMMO.p.getLogger().info("MySQL Updated from users file, " + converted + " items added/updated to MySQL DB"); mcMMO.p.getLogger().info("Database updated from users file, " + converted + " items added/updated to DB");
in.close(); in.close();
} }
catch (Exception e) { catch (Exception e) {
mcMMO.p.getLogger().severe("Exception while reading " + location + " (Are you sure you formatted it correctly?)" + e.toString()); mcMMO.p.getLogger().severe("Exception while reading " + location + " (Are you sure you formatted it correctly?) " + e.toString());
} }
} }
} }