mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2025-06-26 18:54:44 +02:00
Squashed commit of the following:
commit cb3e057dee1f2b29838ab654a526baac1baab7d6 Author: NuclearW <incongruency@gmail.com> Date: Fri Mar 1 00:43:57 2013 -0500 1.4.00 release commit 4f9628d2e4cde31c8946e9a911ee6f10e1fb6b35 Author: NuclearW <incongruency@gmail.com> Date: Fri Mar 1 00:07:30 2013 -0500 \r -> \n commit b2ca22e0477c747143b0f08a28a096967ee6ffd7 Author: GJ <gjmcferrin@gmail.com> Date: Thu Feb 28 23:53:56 2013 -0500 Commented-out code shouldn't be done like that. commit 92f131712cc671e3e616c14a22e22769ef6d6d0b Author: GJ <gjmcferrin@gmail.com> Date: Thu Feb 28 23:45:36 2013 -0500 More things we missed. commit 408b03766f6261a03a862a1ab7f5835772feda4a Author: NuclearW <incongruency@gmail.com> Date: Thu Feb 28 23:20:13 2013 -0500 Format: util through spout and backup lib commit d6bd2c29bbb51bee3607247468cfe145d4f38c9e Author: GJ <gjmcferrin@gmail.com> Date: Thu Feb 28 22:50:08 2013 -0500 The things we missed the first time through... commit 393f0b889aa1b7011ee81ee7b15413d8824b8cfb Author: GJ <gjmcferrin@gmail.com> Date: Thu Feb 28 22:05:29 2013 -0500 Formatting: Skills commit c097a6e188a7b760dd1b4389ed81dca417146b16 Author: GJ <gjmcferrin@gmail.com> Date: Thu Feb 28 19:30:12 2013 -0500 Organize imports. commit 34c3e74be7eb5f983f21d969e30155c5d82c01c1 Author: GJ <gjmcferrin@gmail.com> Date: Thu Feb 28 19:09:45 2013 -0500 Fixed a missing fallthrough comment from ChatCommand commit b4a76c9f022a2fd98bdd8f083accfea03becfd71 Author: GJ <gjmcferrin@gmail.com> Date: Thu Feb 28 19:09:36 2013 -0500 Formatting: datatypes.* through events.* commit 3e57dd41d3265a7c8106c7eb026df926770a4d15 Author: NuclearW <incongruency@gmail.com> Date: Thu Feb 28 17:56:15 2013 -0500 Fix issue with bad rebase commit e8c8e06b2971555b7334e49128257e3af6f36892 Author: GJ <gjmcferrin@gmail.com> Date: Thu Feb 28 17:35:32 2013 -0500 Formatting: DatabaseManager, LeaderboardManager, DatabaseUpdateType, and PlayerStat commit 13ecf1cc41f377a12991e357ac10abdcda24d6de Author: NuclearW <incongruency@gmail.com> Date: Thu Feb 28 17:31:43 2013 -0500 Format: listeners.* through runnables.* commit 71686e3c0d96c2dcf25442b91703fadda1ea3bb0 Author: NuclearW <incongruency@gmail.com> Date: Thu Feb 28 17:13:57 2013 -0500 Format PartyLockCommand commit d50abed10bf94e1a88df3dc5cc07c259aea920ea Author: NuclearW <incongruency@gmail.com> Date: Thu Feb 28 16:54:08 2013 -0500 Format: base through config.* commit 7004823eeebbae5be7728bf9cafc3b04e57b64cf Author: NuclearW <incongruency@gmail.com> Date: Thu Feb 28 15:21:40 2013 -0500 Example of using spaces to align like things commit 534190cfe2481e466fe459d65628550458cc2993 Author: NuclearW <incongruency@gmail.com> Date: Thu Feb 28 15:12:19 2013 -0500 Capitalization commit 5b61d3ba4c8d81e6f358b0cf4f460abfe9798414 Author: NuclearW <incongruency@gmail.com> Date: Thu Feb 28 15:07:43 2013 -0500 Updated readme, added standards.md commit 5ec0df70fb82c527420a2f437f27f31bd758f884 Author: NuclearW <incongruency@gmail.com> Date: Thu Feb 28 14:42:16 2013 -0500 Markdown was here, Creole is a loser commit 70d557c59d086b6a5fb5e0e63c0c1d8eb4c8d19c Author: NuclearW <incongruency@gmail.com> Date: Thu Feb 28 13:46:24 2013 -0500 Move MCStats shading to .metrics.mcstats commit eb9d67e66b1659d6abd2397ecf403343cfeffdda Author: GJ <gjmcferrin@gmail.com> Date: Thu Feb 28 13:37:37 2013 -0500 Move ALL the packages! commit 8ffa9e7b75417b6c7f158613d4b4ffb783dcf2d0 Author: NuclearW <incongruency@gmail.com> Date: Thu Feb 28 12:37:12 2013 -0500 /r/n -> /n
This commit is contained in:
@ -10,19 +10,19 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.gmail.nossr50.mcMMO;
|
||||
import com.gmail.nossr50.config.Config;
|
||||
import com.gmail.nossr50.database.runnables.SQLReconnect;
|
||||
import com.gmail.nossr50.datatypes.McMMOPlayer;
|
||||
import com.gmail.nossr50.skills.utilities.SkillType;
|
||||
import com.gmail.nossr50.spout.SpoutTools;
|
||||
import com.gmail.nossr50.spout.huds.SpoutHud;
|
||||
import com.gmail.nossr50.util.Users;
|
||||
import com.gmail.nossr50.datatypes.database.DatabaseUpdateType;
|
||||
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
|
||||
import com.gmail.nossr50.datatypes.skills.SkillType;
|
||||
import com.gmail.nossr50.datatypes.spout.huds.McMMOHud;
|
||||
import com.gmail.nossr50.runnables.database.SQLReconnectTask;
|
||||
import com.gmail.nossr50.util.player.UserManager;
|
||||
import com.gmail.nossr50.util.spout.SpoutUtils;
|
||||
|
||||
public final class Database {
|
||||
public final class DatabaseManager {
|
||||
private static String connectionString;
|
||||
|
||||
private static String tablePrefix = Config.getInstance().getMySQLTablePrefix();
|
||||
@ -32,7 +32,7 @@ public final class Database {
|
||||
private static final double SCALING_FACTOR = 40;
|
||||
|
||||
// Minimum wait in nanoseconds (default 500ms)
|
||||
private static final long MIN_WAIT = 500L*1000000L;
|
||||
private static final long MIN_WAIT = 500L * 1000000L;
|
||||
|
||||
// Maximum time to wait between reconnects (default 5 minutes)
|
||||
private static final long MAX_WAIT = 5L * 60L * 1000L * 1000000L;
|
||||
@ -43,10 +43,12 @@ public final class Database {
|
||||
// When next to try connecting to Database in nanoseconds
|
||||
private static long nextReconnectTimestamp = 0L;
|
||||
|
||||
// How many connection attemtps have failed
|
||||
// How many connection attempts have failed
|
||||
private static int reconnectAttempt = 0;
|
||||
|
||||
private Database() {}
|
||||
private static final long ONE_MONTH = 2630000000L;
|
||||
|
||||
private DatabaseManager() {}
|
||||
|
||||
/**
|
||||
* Attempt to connect to the mySQL database.
|
||||
@ -68,13 +70,17 @@ public final class Database {
|
||||
connection = DriverManager.getConnection(connectionString, connectionProperties);
|
||||
|
||||
mcMMO.p.getLogger().info("Connection to MySQL was a success!");
|
||||
} catch (SQLException ex) {
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
connection = null;
|
||||
|
||||
if (reconnectAttempt == 0 || reconnectAttempt >= 11) {
|
||||
mcMMO.p.getLogger().info("Connection to MySQL failed!");
|
||||
}
|
||||
} catch (ClassNotFoundException ex) {
|
||||
}
|
||||
catch (ClassNotFoundException ex) {
|
||||
connection = null;
|
||||
|
||||
if (reconnectAttempt == 0 || reconnectAttempt >= 11) {
|
||||
mcMMO.p.getLogger().info("MySQL database driver not found!");
|
||||
}
|
||||
@ -147,108 +153,10 @@ public final class Database {
|
||||
+ "FOREIGN KEY (`user_id`) REFERENCES `" + tablePrefix + "users` (`id`) "
|
||||
+ "ON DELETE CASCADE) ENGINE=MyISAM DEFAULT CHARSET=latin1;");
|
||||
|
||||
checkDatabaseStructure(DatabaseUpdate.FISHING);
|
||||
checkDatabaseStructure(DatabaseUpdate.BLAST_MINING);
|
||||
checkDatabaseStructure(DatabaseUpdate.CASCADE_DELETE);
|
||||
checkDatabaseStructure(DatabaseUpdate.INDEX);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check database structure for missing values.
|
||||
*
|
||||
* @param update Type of data to check updates for
|
||||
*/
|
||||
private static void checkDatabaseStructure(DatabaseUpdate update) {
|
||||
String sql = null;
|
||||
ResultSet resultSet = null;
|
||||
HashMap<Integer, ArrayList<String>> rows = new HashMap<Integer, ArrayList<String>>();
|
||||
|
||||
switch (update) {
|
||||
case BLAST_MINING:
|
||||
sql = "SELECT * FROM `" + tablePrefix + "cooldowns` ORDER BY `" + tablePrefix + "cooldowns`.`blast_mining` ASC LIMIT 0 , 30";
|
||||
break;
|
||||
|
||||
case CASCADE_DELETE:
|
||||
write("ALTER TABLE `" + tablePrefix + "huds` ADD FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE;");
|
||||
write("ALTER TABLE `" + tablePrefix + "experience` ADD FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE;");
|
||||
write("ALTER TABLE `" + tablePrefix + "cooldowns` ADD FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE;");
|
||||
write("ALTER TABLE `" + tablePrefix + "skills` ADD FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE;");
|
||||
break;
|
||||
|
||||
case FISHING:
|
||||
sql = "SELECT * FROM `" + tablePrefix + "experience` ORDER BY `" + tablePrefix + "experience`.`fishing` ASC LIMIT 0 , 30";
|
||||
break;
|
||||
|
||||
case INDEX:
|
||||
if (read("SHOW INDEX FROM " + tablePrefix + "skills").size() != 13 && checkConnected()) {
|
||||
mcMMO.p.getLogger().info("Indexing tables, this may take a while on larger databases");
|
||||
write("ALTER TABLE `" + tablePrefix + "skills` ADD INDEX `idx_taming` (`taming`) USING BTREE, "
|
||||
+ "ADD INDEX `idx_mining` (`mining`) USING BTREE, "
|
||||
+ "ADD INDEX `idx_woodcutting` (`woodcutting`) USING BTREE, "
|
||||
+ "ADD INDEX `idx_repair` (`repair`) USING BTREE, "
|
||||
+ "ADD INDEX `idx_unarmed` (`unarmed`) USING BTREE, "
|
||||
+ "ADD INDEX `idx_herbalism` (`herbalism`) USING BTREE, "
|
||||
+ "ADD INDEX `idx_excavation` (`excavation`) USING BTREE, "
|
||||
+ "ADD INDEX `idx_archery` (`archery`) USING BTREE, "
|
||||
+ "ADD INDEX `idx_swords` (`swords`) USING BTREE, "
|
||||
+ "ADD INDEX `idx_axes` (`axes`) USING BTREE, "
|
||||
+ "ADD INDEX `idx_acrobatics` (`acrobatics`) USING BTREE, "
|
||||
+ "ADD INDEX `idx_fishing` (`fishing`) USING BTREE;");
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
PreparedStatement statement = null;
|
||||
try {
|
||||
if (!checkConnected()) return;
|
||||
statement = connection.prepareStatement(sql);
|
||||
resultSet = statement.executeQuery();
|
||||
|
||||
while (resultSet.next()) {
|
||||
ArrayList<String> column = new ArrayList<String>();
|
||||
|
||||
for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) {
|
||||
column.add(resultSet.getString(i));
|
||||
}
|
||||
|
||||
rows.put(resultSet.getRow(), column);
|
||||
}
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
switch (update) {
|
||||
case BLAST_MINING:
|
||||
mcMMO.p.getLogger().info("Updating mcMMO MySQL tables for Blast Mining...");
|
||||
write("ALTER TABLE `"+tablePrefix + "cooldowns` ADD `blast_mining` int(32) NOT NULL DEFAULT '0' ;");
|
||||
break;
|
||||
|
||||
case FISHING:
|
||||
mcMMO.p.getLogger().info("Updating mcMMO MySQL tables for Fishing...");
|
||||
write("ALTER TABLE `"+tablePrefix + "skills` ADD `fishing` int(10) NOT NULL DEFAULT '0' ;");
|
||||
write("ALTER TABLE `"+tablePrefix + "experience` ADD `fishing` int(10) NOT NULL DEFAULT '0' ;");
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} finally {
|
||||
if (resultSet != null) {
|
||||
try {
|
||||
resultSet.close();
|
||||
} catch (SQLException e) {
|
||||
// Ignore the error, we're leaving
|
||||
}
|
||||
}
|
||||
if (statement != null) {
|
||||
try {
|
||||
statement.close();
|
||||
} catch (SQLException e) {
|
||||
// Ignore the error, we're leaving
|
||||
}
|
||||
}
|
||||
}
|
||||
checkDatabaseStructure(DatabaseUpdateType.FISHING);
|
||||
checkDatabaseStructure(DatabaseUpdateType.BLAST_MINING);
|
||||
checkDatabaseStructure(DatabaseUpdateType.CASCADE_DELETE);
|
||||
checkDatabaseStructure(DatabaseUpdateType.INDEX);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -258,29 +166,31 @@ public final class Database {
|
||||
* @return true if the query was successfully written, false otherwise.
|
||||
*/
|
||||
public static boolean write(String sql) {
|
||||
if (checkConnected()) {
|
||||
PreparedStatement statement = null;
|
||||
try {
|
||||
statement = connection.prepareStatement(sql);
|
||||
statement.executeUpdate();
|
||||
return true;
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
printErrors(ex);
|
||||
return false;
|
||||
} finally {
|
||||
if (statement != null) {
|
||||
try {
|
||||
statement.close();
|
||||
} catch (SQLException e) {
|
||||
printErrors(e);
|
||||
return false;
|
||||
}
|
||||
if (!checkConnected()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
PreparedStatement statement = null;
|
||||
try {
|
||||
statement = connection.prepareStatement(sql);
|
||||
statement.executeUpdate();
|
||||
return true;
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
printErrors(ex);
|
||||
return false;
|
||||
}
|
||||
finally {
|
||||
if (statement != null) {
|
||||
try {
|
||||
statement.close();
|
||||
}
|
||||
catch (SQLException e) {
|
||||
printErrors(e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -291,22 +201,23 @@ public final class Database {
|
||||
*/
|
||||
public static int update(String sql) {
|
||||
int ret = 0;
|
||||
|
||||
if (checkConnected()) {
|
||||
PreparedStatement statement = null;
|
||||
try {
|
||||
statement = connection.prepareStatement(sql);
|
||||
ret = statement.executeUpdate();
|
||||
return ret;
|
||||
} catch (SQLException ex) {
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
printErrors(ex);
|
||||
return 0;
|
||||
} finally {
|
||||
}
|
||||
finally {
|
||||
if (statement != null) {
|
||||
try {
|
||||
statement.close();
|
||||
} catch (SQLException e) {
|
||||
}
|
||||
catch (SQLException e) {
|
||||
printErrors(e);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -322,12 +233,14 @@ public final class Database {
|
||||
* @return the value in the first row / first field
|
||||
*/
|
||||
public static int getInt(String sql) {
|
||||
ResultSet resultSet;
|
||||
ResultSet resultSet = null;
|
||||
int result = 0;
|
||||
|
||||
if (checkConnected()) {
|
||||
PreparedStatement statement = null;
|
||||
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement(sql);
|
||||
statement = connection.prepareStatement(sql);
|
||||
resultSet = statement.executeQuery();
|
||||
|
||||
if (resultSet.next()) {
|
||||
@ -336,12 +249,20 @@ public final class Database {
|
||||
else {
|
||||
result = 0;
|
||||
}
|
||||
|
||||
statement.close();
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
printErrors(ex);
|
||||
}
|
||||
finally {
|
||||
if (statement != null) {
|
||||
try {
|
||||
statement.close();
|
||||
}
|
||||
catch (SQLException e) {
|
||||
printErrors(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
@ -375,7 +296,8 @@ public final class Database {
|
||||
if (exists) {
|
||||
try {
|
||||
isClosed = connection.isClosed();
|
||||
} catch (SQLException e) {
|
||||
}
|
||||
catch (SQLException e) {
|
||||
isClosed = true;
|
||||
e.printStackTrace();
|
||||
printErrors(e);
|
||||
@ -384,9 +306,9 @@ public final class Database {
|
||||
if (!isClosed) {
|
||||
try {
|
||||
isValid = connection.isValid(VALID_TIMEOUT);
|
||||
} catch (SQLException e) {
|
||||
// Don't print stack trace because it's valid to lose idle connections
|
||||
// to the server and have to restart them.
|
||||
}
|
||||
catch (SQLException e) {
|
||||
// Don't print stack trace because it's valid to lose idle connections to the server and have to restart them.
|
||||
isValid = false;
|
||||
}
|
||||
}
|
||||
@ -404,7 +326,8 @@ public final class Database {
|
||||
if (exists && !isClosed) {
|
||||
try {
|
||||
connection.close();
|
||||
} catch (SQLException ex) {
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
// This is a housekeeping exercise, ignore errors
|
||||
}
|
||||
}
|
||||
@ -417,22 +340,21 @@ public final class Database {
|
||||
if (connection != null && !connection.isClosed()) {
|
||||
// Schedule a database save if we really had an outage
|
||||
if (reconnectAttempt > 1) {
|
||||
mcMMO.p.getServer().getScheduler().scheduleSyncDelayedTask(mcMMO.p, new SQLReconnect(), 5);
|
||||
mcMMO.p.getServer().getScheduler().scheduleSyncDelayedTask(mcMMO.p, new SQLReconnectTask(), 5);
|
||||
}
|
||||
nextReconnectTimestamp = 0;
|
||||
reconnectAttempt = 0;
|
||||
return true;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
}
|
||||
catch (SQLException e) {
|
||||
// Failed to check isClosed, so presume connection is bad and attempt later
|
||||
e.printStackTrace();
|
||||
printErrors(e);
|
||||
}
|
||||
|
||||
reconnectAttempt++;
|
||||
|
||||
nextReconnectTimestamp = (long)(System.nanoTime() + Math.min(MAX_WAIT, (reconnectAttempt*SCALING_FACTOR*MIN_WAIT)));
|
||||
|
||||
nextReconnectTimestamp = (long)(System.nanoTime() + Math.min(MAX_WAIT, (reconnectAttempt * SCALING_FACTOR * MIN_WAIT)));
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -447,8 +369,10 @@ public final class Database {
|
||||
HashMap<Integer, ArrayList<String>> rows = new HashMap<Integer, ArrayList<String>>();
|
||||
|
||||
if (checkConnected()) {
|
||||
PreparedStatement statement = null;
|
||||
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement(sql);
|
||||
statement = connection.prepareStatement(sql);
|
||||
resultSet = statement.executeQuery();
|
||||
|
||||
while (resultSet.next()) {
|
||||
@ -460,12 +384,20 @@ public final class Database {
|
||||
|
||||
rows.put(resultSet.getRow(), column);
|
||||
}
|
||||
|
||||
statement.close();
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
printErrors(ex);
|
||||
}
|
||||
finally {
|
||||
if (statement != null) {
|
||||
try {
|
||||
statement.close();
|
||||
}
|
||||
catch (SQLException e) {
|
||||
printErrors(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return rows;
|
||||
@ -477,12 +409,15 @@ public final class Database {
|
||||
|
||||
if (checkConnected()) {
|
||||
try {
|
||||
for (SkillType skillType: SkillType.values()) {
|
||||
for (SkillType skillType : SkillType.values()) {
|
||||
if (skillType.isChildSkill()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String sql = "SELECT COUNT(*) AS rank FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id WHERE " + skillType.name().toLowerCase() + " > 0 AND " + skillType.name().toLowerCase() + " > (SELECT " + skillType.name().toLowerCase() + " FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id WHERE user = '" + playerName + "')";
|
||||
String skillName = skillType.name().toLowerCase();
|
||||
String sql = "SELECT COUNT(*) AS rank FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id WHERE " + skillName + " > 0 " +
|
||||
"AND " + skillName + " > (SELECT " + skillName + " FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id " +
|
||||
"WHERE user = '" + playerName + "')";
|
||||
|
||||
PreparedStatement statement = connection.prepareStatement(sql);
|
||||
resultSet = statement.executeQuery();
|
||||
@ -491,7 +426,9 @@ public final class Database {
|
||||
|
||||
int rank = resultSet.getInt("rank");
|
||||
|
||||
sql = "SELECT user, " + skillType.name().toLowerCase() + " FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id WHERE " + skillType.name().toLowerCase() + " > 0 AND " + skillType.name().toLowerCase() + " = (SELECT " + skillType.name().toLowerCase() + " FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id WHERE user = '" + playerName + "') ORDER BY user";
|
||||
sql = "SELECT user, " + skillName + " FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id WHERE " + skillName + " > 0 " +
|
||||
"AND " + skillName + " = (SELECT " + skillName + " FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id " +
|
||||
"WHERE user = '" + playerName + "') ORDER BY user";
|
||||
|
||||
statement = connection.prepareStatement(sql);
|
||||
resultSet = statement.executeQuery();
|
||||
@ -506,7 +443,11 @@ public final class Database {
|
||||
statement.close();
|
||||
}
|
||||
|
||||
String sql = "SELECT COUNT(*) AS rank FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id WHERE taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing > 0 AND taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing > (SELECT taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id WHERE user = '" + playerName + "')";
|
||||
String sql = "SELECT COUNT(*) AS rank FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id " +
|
||||
"WHERE taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing > 0 " +
|
||||
"AND taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing > " +
|
||||
"(SELECT taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing " +
|
||||
"FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id WHERE user = '" + playerName + "')";
|
||||
|
||||
PreparedStatement statement = connection.prepareStatement(sql);
|
||||
resultSet = statement.executeQuery();
|
||||
@ -515,7 +456,12 @@ public final class Database {
|
||||
|
||||
int rank = resultSet.getInt("rank");
|
||||
|
||||
sql = "SELECT user, taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id WHERE taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing > 0 AND taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing = (SELECT taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id WHERE user = '" + playerName + "') ORDER BY user";
|
||||
sql = "SELECT user, taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing " +
|
||||
"FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id " +
|
||||
"WHERE taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing > 0 " +
|
||||
"AND taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing = " +
|
||||
"(SELECT taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing " +
|
||||
"FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id WHERE user = '" + playerName + "') ORDER BY user";
|
||||
|
||||
statement = connection.prepareStatement(sql);
|
||||
resultSet = statement.executeQuery();
|
||||
@ -539,14 +485,20 @@ public final class Database {
|
||||
|
||||
public static void purgePowerlessSQL() {
|
||||
mcMMO.p.getLogger().info("Purging powerless users...");
|
||||
HashMap<Integer, ArrayList<String>> usernames = read("SELECT u.user FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u WHERE s.user_id = u.id AND (s.taming+s.mining+s.woodcutting+s.repair+s.unarmed+s.herbalism+s.excavation+s.archery+s.swords+s.axes+s.acrobatics+s.fishing) = 0");
|
||||
write("DELETE FROM " + tablePrefix + "users WHERE " + tablePrefix + "users.id IN (SELECT * FROM (SELECT u.id FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u WHERE s.user_id = u.id AND (s.taming+s.mining+s.woodcutting+s.repair+s.unarmed+s.herbalism+s.excavation+s.archery+s.swords+s.axes+s.acrobatics+s.fishing) = 0) AS p)");
|
||||
HashMap<Integer, ArrayList<String>> usernames;
|
||||
|
||||
usernames = read("SELECT u.user FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u " + "WHERE s.user_id = u.id AND " +
|
||||
"(s.taming+s.mining+s.woodcutting+s.repair+s.unarmed+s.herbalism+s.excavation+s.archery+s.swords+s.axes+s.acrobatics+s.fishing) = 0");
|
||||
|
||||
write("DELETE FROM " + tablePrefix + "users WHERE " + tablePrefix + "users.id IN (SELECT * FROM " +
|
||||
"(SELECT u.id FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u " + "WHERE s.user_id = u.id " +
|
||||
"AND (s.taming+s.mining+s.woodcutting+s.repair+s.unarmed+s.herbalism+s.excavation+s.archery+s.swords+s.axes+s.acrobatics+s.fishing) = 0) AS p)");
|
||||
|
||||
int purgedUsers = 0;
|
||||
for (int i = 1; i <= usernames.size(); i++) {
|
||||
String playerName = usernames.get(i).get(0);
|
||||
|
||||
if (playerName == null || Bukkit.getOfflinePlayer(playerName).isOnline()) {
|
||||
if (playerName == null || mcMMO.p.getServer().getOfflinePlayer(playerName).isOnline()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -560,7 +512,7 @@ public final class Database {
|
||||
public static void purgeOldSQL() {
|
||||
mcMMO.p.getLogger().info("Purging old users...");
|
||||
long currentTime = System.currentTimeMillis();
|
||||
long purgeTime = 2630000000L * Config.getInstance().getOldUsersCutoff();
|
||||
long purgeTime = ONE_MONTH * Config.getInstance().getOldUsersCutoff();
|
||||
HashMap<Integer, ArrayList<String>> usernames = read("SELECT user FROM " + tablePrefix + "users WHERE ((" + currentTime + " - lastlogin*1000) > " + purgeTime + ")");
|
||||
write("DELETE FROM " + tablePrefix + "users WHERE " + tablePrefix + "users.id IN (SELECT * FROM (SELECT id FROM " + tablePrefix + "users WHERE ((" + currentTime + " - lastlogin*1000) > " + purgeTime + ")) AS p)");
|
||||
|
||||
@ -579,32 +531,137 @@ public final class Database {
|
||||
mcMMO.p.getLogger().info("Purged " + purgedUsers + " users from the database.");
|
||||
}
|
||||
|
||||
private static void printErrors(SQLException ex) {
|
||||
mcMMO.p.getLogger().severe("SQLException: " + ex.getMessage());
|
||||
mcMMO.p.getLogger().severe("SQLState: " + ex.getSQLState());
|
||||
mcMMO.p.getLogger().severe("VendorError: " + ex.getErrorCode());
|
||||
}
|
||||
|
||||
public static void profileCleanup(String playerName) {
|
||||
McMMOPlayer mcmmoPlayer = Users.getPlayer(playerName);
|
||||
McMMOPlayer mcMMOPlayer = UserManager.getPlayer(playerName);
|
||||
|
||||
if (mcmmoPlayer != null) {
|
||||
Player player = mcmmoPlayer.getPlayer();
|
||||
SpoutHud spoutHud = mcmmoPlayer.getProfile().getSpoutHud();
|
||||
if (mcMMOPlayer != null) {
|
||||
Player player = mcMMOPlayer.getPlayer();
|
||||
McMMOHud spoutHud = mcMMOPlayer.getProfile().getSpoutHud();
|
||||
|
||||
if (spoutHud != null) {
|
||||
spoutHud.removeWidgets();
|
||||
}
|
||||
|
||||
Users.remove(playerName);
|
||||
UserManager.remove(playerName);
|
||||
|
||||
if (player.isOnline()) {
|
||||
Users.addUser(player);
|
||||
UserManager.addUser(player);
|
||||
|
||||
if (mcMMO.spoutEnabled) {
|
||||
SpoutTools.reloadSpoutPlayer(player);
|
||||
SpoutUtils.reloadSpoutPlayer(player);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check database structure for missing values.
|
||||
*
|
||||
* @param update Type of data to check updates for
|
||||
*/
|
||||
private static void checkDatabaseStructure(DatabaseUpdateType update) {
|
||||
String sql = null;
|
||||
ResultSet resultSet = null;
|
||||
HashMap<Integer, ArrayList<String>> rows = new HashMap<Integer, ArrayList<String>>();
|
||||
|
||||
switch (update) {
|
||||
case BLAST_MINING:
|
||||
sql = "SELECT * FROM `" + tablePrefix + "cooldowns` ORDER BY `" + tablePrefix + "cooldowns`.`blast_mining` ASC LIMIT 0 , 30";
|
||||
break;
|
||||
|
||||
case CASCADE_DELETE:
|
||||
write("ALTER TABLE `" + tablePrefix + "huds` ADD FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE;");
|
||||
write("ALTER TABLE `" + tablePrefix + "experience` ADD FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE;");
|
||||
write("ALTER TABLE `" + tablePrefix + "cooldowns` ADD FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE;");
|
||||
write("ALTER TABLE `" + tablePrefix + "skills` ADD FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE;");
|
||||
break;
|
||||
|
||||
case FISHING:
|
||||
sql = "SELECT * FROM `" + tablePrefix + "experience` ORDER BY `" + tablePrefix + "experience`.`fishing` ASC LIMIT 0 , 30";
|
||||
break;
|
||||
|
||||
case INDEX:
|
||||
if (read("SHOW INDEX FROM " + tablePrefix + "skills").size() != 13 && checkConnected()) {
|
||||
mcMMO.p.getLogger().info("Indexing tables, this may take a while on larger databases");
|
||||
write("ALTER TABLE `" + tablePrefix + "skills` ADD INDEX `idx_taming` (`taming`) USING BTREE, "
|
||||
+ "ADD INDEX `idx_mining` (`mining`) USING BTREE, "
|
||||
+ "ADD INDEX `idx_woodcutting` (`woodcutting`) USING BTREE, "
|
||||
+ "ADD INDEX `idx_repair` (`repair`) USING BTREE, "
|
||||
+ "ADD INDEX `idx_unarmed` (`unarmed`) USING BTREE, "
|
||||
+ "ADD INDEX `idx_herbalism` (`herbalism`) USING BTREE, "
|
||||
+ "ADD INDEX `idx_excavation` (`excavation`) USING BTREE, "
|
||||
+ "ADD INDEX `idx_archery` (`archery`) USING BTREE, "
|
||||
+ "ADD INDEX `idx_swords` (`swords`) USING BTREE, "
|
||||
+ "ADD INDEX `idx_axes` (`axes`) USING BTREE, "
|
||||
+ "ADD INDEX `idx_acrobatics` (`acrobatics`) USING BTREE, "
|
||||
+ "ADD INDEX `idx_fishing` (`fishing`) USING BTREE;");
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
PreparedStatement statement = null;
|
||||
try {
|
||||
if (!checkConnected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
statement = connection.prepareStatement(sql);
|
||||
resultSet = statement.executeQuery();
|
||||
|
||||
while (resultSet.next()) {
|
||||
ArrayList<String> column = new ArrayList<String>();
|
||||
|
||||
for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) {
|
||||
column.add(resultSet.getString(i));
|
||||
}
|
||||
|
||||
rows.put(resultSet.getRow(), column);
|
||||
}
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
switch (update) {
|
||||
case BLAST_MINING:
|
||||
mcMMO.p.getLogger().info("Updating mcMMO MySQL tables for Blast Mining...");
|
||||
write("ALTER TABLE `"+tablePrefix + "cooldowns` ADD `blast_mining` int(32) NOT NULL DEFAULT '0' ;");
|
||||
break;
|
||||
|
||||
case FISHING:
|
||||
mcMMO.p.getLogger().info("Updating mcMMO MySQL tables for Fishing...");
|
||||
write("ALTER TABLE `"+tablePrefix + "skills` ADD `fishing` int(10) NOT NULL DEFAULT '0' ;");
|
||||
write("ALTER TABLE `"+tablePrefix + "experience` ADD `fishing` int(10) NOT NULL DEFAULT '0' ;");
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
finally {
|
||||
if (resultSet != null) {
|
||||
try {
|
||||
resultSet.close();
|
||||
}
|
||||
catch (SQLException e) {
|
||||
// Ignore the error, we're leaving
|
||||
}
|
||||
}
|
||||
|
||||
if (statement != null) {
|
||||
try {
|
||||
statement.close();
|
||||
}
|
||||
catch (SQLException e) {
|
||||
// Ignore the error, we're leaving
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void printErrors(SQLException ex) {
|
||||
mcMMO.p.getLogger().severe("SQLException: " + ex.getMessage());
|
||||
mcMMO.p.getLogger().severe("SQLState: " + ex.getSQLState());
|
||||
mcMMO.p.getLogger().severe("VendorError: " + ex.getErrorCode());
|
||||
}
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
package com.gmail.nossr50.database;
|
||||
|
||||
public enum DatabaseUpdate {
|
||||
FISHING,
|
||||
BLAST_MINING,
|
||||
CASCADE_DELETE,
|
||||
INDEX;
|
||||
}
|
@ -10,48 +10,49 @@ import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import com.gmail.nossr50.mcMMO;
|
||||
import com.gmail.nossr50.config.Config;
|
||||
import com.gmail.nossr50.skills.utilities.SkillType;
|
||||
import com.gmail.nossr50.datatypes.database.PlayerStat;
|
||||
import com.gmail.nossr50.datatypes.skills.SkillType;
|
||||
import com.gmail.nossr50.util.StringUtils;
|
||||
|
||||
public final class Leaderboard {
|
||||
public final class LeaderboardManager {
|
||||
private static HashMap<SkillType, List<PlayerStat>> playerStatHash = new HashMap<SkillType, List<PlayerStat>>();
|
||||
private static List<PlayerStat> powerLevels = new ArrayList<PlayerStat>();
|
||||
private static long lastUpdate = 0;
|
||||
|
||||
private Leaderboard() {}
|
||||
private static final long UPDATE_WAIT_TIME = 600000L; // 10 minutes
|
||||
private static final long ONE_MONTH = 2630000000L;
|
||||
|
||||
private LeaderboardManager() {}
|
||||
|
||||
/**
|
||||
* Update the leader boards.
|
||||
*/
|
||||
public static void updateLeaderboards() {
|
||||
if(System.currentTimeMillis() < lastUpdate + 600000) {
|
||||
return; //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) {
|
||||
return;
|
||||
}
|
||||
|
||||
lastUpdate = System.currentTimeMillis(); //Log when the last update was run
|
||||
lastUpdate = System.currentTimeMillis(); // Log when the last update was run
|
||||
|
||||
//Initialize lists
|
||||
List<PlayerStat> mining, woodcutting, herbalism, excavation, acrobatics, repair, swords, axes, archery, unarmed, taming, fishing;
|
||||
// Initialize lists
|
||||
List<PlayerStat> mining = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> woodcutting = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> herbalism = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> excavation = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> acrobatics = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> repair = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> swords = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> axes = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> archery = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> unarmed = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> taming = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> fishing = new ArrayList<PlayerStat>();
|
||||
List<PlayerStat> powerLevels = new ArrayList<PlayerStat>();
|
||||
|
||||
mining = new ArrayList<PlayerStat>();
|
||||
woodcutting = new ArrayList<PlayerStat>();
|
||||
herbalism = new ArrayList<PlayerStat>();
|
||||
excavation = new ArrayList<PlayerStat>();
|
||||
acrobatics = new ArrayList<PlayerStat>();
|
||||
repair = new ArrayList<PlayerStat>();
|
||||
swords = new ArrayList<PlayerStat>();
|
||||
axes = new ArrayList<PlayerStat>();
|
||||
archery = new ArrayList<PlayerStat>();
|
||||
unarmed = new ArrayList<PlayerStat>();
|
||||
taming = new ArrayList<PlayerStat>();
|
||||
fishing = new ArrayList<PlayerStat>();
|
||||
powerLevels = new ArrayList<PlayerStat>();
|
||||
|
||||
//Read from the FlatFile database and fill our arrays with information
|
||||
// Read from the FlatFile database and fill our arrays with information
|
||||
try {
|
||||
FileReader file = new FileReader(mcMMO.getUsersFilePath());
|
||||
BufferedReader in = new BufferedReader(file);
|
||||
@ -64,7 +65,7 @@ public final class Leaderboard {
|
||||
String p = character[0];
|
||||
int powerLevel = 0;
|
||||
|
||||
//Prevent the same player from being added multiple times (I'd like to note that this shouldn't happen...)
|
||||
// Prevent the same player from being added multiple times (I'd like to note that this shouldn't happen...)
|
||||
if (players.contains(p)) {
|
||||
continue;
|
||||
}
|
||||
@ -136,10 +137,11 @@ public final class Leaderboard {
|
||||
in.close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
mcMMO.p.getLogger().severe(("Exception while reading " + mcMMO.getUsersFilePath() + " (Are you sure you formatted it correctly?)" + e.toString()));
|
||||
mcMMO.p.getLogger().severe("Exception while reading " + mcMMO.getUsersFilePath() + " (Are you sure you formatted it correctly?)" + e.toString());
|
||||
}
|
||||
|
||||
SkillComparator c = new SkillComparator();
|
||||
|
||||
Collections.sort(mining, c);
|
||||
Collections.sort(woodcutting, c);
|
||||
Collections.sort(repair, c);
|
||||
@ -227,6 +229,7 @@ public final class Leaderboard {
|
||||
currentPos++;
|
||||
continue;
|
||||
}
|
||||
|
||||
return new int[] {0, 0};
|
||||
}
|
||||
|
||||
@ -246,19 +249,13 @@ public final class Leaderboard {
|
||||
currentPos++;
|
||||
continue;
|
||||
}
|
||||
|
||||
return new int[] {0, 0};
|
||||
}
|
||||
|
||||
return new int[] {0, 0};
|
||||
}
|
||||
|
||||
private static class SkillComparator implements Comparator<PlayerStat> {
|
||||
@Override
|
||||
public int compare(PlayerStat o1, PlayerStat o2) {
|
||||
return (o2.statVal - o1.statVal);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean removeFlatFileUser(String playerName) {
|
||||
boolean worked = false;
|
||||
|
||||
@ -274,18 +271,18 @@ public final class Leaderboard {
|
||||
|
||||
while ((line = in.readLine()) != null) {
|
||||
|
||||
/* Write out the same file but when we get to the player we want to remove, we skip his line. */
|
||||
// Write out the same file but when we get to the player we want to remove, we skip his line.
|
||||
if (!line.split(":")[0].equalsIgnoreCase(playerName)) {
|
||||
writer.append(line).append("\r\n");
|
||||
}
|
||||
else {
|
||||
mcMMO.p.getLogger().info("User found, removing...");
|
||||
worked = true;
|
||||
continue; //Skip the player
|
||||
continue; // Skip the player
|
||||
}
|
||||
}
|
||||
|
||||
out = new FileWriter(usersFilePath); //Write out the new file
|
||||
out = new FileWriter(usersFilePath); // Write out the new file
|
||||
out.write(writer.toString());
|
||||
}
|
||||
catch (Exception e) {
|
||||
@ -314,13 +311,12 @@ public final class Leaderboard {
|
||||
return worked;
|
||||
}
|
||||
|
||||
|
||||
public static void purgePowerlessFlatfile() {
|
||||
mcMMO.p.getLogger().info("Purging powerless users...");
|
||||
|
||||
int purgedUsers = 0;
|
||||
for (PlayerStat stat : powerLevels) {
|
||||
if (stat.statVal == 0 && removeFlatFileUser(stat.name) && !Bukkit.getOfflinePlayer(stat.name).isOnline()) {
|
||||
if (stat.statVal == 0 && removeFlatFileUser(stat.name) && !mcMMO.p.getServer().getOfflinePlayer(stat.name).isOnline()) {
|
||||
purgedUsers++;
|
||||
}
|
||||
}
|
||||
@ -337,7 +333,7 @@ public final class Leaderboard {
|
||||
private static int removeOldFlatfileUsers() {
|
||||
int removedPlayers = 0;
|
||||
long currentTime = System.currentTimeMillis();
|
||||
long purgeTime = 2630000000L * Config.getInstance().getOldUsersCutoff();
|
||||
long purgeTime = ONE_MONTH * Config.getInstance().getOldUsersCutoff();
|
||||
|
||||
BufferedReader in = null;
|
||||
FileWriter out = null;
|
||||
@ -351,7 +347,7 @@ public final class Leaderboard {
|
||||
|
||||
while ((line = in.readLine()) != null) {
|
||||
|
||||
/* Write out the same file but when we get to the player we want to remove, we skip his line. */
|
||||
// Write out the same file but when we get to the player we want to remove, we skip his line.
|
||||
String[] splitLine = line.split(":");
|
||||
|
||||
if (splitLine.length > 37) {
|
||||
@ -361,7 +357,7 @@ public final class Leaderboard {
|
||||
else {
|
||||
mcMMO.p.getLogger().info("User found, removing...");
|
||||
removedPlayers++;
|
||||
continue; //Skip the player
|
||||
continue; // Skip the player
|
||||
}
|
||||
}
|
||||
else {
|
||||
@ -369,7 +365,7 @@ public final class Leaderboard {
|
||||
}
|
||||
}
|
||||
|
||||
out = new FileWriter(usersFilePath); //Write out the new file
|
||||
out = new FileWriter(usersFilePath); // Write out the new file
|
||||
out.write(writer.toString());
|
||||
}
|
||||
catch (Exception e) {
|
||||
@ -398,4 +394,10 @@ public final class Leaderboard {
|
||||
return removedPlayers;
|
||||
}
|
||||
|
||||
private static class SkillComparator implements Comparator<PlayerStat> {
|
||||
@Override
|
||||
public int compare(PlayerStat o1, PlayerStat o2) {
|
||||
return (o2.statVal - o1.statVal);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
package com.gmail.nossr50.database;
|
||||
|
||||
public class PlayerStat {
|
||||
public String name;
|
||||
public int statVal = 0;
|
||||
|
||||
public PlayerStat(String name, int value) {
|
||||
this.name = name;
|
||||
this.statVal = value;
|
||||
}
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
package com.gmail.nossr50.database.commands;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import com.gmail.nossr50.config.Config;
|
||||
import com.gmail.nossr50.database.Database;
|
||||
import com.gmail.nossr50.database.Leaderboard;
|
||||
import com.gmail.nossr50.locale.LocaleLoader;
|
||||
import com.gmail.nossr50.util.Permissions;
|
||||
|
||||
public class McpurgeCommand implements CommandExecutor{
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
if (!Permissions.mcpurge(sender)) {
|
||||
sender.sendMessage(command.getPermissionMessage());
|
||||
return true;
|
||||
}
|
||||
|
||||
switch (args.length) {
|
||||
case 0:
|
||||
if (Config.getInstance().getUseMySQL()) {
|
||||
Database.purgePowerlessSQL();
|
||||
|
||||
if (Config.getInstance().getOldUsersCutoff() != -1) {
|
||||
Database.purgeOldSQL();
|
||||
}
|
||||
}
|
||||
else {
|
||||
Leaderboard.purgePowerlessFlatfile();
|
||||
|
||||
if (Config.getInstance().getOldUsersCutoff() != -1) {
|
||||
Leaderboard.purgeOldFlatfile();
|
||||
}
|
||||
}
|
||||
|
||||
sender.sendMessage(LocaleLoader.getString("Commands.mcpurge.Success"));
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,51 +0,0 @@
|
||||
package com.gmail.nossr50.database.commands;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import com.gmail.nossr50.config.Config;
|
||||
import com.gmail.nossr50.database.Database;
|
||||
import com.gmail.nossr50.database.Leaderboard;
|
||||
import com.gmail.nossr50.locale.LocaleLoader;
|
||||
import com.gmail.nossr50.util.Permissions;
|
||||
|
||||
public class McremoveCommand implements CommandExecutor {
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
if (!Permissions.mcremove(sender)) {
|
||||
sender.sendMessage(command.getPermissionMessage());
|
||||
return true;
|
||||
}
|
||||
|
||||
switch (args.length) {
|
||||
case 1:
|
||||
/* MySQL */
|
||||
if (Config.getInstance().getUseMySQL()) {
|
||||
String tablePrefix = Config.getInstance().getMySQLTablePrefix();
|
||||
|
||||
if (Database.update("DELETE FROM " + tablePrefix + "users WHERE " + tablePrefix + "users.user = '" + args[0] + "'") != 0) {
|
||||
Database.profileCleanup(args[0]);
|
||||
sender.sendMessage(LocaleLoader.getString("Commands.mcremove.Success", args[0]));
|
||||
}
|
||||
else {
|
||||
sender.sendMessage(LocaleLoader.getString("Commands.DoesNotExist"));
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (Leaderboard.removeFlatFileUser(args[0])) {
|
||||
Database.profileCleanup(args[0]);
|
||||
sender.sendMessage(LocaleLoader.getString("Commands.mcremove.Success", args[0]));
|
||||
}
|
||||
else {
|
||||
sender.sendMessage(LocaleLoader.getString("Commands.DoesNotExist"));
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,51 +0,0 @@
|
||||
package com.gmail.nossr50.database.commands;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.gmail.nossr50.mcMMO;
|
||||
import com.gmail.nossr50.config.Config;
|
||||
import com.gmail.nossr50.database.runnables.SQLConversionTask;
|
||||
import com.gmail.nossr50.locale.LocaleLoader;
|
||||
import com.gmail.nossr50.util.Permissions;
|
||||
import com.gmail.nossr50.util.Users;
|
||||
|
||||
public class MmoupdateCommand implements CommandExecutor {
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
if (!Permissions.mmoupdate(sender)) {
|
||||
sender.sendMessage(command.getPermissionMessage());
|
||||
return true;
|
||||
}
|
||||
|
||||
switch (args.length) {
|
||||
case 0:
|
||||
sender.sendMessage(LocaleLoader.getString("Commands.mmoupdate.Start"));
|
||||
Users.clearAll();
|
||||
convertToMySQL();
|
||||
|
||||
for (Player player : mcMMO.p.getServer().getOnlinePlayers()) {
|
||||
Users.addUser(player);
|
||||
}
|
||||
|
||||
sender.sendMessage(LocaleLoader.getString("Commands.mmoupdate.Finish"));
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert FlatFile data to MySQL data.
|
||||
*/
|
||||
private void convertToMySQL() {
|
||||
if (!Config.getInstance().getUseMySQL()) {
|
||||
return;
|
||||
}
|
||||
|
||||
mcMMO.p.getServer().getScheduler().runTaskLaterAsynchronously(mcMMO.p, new SQLConversionTask(), 1);
|
||||
}
|
||||
}
|
@ -1,285 +0,0 @@
|
||||
package com.gmail.nossr50.database.runnables;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
|
||||
import com.gmail.nossr50.mcMMO;
|
||||
import com.gmail.nossr50.config.Config;
|
||||
import com.gmail.nossr50.database.Database;
|
||||
import com.gmail.nossr50.util.Misc;
|
||||
import com.gmail.nossr50.util.StringUtils;
|
||||
|
||||
public class SQLConversionTask implements Runnable {
|
||||
private String tablePrefix = Config.getInstance().getMySQLTablePrefix();
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
String location = mcMMO.getUsersFilePath();
|
||||
|
||||
try {
|
||||
FileReader file = new FileReader(location);
|
||||
BufferedReader in = new BufferedReader(file);
|
||||
String line = "";
|
||||
String playerName = null;
|
||||
//String party = null;
|
||||
String mining = null;
|
||||
String woodcutting = null;
|
||||
String repair = null;
|
||||
String unarmed = null;
|
||||
String herbalism = null;
|
||||
String excavation = null;
|
||||
String archery = null;
|
||||
String swords = null;
|
||||
String axes = null;
|
||||
String acrobatics = null;
|
||||
String taming = null;
|
||||
String fishing = null;
|
||||
String miningXP = null;
|
||||
String woodCuttingXP = null;
|
||||
String repairXP = null;
|
||||
String unarmedXP = null;
|
||||
String herbalismXP = null;
|
||||
String excavationXP = null;
|
||||
String archeryXP = null;
|
||||
String swordsXP = null;
|
||||
String axesXP = null;
|
||||
String acrobaticsXP = null;
|
||||
String tamingXP = null;
|
||||
String fishingXP = null;
|
||||
int id = 0;
|
||||
int theCount = 0;
|
||||
|
||||
while ((line = in.readLine()) != null) {
|
||||
|
||||
//Find if the line contains the player we want.
|
||||
String[] character = line.split(":");
|
||||
playerName = character[0];
|
||||
|
||||
//Check for things we don't want put in the DB
|
||||
if (playerName == null || playerName.equals("null") || playerName.equals("#Storage place for user information")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (character.length > 1) {
|
||||
mining = character[1];
|
||||
}
|
||||
|
||||
/*
|
||||
* Looks like we still have party as the 4th string in the array but we don't use it anymore
|
||||
if (character.length > 3) {
|
||||
party = character[3];
|
||||
}
|
||||
*/
|
||||
|
||||
if (character.length > 4) {
|
||||
miningXP = character[4];
|
||||
}
|
||||
|
||||
if (character.length > 5) {
|
||||
woodcutting = character[5];
|
||||
}
|
||||
|
||||
if (character.length > 6) {
|
||||
woodCuttingXP = character[6];
|
||||
}
|
||||
|
||||
if (character.length > 7) {
|
||||
repair = character[7];
|
||||
}
|
||||
|
||||
if (character.length > 8) {
|
||||
unarmed = character[8];
|
||||
}
|
||||
|
||||
if (character.length > 9) {
|
||||
herbalism = character[9];
|
||||
}
|
||||
|
||||
if (character.length > 10) {
|
||||
excavation = character[10];
|
||||
}
|
||||
|
||||
if (character.length > 11) {
|
||||
archery = character[11];
|
||||
}
|
||||
|
||||
if (character.length > 12) {
|
||||
swords = character[12];
|
||||
}
|
||||
|
||||
if (character.length > 13) {
|
||||
axes = character[13];
|
||||
}
|
||||
|
||||
if (character.length > 14) {
|
||||
acrobatics = character[14];
|
||||
}
|
||||
|
||||
if (character.length > 15) {
|
||||
repairXP = character[15];
|
||||
}
|
||||
|
||||
if (character.length > 16) {
|
||||
unarmedXP = character[16];
|
||||
}
|
||||
|
||||
if (character.length > 17) {
|
||||
herbalismXP = character[17];
|
||||
}
|
||||
|
||||
if (character.length > 18) {
|
||||
excavationXP = character[18];
|
||||
}
|
||||
|
||||
if (character.length > 19) {
|
||||
archeryXP = character[19];
|
||||
}
|
||||
|
||||
if (character.length > 20) {
|
||||
swordsXP = character[20];
|
||||
}
|
||||
|
||||
if (character.length > 21) {
|
||||
axesXP = character[21];
|
||||
}
|
||||
|
||||
if (character.length > 22) {
|
||||
acrobaticsXP = character[22];
|
||||
}
|
||||
|
||||
if (character.length > 24) {
|
||||
taming = character[24];
|
||||
}
|
||||
|
||||
if (character.length > 25) {
|
||||
tamingXP = character[25];
|
||||
}
|
||||
|
||||
if (character.length > 34) {
|
||||
fishing = character[34];
|
||||
}
|
||||
|
||||
if (character.length > 35) {
|
||||
fishingXP = character[35];
|
||||
}
|
||||
|
||||
//Check to see if the user is in the DB
|
||||
id = Database.getInt("SELECT id FROM "
|
||||
+ tablePrefix
|
||||
+ "users WHERE user = '" + playerName + "'");
|
||||
|
||||
if (id > 0) {
|
||||
theCount++;
|
||||
|
||||
//Update the skill values
|
||||
Database.write("UPDATE "
|
||||
+ tablePrefix
|
||||
+ "users SET lastlogin = " + 0
|
||||
+ " WHERE id = " + id);
|
||||
Database.write("UPDATE "
|
||||
+ tablePrefix
|
||||
+ "skills SET "
|
||||
+ " taming = taming+" + StringUtils.getInt(taming)
|
||||
+ ", mining = mining+" + StringUtils.getInt(mining)
|
||||
+ ", repair = repair+" + StringUtils.getInt(repair)
|
||||
+ ", woodcutting = woodcutting+" + StringUtils.getInt(woodcutting)
|
||||
+ ", unarmed = unarmed+" + StringUtils.getInt(unarmed)
|
||||
+ ", herbalism = herbalism+" + StringUtils.getInt(herbalism)
|
||||
+ ", excavation = excavation+" + StringUtils.getInt(excavation)
|
||||
+ ", archery = archery+" + StringUtils.getInt(archery)
|
||||
+ ", swords = swords+" + StringUtils.getInt(swords)
|
||||
+ ", axes = axes+" + StringUtils.getInt(axes)
|
||||
+ ", acrobatics = acrobatics+" + StringUtils.getInt(acrobatics)
|
||||
+ ", fishing = fishing+" + StringUtils.getInt(fishing)
|
||||
+ " WHERE user_id = " + id);
|
||||
Database.write("UPDATE "
|
||||
+ tablePrefix
|
||||
+ "experience SET "
|
||||
+ " taming = " + StringUtils.getInt(tamingXP)
|
||||
+ ", mining = " + StringUtils.getInt(miningXP)
|
||||
+ ", repair = " + StringUtils.getInt(repairXP)
|
||||
+ ", woodcutting = " + StringUtils.getInt(woodCuttingXP)
|
||||
+ ", unarmed = " + StringUtils.getInt(unarmedXP)
|
||||
+ ", herbalism = " + StringUtils.getInt(herbalismXP)
|
||||
+ ", excavation = " + StringUtils.getInt(excavationXP)
|
||||
+ ", archery = " + StringUtils.getInt(archeryXP)
|
||||
+ ", swords = " + StringUtils.getInt(swordsXP)
|
||||
+ ", axes = " + StringUtils.getInt(axesXP)
|
||||
+ ", acrobatics = " + StringUtils.getInt(acrobaticsXP)
|
||||
+ ", fishing = " + StringUtils.getInt(fishingXP)
|
||||
+ " WHERE user_id = " + id);
|
||||
}
|
||||
else {
|
||||
theCount++;
|
||||
|
||||
//Create the user in the DB
|
||||
Database.write("INSERT INTO "
|
||||
+ tablePrefix
|
||||
+ "users (user, lastlogin) VALUES ('"
|
||||
+ playerName + "',"
|
||||
+ System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR + ")");
|
||||
id = Database.getInt("SELECT id FROM "
|
||||
+ tablePrefix
|
||||
+ "users WHERE user = '"
|
||||
+ playerName + "'");
|
||||
Database.write("INSERT INTO "
|
||||
+ tablePrefix
|
||||
+ "skills (user_id) VALUES (" + id + ")");
|
||||
Database.write("INSERT INTO "
|
||||
+ tablePrefix
|
||||
+ "experience (user_id) VALUES (" + id
|
||||
+ ")");
|
||||
//Update the skill values
|
||||
Database.write("UPDATE "
|
||||
+ tablePrefix
|
||||
+ "users SET lastlogin = " + 0
|
||||
+ " WHERE id = " + id);
|
||||
/*
|
||||
Database.write("UPDATE "
|
||||
+ tablePrefix
|
||||
+ "users SET party = '" + party
|
||||
+ "' WHERE id = " + id);
|
||||
*/
|
||||
Database.write("UPDATE "
|
||||
+ tablePrefix
|
||||
+ "skills SET "
|
||||
+ " taming = taming+" + StringUtils.getInt(taming)
|
||||
+ ", mining = mining+" + StringUtils.getInt(mining)
|
||||
+ ", repair = repair+" + StringUtils.getInt(repair)
|
||||
+ ", woodcutting = woodcutting+" + StringUtils.getInt(woodcutting)
|
||||
+ ", unarmed = unarmed+" + StringUtils.getInt(unarmed)
|
||||
+ ", herbalism = herbalism+" + StringUtils.getInt(herbalism)
|
||||
+ ", excavation = excavation+" + StringUtils.getInt(excavation)
|
||||
+ ", archery = archery+" + StringUtils.getInt(archery)
|
||||
+ ", swords = swords+" + StringUtils.getInt(swords)
|
||||
+ ", axes = axes+" + StringUtils.getInt(axes)
|
||||
+ ", acrobatics = acrobatics+" + StringUtils.getInt(acrobatics)
|
||||
+ ", fishing = fishing+" + StringUtils.getInt(fishing)
|
||||
+ " WHERE user_id = " + id);
|
||||
Database.write("UPDATE "
|
||||
+ tablePrefix
|
||||
+ "experience SET "
|
||||
+ " taming = " + StringUtils.getInt(tamingXP)
|
||||
+ ", mining = " + StringUtils.getInt(miningXP)
|
||||
+ ", repair = " + StringUtils.getInt(repairXP)
|
||||
+ ", woodcutting = " + StringUtils.getInt(woodCuttingXP)
|
||||
+ ", unarmed = " + StringUtils.getInt(unarmedXP)
|
||||
+ ", herbalism = " + StringUtils.getInt(herbalismXP)
|
||||
+ ", excavation = " + StringUtils.getInt(excavationXP)
|
||||
+ ", archery = " + StringUtils.getInt(archeryXP)
|
||||
+ ", swords = " + StringUtils.getInt(swordsXP)
|
||||
+ ", axes = " + StringUtils.getInt(axesXP)
|
||||
+ ", acrobatics = " + StringUtils.getInt(acrobaticsXP)
|
||||
+ ", fishing = " + StringUtils.getInt(fishingXP)
|
||||
+ " WHERE user_id = " + id);
|
||||
}
|
||||
}
|
||||
|
||||
mcMMO.p.getLogger().info("[mcMMO] MySQL Updated from users file, " + theCount + " items added/updated to MySQL DB");
|
||||
in.close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
mcMMO.p.getLogger().severe("Exception while reading " + location + " (Are you sure you formatted it correctly?)" + e.toString());
|
||||
}
|
||||
}
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
package com.gmail.nossr50.database.runnables;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.gmail.nossr50.mcMMO;
|
||||
import com.gmail.nossr50.database.Database;
|
||||
import com.gmail.nossr50.util.Users;
|
||||
|
||||
public class SQLReconnect implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
if (Database.checkConnected()) {
|
||||
Users.saveAll(); //Save all profiles
|
||||
Users.clearAll(); //Clear the profiles
|
||||
|
||||
for (Player player : mcMMO.p.getServer().getOnlinePlayers()) {
|
||||
Users.addUser(player); //Add in new profiles, forcing them to 'load' again from MySQL
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
package com.gmail.nossr50.database.runnables;
|
||||
|
||||
import com.gmail.nossr50.config.Config;
|
||||
import com.gmail.nossr50.database.Database;
|
||||
import com.gmail.nossr50.database.Leaderboard;
|
||||
|
||||
public class UserPurgeTask implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
if (Config.getInstance().getUseMySQL()) {
|
||||
Database.purgePowerlessSQL();
|
||||
|
||||
if (Config.getInstance().getOldUsersCutoff() != -1) {
|
||||
Database.purgeOldSQL();
|
||||
}
|
||||
}
|
||||
else {
|
||||
Leaderboard.purgePowerlessFlatfile();
|
||||
|
||||
if (Config.getInstance().getOldUsersCutoff() != -1) {
|
||||
Leaderboard.purgeOldFlatfile();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user