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

109 lines
4.0 KiB
Java
Raw Normal View History

2015-09-11 12:09:22 +02:00
package com.plotsquared.bukkit.util;
import com.intellectualcrafters.plot.commands.MainCommand;
import com.intellectualcrafters.plot.object.ConsolePlayer;
import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.util.Permissions;
import com.intellectualcrafters.plot.util.StringComparison;
2016-02-10 20:43:47 +01:00
import com.plotsquared.bukkit.commands.DebugUUID;
2015-09-11 12:09:22 +02:00
import com.plotsquared.general.commands.Command;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
2015-09-11 12:09:22 +02:00
/**
* Created 2015-02-20 for PlotSquared
*
2015-09-11 12:09:22 +02:00
*/
2015-09-13 06:04:31 +02:00
public class BukkitCommand implements CommandExecutor, TabCompleter {
2015-09-13 06:04:31 +02:00
public BukkitCommand() {
2015-09-11 12:09:22 +02:00
MainCommand.getInstance().addCommand(new DebugUUID());
}
2015-09-11 12:09:22 +02:00
@Override
public boolean onCommand(final CommandSender commandSender, final org.bukkit.command.Command command, final String commandLabel,
final String[] args) {
2015-09-13 06:04:31 +02:00
if (commandSender instanceof Player) {
return MainCommand.onCommand(BukkitUtil.getPlayer((Player) commandSender), commandLabel, args);
}
2015-10-19 08:27:51 +02:00
if (commandSender == null || commandSender.getClass() == Bukkit.getConsoleSender().getClass()) {
return MainCommand.onCommand(ConsolePlayer.getConsole(), commandLabel, args);
}
@SuppressWarnings("deprecation")
ConsolePlayer sender = new ConsolePlayer() {
@Override
public void sendMessage(String message) {
commandSender.sendMessage(commandLabel);
}
2015-10-19 08:27:51 +02:00
@Override
public boolean hasPermission(String perm) {
return commandSender.hasPermission(commandLabel);
}
2015-10-19 08:27:51 +02:00
@Override
public String getName() {
if (commandSender.getName().equals("CONSOLE")) {
return "*";
}
return commandSender.getName();
}
};
sender.teleport(ConsolePlayer.getConsole().getLocationFull());
boolean result = MainCommand.onCommand(sender, commandLabel, args);
ConsolePlayer.getConsole().teleport(sender.getLocationFull());
return result;
2015-09-11 12:09:22 +02:00
}
2015-09-11 12:09:22 +02:00
@Override
public List<String> onTabComplete(final CommandSender commandSender, final org.bukkit.command.Command command, final String s,
final String[] strings) {
2015-09-13 06:04:31 +02:00
if (!(commandSender instanceof Player)) {
return null;
}
2015-09-11 12:09:22 +02:00
final PlotPlayer player = BukkitUtil.getPlayer((Player) commandSender);
2015-09-13 06:04:31 +02:00
if (strings.length < 1) {
if ((strings.length == 0) || "plots".startsWith(s)) {
return Collections.singletonList("plots");
}
}
if (strings.length > 1) {
return null;
}
2015-09-11 12:09:22 +02:00
final Set<String> tabOptions = new HashSet<>();
final String arg = strings[0].toLowerCase();
ArrayList<String> labels = new ArrayList<>();
2015-09-13 06:04:31 +02:00
for (final Command<PlotPlayer> cmd : MainCommand.getInstance().getCommands()) {
2015-09-11 12:09:22 +02:00
final String label = cmd.getCommand();
HashSet<String> aliases = new HashSet<>(cmd.getAliases());
aliases.add(label);
for (String alias : aliases) {
labels.add(alias);
if (alias.startsWith(arg)) {
2015-09-13 06:04:31 +02:00
if (Permissions.hasPermission(player, cmd.getPermission())) {
tabOptions.add(label);
} else {
break;
2015-09-11 12:09:22 +02:00
}
}
}
}
String best = new StringComparison<>(arg, labels).getBestMatch();
tabOptions.add(best);
if (!tabOptions.isEmpty()) {
2015-09-13 06:04:31 +02:00
return new ArrayList<>(tabOptions);
}
2015-09-11 12:09:22 +02:00
return null;
}
}