2014-11-15 12:05:48 +01:00
package com.intellectualcrafters.plot.commands ;
2014-12-16 06:03:20 +01:00
import java.sql.Connection ;
import java.sql.DatabaseMetaData ;
import java.sql.ResultSet ;
import java.sql.SQLException ;
import java.util.ArrayList ;
import java.util.UUID ;
import org.bukkit.entity.Player ;
import org.bukkit.plugin.Plugin ;
2014-11-15 12:05:48 +01:00
import com.intellectualcrafters.plot.PlotMain ;
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 ;
import com.intellectualcrafters.plot.util.PlayerFunctions ;
import com.intellectualcrafters.plot.util.StringComparison ;
2014-12-16 05:57:40 +01:00
import com.intellectualcrafters.plot.util.UUIDHandler ;
2014-12-13 14:30:29 +01:00
2014-11-15 12:05:48 +01:00
/ * *
* Created 2014 - 11 - 15 for PlotSquared
*
* @author Citymonstret
* /
public class Database extends SubCommand {
2014-12-16 06:03:20 +01:00
final String [ ] tables = new String [ ] { " plot_trusted " , " plot_ratings " , " plot_comments " } ;
2014-11-15 12:05:48 +01:00
public Database ( ) {
super ( Command . DATABASE , " Convert/Backup Storage " , " database [type] [...details] " , CommandCategory . DEBUG , false ) ;
}
2014-12-16 06:03:20 +01:00
private static boolean sendMessageU ( final UUID uuid , final String msg ) {
2014-11-15 12:05:48 +01:00
if ( uuid = = null ) {
PlotMain . sendConsoleSenderMessage ( msg ) ;
2014-12-16 06:03:20 +01:00
}
else {
final Player p = UUIDHandler . uuidWrapper . getPlayer ( uuid ) ;
if ( ( p ! = null ) & & p . isOnline ( ) ) {
2014-11-15 12:05:48 +01:00
return PlayerFunctions . sendMessage ( p , msg ) ;
2014-12-16 06:03:20 +01:00
}
else {
2014-11-15 12:05:48 +01:00
return sendMessageU ( null , msg ) ;
2014-12-16 06:03:20 +01:00
}
2014-11-15 12:05:48 +01:00
}
return true ;
}
public static void insertPlots ( final SQLManager manager , final UUID requester , final Connection c ) {
2014-12-16 06:03:20 +01:00
final Plugin p = PlotMain . getMain ( ) ;
2014-11-15 12:05:48 +01:00
final java . util . Set < Plot > plots = PlotMain . getPlots ( ) ;
p . getServer ( ) . getScheduler ( ) . runTaskAsynchronously ( p , new Runnable ( ) {
@Override
public void run ( ) {
try {
2014-12-16 06:03:20 +01: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 06:03:20 +01:00
}
2014-11-15 12:05:48 +01:00
manager . createPlots ( ps ) ;
manager . createAllSettingsAndHelpers ( ps ) ;
sendMessageU ( requester , " &6Database conversion finished " ) ;
2014-12-16 06:03:20 +01: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-16 06:03:20 +01:00
}
catch ( final SQLException e ) {
2014-11-15 12:05:48 +01:00
e . printStackTrace ( ) ;
}
}
} ) ;
}
@Override
2014-12-16 06:03:20 +01:00
public boolean execute ( final Player plr , final String . . . args ) {
2014-11-15 12:05:48 +01:00
if ( args . length < 1 ) {
return sendMessage ( plr , " /plot database [sqlite/mysql] " ) ;
}
2014-12-16 06:03:20 +01: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 06:03:20 +01: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 {
2014-12-16 06:03:20 +01:00
n = new MySQL ( PlotMain . getMain ( ) , 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-16 06:03:20 +01: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 06:03:20 +01:00
final SQLManager manager = new SQLManager ( n , prefix ) ;
2014-11-15 12:05:48 +01:00
try {
final DatabaseMetaData meta = n . getMetaData ( ) ;
ResultSet set = meta . getTables ( null , null , prefix + " plot " , null ) ;
if ( ! set . next ( ) ) {
manager . createTables ( " mysql " , true ) ;
2014-12-16 06:03:20 +01:00
}
else {
for ( final String s : this . tables ) {
2014-11-15 12:05:48 +01:00
set = meta . getTables ( null , null , prefix + s , null ) ;
if ( ! set . next ( ) ) {
manager . createTables ( " mysql " , false ) ;
}
}
}
2014-12-16 06:03:20 +01:00
}
catch ( final SQLException e ) {
2014-11-15 12:05:48 +01:00
e . printStackTrace ( ) ;
2014-12-16 06:03:20 +01: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 ) {
requester = plr . getUniqueId ( ) ;
}
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 ;
}
2014-12-16 06:03:20 +01:00
private boolean sendMessage ( final Player player , final String msg ) {
2014-11-15 12:05:48 +01:00
if ( player = = null ) {
PlotMain . sendConsoleSenderMessage ( msg ) ;
2014-12-16 06:03:20 +01:00
}
else {
2014-11-15 12:05:48 +01:00
PlayerFunctions . sendMessage ( player , msg ) ;
}
return true ;
}
}