mirror of
https://github.com/IntellectualSites/PlotSquared.git
synced 2025-07-18 05:14:42 +02:00
Fixes #538
This commit is contained in:
@ -1,19 +1,18 @@
|
||||
package com.intellectualcrafters.plot.commands;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.UUID;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
|
||||
import com.intellectualcrafters.plot.PS;
|
||||
import com.intellectualcrafters.plot.database.MySQL;
|
||||
import com.intellectualcrafters.plot.database.SQLManager;
|
||||
import com.intellectualcrafters.plot.database.SQLite;
|
||||
import com.intellectualcrafters.plot.object.Plot;
|
||||
import com.intellectualcrafters.plot.object.PlotPlayer;
|
||||
import com.intellectualcrafters.plot.util.MainUtil;
|
||||
import com.intellectualcrafters.plot.util.StringComparison;
|
||||
import com.intellectualcrafters.plot.util.TaskManager;
|
||||
import com.intellectualcrafters.plot.util.UUIDHandler;
|
||||
import com.plotsquared.general.commands.CommandDeclaration;
|
||||
|
||||
@CommandDeclaration(
|
||||
@ -23,27 +22,12 @@ import com.plotsquared.general.commands.CommandDeclaration;
|
||||
permission = "plots.database",
|
||||
description = "Convert/Backup Storage",
|
||||
requiredType = RequiredType.CONSOLE,
|
||||
usage = "/plots database <type> [details...]"
|
||||
usage = "/plots database [world] <sqlite|mysql>"
|
||||
|
||||
)
|
||||
public class Database extends SubCommand {
|
||||
|
||||
private static boolean sendMessageU(final UUID uuid, final String msg) {
|
||||
if (uuid == null) {
|
||||
PS.debug(msg);
|
||||
} else {
|
||||
final PlotPlayer p = UUIDHandler.getPlayer(uuid);
|
||||
if ((p != null) && p.isOnline()) {
|
||||
return MainUtil.sendMessage(p, msg);
|
||||
} else {
|
||||
return sendMessageU(null, msg);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void insertPlots(final SQLManager manager, final UUID requester, final Connection c) {
|
||||
final java.util.Set<Plot> plots = PS.get().getPlots();
|
||||
public static void insertPlots(final SQLManager manager, final ArrayList<Plot> plots, final PlotPlayer player) {
|
||||
TaskManager.runTaskAsync(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
@ -52,20 +36,16 @@ public class Database extends SubCommand {
|
||||
for (final Plot p : plots) {
|
||||
ps.add(p);
|
||||
}
|
||||
sendMessageU(requester, "&6Starting...");
|
||||
MainUtil.sendMessage(player, "&6Starting...");
|
||||
manager.createPlotsAndData(ps, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
sendMessageU(requester, "&6Database conversion finished!");
|
||||
MainUtil.sendMessage(player, "&6Database conversion finished!");
|
||||
manager.close();
|
||||
}
|
||||
});
|
||||
} catch (final Exception e) {
|
||||
sendMessageU(requester, "Failed to insert plot objects, see stacktrace for info");
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
c.close();
|
||||
} catch (final SQLException e) {
|
||||
MainUtil.sendMessage(player, "Failed to insert plot objects, see stacktrace for info");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
@ -73,61 +53,72 @@ public class Database extends SubCommand {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(PlotPlayer plr, String[] args) {
|
||||
public boolean onCommand(PlotPlayer player, String[] args) {
|
||||
if (args.length < 1) {
|
||||
return sendMessage(plr, "/plot database [sqlite/mysql]");
|
||||
MainUtil.sendMessage(player, "/plot database [world] <sqlite|mysql>");
|
||||
return false;
|
||||
}
|
||||
final String type = new StringComparison(args[0], new String[] { "mysql", "sqlite" }).getBestMatch().toLowerCase();
|
||||
switch (type) {
|
||||
case "mysql":
|
||||
if (args.length < 6) {
|
||||
return sendMessage(plr, "/plot database mysql [host] [port] [username] [password] [database] {prefix}");
|
||||
}
|
||||
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 = "";
|
||||
if (args.length > 6) {
|
||||
prefix = args[6];
|
||||
}
|
||||
Connection n;
|
||||
try {
|
||||
n = new MySQL(host, port, database, username, password).openConnection();
|
||||
// Connection
|
||||
if (n.isClosed()) {
|
||||
return sendMessage(plr, "Failed to open connection");
|
||||
ArrayList<Plot> plots;
|
||||
if (PS.get().isPlotWorld(args[0])) {
|
||||
plots = PS.get().sortPlotsByTemp(PS.get().getPlotsInWorld(args[0]));
|
||||
args = Arrays.copyOfRange(args, 1, args.length);
|
||||
}
|
||||
else {
|
||||
plots = PS.get().sortPlotsByTemp(PS.get().getPlotsRaw());
|
||||
}
|
||||
if (args.length < 1) {
|
||||
MainUtil.sendMessage(player, "/plot database [world] <sqlite|mysql>");
|
||||
MainUtil.sendMessage(player, "[arg] indicates an optional argument");
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
com.intellectualcrafters.plot.database.Database implementation;
|
||||
String prefix = "";
|
||||
switch (args[0].toLowerCase()) {
|
||||
case "mysql":
|
||||
if (args.length < 6) {
|
||||
return MainUtil.sendMessage(player, "/plot database mysql [host] [port] [username] [password] [database] {prefix}");
|
||||
}
|
||||
} catch (SQLException | ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
return sendMessage(plr, "Failed to open connection, read stacktrace for info");
|
||||
}
|
||||
final SQLManager manager = new SQLManager(n, prefix);
|
||||
try {
|
||||
manager.createTables("mysql");
|
||||
} catch (final SQLException e) {
|
||||
e.printStackTrace();
|
||||
return sendMessage(plr, "Could not create the required tables and/or load the database") && sendMessage(plr, "Please see the stacktrace for more information");
|
||||
}
|
||||
UUID requester = null;
|
||||
requester = UUIDHandler.getUUID(plr);
|
||||
insertPlots(manager, requester, n);
|
||||
break;
|
||||
case "sqlite":
|
||||
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");
|
||||
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];
|
||||
if (args.length > 6) {
|
||||
prefix = args[6];
|
||||
}
|
||||
implementation = new MySQL(host, port, database, username, password);
|
||||
break;
|
||||
case "sqlite":
|
||||
if (args.length < 2) {
|
||||
return MainUtil.sendMessage(player, "/plot database sqlite [file]: " + args.length + " | " + args[0]);
|
||||
}
|
||||
implementation = new SQLite(PS.get().IMP.getDirectory() + File.separator + args[1] + ".db");
|
||||
break;
|
||||
default:
|
||||
return MainUtil.sendMessage(player, "/plot database [sqlite/mysql]");
|
||||
}
|
||||
try {
|
||||
final SQLManager manager = new SQLManager(implementation, prefix, true);
|
||||
insertPlots(manager, plots, player);
|
||||
return true;
|
||||
}
|
||||
catch (Exception e) {
|
||||
MainUtil.sendMessage(player, "$1Failed to save plots, read stacktrace for info");
|
||||
MainUtil.sendMessage(player, "&d==== Here is an ugly stacktrace, if you are interested in those things ===");
|
||||
e.printStackTrace();
|
||||
MainUtil.sendMessage(player, "&d==== End of stacktrace ====");
|
||||
MainUtil.sendMessage(player, "$1Please make sure you are using the correct arguments!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
MainUtil.sendMessage(player, "$1Failed to open connection, read stacktrace for info");
|
||||
MainUtil.sendMessage(player, "&d==== Here is an ugly stacktrace, if you are interested in those things ===");
|
||||
e.printStackTrace();
|
||||
MainUtil.sendMessage(player, "&d==== End of stacktrace ====");
|
||||
MainUtil.sendMessage(player, "$1Please make sure you are using the correct arguments!");
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean sendMessage(final PlotPlayer player, final String msg) {
|
||||
MainUtil.sendMessage(player, msg);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -41,6 +41,7 @@ import com.plotsquared.general.commands.CommandDeclaration;
|
||||
@CommandDeclaration(
|
||||
command = "flag",
|
||||
aliases = {"f"},
|
||||
usage = "/plot flag <set|remove|add|list|info> <flag> <value>",
|
||||
description = "Manage plot flags",
|
||||
category = CommandCategory.ACTIONS,
|
||||
requiredType = RequiredType.NONE,
|
||||
@ -48,6 +49,11 @@ import com.plotsquared.general.commands.CommandDeclaration;
|
||||
)
|
||||
public class FlagCmd extends SubCommand {
|
||||
|
||||
@Override
|
||||
public String getUsage() {
|
||||
return super.getUsage().replaceAll("<flag>", StringMan.join(FlagManager.getFlags(), "|"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(final PlotPlayer player, final String ... args) {
|
||||
|
||||
|
@ -22,6 +22,7 @@ package com.intellectualcrafters.plot.commands;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
@ -136,7 +137,7 @@ public class MainCommand extends CommandManager<PlotPlayer> {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static List<Command<PlotPlayer>> getCommands(final CommandCategory category, final PlotPlayer player) {
|
||||
public static List<Command<PlotPlayer>> getCommandAndAliases(final CommandCategory category, final PlotPlayer player) {
|
||||
List<Command<PlotPlayer>> commands = new ArrayList<>();
|
||||
for (Command<PlotPlayer> command : getInstance().getCommands()) {
|
||||
if (category != null && !command.getCategory().equals(category)) {
|
||||
@ -150,6 +151,20 @@ public class MainCommand extends CommandManager<PlotPlayer> {
|
||||
return commands;
|
||||
}
|
||||
|
||||
public static List<Command<PlotPlayer>> getCommands(final CommandCategory category, final PlotPlayer player) {
|
||||
List<Command<PlotPlayer>> commands = new ArrayList<>();
|
||||
for (Command<PlotPlayer> command : new HashSet<>(getInstance().getCommands())) {
|
||||
if (category != null && !command.getCategory().equals(category)) {
|
||||
continue;
|
||||
}
|
||||
if (player != null && !Permissions.hasPermission(player, command.getPermission())) {
|
||||
continue;
|
||||
}
|
||||
commands.add(command);
|
||||
}
|
||||
return commands;
|
||||
}
|
||||
|
||||
//// public static List<String> helpMenu(final PlotPlayer player, final CommandCategory category, int page) {
|
||||
// List<Command<PlotPlayer>> commands;
|
||||
// // commands = getCommands(category, player);
|
||||
@ -338,10 +353,57 @@ public class MainCommand extends CommandManager<PlotPlayer> {
|
||||
MainUtil.sendMessage(plr, C.NOT_VALID_SUBCOMMAND);
|
||||
{
|
||||
List<Command<PlotPlayer>> cmds = getCommands(null, plr);
|
||||
if (label == null || cmds.size() == 0 || (cmd = new StringComparison<>(label, cmds).getMatchObject()) == null) {
|
||||
if (label == null || cmds.size() == 0) {
|
||||
MainUtil.sendMessage(plr, C.DID_YOU_MEAN, "/plot help");
|
||||
}
|
||||
else {
|
||||
HashSet<String> setargs = new HashSet<>(args.length + 1);
|
||||
for (String arg : args) {
|
||||
setargs.add(arg.toLowerCase());
|
||||
}
|
||||
setargs.add(label.toLowerCase());
|
||||
String[] allargs = setargs.toArray(new String[setargs.size()]);
|
||||
int best = 0;
|
||||
for (Command<PlotPlayer> current : cmds) {
|
||||
if (current.getUsage() != null) {
|
||||
int count = 0;
|
||||
for (String word : new HashSet<String>(Arrays.asList((current.getUsage() + " " + current.getPermission() + " " + current.getCategory().name()).toLowerCase().replaceAll("\\||\\>|\\<|\\[|\\]|\\{|\\}|\\_|\\/", " ").trim().replaceAll("\\s+", " ").split(" ")))) {
|
||||
for (int i = 0; i < allargs.length; i++) {
|
||||
String arg = allargs[i];
|
||||
if (best - count - (allargs.length - i) * 3 >= 0) {
|
||||
continue;
|
||||
}
|
||||
if (StringMan.isEqual(arg, word)) {
|
||||
count+=3;
|
||||
}
|
||||
else if (word.length() > arg.length() && word.contains(arg)) {
|
||||
count+=2;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (String word : new HashSet<String>(Arrays.asList((current.getDescription()).toLowerCase().replaceAll("\\||\\>|\\<|\\[|\\]|\\{|\\}|\\_|\\/", " ").trim().replaceAll("\\s+", " ").split(" ")))) {
|
||||
for (int i = 0; i < allargs.length; i++) {
|
||||
String arg = allargs[i];
|
||||
if (best - count - (allargs.length - i) * 2 >= 0) {
|
||||
continue;
|
||||
}
|
||||
if (StringMan.isEqual(arg, word)) {
|
||||
count+=2;
|
||||
}
|
||||
else if (word.length() > arg.length() && word.contains(arg)) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (count > best) {
|
||||
best = count;
|
||||
cmd = current;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (cmd == null) {
|
||||
cmd = new StringComparison<>(label, getCommandAndAliases(null, plr)).getMatchObject();
|
||||
}
|
||||
MainUtil.sendMessage(plr, C.DID_YOU_MEAN, cmd.getUsage().replaceAll("\\{label\\}", parts[0]));
|
||||
}
|
||||
}
|
||||
|
@ -54,7 +54,7 @@ import com.plotsquared.listener.PlotListener;
|
||||
command = "set",
|
||||
description = "Set a plot value",
|
||||
aliases = {"s"},
|
||||
usage = "/plot set <arg> <value(s)...>",
|
||||
usage = "/plot set <biome|alias|home|flag> <value...>",
|
||||
permission = "plots.set",
|
||||
category = CommandCategory.ACTIONS,
|
||||
requiredType = RequiredType.NONE
|
||||
|
Reference in New Issue
Block a user