2014-11-15 12:05:48 +01:00
package com.intellectualcrafters.plot.commands ;
2015-07-03 22:15:20 +10:00
import com.intellectualcrafters.plot.PS ;
2015-06-05 20:45:13 +10:00
import com.intellectualcrafters.plot.config.Settings ;
2014-11-15 12:05:48 +01:00
import com.intellectualcrafters.plot.database.MySQL ;
import com.intellectualcrafters.plot.database.SQLManager ;
2014-11-16 10:48:18 +01:00
import com.intellectualcrafters.plot.object.Plot ;
2015-02-21 22:38:44 +11:00
import com.intellectualcrafters.plot.object.PlotPlayer ;
import com.intellectualcrafters.plot.util.MainUtil ;
2014-11-16 10:48:18 +01:00
import com.intellectualcrafters.plot.util.StringComparison ;
2015-02-20 15:32:40 +11:00
import com.intellectualcrafters.plot.util.TaskManager ;
2015-02-20 17:28:21 +11:00
import com.intellectualcrafters.plot.util.bukkit.UUIDHandler ;
2014-12-14 00:30:29 +11:00
2015-07-03 11:30:26 +02:00
import java.sql.Connection ;
import java.sql.SQLException ;
import java.util.ArrayList ;
import java.util.UUID ;
2014-11-15 12:05:48 +01:00
/ * *
* Created 2014 - 11 - 15 for PlotSquared
*
* @author Citymonstret
* /
public class Database extends SubCommand {
public Database ( ) {
super ( Command . DATABASE , " Convert/Backup Storage " , " database [type] [...details] " , CommandCategory . DEBUG , false ) ;
}
2015-02-23 12:32:27 +11:00
2014-12-16 16:03:20 +11:00
private static boolean sendMessageU ( final UUID uuid , final String msg ) {
2014-11-15 12:05:48 +01:00
if ( uuid = = null ) {
2015-07-03 22:15:20 +10:00
PS . log ( msg ) ;
2014-12-17 20:15:11 -06:00
} else {
2015-02-22 17:14:37 +11:00
final PlotPlayer p = UUIDHandler . getPlayer ( uuid ) ;
2014-12-16 16:03:20 +11:00
if ( ( p ! = null ) & & p . isOnline ( ) ) {
2015-02-21 22:24:46 +11:00
return MainUtil . sendMessage ( p , msg ) ;
2014-12-17 20:15:11 -06:00
} else {
2014-11-15 12:05:48 +01:00
return sendMessageU ( null , msg ) ;
2014-12-16 16:03:20 +11:00
}
2014-11-15 12:05:48 +01:00
}
return true ;
}
2015-02-23 12:32:27 +11:00
2014-11-15 12:05:48 +01:00
public static void insertPlots ( final SQLManager manager , final UUID requester , final Connection c ) {
2015-07-03 22:15:20 +10:00
final java . util . Set < Plot > plots = PS . get ( ) . getPlots ( ) ;
2015-02-20 15:32:40 +11:00
TaskManager . runTaskAsync ( new Runnable ( ) {
2014-11-15 12:05:48 +01:00
@Override
public void run ( ) {
try {
2014-12-16 16:03:20 +11:00
final ArrayList < Plot > ps = new ArrayList < > ( ) ;
for ( final Plot p : plots ) {
2014-11-15 12:05:48 +01:00
ps . add ( p ) ;
2014-12-16 16:03:20 +11:00
}
2015-04-29 22:04:25 +10:00
sendMessageU ( requester , " &6Starting... " ) ;
manager . createPlotsAndData ( ps , new Runnable ( ) {
@Override
public void run ( ) {
sendMessageU ( requester , " &6Database conversion finished! " ) ;
}
} ) ;
2014-12-17 20:15:11 -06:00
} catch ( final Exception e ) {
2014-11-15 12:05:48 +01:00
sendMessageU ( requester , " Failed to insert plot objects, see stacktrace for info " ) ;
e . printStackTrace ( ) ;
}
try {
c . close ( ) ;
2014-12-17 20:15:11 -06:00
} catch ( final SQLException e ) {
2014-11-15 12:05:48 +01:00
e . printStackTrace ( ) ;
}
}
} ) ;
}
2015-02-23 12:32:27 +11:00
2014-11-15 12:05:48 +01:00
@Override
2015-02-21 18:30:55 +11:00
public boolean execute ( final PlotPlayer plr , final String . . . args ) {
2014-11-15 12:05:48 +01:00
if ( args . length < 1 ) {
return sendMessage ( plr , " /plot database [sqlite/mysql] " ) ;
}
2015-02-20 17:34:19 +11:00
final String type = new StringComparison ( args [ 0 ] , new String [ ] { " mysql " , " sqlite " } ) . getBestMatch ( ) . toLowerCase ( ) ;
2014-11-15 12:05:48 +01:00
switch ( type ) {
2014-11-15 12:53:26 +01:00
case " mysql " :
2014-11-15 12:05:48 +01:00
if ( args . length < 6 ) {
return sendMessage ( plr , " /plot database mysql [host] [port] [username] [password] [database] {prefix} " ) ;
}
2014-12-16 16:03:20 +11:00
final String host = args [ 1 ] ;
final String port = args [ 2 ] ;
final String username = args [ 3 ] ;
final String password = args [ 4 ] ;
final String database = args [ 5 ] ;
String prefix = " " ;
2014-11-15 12:05:48 +01:00
if ( args . length > 6 ) {
prefix = args [ 6 ] ;
}
Connection n ;
try {
2015-07-03 22:15:20 +10:00
n = new MySQL ( PS . get ( ) , host , port , database , username , password ) . openConnection ( ) ;
2014-11-15 12:05:48 +01:00
// Connection
if ( n . isClosed ( ) ) {
return sendMessage ( plr , " Failed to open connection " ) ;
}
2014-12-17 20:15:11 -06:00
} catch ( SQLException | ClassNotFoundException e ) {
2014-11-15 12:05:48 +01:00
e . printStackTrace ( ) ;
return sendMessage ( plr , " Failed to open connection, read stacktrace for info " ) ;
}
2014-12-16 16:03:20 +11:00
final SQLManager manager = new SQLManager ( n , prefix ) ;
2014-11-15 12:05:48 +01:00
try {
2015-06-05 20:45:13 +10:00
manager . createTables ( Settings . DB . USE_MYSQL ? " mysql " : " sqlite " ) ;
2014-12-17 20:15:11 -06:00
} catch ( final SQLException e ) {
2014-11-15 12:05:48 +01:00
e . printStackTrace ( ) ;
2014-12-16 16:03:20 +11:00
return sendMessage ( plr , " Could not create the required tables and/or load the database " ) & & sendMessage ( plr , " Please see the stacktrace for more information " ) ;
2014-11-15 12:05:48 +01:00
}
UUID requester = null ;
if ( plr ! = null ) {
2014-12-31 00:09:11 +11:00
requester = UUIDHandler . getUUID ( plr ) ;
2014-11-15 12:05:48 +01:00
}
insertPlots ( manager , requester , n ) ;
break ;
2014-11-15 12:53:26 +01:00
case " sqlite " :
2014-11-15 12:05:48 +01:00
if ( args . length < 2 ) {
return sendMessage ( plr , " /plot database sqlite [file name] " ) ;
}
sendMessage ( plr , " This is not supported yet " ) ;
break ;
default :
return sendMessage ( plr , " Unknown database type " ) ;
}
return false ;
}
2015-02-23 12:32:27 +11:00
2015-02-22 17:14:37 +11:00
private boolean sendMessage ( final PlotPlayer player , final String msg ) {
2014-11-15 12:05:48 +01:00
if ( player = = null ) {
2015-07-03 22:15:20 +10:00
PS . log ( msg ) ;
2014-12-17 20:15:11 -06:00
} else {
2015-02-22 17:14:37 +11:00
MainUtil . sendMessage ( player , msg ) ;
2014-11-15 12:05:48 +01:00
}
return true ;
}
}