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

74 lines
2.6 KiB
Java
Raw Normal View History

2015-02-20 11:53:18 +01:00
package com.intellectualcrafters.plot.commands;
2015-02-21 06:48:49 +01:00
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
2015-02-20 11:53:18 +01:00
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
2015-02-21 06:48:49 +01:00
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import com.intellectualcrafters.plot.object.PlotPlayer;
2015-05-02 16:20:04 +02:00
import com.intellectualcrafters.plot.util.StringComparison;
2015-07-26 16:51:12 +02:00
import com.plotsquared.bukkit.util.bukkit.BukkitUtil;
2015-02-20 11:53:18 +01:00
/**
* Created 2015-02-20 for PlotSquared
*
* @author Citymonstret
*/
2015-02-21 06:48:49 +01:00
public class BukkitCommand implements CommandExecutor, TabCompleter {
2015-02-23 02:32:27 +01:00
2015-02-20 11:53:18 +01:00
@Override
2015-02-23 02:32:27 +01:00
public boolean onCommand(final CommandSender commandSender, final Command command, final String commandLabel, final String[] args) {
2015-02-21 06:48:49 +01:00
if (commandSender instanceof Player) {
2015-02-23 01:05:25 +01:00
return MainCommand.onCommand(BukkitUtil.getPlayer((Player) commandSender), commandLabel, args);
2015-02-21 06:48:49 +01:00
}
2015-02-23 01:05:25 +01:00
return MainCommand.onCommand(null, commandLabel, args);
2015-02-21 06:48:49 +01:00
}
2015-02-23 02:32:27 +01:00
2015-02-21 06:48:49 +01:00
@Override
public List<String> onTabComplete(final CommandSender commandSender, final Command command, final String s, final String[] strings) {
if (!(commandSender instanceof Player)) {
return null;
}
final PlotPlayer player = BukkitUtil.getPlayer((Player) commandSender);
if (strings.length < 1) {
if ((strings.length == 0) || "plots".startsWith(s)) {
return Arrays.asList("plots");
}
}
if (strings.length > 1) {
return null;
}
if (!command.getLabel().equalsIgnoreCase("plots")) {
return null;
}
final List<String> tabOptions = new ArrayList<>();
2015-05-02 16:20:04 +02:00
final String[] commands = new String[MainCommand.subCommands.size()];
for (int x = 0; x < MainCommand.subCommands.size(); x++) {
commands[x] = MainCommand.subCommands.get(x).cmd;
}
String best = new StringComparison(strings[0], commands).getBestMatch();
tabOptions.add(best);
2015-02-21 06:48:49 +01:00
final String arg = strings[0].toLowerCase();
for (final SubCommand cmd : MainCommand.subCommands) {
2015-05-02 16:20:04 +02:00
if (!cmd.cmd.equalsIgnoreCase(best)) {
if (cmd.permission.hasPermission(player)) {
if (cmd.cmd.startsWith(arg)) {
tabOptions.add(cmd.cmd);
} else if (cmd.alias.size() > 0 && cmd.alias.get(0).startsWith(arg)) {
tabOptions.add(cmd.alias.get(0));
}
2015-02-21 06:48:49 +01:00
}
}
}
if (tabOptions.size() > 0) {
return tabOptions;
}
return null;
2015-02-20 11:53:18 +01:00
}
}