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

76 lines
2.9 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-02-21 06:48:49 +01:00
import java.util.List;
import com.intellectualcrafters.plot.commands.MainCommand;
import com.intellectualsites.commands.Command;
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;
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-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 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;
}
final List<String> tabOptions = new ArrayList<>();
final String[] commands = new String[MainCommand.instance.getCommands().size()];
for (int x = 0; x < MainCommand.instance.getCommands().size(); x++) {
commands[x] = MainCommand.instance.getCommands().get(x).getCommand();
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()) {
if (!cmd.getCommand().equalsIgnoreCase(best)) {
if (player.hasPermission(cmd.getPermission())) {
if (cmd.getCommand().startsWith(arg)) {
tabOptions.add(cmd.getCommand());
} else if (cmd.getAliases().length > 0 && cmd.getAliases()[0].startsWith(arg)) {
tabOptions.add(cmd.getAliases()[0]);
2015-05-02 16:20:04 +02:00
}
2015-02-21 06:48:49 +01:00
}
}
}
if (tabOptions.size() > 0) {
return tabOptions;
}
return null;
2015-02-20 11:53:18 +01:00
}
}