TF-PlotSquared/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Database.java

149 lines
5.8 KiB
Java
Raw Normal View History

2014-11-15 12:05:48 +01:00
package com.intellectualcrafters.plot.commands;
2015-01-14 03:38:15 +11: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;
2015-02-19 19:51:10 +11:00
import com.intellectualcrafters.plot.PlotSquared;
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;
import com.intellectualcrafters.plot.util.StringComparison;
2015-02-20 15:32:40 +11:00
import com.intellectualcrafters.plot.util.TaskManager;
2015-02-20 22:24:58 +11:00
import com.intellectualcrafters.plot.util.bukkit.BukkitPlayerFunctions;
2015-02-20 17:28:21 +11:00
import com.intellectualcrafters.plot.util.bukkit.UUIDHandler;
2014-11-15 12:05:48 +01:00
/**
* Created 2014-11-15 for PlotSquared
*
* @author Citymonstret
*/
public class Database extends SubCommand {
2015-02-20 17:34:19 +11: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);
}
2015-02-20 17:34:19 +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-02-19 21:23:36 +11:00
PlotSquared.log(msg);
2014-12-17 20:15:11 -06:00
} else {
2014-12-16 16:03:20 +11:00
final Player p = UUIDHandler.uuidWrapper.getPlayer(uuid);
if ((p != null) && p.isOnline()) {
2015-02-20 22:24:58 +11:00
return BukkitPlayerFunctions.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-20 17:34:19 +11:00
2014-11-15 12:05:48 +01:00
public static void insertPlots(final SQLManager manager, final UUID requester, final Connection c) {
2015-02-19 19:51:10 +11:00
final java.util.Set<Plot> plots = PlotSquared.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
}
2014-11-15 12:05:48 +01:00
manager.createPlots(ps);
manager.createAllSettingsAndHelpers(ps);
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-20 17:34:19 +11:00
2014-11-15 12:05:48 +01:00
@Override
2014-12-16 16:03:20 +11: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]");
}
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-02-20 15:32:40 +11:00
n = new MySQL(PlotSquared.THIS, 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 {
final DatabaseMetaData meta = n.getMetaData();
ResultSet set = meta.getTables(null, null, prefix + "plot", null);
if (!set.next()) {
manager.createTables("mysql", true);
2014-12-17 20:15:11 -06:00
} else {
2014-12-16 16:03:20 +11:00
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-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-20 17:34:19 +11:00
2014-12-16 16:03:20 +11:00
private boolean sendMessage(final Player player, final String msg) {
2014-11-15 12:05:48 +01:00
if (player == null) {
2015-02-19 21:23:36 +11:00
PlotSquared.log(msg);
2014-12-17 20:15:11 -06:00
} else {
2015-02-21 15:32:01 +11:00
MainUtil.sendMessage(BukkitUtil.getPlayer(player), msg);
2014-11-15 12:05:48 +01:00
}
return true;
}
}