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;
|
|
|
|
|
2014-11-06 09:37:46 +01:00
|
|
|
import com.intellectualcrafters.plot.PlotMain;
|
2014-11-16 10:48:18 +01:00
|
|
|
import com.intellectualcrafters.plot.config.C;
|
|
|
|
import com.intellectualcrafters.plot.util.PlayerFunctions;
|
|
|
|
import com.intellectualcrafters.plot.util.StringComparison;
|
2014-09-22 13:02:14 +02:00
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.bukkit.command.Command;
|
|
|
|
import org.bukkit.command.CommandExecutor;
|
|
|
|
import org.bukkit.command.CommandSender;
|
2014-11-01 16:13:44 +01:00
|
|
|
import org.bukkit.command.TabCompleter;
|
2014-09-22 13:02:14 +02:00
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
2014-11-06 09:37:46 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
2014-09-22 13:02:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* PlotMain command class
|
2014-10-11 00:33:10 -07:00
|
|
|
*
|
2014-09-22 13:02:14 +02:00
|
|
|
* @author Citymonstret
|
|
|
|
*/
|
2014-11-01 16:13:44 +01:00
|
|
|
public class MainCommand implements CommandExecutor, TabCompleter {
|
2014-09-22 13:02:14 +02:00
|
|
|
|
2014-11-20 00:00:38 +01:00
|
|
|
/**
|
|
|
|
* Main Permission Node
|
|
|
|
*/
|
2014-11-06 09:46:37 +01:00
|
|
|
public static final String
|
|
|
|
MAIN_PERMISSION = "plots.use";
|
|
|
|
|
2014-11-21 23:45:46 +01:00
|
|
|
private final static SubCommand[] _subCommands =
|
2014-11-15 12:05:48 +01:00
|
|
|
new SubCommand[]{
|
2014-12-11 18:15:30 +11:00
|
|
|
new Ban(),
|
|
|
|
new Unban(),
|
|
|
|
new OP(),
|
|
|
|
new DEOP(),
|
|
|
|
new Claim(),
|
|
|
|
new Paste(),
|
|
|
|
new Copy(),
|
|
|
|
new Clipboard(),
|
|
|
|
new Auto(),
|
|
|
|
new Home(),
|
|
|
|
new Visit(),
|
|
|
|
new TP(),
|
|
|
|
new Set(),
|
|
|
|
new Clear(),
|
|
|
|
new Delete(),
|
|
|
|
new SetOwner(),
|
|
|
|
new Denied(),
|
|
|
|
new Helpers(),
|
|
|
|
new Trusted(),
|
|
|
|
new Info(),
|
|
|
|
new list(),
|
|
|
|
new Help(),
|
|
|
|
new Debug(),
|
|
|
|
new Schematic(),
|
|
|
|
new plugin(),
|
|
|
|
new Inventory(),
|
|
|
|
new Purge(),
|
|
|
|
new Reload(),
|
|
|
|
new Merge(),
|
|
|
|
new Unlink(),
|
|
|
|
new Kick(),
|
|
|
|
new Setup(),
|
2014-12-16 00:02:31 +11:00
|
|
|
new Rating(),
|
2014-12-11 18:15:30 +11:00
|
|
|
new DebugClaimTest(),
|
|
|
|
new Inbox(),
|
|
|
|
new Comment(),
|
|
|
|
new Database(),
|
|
|
|
new Unclaim(),
|
|
|
|
new Swap(),
|
|
|
|
new MusicSubcommand()};
|
2014-09-22 13:02:14 +02:00
|
|
|
|
2014-11-21 23:45:46 +01:00
|
|
|
public final static ArrayList<SubCommand> subCommands = new ArrayList<SubCommand>() {
|
2014-11-05 14:42:08 +11:00
|
|
|
{
|
|
|
|
addAll(Arrays.asList(_subCommands));
|
|
|
|
}
|
|
|
|
};
|
2014-09-22 13:02:14 +02:00
|
|
|
|
2014-11-05 14:42:08 +11:00
|
|
|
public static boolean no_permission(final Player player, final String permission) {
|
|
|
|
PlayerFunctions.sendMessage(player, C.NO_PERMISSION, permission);
|
|
|
|
return false;
|
|
|
|
}
|
2014-09-24 22:21:43 +10:00
|
|
|
|
2014-11-09 16:55:43 +01:00
|
|
|
public static List<SubCommand> getCommands(final SubCommand.CommandCategory category, final Player player) {
|
|
|
|
final List<SubCommand> cmds = new ArrayList<>();
|
|
|
|
for (final SubCommand c : subCommands) {
|
|
|
|
if ((c.category.equals(category)) && c.permission.hasPermission(player)) {
|
|
|
|
cmds.add(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cmds;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static List<String> helpMenu(final Player player, final SubCommand.CommandCategory category, int page) {
|
|
|
|
final List<SubCommand> commands = getCommands(category, player);
|
|
|
|
// final int totalPages = ((int) Math.ceil(12 * (commands.size()) /
|
|
|
|
// 100));
|
|
|
|
final int perPage = 5;
|
|
|
|
final int totalPages = (int) Math.ceil(commands.size() / perPage);
|
|
|
|
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.toString())
|
|
|
|
.replace("%current%", "" + (page + 1))
|
|
|
|
.replace("%max%", "" + (totalPages + 1))
|
2014-11-19 12:10:40 +11:00
|
|
|
.replace("%dis%", "" + (commands.size() % perPage))
|
2014-11-09 16:55:43 +01:00
|
|
|
.replace("%total%", "" + commands.size())
|
|
|
|
);
|
|
|
|
|
|
|
|
SubCommand cmd;
|
|
|
|
|
|
|
|
final int start = page * perPage;
|
|
|
|
for (int x = start; x < max; x++) {
|
|
|
|
cmd = commands.get(x);
|
|
|
|
String s = t(C.HELP_ITEM.s());
|
|
|
|
s = s
|
|
|
|
.replace("%alias%", cmd.alias)
|
|
|
|
.replace("%usage%", cmd.usage.contains("plot") ? cmd.usage : "/plot " + cmd.usage)
|
|
|
|
.replace("%cmd%", cmd.cmd)
|
|
|
|
.replace("%desc%", cmd.description)
|
|
|
|
;
|
|
|
|
help.add(s);
|
|
|
|
}
|
|
|
|
if (help.size() < 2) {
|
|
|
|
help.add(t(C.NO_COMMANDS.s()));
|
|
|
|
}
|
|
|
|
return help;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String t(final String s) {
|
|
|
|
return ChatColor.translateAlternateColorCodes('&', s);
|
|
|
|
}
|
|
|
|
|
2014-11-05 14:42:08 +11:00
|
|
|
@Override
|
|
|
|
public boolean onCommand(final CommandSender sender, final Command cmd, final String commandLabel, final String[] args) {
|
|
|
|
final Player player = (sender instanceof Player) ? (Player) sender : null;
|
2014-11-03 20:36:50 +01:00
|
|
|
|
2014-11-06 09:46:37 +01:00
|
|
|
if (!PlotMain.hasPermission(player, MAIN_PERMISSION))
|
|
|
|
return no_permission(player, MAIN_PERMISSION);
|
2014-11-03 20:36:50 +01:00
|
|
|
|
2014-11-05 14:42:08 +11: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()));
|
|
|
|
}
|
2014-11-06 09:46:37 +01:00
|
|
|
return PlayerFunctions.sendMessage(player, builder.toString());
|
2014-11-05 14:42:08 +11: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cato == null) {
|
|
|
|
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()));
|
|
|
|
}
|
2014-11-06 09:46:37 +01:00
|
|
|
return PlayerFunctions.sendMessage(player, builder.toString());
|
2014-11-05 14:42:08 +11:00
|
|
|
}
|
|
|
|
final StringBuilder help = new StringBuilder();
|
2014-11-02 21:49:26 +01:00
|
|
|
int page = 0;
|
2014-11-03 20:36:50 +01:00
|
|
|
|
|
|
|
boolean digit = true;
|
2014-11-08 20:27:09 +01:00
|
|
|
|
2014-11-05 22:01:59 +11:00
|
|
|
String arg2;
|
2014-11-08 20:27:09 +01:00
|
|
|
if (args.length > 2) {
|
2014-11-05 22:01:59 +11:00
|
|
|
arg2 = args[2];
|
2014-11-08 20:27:09 +01:00
|
|
|
} else {
|
2014-11-05 22:01:59 +11:00
|
|
|
arg2 = "1";
|
|
|
|
}
|
2014-11-08 20:27:09 +01:00
|
|
|
|
2014-11-05 22:01:59 +11:00
|
|
|
for (final char c : arg2.toCharArray()) {
|
2014-11-05 14:42:08 +11:00
|
|
|
if (!Character.isDigit(c)) {
|
2014-11-03 20:36:50 +01:00
|
|
|
digit = false;
|
|
|
|
break;
|
|
|
|
}
|
2014-11-05 14:42:08 +11:00
|
|
|
}
|
|
|
|
if (digit) {
|
2014-11-05 22:01:59 +11:00
|
|
|
page = Integer.parseInt(arg2);
|
2014-11-05 14:42:08 +11:00
|
|
|
if (--page < 0) {
|
|
|
|
page = 0;
|
|
|
|
}
|
2014-11-03 20:36:50 +01:00
|
|
|
}
|
|
|
|
|
2014-11-05 14:42:08 +11:00
|
|
|
for (final String string : helpMenu(player, cato, page)) {
|
2014-11-03 20:36:50 +01:00
|
|
|
help.append(string).append("\n");
|
2014-11-05 14:42:08 +11:00
|
|
|
}
|
2014-11-09 16:55:43 +01:00
|
|
|
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', help.toString()));
|
|
|
|
//return PlayerFunctions.sendMessage(player, help.toString());
|
2014-11-08 20:27:09 +01:00
|
|
|
} else {
|
2014-11-05 14:42:08 +11:00
|
|
|
for (final SubCommand command : subCommands) {
|
|
|
|
if (command.cmd.equalsIgnoreCase(args[0]) || command.alias.equalsIgnoreCase(args[0])) {
|
|
|
|
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 14:42:08 +11:00
|
|
|
if (command.permission.hasPermission(player)) {
|
|
|
|
if ((player != null) || !command.isPlayer) {
|
|
|
|
return command.execute(player, arguments);
|
2014-11-08 20:27:09 +01:00
|
|
|
} else {
|
2014-11-06 09:46:37 +01:00
|
|
|
return !PlayerFunctions.sendMessage(null, C.IS_CONSOLE);
|
2014-11-05 14:42:08 +11:00
|
|
|
}
|
2014-11-08 20:27:09 +01:00
|
|
|
} else {
|
2014-11-05 14:42:08 +11:00
|
|
|
return no_permission(player, command.permission.permission.toLowerCase());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PlayerFunctions.sendMessage(player, C.NOT_VALID_SUBCOMMAND);
|
2014-11-01 16:13:44 +01:00
|
|
|
|
2014-11-05 14:42:08 +11: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 14:42:08 +11:00
|
|
|
}
|
2014-11-01 16:13:44 +01:00
|
|
|
|
2014-11-06 09:37:46 +01:00
|
|
|
/* Let's try to get a proper usage string */
|
2014-11-16 10:48:18 +01:00
|
|
|
String command = new StringComparison(args[0], commands).getBestMatch();
|
2014-11-08 23:53:04 +01:00
|
|
|
return PlayerFunctions.sendMessage(player, C.DID_YOU_MEAN, "/plot " + command);
|
2014-11-06 09:37:46 +01:00
|
|
|
//PlayerFunctions.sendMessage(player, C.DID_YOU_MEAN, new StringComparsion(args[0], commands).getBestMatch());
|
2014-11-05 14:42:08 +11:00
|
|
|
}
|
2014-11-09 16:55:43 +01:00
|
|
|
return true;
|
2014-11-05 14:42:08 +11:00
|
|
|
}
|
2014-09-22 13:02:14 +02:00
|
|
|
|
2014-11-01 16:13:44 +01:00
|
|
|
@Override
|
2014-11-05 14:42:08 +11:00
|
|
|
public List<String> onTabComplete(final CommandSender commandSender, final Command command, final String s, final String[] strings) {
|
|
|
|
if (!(commandSender instanceof Player)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
final Player player = (Player) commandSender;
|
|
|
|
|
|
|
|
if (strings.length < 1) {
|
|
|
|
if ((strings.length == 0) || "plots".startsWith(s)) {
|
2014-11-06 09:46:37 +01:00
|
|
|
return Arrays.asList("plots");
|
2014-11-03 17:53:10 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (strings.length > 1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!command.getLabel().equalsIgnoreCase("plots")) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-11-06 09:46:37 +01:00
|
|
|
final List<String> tabOptions = new ArrayList<>();
|
2014-11-05 14:42:08 +11:00
|
|
|
final String arg = strings[0].toLowerCase();
|
|
|
|
for (final SubCommand cmd : subCommands) {
|
2014-11-03 17:53:10 +11:00
|
|
|
if (cmd.permission.hasPermission(player)) {
|
|
|
|
if (cmd.cmd.startsWith(arg)) {
|
|
|
|
tabOptions.add(cmd.cmd);
|
2014-11-08 20:27:09 +01:00
|
|
|
} else if (cmd.alias.startsWith(arg)) {
|
2014-11-03 17:53:10 +11:00
|
|
|
tabOptions.add(cmd.alias);
|
2014-11-01 16:13:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-05 14:42:08 +11:00
|
|
|
if (tabOptions.size() > 0) {
|
2014-11-03 17:53:10 +11:00
|
|
|
return tabOptions;
|
|
|
|
}
|
2014-11-01 16:13:44 +01:00
|
|
|
return null;
|
|
|
|
}
|
2014-09-22 13:02:14 +02:00
|
|
|
}
|