PlotSquared/src/main/java/com/plotsquared/bukkit/commands/BukkitCommand.java

83 lines
3.0 KiB
Java
Raw Normal View History

package com.plotsquared.bukkit.commands;
2015-02-20 11:53:18 +01:00
2015-02-21 06:48:49 +01:00
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
2015-07-27 08:00:20 +02:00
import java.util.HashSet;
2015-02-21 06:48:49 +01:00
import java.util.List;
2015-07-27 08:00:20 +02:00
import java.util.Set;
2015-02-21 06:48:49 +01:00
import com.intellectualcrafters.plot.commands.MainCommand;
import com.intellectualsites.commands.Command;
2015-07-27 08:00:20 +02:00
2015-02-20 11:53:18 +01:00
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;
2015-07-27 15:10:14 +02:00
import com.intellectualcrafters.plot.object.ConsolePlayer;
2015-02-21 06:48:49 +01:00
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
public boolean onCommand(final CommandSender commandSender, final org.bukkit.command.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-07-27 15:10:14 +02:00
return MainCommand.onCommand(ConsolePlayer.getConsole(), 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 org.bukkit.command.Command command, final String s, final String[] strings) {
2015-02-21 06:48:49 +01:00
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 Collections.singletonList("plots");
2015-02-21 06:48:49 +01:00
}
}
if (strings.length > 1) {
return null;
}
if (!command.getLabel().equalsIgnoreCase("plots")) {
return null;
}
2015-07-27 08:00:20 +02:00
final Set<String> tabOptions = new HashSet<>();
ArrayList<Command> commands = MainCommand.instance.getCommands();
2015-05-02 16:20:04 +02:00
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 Command cmd : MainCommand.instance.getCommands()) {
2015-07-27 08:00:20 +02:00
String label = cmd.getCommand();
if (!label.equalsIgnoreCase(best)) {
if (label.startsWith(arg)) {
if (player.hasPermission(cmd.getPermission())) {
tabOptions.add(cmd.getCommand());
2015-07-27 08:00:20 +02:00
} else if (cmd.getAliases().size() > 0) {
for (String alias : cmd.getAliases()) {
if (alias.startsWith(arg)) {
tabOptions.add(label);
break;
}
}
2015-05-02 16:20:04 +02:00
}
2015-02-21 06:48:49 +01:00
}
}
}
if (tabOptions.size() > 0) {
2015-07-27 08:00:20 +02:00
return new ArrayList<>(tabOptions);
2015-02-21 06:48:49 +01:00
}
return null;
2015-02-20 11:53:18 +01:00
}
}