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

190 lines
9.4 KiB
Java
Raw Normal View History

2014-11-08 20:27:09 +01: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 /
////////////////////////////////////////////////////////////////////////////////////////////////////
2014-09-22 13:02:14 +02:00
package com.intellectualcrafters.plot.commands;
2015-02-21 05:27:01 +01:00
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
2015-02-15 08:40:55 +01:00
import com.intellectualcrafters.plot.config.C;
2015-02-20 11:53:18 +01:00
import com.intellectualcrafters.plot.object.PlotPlayer;
2015-02-21 06:48:49 +01:00
import com.intellectualcrafters.plot.util.MainUtil;
2015-02-15 08:40:55 +01:00
import com.intellectualcrafters.plot.util.StringComparison;
2014-09-22 13:02:14 +02:00
/**
2015-02-19 09:51:10 +01:00
* PlotSquared command class
*
2014-09-22 13:02:14 +02:00
* @author Citymonstret
*/
2015-02-20 11:53:18 +01:00
public class MainCommand {
2014-11-20 00:00:38 +01:00
/**
* Main Permission Node
*/
2015-04-26 13:38:29 +02:00
private final static SubCommand[] _subCommands = new SubCommand[] { };
2014-12-18 03:15:11 +01:00
public final static ArrayList<SubCommand> subCommands = new ArrayList<SubCommand>() {
2014-11-05 04:42:08 +01:00
{
addAll(Arrays.asList(_subCommands));
}
};
2015-02-23 02:32:27 +01:00
2015-02-20 11:53:18 +01:00
public static boolean no_permission(final PlotPlayer player, final String permission) {
2015-02-21 06:48:49 +01:00
MainUtil.sendMessage(player, C.NO_PERMISSION, permission);
2014-11-05 04:42:08 +01:00
return false;
}
2015-02-23 02:32:27 +01:00
2015-02-20 11:53:18 +01:00
public static List<SubCommand> getCommands(final SubCommand.CommandCategory category, final PlotPlayer player) {
2014-11-09 16:55:43 +01:00
final List<SubCommand> cmds = new ArrayList<>();
for (final SubCommand c : subCommands) {
2015-02-20 07:34:19 +01:00
if (!c.isPlayer || (player != null)) {
if ((c.category.equals(category)) && c.permission.hasPermission(player)) {
cmds.add(c);
}
}
2014-11-09 16:55:43 +01:00
}
return cmds;
}
2015-02-23 02:32:27 +01:00
2015-02-20 11:53:18 +01:00
public static List<String> helpMenu(final PlotPlayer player, final SubCommand.CommandCategory category, int page) {
List<SubCommand> commands;
if (category != null) {
commands = getCommands(category, player);
} else {
commands = subCommands;
}
2014-11-09 16:55:43 +01:00
// final int totalPages = ((int) Math.ceil(12 * (commands.size()) /
2014-12-18 03:15:11 +01:00
// 100));
2014-11-09 16:55:43 +01:00
final int perPage = 5;
final int totalPages = (commands.size() / perPage) + (commands.size() % perPage == 0 ? 0 : 1);
2014-11-09 16:55:43 +01:00
if (page > totalPages) {
page = totalPages;
}
int max = (page * perPage) + perPage;
if (max > commands.size()) {
max = commands.size();
}
final List<String> help = new ArrayList<>();
help.add(C.HELP_HEADER.s());
// HELP_CATEGORY("&cCategory: &6%category%&c, Page: %current%&c/&6%max%&c, Displaying: &6%dis%&c/&6%total%"),
help.add(C.HELP_CATEGORY.s().replace("%category%", category == null ? "All" : category.toString()).replace("%current%", "" + (page + 1)).replace("%max%", "" + (totalPages)).replace("%dis%", "" + (commands.size() % perPage)).replace("%total%", "" + commands.size()));
2014-11-09 16:55:43 +01:00
SubCommand cmd;
final int start = page * perPage;
for (int x = start; x < max; x++) {
cmd = commands.get(x);
2015-07-17 12:48:13 +02:00
String s = C.HELP_ITEM.s();
if (cmd.alias.size() > 0) {
s = s.replace("%alias%", cmd.alias.get(0));
}
else {
s = s.replace("%alias%", "");
}
s = s.replace("%usage%", cmd.usage.contains("plot") ? cmd.usage : "/plot " + cmd.usage).replace("%cmd%", cmd.cmd).replace("%desc%", cmd.description).replace("[]", "");
2014-11-09 16:55:43 +01:00
help.add(s);
}
if (help.size() < 2) {
2015-07-17 12:48:13 +02:00
help.add(C.NO_COMMANDS.s());
2014-11-09 16:55:43 +01:00
}
return help;
}
2015-02-23 02:32:27 +01:00
2015-02-21 06:48:49 +01:00
public static boolean onCommand(final PlotPlayer player, final String cmd, final String... args) {
2014-11-05 04:42:08 +01:00
if ((args.length < 1) || ((args.length >= 1) && (args[0].equalsIgnoreCase("help") || args[0].equalsIgnoreCase("he")))) {
if (args.length < 2) {
final StringBuilder builder = new StringBuilder();
builder.append(C.HELP_INFO.s());
for (final SubCommand.CommandCategory category : SubCommand.CommandCategory.values()) {
builder.append("\n").append(C.HELP_INFO_ITEM.s().replaceAll("%category%", category.toString().toLowerCase()).replaceAll("%category_desc%", category.toString()));
}
builder.append("\n").append(C.HELP_INFO_ITEM.s().replaceAll("%category%", "all").replaceAll("%category_desc%", "Display all commands"));
2015-02-21 06:48:49 +01:00
return MainUtil.sendMessage(player, builder.toString());
2014-11-05 04:42:08 +01:00
}
final String cat = args[1];
SubCommand.CommandCategory cato = null;
for (final SubCommand.CommandCategory category : SubCommand.CommandCategory.values()) {
if (cat.equalsIgnoreCase(category.toString())) {
cato = category;
break;
}
}
2015-02-20 07:34:19 +01:00
if ((cato == null) && !cat.equalsIgnoreCase("all")) {
2014-11-05 04:42:08 +01:00
final StringBuilder builder = new StringBuilder();
builder.append(C.HELP_INFO.s());
for (final SubCommand.CommandCategory category : SubCommand.CommandCategory.values()) {
builder.append("\n").append(C.HELP_INFO_ITEM.s().replaceAll("%category%", category.toString().toLowerCase()).replaceAll("%category_desc%", category.toString()));
}
2015-02-21 06:48:49 +01:00
return MainUtil.sendMessage(player, builder.toString(), false);
2014-11-05 04:42:08 +01:00
}
final StringBuilder help = new StringBuilder();
2014-11-02 21:49:26 +01:00
int page = 0;
boolean digit = true;
2014-11-05 12:01:59 +01:00
String arg2;
2014-11-08 20:27:09 +01:00
if (args.length > 2) {
2014-11-05 12:01:59 +01:00
arg2 = args[2];
2014-12-18 03:15:11 +01:00
} else {
2014-11-05 12:01:59 +01:00
arg2 = "1";
}
for (final char c : arg2.toCharArray()) {
2014-11-05 04:42:08 +01:00
if (!Character.isDigit(c)) {
digit = false;
break;
}
2014-11-05 04:42:08 +01:00
}
if (digit) {
2014-11-05 12:01:59 +01:00
page = Integer.parseInt(arg2);
2014-11-05 04:42:08 +01:00
if (--page < 0) {
page = 0;
}
}
2014-11-05 04:42:08 +01:00
for (final String string : helpMenu(player, cato, page)) {
help.append(string).append("\n");
2014-11-05 04:42:08 +01:00
}
2015-02-23 00:12:33 +01:00
MainUtil.sendMessage(player, help.toString());
2014-12-16 06:03:20 +01:00
// return PlayerFunctions.sendMessage(player, help.toString());
2014-12-18 03:15:11 +01:00
} else {
2014-11-05 04:42:08 +01:00
for (final SubCommand command : subCommands) {
2014-12-16 02:02:05 +01:00
if (command.cmd.equalsIgnoreCase(args[0]) || command.alias.contains(args[0].toLowerCase())) {
2014-11-05 04:42:08 +01:00
final String[] arguments = new String[args.length - 1];
2014-11-02 20:01:04 +01:00
System.arraycopy(args, 1, arguments, 0, args.length - 1);
2014-11-05 04:42:08 +01:00
if (command.permission.hasPermission(player)) {
if ((player != null) || !command.isPlayer) {
return command.execute(player, arguments);
2014-12-18 03:15:11 +01:00
} else {
2015-02-21 06:48:49 +01:00
return !MainUtil.sendMessage(null, C.IS_CONSOLE);
2014-11-05 04:42:08 +01:00
}
2014-12-18 03:15:11 +01:00
} else {
2014-11-05 04:42:08 +01:00
return no_permission(player, command.permission.permission.toLowerCase());
}
}
}
2015-02-21 06:48:49 +01:00
MainUtil.sendMessage(player, C.NOT_VALID_SUBCOMMAND);
2014-11-05 04:42:08 +01:00
final String[] commands = new String[subCommands.size()];
for (int x = 0; x < subCommands.size(); x++) {
2014-11-01 16:13:44 +01:00
commands[x] = subCommands.get(x).cmd;
2014-11-05 04:42:08 +01:00
}
/* Let's try to get a proper usage string */
2014-12-16 06:03:20 +01:00
final String command = new StringComparison(args[0], commands).getBestMatch();
2015-02-21 06:48:49 +01:00
return MainUtil.sendMessage(player, C.DID_YOU_MEAN, "/plot " + command);
2014-12-16 06:03:20 +01:00
// PlayerFunctions.sendMessage(player, C.DID_YOU_MEAN, new
// StringComparsion(args[0], commands).getBestMatch());
2014-11-05 04:42:08 +01:00
}
2014-11-09 16:55:43 +01:00
return true;
2014-11-05 04:42:08 +01:00
}
2014-09-22 13:02:14 +02:00
}