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

259 lines
10 KiB
Java
Raw Normal View History

2014-09-22 13:02:14 +02:00
/*
* Copyright (c) IntellectualCrafters - 2014. You are not allowed to distribute
* and/or monetize any of our intellectual property. IntellectualCrafters is not
* affiliated with Mojang AB. Minecraft is a trademark of Mojang AB.
2014-11-05 14:42:08 +11:00
*
* >> File = MainCommand.java >> Generated by: Citymonstret at 2014-08-09 01:42
2014-09-22 13:02:14 +02:00
*/
package com.intellectualcrafters.plot.commands;
2014-11-05 14:42:08 +11:00
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
2014-11-03 17:53:10 +11:00
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;
import org.bukkit.util.ChatPaginator;
2014-09-22 13:02:14 +02:00
2014-11-05 14:42:08 +11:00
import com.intellectualcrafters.plot.C;
import com.intellectualcrafters.plot.PlayerFunctions;
import com.intellectualcrafters.plot.PlotMain;
import com.intellectualcrafters.plot.StringComparsion;
2014-09-22 13:02:14 +02:00
/**
* PlotMain command class
*
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-05 14:42:08 +11:00
private static SubCommand[] _subCommands = new SubCommand[] { 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(), new DebugClaimTest(), new Inbox(), new Comment(), new Swap(), new MusicSubcommand() };
2014-09-22 13:02:14 +02:00
2014-11-05 14:42:08 +11:00
public static ArrayList<SubCommand> subCommands = new ArrayList<SubCommand>() {
{
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-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-05 14:42:08 +11:00
if (!PlotMain.hasPermission(player, "plots.use")) {
return no_permission(player, "plots.use");
}
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()));
}
PlayerFunctions.sendMessage(player, builder.toString());
return true;
}
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()));
}
PlayerFunctions.sendMessage(player, builder.toString());
return true;
}
final StringBuilder help = new StringBuilder();
2014-11-02 21:49:26 +01:00
int page = 0;
boolean digit = true;
2014-11-05 14:42:08 +11:00
for (final char c : args[2].toCharArray()) {
if (!Character.isDigit(c)) {
digit = false;
break;
}
2014-11-05 14:42:08 +11:00
}
if (digit) {
2014-11-02 21:49:26 +01:00
page = Integer.parseInt(args[2]);
2014-11-05 14:42:08 +11:00
if (--page < 0) {
page = 0;
}
}
2014-11-05 14:42:08 +11:00
for (final String string : helpMenu(player, cato, page)) {
help.append(string).append("\n");
2014-11-05 14:42:08 +11:00
}
2014-11-05 14:42:08 +11:00
PlayerFunctions.sendMessage(player, help.toString());
return true;
}
else {
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);
}
else {
PlayerFunctions.sendMessage(null, C.IS_CONSOLE);
return false;
}
}
else {
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
PlayerFunctions.sendMessage(player, C.DID_YOU_MEAN, new StringComparsion(args[0], commands).getBestMatch());
2014-11-05 14:42:08 +11:00
}
return false;
}
2014-11-02 21:49:26 +01:00
2014-11-05 14:42:08 +11: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 == category) && c.permission.hasPermission(player)) {
2014-11-02 21:49:26 +01:00
cmds.add(c);
2014-11-05 14:42:08 +11:00
}
}
2014-11-02 21:49:26 +01:00
return cmds;
}
/*
2014-11-05 14:42:08 +11:00
* // Current page
* int page = 0;
* //is a page specified? else use 0
* if(args.length > 1) {
* try {
* page = Integer.parseInt(args[1]);
* } catch(Exception e) {
* page = 0;
* }
* }
* //Get the total pages
* int totalPages = ((int) Math.ceil(12 * (PlotMain.getPlotsSorted().size())
* / 100));
* if(page > totalPages)
* page = totalPages;
* //Only display 12!
* int max = (page * 12) + 12;
* if(max > PlotMain.getPlotsSorted().size())
* max = PlotMain.getPlotsSorted().size();
* StringBuilder string = new StringBuilder();
* string.append(C.PLOT_LIST_HEADER_PAGED.s().replaceAll("%cur", page + 1 +
* "").replaceAll("%max", totalPages + 1 + "").replaceAll("%word%", "all") +
* "\n");
* Plot p;
* //This might work xD
* for (int x = (page * 12); x < max; x++) {
2014-11-02 21:49:26 +01:00
*/
2014-11-05 14:42:08 +11:00
public static ArrayList<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) {
2014-11-02 21:49:26 +01:00
page = totalPages;
2014-11-05 14:42:08 +11:00
}
int max = (page * perPage) + perPage;
2014-11-05 14:42:08 +11:00
if (max > commands.size()) {
2014-11-02 21:49:26 +01:00
max = commands.size();
2014-11-05 14:42:08 +11:00
}
final ArrayList<String> help = new ArrayList<>(Arrays.asList(t(C.HELP_HEADER.s().replaceAll("%cur", page + 1 + "").replaceAll("%max", totalPages + 1 + "")), t(C.HELP_CATEGORY.s().replaceAll("%category%", category.toString()))));
2014-11-02 21:49:26 +01:00
SubCommand cmd;
String lines = "";
2014-11-05 14:42:08 +11:00
for (int x = 0; x < (ChatPaginator.GUARANTEED_NO_WRAP_CHAT_PAGE_WIDTH * 0.75); x++) {
lines += "-";
}
2014-11-05 14:42:08 +11:00
final int start = page * perPage;
for (int x = start; x < max; x++) {
cmd = commands.get(x);
2014-11-02 21:49:26 +01:00
String s = t(C.HELP_PAGE.s());
s = s.replaceAll("%alias%", cmd.alias);
s = s.replaceAll("%usage%", cmd.usage.contains("plot") ? cmd.usage : "/plot " + cmd.usage);
s = s.replaceAll("%cmd%", cmd.cmd);
s = s.replaceAll("%desc%", cmd.description);
2014-11-02 21:49:26 +01:00
help.add(s);
2014-11-05 14:42:08 +11:00
if ((x != start) && (x != (max - 1))) {
help.add(t(C.HELP_ITEM_SEPARATOR.s().replaceAll("%lines", lines)));
}
2014-11-02 21:49:26 +01:00
}
2014-11-05 14:42:08 +11:00
if (help.size() < 2) {
help.add(t(C.NO_COMMANDS.s()));
}
return help;
}
2014-09-22 13:02:14 +02:00
2014-11-05 14:42:08 +11:00
private static String t(final String s) {
return ChatColor.translateAlternateColorCodes('&', s);
}
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)) {
return Arrays.asList(new String[] { "plots" });
2014-11-03 17:53:10 +11:00
}
}
if (strings.length > 1) {
return null;
}
if (!command.getLabel().equalsIgnoreCase("plots")) {
return null;
}
2014-11-05 14:42:08 +11:00
final List<String> tabOptions = new ArrayList<String>();
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);
}
else if (cmd.alias.startsWith(arg)) {
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
}