2013-01-25 04:53:02 +01:00
package com.gmail.nossr50.database ;
2012-04-27 11:47:11 +02:00
2013-04-18 23:37:36 +02:00
import java.io.File ;
import java.io.IOException ;
2012-06-05 16:13:10 +02:00
2012-06-07 00:02:22 +02:00
import com.gmail.nossr50.mcMMO ;
2013-03-12 21:25:42 +01:00
import com.gmail.nossr50.util.Misc ;
2013-03-01 06:52:01 +01:00
2013-04-18 23:37:36 +02:00
public class DatabaseManager {
private final mcMMO plugin ;
private final boolean isUsingSQL ;
private File usersFile ;
2012-07-10 18:04:18 +02:00
2013-04-18 23:37:36 +02:00
public DatabaseManager ( final mcMMO plugin , final boolean isUsingSQL ) {
this . plugin = plugin ;
this . isUsingSQL = isUsingSQL ;
2012-10-22 14:45:16 +02:00
2013-04-18 23:37:36 +02:00
if ( isUsingSQL ) {
SQLDatabaseManager . checkConnected ( ) ;
SQLDatabaseManager . createStructure ( ) ;
2013-03-01 06:52:01 +01:00
}
2013-04-18 23:37:36 +02:00
else {
usersFile = new File ( mcMMO . getUsersFilePath ( ) ) ;
createFlatfileDatabase ( ) ;
FlatfileDatabaseManager . updateLeaderboards ( ) ;
2013-01-16 01:03:13 +01:00
}
2012-04-27 11:47:11 +02:00
}
2013-04-18 23:37:36 +02:00
public void purgePowerlessUsers ( ) {
plugin . getLogger ( ) . info ( " Purging powerless users... " ) ;
plugin . getLogger ( ) . info ( " Purged " + ( isUsingSQL ? SQLDatabaseManager . purgePowerlessSQL ( ) : FlatfileDatabaseManager . purgePowerlessFlatfile ( ) ) + " users from the database. " ) ;
2013-01-16 04:08:59 +01:00
}
2013-04-18 23:37:36 +02:00
public void purgeOldUsers ( ) {
plugin . getLogger ( ) . info ( " Purging old users... " ) ;
plugin . getLogger ( ) . info ( " Purged " + ( isUsingSQL ? SQLDatabaseManager . purgeOldSQL ( ) : FlatfileDatabaseManager . removeOldFlatfileUsers ( ) ) + " users from the database. " ) ;
2012-04-27 11:47:11 +02:00
}
2012-06-05 15:57:10 +02:00
2013-04-18 23:37:36 +02:00
public boolean removeUser ( String playerName ) {
if ( isUsingSQL ? SQLDatabaseManager . removeUserSQL ( playerName ) : FlatfileDatabaseManager . removeFlatFileUser ( playerName ) ) {
Misc . profileCleanup ( playerName ) ;
2012-12-24 22:56:25 +01:00
return true ;
}
return false ;
2012-04-27 11:47:11 +02:00
}
2013-04-18 23:37:36 +02:00
private void createFlatfileDatabase ( ) {
if ( usersFile . exists ( ) ) {
return ;
2013-01-14 06:47:47 +01:00
}
2013-04-18 23:37:36 +02:00
usersFile . getParentFile ( ) . mkdir ( ) ;
2013-03-01 06:52:01 +01:00
try {
2013-04-18 23:37:36 +02:00
plugin . debug ( " Creating mcmmo.users file... " ) ;
new File ( mcMMO . getUsersFilePath ( ) ) . createNewFile ( ) ;
2013-01-14 06:47:47 +01:00
}
2013-04-18 23:37:36 +02:00
catch ( IOException e ) {
e . printStackTrace ( ) ;
2013-03-01 06:52:01 +01:00
}
}
2013-01-16 01:03:13 +01:00
}