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

140 lines
5.3 KiB
Java
Raw Normal View History

2014-11-15 12:05:48 +01:00
package com.intellectualcrafters.plot.commands;
2015-07-05 17:44:10 +02:00
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.UUID;
import com.intellectualcrafters.plot.PS;
2015-07-27 15:10:14 +02:00
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 12:38:44 +01: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 05:32:40 +01:00
import com.intellectualcrafters.plot.util.TaskManager;
2015-07-27 09:26:50 +02:00
import com.intellectualcrafters.plot.util.UUIDHandler;
import com.intellectualsites.commands.CommandDeclaration;
2015-07-27 15:10:14 +02:00
import com.intellectualcrafters.plot.object.PlotPlayer;
@CommandDeclaration(
command = "database",
aliases = {"convert"},
category = CommandCategory.DEBUG,
permission = "plots.database",
description = "Convert/Backup Storage",
2015-07-27 15:10:14 +02:00
requiredType = RequiredType.CONSOLE,
usage = "/plots database <type> [details...]"
2015-07-27 15:10:14 +02:00
)
2014-11-15 12:05:48 +01:00
public class Database extends SubCommand {
2015-02-23 02:32:27 +01:00
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) {
PS.log(msg);
2014-12-18 03:15:11 +01:00
} else {
2015-02-22 07:14:37 +01:00
final PlotPlayer p = UUIDHandler.getPlayer(uuid);
2014-12-16 06:03:20 +01:00
if ((p != null) && p.isOnline()) {
2015-02-21 12:24:46 +01:00
return MainUtil.sendMessage(p, msg);
2014-12-18 03:15:11 +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;
}
2015-02-23 02:32:27 +01:00
2014-11-15 12:05:48 +01:00
public static void insertPlots(final SQLManager manager, final UUID requester, final Connection c) {
final java.util.Set<Plot> plots = PS.get().getPlots();
2015-02-20 05:32:40 +01:00
TaskManager.runTaskAsync(new Runnable() {
2014-11-15 12:05:48 +01:00
@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
}
2015-04-29 14:04:25 +02:00
sendMessageU(requester, "&6Starting...");
manager.createPlotsAndData(ps, new Runnable() {
@Override
public void run() {
sendMessageU(requester, "&6Database conversion finished!");
}
});
2014-12-18 03:15:11 +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-18 03:15:11 +01:00
} catch (final SQLException e) {
2014-11-15 12:05:48 +01:00
e.printStackTrace();
}
}
});
}
2015-02-23 02:32:27 +01:00
2014-11-15 12:05:48 +01:00
@Override
2015-07-27 15:10:14 +02:00
public boolean onCommand(PlotPlayer plr, String[] args) {
2014-11-15 12:05:48 +01:00
if (args.length < 1) {
return sendMessage(plr, "/plot database [sqlite/mysql]");
}
2015-02-20 07:34:19 +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 {
n = new MySQL(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-18 03:15:11 +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 {
2015-07-15 18:32:16 +02:00
manager.createTables("mysql");
2014-12-18 03:15:11 +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;
2015-07-27 17:14:38 +02: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 02:32:27 +01:00
2015-02-22 07:14:37 +01:00
private boolean sendMessage(final PlotPlayer player, final String msg) {
2014-11-15 12:05:48 +01:00
if (player == null) {
PS.log(msg);
2014-12-18 03:15:11 +01:00
} else {
2015-02-22 07:14:37 +01:00
MainUtil.sendMessage(player, msg);
2014-11-15 12:05:48 +01:00
}
return true;
}
}