2016-02-23 05:11:28 +01: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;
|
|
|
|
import com.plotsquared.bukkit.commands.DebugUUID;
|
|
|
|
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;
|
|
|
|
|
|
|
|
public class BukkitCommand implements CommandExecutor, TabCompleter {
|
|
|
|
|
|
|
|
public BukkitCommand() {
|
|
|
|
MainCommand.getInstance().addCommand(new DebugUUID());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-03-23 02:41:37 +01:00
|
|
|
public boolean onCommand(final CommandSender commandSender, org.bukkit.command.Command command, final String commandLabel,
|
|
|
|
String[] args) {
|
2016-02-23 05:11:28 +01:00
|
|
|
if (commandSender instanceof Player) {
|
|
|
|
return MainCommand.onCommand(BukkitUtil.getPlayer((Player) commandSender), commandLabel, args);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-03-23 02:41:37 +01:00
|
|
|
public boolean hasPermission(String permission) {
|
2016-02-23 05:11:28 +01:00
|
|
|
return commandSender.hasPermission(commandLabel);
|
|
|
|
}
|
|
|
|
|
|
|
|
@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;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-03-23 02:41:37 +01:00
|
|
|
public List<String> onTabComplete(CommandSender commandSender, org.bukkit.command.Command command, String s,
|
|
|
|
String[] strings) {
|
2016-02-23 05:11:28 +01:00
|
|
|
if (!(commandSender instanceof Player)) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
PlotPlayer player = BukkitUtil.getPlayer((Player) commandSender);
|
2016-02-23 05:11:28 +01:00
|
|
|
if (strings.length < 1) {
|
2016-03-21 03:52:16 +01:00
|
|
|
if (strings.length == 0 || "plots".startsWith(s)) {
|
2016-02-23 05:11:28 +01:00
|
|
|
return Collections.singletonList("plots");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (strings.length > 1) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
Set<String> tabOptions = new HashSet<>();
|
|
|
|
String arg = strings[0].toLowerCase();
|
2016-02-23 05:11:28 +01:00
|
|
|
ArrayList<String> labels = new ArrayList<>();
|
2016-03-23 02:41:37 +01:00
|
|
|
for (Command<PlotPlayer> cmd : MainCommand.getInstance().getCommands()) {
|
|
|
|
String label = cmd.getCommand();
|
2016-02-23 05:11:28 +01:00
|
|
|
HashSet<String> aliases = new HashSet<>(cmd.getAliases());
|
|
|
|
aliases.add(label);
|
|
|
|
for (String alias : aliases) {
|
|
|
|
labels.add(alias);
|
|
|
|
if (alias.startsWith(arg)) {
|
|
|
|
if (Permissions.hasPermission(player, cmd.getPermission())) {
|
|
|
|
tabOptions.add(label);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
String best = new StringComparison<>(arg, labels).getBestMatch();
|
|
|
|
tabOptions.add(best);
|
|
|
|
if (!tabOptions.isEmpty()) {
|
|
|
|
return new ArrayList<>(tabOptions);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|