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

85 lines
3.1 KiB
Java
Raw Normal View History

2015-07-30 19:24:01 +02:00
package com.plotsquared.bukkit.util;
2015-02-20 11:53:18 +01:00
2015-07-30 16:25:16 +02:00
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import com.intellectualcrafters.plot.commands.DebugUUID;
import com.intellectualcrafters.plot.commands.MainCommand;
2015-07-27 19:50:04 +02:00
import com.intellectualcrafters.plot.object.ConsolePlayer;
import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.util.StringComparison;
import com.plotsquared.general.commands.Command;
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-07-30 16:25:16 +02:00
public BukkitCommand() {
MainCommand.getInstance().addCommand(new DebugUUID());
}
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<>();
2015-07-27 17:41:06 +02:00
ArrayList<Command<PlotPlayer>> commands = MainCommand.getInstance().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();
2015-07-27 17:41:06 +02:00
for (final Command<PlotPlayer> cmd : MainCommand.getInstance().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
}
}