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

469 lines
20 KiB
Java
Raw Normal View History

2015-09-11 12:09:22 +02:00
////////////////////////////////////////////////////////////////////////////////////////////////////
// PlotSquared - A plot manager and world generator for the Bukkit API /
// Copyright (c) 2014 IntellectualSites/IntellectualCrafters /
// /
// This program is free software; you can redistribute it and/or modify /
// it under the terms of the GNU General Public License as published by /
// the Free Software Foundation; either version 3 of the License, or /
// (at your option) any later version. /
// /
// This program is distributed in the hope that it will be useful, /
// but WITHOUT ANY WARRANTY; without even the implied warranty of /
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /
// GNU General Public License for more details. /
// /
// You should have received a copy of the GNU General Public License /
// along with this program; if not, write to the Free Software Foundation, /
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA /
// /
// You can contact us via: support@intellectualsites.com /
////////////////////////////////////////////////////////////////////////////////////////////////////
package com.intellectualcrafters.plot.commands;
import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.config.Settings;
import com.intellectualcrafters.plot.object.ConsolePlayer;
import com.intellectualcrafters.plot.object.Location;
2015-09-11 12:09:22 +02:00
import com.intellectualcrafters.plot.object.Plot;
2016-02-10 19:59:51 +01:00
import com.intellectualcrafters.plot.object.PlotArea;
2015-09-11 12:09:22 +02:00
import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.util.MainUtil;
import com.intellectualcrafters.plot.util.MathMan;
import com.intellectualcrafters.plot.util.Permissions;
import com.intellectualcrafters.plot.util.StringComparison;
import com.intellectualcrafters.plot.util.StringMan;
import com.intellectualcrafters.plot.util.helpmenu.HelpMenu;
import com.plotsquared.general.commands.Argument;
import com.plotsquared.general.commands.Command;
import com.plotsquared.general.commands.CommandHandlingOutput;
import com.plotsquared.general.commands.CommandManager;
2016-02-11 02:37:21 +01:00
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
2015-09-11 12:09:22 +02:00
/**
* PlotSquared command class
*
2015-09-11 12:09:22 +02:00
*/
2015-09-13 06:04:31 +02:00
public class MainCommand extends CommandManager<PlotPlayer> {
2015-09-11 12:09:22 +02:00
private static MainCommand instance;
private HashMap<String, Command<PlotPlayer>> setCommands;
2015-09-13 06:04:31 +02:00
private MainCommand() {
2015-09-11 12:09:22 +02:00
super(null, new ArrayList<Command<PlotPlayer>>());
instance = this;
createCommand(new Buy());
createCommand(new Save());
createCommand(new Load());
createCommand(new Confirm());
createCommand(new Template());
createCommand(new Download());
createCommand(new Update());
createCommand(new Template());
createCommand(new Setup());
createCommand(new Area());
2015-09-11 12:09:22 +02:00
createCommand(new DebugSaveTest());
createCommand(new DebugLoadTest());
createCommand(new CreateRoadSchematic());
createCommand(new DebugAllowUnsafe());
createCommand(new RegenAllRoads());
createCommand(new Claim());
createCommand(new Auto());
createCommand(new Visit());
createCommand(new Home());
2015-09-11 12:09:22 +02:00
createCommand(new Set());
createCommand(new Toggle());
createCommand(new Clear());
createCommand(new Delete());
createCommand(new Trust());
createCommand(new Add());
createCommand(new Deny());
createCommand(new Untrust());
createCommand(new Remove());
createCommand(new Undeny());
createCommand(new Info());
createCommand(new list());
createCommand(new Help());
createCommand(new Debug());
createCommand(new SchematicCmd());
createCommand(new plugin());
createCommand(new Purge());
createCommand(new Reload());
createCommand(new Merge());
createCommand(new DebugPaste());
createCommand(new Unlink());
createCommand(new Kick());
createCommand(new Rate());
createCommand(new DebugClaimTest());
createCommand(new Inbox());
createCommand(new Comment());
createCommand(new Database());
createCommand(new Swap());
createCommand(new MusicSubcommand());
createCommand(new DebugRoadRegen());
createCommand(new Trust());
createCommand(new DebugExec());
createCommand(new FlagCmd());
createCommand(new Target());
createCommand(new DebugFixFlags());
createCommand(new Move());
createCommand(new Condense());
createCommand(new Condense());
createCommand(new Copy());
createCommand(new Chat());
createCommand(new Trim());
createCommand(new Done());
createCommand(new Continue());
createCommand(new BO3());
createCommand(new Middle());
createCommand(new Grant());
// set commands
createCommand(new Owner());
createCommand(new Desc());
createCommand(new Biome());
createCommand(new Alias());
createCommand(new SetHome());
2015-09-13 06:04:31 +02:00
if (Settings.ENABLE_CLUSTERS) {
2015-09-11 12:09:22 +02:00
MainCommand.getInstance().addCommand(new Cluster());
}
}
public static MainCommand getInstance() {
if (instance == null) {
instance = new MainCommand();
}
return instance;
}
2015-09-13 06:04:31 +02:00
public static boolean no_permission(final PlotPlayer player, final String permission) {
2015-09-11 12:09:22 +02:00
MainUtil.sendMessage(player, C.NO_PERMISSION, permission);
return false;
}
2015-09-13 06:04:31 +02:00
public static List<Command<PlotPlayer>> getCommandAndAliases(final CommandCategory category, final PlotPlayer player) {
2015-09-11 12:09:22 +02:00
final List<Command<PlotPlayer>> commands = new ArrayList<>();
2015-09-13 06:04:31 +02:00
for (final Command<PlotPlayer> command : getInstance().getCommands()) {
if ((category != null) && !command.getCategory().equals(category)) {
2015-09-11 12:09:22 +02:00
continue;
}
2015-09-13 06:04:31 +02:00
if ((player != null) && !Permissions.hasPermission(player, command.getPermission())) {
2015-09-11 12:09:22 +02:00
continue;
}
commands.add(command);
}
return commands;
}
2015-09-13 06:04:31 +02:00
public static List<Command<PlotPlayer>> getCommands(final CommandCategory category, final PlotPlayer player) {
2015-09-11 12:09:22 +02:00
final List<Command<PlotPlayer>> commands = new ArrayList<>();
2015-09-13 06:04:31 +02:00
for (final Command<PlotPlayer> command : new HashSet<>(getInstance().getCommands())) {
if ((category != null) && !command.getCategory().equals(category)) {
2015-09-11 12:09:22 +02:00
continue;
}
2015-09-13 06:04:31 +02:00
if ((player != null) && !Permissions.hasPermission(player, command.getPermission())) {
2015-09-11 12:09:22 +02:00
continue;
}
commands.add(command);
}
return commands;
}
2015-09-13 06:04:31 +02:00
public static void displayHelp(final PlotPlayer player, String cat, int page, final String label) {
2015-09-11 12:09:22 +02:00
CommandCategory catEnum = null;
2015-09-13 06:04:31 +02:00
if (cat != null) {
if (StringMan.isEqualIgnoreCase(cat, "all")) {
2015-09-11 12:09:22 +02:00
catEnum = null;
2015-09-13 06:04:31 +02:00
} else {
for (final CommandCategory c : CommandCategory.values()) {
if (StringMan.isEqualIgnoreCaseToAny(cat, c.name(), c.toString())) {
2015-09-11 12:09:22 +02:00
catEnum = c;
cat = c.name();
break;
}
}
2015-09-13 06:04:31 +02:00
if (catEnum == null) {
2015-09-11 12:09:22 +02:00
cat = null;
}
}
}
2015-09-13 06:04:31 +02:00
if ((cat == null) && (page == 0)) {
2015-09-11 12:09:22 +02:00
final StringBuilder builder = new StringBuilder();
builder.append(C.HELP_HEADER.s());
2015-09-13 06:04:31 +02:00
for (final CommandCategory c : CommandCategory.values()) {
2015-09-11 12:09:22 +02:00
builder.append("\n" + StringMan.replaceAll(C.HELP_INFO_ITEM.s(), "%category%", c.toString().toLowerCase(), "%category_desc%", c.toString()));
}
builder.append("\n").append(C.HELP_INFO_ITEM.s().replaceAll("%category%", "all").replaceAll("%category_desc%", "Display all commands"));
builder.append("\n" + C.HELP_FOOTER.s());
MainUtil.sendMessage(player, builder.toString(), false);
return;
}
page--;
2015-09-13 06:04:31 +02:00
new HelpMenu(player).setCategory(catEnum).getCommands().generateMaxPages().generatePage(page, label).render();
2015-09-11 12:09:22 +02:00
}
2015-09-13 06:04:31 +02:00
public static boolean onCommand(final PlotPlayer player, final String cmd, String... args) {
// Clear perm caching //
player.deleteMeta("perm");
////////////////////////
2015-09-11 12:09:22 +02:00
int help_index = -1;
String category = null;
Location loc = null;
2015-12-04 10:00:30 +01:00
Plot plot = null;
boolean tp = false;
switch (args.length) {
case 0: {
help_index = 0;
break;
}
case 1: {
if (MathMan.isInteger(args[0])) {
try {
help_index = Integer.parseInt(args[args.length - 1]);
} catch (final NumberFormatException e) {}
2015-09-11 12:09:22 +02:00
break;
}
}
default: {
switch (args[0].toLowerCase()) {
case "he":
case "help":
case "?": {
switch (args.length) {
case 1: {
help_index = 0;
break;
}
case 2: {
if (MathMan.isInteger(args[1])) {
category = null;
try {
help_index = Integer.parseInt(args[1]);
} catch (final NumberFormatException e) {
help_index = 1;
}
} else {
help_index = 1;
category = args[1];
}
break;
}
case 3: {
category = args[1];
if (MathMan.isInteger(args[2])) {
try {
help_index = Integer.parseInt(args[2]);
} catch (final NumberFormatException e) {
help_index = 1;
}
}
break;
}
default: {
C.COMMAND_SYNTAX.send(player, "/" + cmd + "? [#|<term>|category [#]]");
return true;
}
}
break;
}
default: {
if (args.length >= 2) {
2016-02-10 19:59:51 +01:00
PlotArea area = player.getApplicablePlotArea();
Plot newPlot = Plot.fromString(area, args[0]);
2015-12-04 10:00:30 +01:00
if (newPlot == null) {
break;
}
2016-02-11 02:37:21 +01:00
if (!ConsolePlayer.isConsole(player) && (!newPlot.getArea().equals(area) || newPlot.isDenied(player.getUUID())) && !Permissions.hasPermission(player, C.PERMISSION_ADMIN)) {
break;
}
2015-12-04 10:00:30 +01:00
// Save meta
loc = (Location) player.getMeta("location");
2015-12-04 10:00:30 +01:00
plot = (Plot) player.getMeta("lastplot");
tp = true;
// Set loc
player.setMeta("location", newPlot.getBottomAbs());
player.setMeta("lastplot", newPlot);
// Trim command
args = Arrays.copyOfRange(args, 1, args.length);
2015-09-11 12:09:22 +02:00
}
}
}
}
}
2015-09-13 06:04:31 +02:00
if (help_index != -1) {
2015-09-11 12:09:22 +02:00
displayHelp(player, category, help_index, cmd);
return true;
}
2015-10-07 08:33:33 +02:00
if (args[0].contains(":")) {
args[0] = args[0].replaceFirst(":", " ");
2015-09-11 12:09:22 +02:00
}
2015-10-07 08:33:33 +02:00
String fullCmd = StringMan.join(args, " ");
getInstance().handle(player, cmd + " " + fullCmd);
2015-12-04 10:00:30 +01:00
// Restore location
if (tp) {
if (loc == null) {
player.deleteMeta("location");
} else {
player.setMeta("location", loc);
}
if (plot == null) {
player.deleteMeta("lastplot");
} else {
player.setMeta("lastplot", plot);
}
}
2015-09-11 12:09:22 +02:00
return true;
}
2015-09-13 06:04:31 +02:00
2015-09-30 08:11:44 +02:00
public int getMatch(String[] args, Command<PlotPlayer> cmd) {
int count = 0;
String perm = cmd.getPermission();
HashSet<String> desc = new HashSet<String>();
for (String alias : cmd.getAliases()) {
if (alias.startsWith(args[0])) {
count += 5;
}
}
Collections.addAll(desc, cmd.getDescription().split(" "));
2015-09-30 08:11:44 +02:00
for (String arg : args) {
if (perm.startsWith(arg)) {
count++;
}
if (desc.contains(arg)) {
count++;
}
}
String[] usage = cmd.getUsage().split(" ");
for (int i = 0; i < Math.min(4 , usage.length); i++) {
int require;
if (usage[i].startsWith("<")) {
require = 1;
} else {
require = 0;
}
String[] split = usage[i].split("\\|| |\\>|\\<|\\[|\\]|\\{|\\}|\\_|\\/");
for (String aSplit : split) {
2015-09-30 08:11:44 +02:00
for (String arg : args) {
if (StringMan.isEqualIgnoreCase(arg, aSplit)) {
2015-09-30 08:11:44 +02:00
count += 5 - i + require;
}
}
}
}
count += StringMan.intersection(desc, args);
return count;
}
2015-09-11 12:09:22 +02:00
@Override
2015-09-13 06:04:31 +02:00
public int handle(final PlotPlayer plr, final String input) {
2015-09-11 12:09:22 +02:00
final String[] parts = input.split(" ");
String[] args;
String label;
2015-09-13 06:04:31 +02:00
if (parts.length == 1) {
2015-09-11 12:09:22 +02:00
label = null;
args = new String[0];
2015-09-13 06:04:31 +02:00
} else {
2015-09-11 12:09:22 +02:00
label = parts[1];
args = new String[parts.length - 2];
System.arraycopy(parts, 2, args, 0, args.length);
}
Command<PlotPlayer> cmd;
2015-09-13 06:04:31 +02:00
if (label != null) {
if (label.contains(":")) {
// Ref: c:v, this will push value to the last spot in the array
// ex. /p h:2 SomeUsername
// > /p h SomeUsername 2
String[] temp = label.split(":");
label = temp[0];
String[] tempArgs = new String[args.length + 1];
System.arraycopy(args, 0, tempArgs, 0, args.length);
tempArgs[tempArgs.length - 1] = temp[1];
args = tempArgs;
}
2015-11-28 12:56:12 +01:00
cmd = getInstance().commands.get(label.toLowerCase());
2015-09-13 06:04:31 +02:00
} else {
2015-09-11 12:09:22 +02:00
cmd = null;
}
2015-09-13 06:04:31 +02:00
if (cmd == null) {
2015-09-11 12:09:22 +02:00
MainUtil.sendMessage(plr, C.NOT_VALID_SUBCOMMAND);
{
final List<Command<PlotPlayer>> cmds = getCommands(null, plr);
if ((label == null) || (cmds.isEmpty())) {
2015-09-11 12:09:22 +02:00
MainUtil.sendMessage(plr, C.DID_YOU_MEAN, "/plot help");
2015-09-13 06:04:31 +02:00
} else {
2015-09-11 12:09:22 +02:00
final HashSet<String> setargs = new HashSet<>(args.length + 1);
2015-09-13 06:04:31 +02:00
for (final String arg : args) {
2015-09-11 12:09:22 +02:00
setargs.add(arg.toLowerCase());
}
2015-09-30 08:11:44 +02:00
setargs.add(label);
2015-09-11 12:09:22 +02:00
final String[] allargs = setargs.toArray(new String[setargs.size()]);
int best = 0;
2015-09-13 06:04:31 +02:00
for (final Command<PlotPlayer> current : cmds) {
2015-09-30 08:11:44 +02:00
int match = getMatch(allargs, current);
if (match > best) {
cmd = current;
2015-09-11 12:09:22 +02:00
}
}
2015-09-13 06:04:31 +02:00
if (cmd == null) {
2015-09-11 12:09:22 +02:00
cmd = new StringComparison<>(label, getCommandAndAliases(null, plr)).getMatchObject();
}
MainUtil.sendMessage(plr, C.DID_YOU_MEAN, cmd.getUsage().replaceAll("\\{label\\}", parts[0]));
}
}
return CommandHandlingOutput.NOT_FOUND;
}
2015-09-13 06:04:31 +02:00
if (!cmd.getRequiredType().allows(plr)) {
if (ConsolePlayer.isConsole(plr)) {
2015-09-11 12:09:22 +02:00
MainUtil.sendMessage(plr, C.IS_CONSOLE);
2015-09-13 06:04:31 +02:00
} else {
2015-09-11 12:09:22 +02:00
MainUtil.sendMessage(plr, C.NOT_CONSOLE);
}
return CommandHandlingOutput.CALLER_OF_WRONG_TYPE;
}
2015-09-13 06:04:31 +02:00
if (!Permissions.hasPermission(plr, cmd.getPermission())) {
2015-09-11 12:09:22 +02:00
MainUtil.sendMessage(plr, C.NO_PERMISSION, cmd.getPermission());
return CommandHandlingOutput.NOT_PERMITTED;
}
final Argument<?>[] requiredArguments = cmd.getRequiredArguments();
2015-09-13 06:04:31 +02:00
if ((requiredArguments != null) && (requiredArguments.length > 0)) {
2015-09-11 12:09:22 +02:00
boolean success = true;
2015-09-13 06:04:31 +02:00
if (args.length < requiredArguments.length) {
2015-09-11 12:09:22 +02:00
success = false;
2015-09-13 06:04:31 +02:00
} else {
for (int i = 0; i < requiredArguments.length; i++) {
if (requiredArguments[i].parse(args[i]) == null) {
2015-09-11 12:09:22 +02:00
success = false;
break;
}
}
}
2015-09-13 06:04:31 +02:00
if (!success) {
2015-09-11 12:09:22 +02:00
C.COMMAND_SYNTAX.send(plr, cmd.getUsage());
return CommandHandlingOutput.WRONG_USAGE;
}
}
2015-09-13 06:04:31 +02:00
try {
2015-09-11 12:09:22 +02:00
final boolean result = cmd.onCommand(plr, args);
2015-09-13 06:04:31 +02:00
if (!result) {
2015-09-11 12:09:22 +02:00
cmd.getUsage();
// Unecessary!
// if (usage != null && !usage.isEmpty()) {
// MainUtil.sendMessage(plr, usage);
// }
return CommandHandlingOutput.WRONG_USAGE;
}
2015-09-13 06:04:31 +02:00
} catch (final Throwable t) {
2015-09-11 12:09:22 +02:00
t.printStackTrace();
return CommandHandlingOutput.ERROR;
}
return CommandHandlingOutput.SUCCESS;
}
}